Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import errno
- import md5
- import re
- import select
- import socket
- import threading
- import time
- import config
- def get_room_ip(room, tries = 5):
- for i in range(0, tries):
- try:
- sock = socket.socket()
- sock.connect(("176.9.64.22", 22001))
- sock.send("join %s\n" % room)
- except Exception as e:
- time.sleep(5)
- else:
- start = time.time()
- while start + 5 > time.time():
- data = sock.recv(1024)
- if data.split(";")[0] == "FORWARD 0":
- return data.split(";")[-1].split(":")
- else:
- raise Exception(e)
- class ToribashClient():
- line = re.compile(r"([A-Z]+) (\d+);(.*)")
- say_re = re.compile(r"(?:\^\d{1,2})?<([\^[\[\]\w\(\)]+)> (.+)")
- running = True
- def __init__(self, username, password, room):
- self.username = username
- self.password = password
- self.room = room
- def stripStyle(self, msg):
- return re.sub("\^([0-9]{1,2})", "", msg)
- def stripTag(self, username):
- return username.split("]")[-1].split(")")[-1]
- def send(self, msg):
- print "[client]", msg
- self.sock.sendall(msg + "\n")
- def say(self, msg):
- try:
- self.send("SAY %s" % msg)
- except:
- pass
- def connect(self, room):
- ip, port = get_room_ip(room)
- for i in range(5):
- try:
- self.sock = socket.socket()
- self.sock.connect((ip, int(port)))
- self.send("NICK %s" % self.username)
- self.send("mlogin %s %s 0" % (self.username, md5.new(self.password).hexdigest()))
- except Exception as e:
- print e
- time.sleep(2 ** i)
- else:
- break
- else:
- raise Exception(e)
- def run(self):
- last_ping = 0
- _buffer = ""
- self.connect(self.room)
- while self.running:
- ready, _, _ = select.select([self.sock], [], [], 1)
- if last_ping + 30 < time.time():
- self.send("PING")
- last_ping = time.time()
- if not ready:
- continue
- try:
- data = self.sock.recv(1024)
- except socket.timeout:
- time.sleep(15)
- self.connect(self.room)
- if not data:
- time.sleep(15)
- self.connect(self.room)
- _buffer += data
- temp = _buffer.split("\n")
- _buffer = temp.pop()
- for line in temp:
- print "[server]", line
- match = self.line.match(line)
- if not match:
- continue
- cmd, id, args = match.groups()
- cmd = cmd.lower()
- try:
- id = int(id)
- except ValueError:
- continue
- try:
- if hasattr(self, 'command_' + cmd):
- getattr(self, 'command_' + cmd)(id, args)
- except Exception as e:
- print e
- self.send("QUIT")
- nudge_priority = []
- def command_say(self, id, msg):
- match = self.say_re.match(msg)
- if match:
- username, message = match.groups()
- username = self.stripStyle(self.stripTag(username))
- if username != self.username:
- cmd, args = message.split(' ', 1) if ' ' in message else (message, '')
- try:
- if hasattr(self, 'user_command_' + cmd):
- getattr(self, 'user_command_' + cmd)(username, args)
- except Exception as e:
- print e
- else:
- msg = self.stripStyle(msg)
- match = re.match(r'^ \* (.+) has added (\d+) TC to the decap prize', msg)
- if match:
- username = self.stripTag(match.group(1))
- prize = int(match.group(2))
- if prize >= config.nudge_decapprize:
- for i, username_ in enumerate(self.queue[2:], start=1): #ignore the ones playing
- if username_ not in self.nudge_priority:
- self.send("nudge %s %i" % (username, i))
- break
- else:
- self.send("WHISPER %s Looks like you won't get a nudge, since everybody was nudged" % username)
- self.nudge_priority.append(username)
- queue = []
- queue_cache = []
- def command_bout(self, n, args):
- if args.split()[5] == "END":
- self.queue = self.queue_cache
- self.queue_cache = []
- for username in self.queue[:2]: # users that are playing
- if username in self.nudge_priority:
- self.nudge_priority.pop(self.nudge_priority.index(username))
- else:
- username = args.split()[6]
- self.queue_cache.append(self.stripTag(username))
- if self.stripTag(username) == self.username:
- self.send("SPEC")
- bets = [0, 0]
- def command_bet(self, id, args):
- betters, amount = map(int, args.split())
- self.bets[id] = amount
- def command_game(self, frames, args):
- if frames > config.bet_frames:
- if not min(self.bets) and max(self.bets) or \
- max(self.bets) / float(min(self.bets)) >= config.bet_cancel_ratio:
- self.send('cancelbets')
- def user_command_queue(self, username, msg):
- self.send("SAY " + str(self.queue))
- client = ToribashClient(config.username, config.password, config.room)
- client.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement