From 9c775996ac99fd74385489803de173a7f1b4e75b Mon Sep 17 00:00:00 2001 From: jcao219 Date: Mon, 15 Aug 2011 23:07:21 -0500 Subject: [PATCH] split messages that are too large, and also fix a bug where the bot doesn't completely exit on fbye --- oyoyo/client.py | 25 +++++++++++++++++++------ wolfgame.py | 35 ++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/oyoyo/client.py b/oyoyo/client.py index fee2333..f3551cc 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -96,6 +96,8 @@ class IRCClient(object): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.nickname = "" + self.hostmask = "" + self.ident = "" self.real_name = "" self.host = None self.port = None @@ -106,7 +108,6 @@ class IRCClient(object): self.lock = threading.RLock() self.tokenbucket = TokenBucket(28, 1.73) - self.last_messaged = "" self.__dict__.update(kwargs) self.command_handler = cmd_handler @@ -145,10 +146,9 @@ class IRCClient(object): msg = bytes(" ", "utf_8").join(bargs) logging.info('---> send "{0}"'.format(msg)) - + while not self.tokenbucket.consume(1): pass - self.socket.send(msg + bytes("\r\n", "utf_8")) def connect(self): @@ -230,12 +230,25 @@ class IRCClient(object): self.socket.close() def msg(self, user, msg): for line in msg.split('\n'): - self.send("PRIVMSG", user, ":{0}".format(line)) - self.last_messaged = user + maxchars = 494 - len(self.nickname+self.ident+self.hostmask+user) + while line: + extra = "" + if len(line) > maxchars: + extra = line[maxchars:] + line = line[:maxchars] + self.send("PRIVMSG", user, ":{0}".format(line)) + line = extra privmsg = msg # Same thing def notice(self, user, msg): for line in msg.split('\n'): - self.send("NOTICE", user, ":{0}".format(line)) + maxchars = 495 - len(self.nickname+self.ident+self.hostmask+user) + while line: + extra = "" + if len(line) > maxchars: + extra = line[maxchars:] + line = line[:maxchars] + self.send("NOTICE", user, ":{0}".format(line)) + line = extra def quit(self, msg=""): self.send("QUIT :{0}".format(msg)) def part(self, chan, msg=""): diff --git a/wolfgame.py b/wolfgame.py index 1f146f9..e71b3d6 100644 --- a/wolfgame.py +++ b/wolfgame.py @@ -43,9 +43,13 @@ def connect_callback(cli): cli.msg("ChanServ", "op "+botconfig.CHANNEL) @hook("whoreply", id=294) - def on_whoreply(cli, server, dunno, chan, dunno1, + def on_whoreply(cli, server, dunno, chan, ident, cloak, dunno3, user, status, dunno4): if user in var.USERS: return # Don't add someone who is already there + if user == botconfig.NICK: + cli.nickname = user + cli.ident = ident + cli.hostmask = cloak var.USERS.append(user) var.CLOAKS.append(cloak) @@ -412,7 +416,8 @@ def stats(cli, nick, chan, rest): pl = var.list_players() - if nick in pl: # only do this rate-limiting stuff if the person is in game + if nick in pl or var.PHASE == "join": + # only do this rate-limiting stuff if the person is in game if (var.LAST_STATS and var.LAST_STATS + timedelta(seconds=var.STATS_RATE_LIMIT) > datetime.now()): cli.msg(chan, nick+": This command is rate-limited.") @@ -427,7 +432,7 @@ def stats(cli, nick, chan, rest): else: msg = '{0}: \u00021\u0002 player: {1}'.format(nick, pl[0]) - if nick in pl: + if nick in pl or var.PHASE == "join": cli.msg(chan, msg) var.LOGGER.logMessage(msg.replace("\02", "")) else: @@ -471,7 +476,7 @@ def stats(cli, nick, chan, rest): ", ".join(message[0:-1]), message[-1], vb) - if nick in pl: + if nick in pl or var.PHASE == "join": cli.msg(chan, stats_mssg) var.LOGGER.logMessage(stats_mssg.replace("\02", "")) else: @@ -724,23 +729,27 @@ def stop_game(cli, winner = ""): def chk_win(cli): """ Returns True if someone won """ - + chan = botconfig.CHANNEL lpl = len(var.list_players()) - lwolves = (len(var.ROLES["wolf"])+ - len(var.ROLES["traitor"])+ - len(var.ROLES["werecrow"])) - if var.PHASE == "day": - lpl -= len([x for x in var.WOUNDED if x not in var.ROLES["traitor"]]) - lwolves -= len([x for x in var.WOUNDED if x in var.ROLES["traitor"]]) if lpl == 0: cli.msg(chan, "No more players remaining. Game ended.") reset(cli) return True + if var.PHASE == "join": return False - elif lwolves == lpl / 2: + + + lwolves = (len(var.ROLES["wolf"])+ + len(var.ROLES["traitor"])+ + len(var.ROLES["werecrow"])) + if var.PHASE == "day": + lpl -= len([x for x in var.WOUNDED if x not in var.ROLES["traitor"]]) + lwolves -= len([x for x in var.WOUNDED if x in var.ROLES["traitor"]]) + + if lwolves == lpl / 2: cli.msg(chan, ("Game over! There are the same number of wolves as "+ "villagers. The wolves eat everyone, and win.")) var.LOGGER.logMessage(("Game over! There are the same number of wolves as "+ @@ -2163,7 +2172,7 @@ def start(cli, nick, chan, rest): def on_error(cli, pfx, msg): if msg.endswith("(Excess Flood)"): restart_program(cli, "excess flood") - elif msg.endswith("(Client Quit)"): + elif msg.startswith("Closing Link:"): raise SystemExit