Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import errno
  2. import md5
  3. import re
  4. import select
  5. import socket
  6. import threading
  7. import time
  8.  
  9. import config
  10.  
  11. def get_room_ip(room, tries = 5):
  12. for i in range(0, tries):
  13. try:
  14. sock = socket.socket()
  15. sock.connect(("176.9.64.22", 22001))
  16. sock.send("join %s\n" % room)
  17. except Exception as e:
  18. time.sleep(5)
  19. else:
  20. start = time.time()
  21. while start + 5 > time.time():
  22. data = sock.recv(1024)
  23. if data.split(";")[0] == "FORWARD 0":
  24. return data.split(";")[-1].split(":")
  25. else:
  26. raise Exception(e)
  27.  
  28. class ToribashClient():
  29.  
  30. line = re.compile(r"([A-Z]+) (\d+);(.*)")
  31. say_re = re.compile(r"(?:\^\d{1,2})?<([\^[\[\]\w\(\)]+)> (.+)")
  32. running = True
  33.  
  34. def __init__(self, username, password, room):
  35. self.username = username
  36. self.password = password
  37. self.room = room
  38.  
  39. def stripStyle(self, msg):
  40. return re.sub("\^([0-9]{1,2})", "", msg)
  41.  
  42. def stripTag(self, username):
  43. return username.split("]")[-1].split(")")[-1]
  44.  
  45. def send(self, msg):
  46. print "[client]", msg
  47. self.sock.sendall(msg + "\n")
  48.  
  49. def say(self, msg):
  50. try:
  51. self.send("SAY %s" % msg)
  52. except:
  53. pass
  54.  
  55. def connect(self, room):
  56. ip, port = get_room_ip(room)
  57. for i in range(5):
  58. try:
  59. self.sock = socket.socket()
  60. self.sock.connect((ip, int(port)))
  61. self.send("NICK %s" % self.username)
  62. self.send("mlogin %s %s 0" % (self.username, md5.new(self.password).hexdigest()))
  63. except Exception as e:
  64. print e
  65. time.sleep(2 ** i)
  66. else:
  67. break
  68. else:
  69. raise Exception(e)
  70.  
  71. def run(self):
  72. last_ping = 0
  73. _buffer = ""
  74.  
  75. self.connect(self.room)
  76.  
  77. while self.running:
  78. ready, _, _ = select.select([self.sock], [], [], 1)
  79.  
  80. if last_ping + 30 < time.time():
  81. self.send("PING")
  82. last_ping = time.time()
  83.  
  84. if not ready:
  85. continue
  86.  
  87. try:
  88. data = self.sock.recv(1024)
  89. except socket.timeout:
  90. time.sleep(15)
  91. self.connect(self.room)
  92. if not data:
  93. time.sleep(15)
  94. self.connect(self.room)
  95.  
  96. _buffer += data
  97. temp = _buffer.split("\n")
  98. _buffer = temp.pop()
  99.  
  100. for line in temp:
  101. print "[server]", line
  102. match = self.line.match(line)
  103.  
  104. if not match:
  105. continue
  106.  
  107. cmd, id, args = match.groups()
  108. cmd = cmd.lower()
  109.  
  110. try:
  111. id = int(id)
  112. except ValueError:
  113. continue
  114.  
  115. try:
  116. if hasattr(self, 'command_' + cmd):
  117. getattr(self, 'command_' + cmd)(id, args)
  118. except Exception as e:
  119. print e
  120. self.send("QUIT")
  121.  
  122. nudge_priority = []
  123. def command_say(self, id, msg):
  124. match = self.say_re.match(msg)
  125. if match:
  126. username, message = match.groups()
  127. username = self.stripStyle(self.stripTag(username))
  128. if username != self.username:
  129. cmd, args = message.split(' ', 1) if ' ' in message else (message, '')
  130. try:
  131. if hasattr(self, 'user_command_' + cmd):
  132. getattr(self, 'user_command_' + cmd)(username, args)
  133. except Exception as e:
  134. print e
  135. else:
  136. msg = self.stripStyle(msg)
  137. match = re.match(r'^ \* (.+) has added (\d+) TC to the decap prize', msg)
  138. if match:
  139. username = self.stripTag(match.group(1))
  140. prize = int(match.group(2))
  141. if prize >= config.nudge_decapprize:
  142. for i, username_ in enumerate(self.queue[2:], start=1): #ignore the ones playing
  143. if username_ not in self.nudge_priority:
  144. self.send("nudge %s %i" % (username, i))
  145. break
  146. else:
  147. self.send("WHISPER %s Looks like you won't get a nudge, since everybody was nudged" % username)
  148. self.nudge_priority.append(username)
  149.  
  150. queue = []
  151. queue_cache = []
  152. def command_bout(self, n, args):
  153. if args.split()[5] == "END":
  154. self.queue = self.queue_cache
  155. self.queue_cache = []
  156.  
  157. for username in self.queue[:2]: # users that are playing
  158. if username in self.nudge_priority:
  159. self.nudge_priority.pop(self.nudge_priority.index(username))
  160. else:
  161. username = args.split()[6]
  162. self.queue_cache.append(self.stripTag(username))
  163. if self.stripTag(username) == self.username:
  164. self.send("SPEC")
  165.  
  166. bets = [0, 0]
  167. def command_bet(self, id, args):
  168. betters, amount = map(int, args.split())
  169. self.bets[id] = amount
  170.  
  171. def command_game(self, frames, args):
  172. if frames > config.bet_frames:
  173. if not min(self.bets) and max(self.bets) or \
  174. max(self.bets) / float(min(self.bets)) >= config.bet_cancel_ratio:
  175. self.send('cancelbets')
  176.  
  177. def user_command_queue(self, username, msg):
  178. self.send("SAY " + str(self.queue))
  179.  
  180. client = ToribashClient(config.username, config.password, config.room)
  181. client.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement