Guest User

whisperbot.py

a guest
Mar 21st, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.97 KB | None | 0 0
  1. #!/usr/bin/python
  2. file = open("wlog.txt", "a")
  3. from config import *
  4. import select
  5. import socket
  6. import struct
  7. import time
  8. import sys
  9. import re
  10. import os
  11. from time import strftime as date
  12.  
  13. # http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
  14. console_width = int(os.popen('stty size', 'r').read().split()[1])
  15.  
  16. packet_lengths = [
  17.    10,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  18.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  19.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  20.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  21. #0x0040
  22.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  23.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  24.     0,  50,  3, -1, 55, 17,  3, 37, 46, -1, 23, -1,  3,108,  3,  2,
  25.     3, 28, 19, 11,  3, -1,  9,  5, 54, 53, 58, 60, 41,  2,  6,  6,
  26. #0x0080
  27.     7,  3,  2,  2,  2,  5, 16, 12, 10,  7, 29, 23, -1, -1, -1,  0,
  28.     7, 22, 28,  2,  6, 30, -1, -1,  3, -1, -1,  5,  9, 17, 17,  6,
  29.    23,  6,  6, -1, -1, -1, -1,  8,  7,  6,  7,  4,  7,  0, -1,  6,
  30.     8,  8,  3,  3, -1,  6,  6, -1,  7,  6,  2,  5,  6, 44,  5,  3,
  31. #0x00C0
  32.     7,  2,  6,  8,  6,  7, -1, -1, -1, -1,  3,  3,  6,  6,  2, 27,
  33.     3,  4,  4,  2, -1, -1,  3, -1,  6, 14,  3, -1, 28, 29, -1, -1,
  34.    30, 30, 26,  2,  6, 26,  3,  3,  8, 19,  5,  2,  3,  2,  2,  2,
  35.     3,  2,  6,  8, 21,  8,  8,  2,  2, 26,  3, -1,  6, 27, 30, 10,
  36. #0x0100
  37.     2,  6,  6, 30, 79, 31, 10, 10, -1, -1,  4,  6,  6,  2, 11, -1,
  38.    10, 39,  4, 10, 31, 35, 10, 18,  2, 13, 15, 20, 68,  2,  3, 16,
  39.     6, 14, -1, -1, 21,  8,  8,  8,  8,  8,  2,  2,  3,  4,  2, -1,
  40.     6, 86,  6, -1, -1,  7, -1,  6,  3, 16,  4,  4,  4,  6, 24, 26,
  41. #0x0140
  42.    22, 14,  6, 10, 23, 19,  6, 39,  8,  9,  6, 27, -1,  2,  6,  6,
  43.   110,  6, -1, -1, -1, -1, -1,  6, -1, 54, 66, 54, 90, 42,  6, 42,
  44.    -1, -1, -1, -1, -1, 30, -1,  3, 14,  3, 30, 10, 43, 14,186,182,
  45.    14, 30, 10,  3, -1,  6,106, -1,  4,  5,  4, -1,  6,  7, -1, -1,
  46. #0x0180
  47.     6,  3,106, 10, 10, 34,  0,  6,  8,  4,  4,  4, 29, -1, 10,  6,
  48.    90, 86, 24,  6, 30,102,  9,  4,  8,  4, 14, 10,  4,  6,  2,  6,
  49.     3,  3, 35,  5, 11, 26, -1,  4,  4,  6, 10, 12,  6, -1,  4,  4,
  50.    11,  7, -1, 67, 12, 18,114,  6,  3,  6, 26, 26, 26, 26,  2,  3,
  51. #0x01C0
  52.     2, 14, 10, -1, 22, 22,  4,  2, 13, 97,  0,  9,  9, 29,  6, 28,
  53.     8, 14, 10, 35,  6,  8,  4, 11, 54, 53, 60,  2, -1, 47, 33,  6,
  54.    30,  8, 34, 14,  2,  6, 26,  2, 28, 81,  6, 10, 26,  2, -1, -1,
  55.    -1, -1, 20, 10, 32,  9, 34, 14,  2,  6, 48, 56, -1,  4,  5, 10,
  56. #0x2000
  57.    26,  0,  0,  0, 18,  0,  0,  0,  0,  0,  0, 19,  0,  0,  0,  0,
  58.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
  59. ]
  60.  
  61. class PacketBuffer:
  62.     def __init__(self):
  63.         self.buff = ""
  64.  
  65.     def feed(self, data):
  66.         self.buff += data
  67.  
  68.     def drop(self, count):
  69.         self.buff = self.buff[count:]
  70.  
  71.     def __iter__(self):
  72.         return self
  73.  
  74.     def next(self):
  75.         if len(self.buff) < 2:
  76.             raise StopIteration
  77.  
  78.         pkttype = struct.unpack("<H", self.buff[:2])[0]
  79.         assert pkttype < len(packet_lengths)
  80.         assert packet_lengths[pkttype] != 0
  81.         if packet_lengths[pkttype] < 0:
  82.             if len(self.buff) < 4:
  83.                 raise StopIteration
  84.             pktlen = struct.unpack("<H", self.buff[2:4])[0]
  85.             assert pktlen >= 4
  86.         else:
  87.             pktlen = packet_lengths[pkttype]
  88.  
  89.         if len(self.buff) < pktlen:
  90.             raise StopIteration
  91.         packet = self.buff[:pktlen]
  92.         self.buff = self.buff[pktlen:]
  93.         return packet
  94.  
  95. def parse_ip(s):
  96.     return ".".join(map(str, map(ord, s)))
  97.  
  98. def whisper(nick, message):
  99.     return "\x96\0%s%s%s" % (struct.pack("<H", len(message)+28), nick.ljust(24, '\0'), message)
  100.  
  101.  
  102. def main():
  103.     login = socket.socket()
  104.     login.connect((server, port))
  105.     print("login connected")
  106.     login.sendall("\x64\0\0\0\0\0%s%s\0" %
  107.                   (account.ljust(24, '\0'), password.ljust(24, '\0')))
  108.  
  109.     pb = PacketBuffer()
  110.     id1 = accid = id2 = sex = 0
  111.     charip = ""
  112.     charport = 0
  113.     while True:
  114.         data = login.recv(1500)
  115.         if not data:
  116.             break
  117.         pb.feed(data)
  118.         for packet in pb:
  119.             if packet.startswith("\x69\0"): # login succeeded
  120.                 id1, accid, id2 = struct.unpack("<LLL", packet[4:16])
  121.                 sex = ord(packet[46])
  122.                 packet = packet[47:]
  123.                 charip = parse_ip(packet[:4])
  124.                 charport = struct.unpack("<H", packet[4:6])[0]
  125.                 login.close()
  126.                 break
  127.         if charip:
  128.             break
  129.  
  130.     assert charport
  131.  
  132.     char = socket.socket()
  133.     char.connect((charip, charport))
  134.     print("char connected")
  135.     char.sendall("\x65\0%s\0\0%s" % (struct.pack("<LLL", accid, id1, id2), chr(sex)))
  136.     char.recv(4)
  137.  
  138.     pb = PacketBuffer()
  139.     mapip = ""
  140.     mapport = 0
  141.     charid = 0
  142.     while True:
  143.         data = char.recv(1500)
  144.         if not data:
  145.             break
  146.         pb.feed(data)
  147.         for packet in pb:
  148.             if packet.startswith("\x6b\0"):
  149.                 char.sendall("\x66\0%s" % chr(character))
  150.             elif packet.startswith("\x71\0"):
  151.                 charid = struct.unpack("<L", packet[2:6])[0]
  152.                 mapip = parse_ip(packet[22:26])
  153.                 mapport = struct.unpack("<H", packet[26:28])[0]
  154.                 char.close()
  155.                 break
  156.         if mapip:
  157.             break
  158.  
  159.     assert mapport
  160.  
  161.     mapserv = socket.socket()
  162.     mapserv.connect((mapip, mapport))
  163.     print("map connected")
  164.     mapserv.sendall("\x72\0%s" % struct.pack("<LLLLB", accid, charid, id1, id2, sex))
  165.     mapserv.recv(4)
  166.  
  167.     pb = PacketBuffer()
  168.     gotresponse = set()
  169.     while True:
  170.         data = mapserv.recv(1500)
  171.         if not data:
  172.             break
  173.         pb.feed(data)
  174.         for packet in pb:
  175.             if packet.startswith("\x73\0"): # connected
  176.                 mapserv.sendall("\x7d\0") # map loaded
  177.                 #mapserv.sendall("\x89\0\0\0\0\0\x02") # sit
  178.             elif packet.startswith("\x97\0"):
  179.                 nick = packet[4:28].rstrip("\0")
  180.                 message = packet[28:]
  181.                 if nick == master:
  182.                     data = "%s : %s" % (charactername, message)
  183.                     mapserv.sendall("\x8c\0%s%s" % (struct.pack("<H", len(data)+4), data))
  184.                     time.sleep(0.5)
  185.                 elif nick not in gotresponse:
  186.                     gotresponse.add(nick)
  187.                     file.write("[" + date('%H:%M:%S') + "] " + "!!! " + nick + ": " + message + "\n")
  188.                     print "[" + date('%H:%M:%S') + "] " + "!!! " + nick + ": " + message
  189.                     #mapserv.sendall(whisper(nick, "Please talk to %s." % master))
  190.                     time.sleep(0.5)
  191.             elif packet.startswith("\x8e\0"): # server speech
  192.                 message = packet[4:]
  193.                 print "[" + date('%H:%M:%S') + "] " + message
  194.                 if "automaticly banned for spam" in message:
  195.                     time.sleep(3)
  196.             elif packet.startswith("\x8d\0"): # char speech
  197.                 message = re.sub(r'(##[0-9])',r'\1 ',packet[8:])
  198.                 print "[" + date('%H:%M:%S') + "] " + message
  199.                 if len(message) > console_width:
  200.                     print ""
  201. #                mapserv.sendall(whisper(master, message))
  202.                 file.write("[" + date('%H:%M:%S') + "] " + message + "\n")
  203.                 time.sleep(0.1)
  204.         si,so,se = select.select([sys.stdin],[],[], 0.01)
  205.         for s in si:
  206.             if s == sys.stdin:
  207.                 message = sys.stdin.readline()[:-1]
  208.                 data = "%s : %s" % (charactername, message)
  209.                 mapserv.sendall("\x8c\0%s%s" % (struct.pack("<H", len(data)+4), data))
  210.         file.write("[" + date('%H:%M:%S') + "] " + "Me: " + message + "\n")
  211. #                time.sleep(0.5)
  212.  
  213.  
  214. if __name__ == '__main__':
  215.     main()
  216. file.close()
Advertisement
Add Comment
Please, Sign In to add comment