From 1415f740fb6c3de3bf8f13d0382275754b8bcc9c Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 9 Feb 2013 15:05:44 -0600 Subject: [PATCH] remove usages of Module.sendmsg() we're switching to an idiom where the bot is only on one connection, so we don't need to care about connection tracking. new_sendmsg accordingly doesn't take a connection argument. now i can remove the old sendmsg caught in the wake, a bunch of connections being passed here and there can be removed, changing some module method signatures and such. there might be more to remove still --- Module.py | 2 +- modules/Achievements.py | 8 +------ modules/Acro.py | 26 +++++++++++----------- modules/Markov.py | 18 ++++++---------- modules/Storycraft.py | 48 ++++++++++++++++++++--------------------- modules/Twitter.py | 34 ++++++++++++----------------- 6 files changed, 58 insertions(+), 78 deletions(-) diff --git a/Module.py b/Module.py index cf9ec61..55e12e2 100644 --- a/Module.py +++ b/Module.py @@ -140,7 +140,7 @@ class Module(object): for line in msgs: connection.privmsg(target, line) - def new_sendmsg(self,target, msg): + def new_sendmsg(self, target, msg): """Send a privmsg over IRC to target. This should replace Module.sendmsg() once all code has been converted. diff --git a/modules/Achievements.py b/modules/Achievements.py index a37ed2b..7f4ed2f 100644 --- a/modules/Achievements.py +++ b/modules/Achievements.py @@ -61,7 +61,6 @@ class Achievements(Module): Module.__init__(self, irc, config) - self.connection = None self.next_achievements_scan = 0 thread.start_new_thread(self.thread_do, ()) @@ -181,8 +180,6 @@ class Achievements(Module): def track_irc_event(self, connection, event): """Put events in the database.""" - self.connection = connection - if event.source(): if event.source().find('!') >= 0: nick = irclib.nm_to_n(event.source()) @@ -418,16 +415,13 @@ class Achievements(Module): if self.next_achievements_scan < time.time(): self.next_achievements_scan = time.time() + 300 - if self.connection is None: - return - self.log.debug('in achievement scan') settings = self._get_achievements_settings() if settings is not None: channel = settings.channel achievers = self._query_for_new_achievers() for achiever in achievers: - self.sendmsg(self.connection, channel, achiever[0] + ' achieved ' + achiever[1] + '!') + self.new_sendmsg(channel, achiever[0] + ' achieved ' + achiever[1] + '!') def _query_for_new_achievers(self): """Get new achievement earners for each achievement.""" diff --git a/modules/Acro.py b/modules/Acro.py index 09f0545..5be2ed6 100644 --- a/modules/Acro.py +++ b/modules/Acro.py @@ -39,7 +39,6 @@ class Acro(Module): self.quit = False self.scores = dict() self.rounds = [] - self.connection = None self.channel = '' class AcroRound(object): @@ -95,7 +94,7 @@ class Acro(Module): if self.statusre.search(what): return self.irc.reply(event, self.do_status(what)) if self.startre.search(what): - return self.irc.reply(event, self.do_start(connection, target, what)) + return self.irc.reply(event, self.do_start(target, what)) if self.submitre.search(what): match = self.submitre.search(what) return self.irc.reply(event, self.do_submit(nick, match.group(1))) @@ -113,14 +112,14 @@ class Acro(Module): else: return "the game is running." - def do_start(self, connection, target, what): + def do_start(self, target, what): """Start the game, notify the channel, and begin timers.""" if self.game.state != 0: return "the game is alredy running." else: if irclib.is_channel(target): - self._start_new_game(connection, target) + self._start_new_game(target) else: return "you must start the game from a channel." @@ -153,13 +152,12 @@ class Acro(Module): time.sleep(self.game.rounds[-1].seconds_to_vote) self._end_current_round() - def _start_new_game(self, connection, channel): + def _start_new_game(self, channel): """Begin a new game, which will have multiple rounds.""" self.game.state = 1 - self.game.connection = connection self.game.channel = channel - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, "starting a new game of acro. it will run until you tell it to quit.") self._start_new_round() @@ -173,7 +171,7 @@ class Acro(Module): self.game.rounds[-1].acro = acro sleep_time = self.game.rounds[-1].seconds_to_submit + (self.game.rounds[-1].seconds_to_submit_step * (len(acro)-3)) - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, "the round has started! your acronym is '{0:s}'. ".format(acro) + "submit within {0:d} seconds via !acro submit [meaning]".format(sleep_time)) @@ -273,7 +271,7 @@ class Acro(Module): self.game.state = 3 self.game.rounds[-1].sub_shuffle = self.game.rounds[-1].submissions.keys() random.shuffle(self.game.rounds[-1].sub_shuffle) - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, "here are the results. vote with !acro vote [number]") self._print_round_acros() @@ -285,7 +283,7 @@ class Acro(Module): i = 0 for s in self.game.rounds[-1].sub_shuffle: self.log.debug(str(i) + " is " + s) - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, " {0:d}: {1:s}".format(i+1, self.game.rounds[-1].submissions[s])) i += 1 @@ -310,7 +308,7 @@ class Acro(Module): """Clean up and output for ending the current round.""" self.game.state = 4 - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, "voting's over! here are the scores for the round:") self._print_round_scores() self._add_round_scores_to_game_scores() @@ -325,7 +323,7 @@ class Acro(Module): i = 0 for s in self.game.rounds[-1].submissions.keys(): votes = filter(lambda x: x == s, self.game.rounds[-1].votes.values()) - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, " {0:d} ({1:s}): {2:d}".format(i+1, s, len(votes))) i += 1 @@ -353,7 +351,7 @@ class Acro(Module): self.game.state = 0 self.game.quit = False - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, "game's over! here are the final scores:") self._print_game_scores() @@ -361,7 +359,7 @@ class Acro(Module): """Print the final calculated scores.""" for s in self.game.scores.keys(): - self.sendmsg(self.game.connection, self.game.channel, + self.new_sendmsg(self.game.channel, " {0:s}: {1:d}".format(s, self.game.scores[s])) # vi:tabstop=4:expandtab:autoindent diff --git a/modules/Markov.py b/modules/Markov.py index 98bbcc7..fb9ab4a 100644 --- a/modules/Markov.py +++ b/modules/Markov.py @@ -68,7 +68,6 @@ class Markov(Module): self.next_shut_up_check = 0 self.next_chatter_check = 0 - self.connection = None thread.start_new_thread(self.thread_do, ()) irc.xmlrpc_register_function(self._generate_line, @@ -167,7 +166,6 @@ class Markov(Module): target = nick self.lines_seen.append((nick, datetime.now())) - self.connection = connection # don't learn from commands if self.learnre.search(what) or self.replyre.search(what): @@ -181,10 +179,10 @@ class Markov(Module): target = event.target() if self.learnre.search(what): - return self.irc.reply(event, self.markov_learn(connection, event, + return self.irc.reply(event, self.markov_learn(event, nick, userhost, what, admin_unlocked)) elif self.replyre.search(what) and not self.shut_up: - return self.irc.reply(event, self.markov_reply(connection, event, + return self.irc.reply(event, self.markov_reply(event, nick, userhost, what, admin_unlocked)) if not self.shut_up: @@ -203,7 +201,7 @@ class Markov(Module): self.lines_seen.append(('.self.said.', datetime.now())) return self.irc.reply(event, '{0:s}'.format(self._generate_line(target, line=what))) - def markov_learn(self, connection, event, nick, userhost, what, admin_unlocked): + def markov_learn(self, event, nick, userhost, what, admin_unlocked): """Learn one line, as provided to the command.""" target = event.target() @@ -215,7 +213,7 @@ class Markov(Module): # return what was learned, for weird chaining purposes return line - def markov_reply(self, connection, event, nick, userhost, what, admin_unlocked): + def markov_reply(self, event, nick, userhost, what, admin_unlocked): """Generate a reply to one line, without learning it.""" target = event.target() @@ -256,16 +254,12 @@ class Markov(Module): if self.next_chatter_check < time.time(): self.next_chatter_check = time.time() + 600 - if self.connection is None: - # i haven't seen any text yet... - return - targets = self._get_chatter_targets() for t in targets: if t['chance'] > 0: a = random.randint(1, t['chance']) if a == 1: - self.sendmsg(self.connection, t['target'], self._generate_line(t['target'])) + self.new_sendmsg(t['target'], self._generate_line(t['target'])) def _do_shut_up_checks(self): """Check to see if we've been talking too much, and shut up if so.""" @@ -288,7 +282,7 @@ class Markov(Module): self.shut_up = True targets = self._get_chatter_targets() for t in targets: - self.sendmsg(self.connection, t['target'], + self.new_sendmsg(t['target'], 'shutting up for 30 seconds due to last 30 seconds of activity') def _learn_line(self, line, target, event): diff --git a/modules/Storycraft.py b/modules/Storycraft.py index f8e2ce0..5a93928 100644 --- a/modules/Storycraft.py +++ b/modules/Storycraft.py @@ -172,25 +172,25 @@ class Storycraft(Module): """Pass storycraft control commands to the appropriate method based on input.""" if self.statusre.search(what): - return self.irc.reply(event, self.storycraft_status(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_status(event, nick, userhost, what, admin_unlocked)) elif self.newgamere.search(what): - return self.irc.reply(event, self.storycraft_newgame(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_newgame(event, nick, userhost, what, admin_unlocked)) elif self.joingamere.search(what): - return self.irc.reply(event, self.storycraft_joingame(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_joingame(event, nick, userhost, what, admin_unlocked)) elif self.listgamesre.search(what): - return self.irc.reply(event, self.storycraft_listgames(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_listgames(event, nick, userhost, what, admin_unlocked)) elif self.startgamere.search(what): - return self.irc.reply(event, self.storycraft_startgame(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_startgame(event, nick, userhost, what, admin_unlocked)) elif self.showlinere.search(what): - return self.irc.reply(event, self.storycraft_showline(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_showline(event, nick, userhost, what, admin_unlocked)) elif self.addlinere.search(what): - return self.irc.reply(event, self.storycraft_addline(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_addline(event, nick, userhost, what, admin_unlocked)) elif self.gamestatusre.search(what): - return self.irc.reply(event, self.storycraft_gamestatus(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_gamestatus(event, nick, userhost, what, admin_unlocked)) elif self.exportre.search(what): - return self.irc.reply(event, self.storycraft_export(connection, event, nick, userhost, what, admin_unlocked)) + return self.irc.reply(event, self.storycraft_export(event, nick, userhost, what, admin_unlocked)) - def storycraft_status(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_status(self, event, nick, userhost, what, admin_unlocked): """Print information about the storycraft games, or one specific game.""" match = self.statusre.search(what) @@ -203,7 +203,7 @@ class Storycraft(Module): return 'Storycraft {0:s} - {1:d} games completed, {2:d} in progress, {3:d} open. {4:d} slots free.'.format(__version__, count_completed, count_in_progress, count_open, count_free) - def storycraft_gamestatus(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_gamestatus(self, event, nick, userhost, what, admin_unlocked): """Print information about one specific game.""" match = self.gamestatusre.search(what) @@ -221,7 +221,7 @@ class Storycraft(Module): else: return 'Game #{0:d} does not exist.'.format(game_id) - def storycraft_newgame(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_newgame(self, event, nick, userhost, what, admin_unlocked): """Add a game to the system, which users can join.""" match = self.newgamere.search(what) @@ -267,7 +267,7 @@ class Storycraft(Module): self._add_player_to_game(game_id, nick, userhost) # tell the control channel - self.sendmsg(connection, master_channel, '{0:s} created a game of storycraft - do \'!storycraft game {1:d} join\' to take part!'.format(nick, game_id)) + self.new_sendmsg(master_channel, '{0:s} created a game of storycraft - do \'!storycraft game {1:d} join\' to take part!'.format(nick, game_id)) self.log.debug("{0:s} added a new game".format(nick)) return 'Game #{0:d} has been created. When everyone has joined, do \'!storycraft game {0:d} start\''.format(game_id) @@ -276,7 +276,7 @@ class Storycraft(Module): else: return 'All slots are full.' - def storycraft_joingame(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_joingame(self, event, nick, userhost, what, admin_unlocked): """Add a player to an open game.""" match = self.joingamere.search(what) @@ -298,7 +298,7 @@ class Storycraft(Module): settings = self._get_storycraft_settings() master_channel = settings.master_channel - self.sendmsg(connection, master_channel, '{0:s} joined storycraft #{1:d}!'.format(nick, game_id)) + self.new_sendmsg(master_channel, '{0:s} joined storycraft #{1:d}!'.format(nick, game_id)) self.log.debug("{0:s} joined game #{1:d}".format(nick, game_id)) return '{0:s}, welcome to the game.'.format(nick) else: @@ -310,7 +310,7 @@ class Storycraft(Module): else: return 'Game #{0:d} does not exist.'.format(game_id) - def storycraft_listgames(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_listgames(self, event, nick, userhost, what, admin_unlocked): """Get the listing of either open or in progress games.""" match = self.listgamesre.search(what) @@ -343,7 +343,7 @@ class Storycraft(Module): return '\n'.join(gamestrs) - def storycraft_startgame(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_startgame(self, event, nick, userhost, what, admin_unlocked): """Start a game, closing the period to join and starting line trading.""" match = self.startgamere.search(what) @@ -371,7 +371,7 @@ class Storycraft(Module): master_channel = settings.master_channel # tell the control channel - self.sendmsg(connection, master_channel, '{0:s} started storycraft #{1:d}! - first player is {2:s}, do \'!storycraft game {1:d} show line\' when the game is assigned to you.'.format(nick, game_id, player.nick)) + self.new_sendmsg(master_channel, '{0:s} started storycraft #{1:d}! - first player is {2:s}, do \'!storycraft game {1:d} show line\' when the game is assigned to you.'.format(nick, game_id, player.nick)) self.log.debug("{0:s} started game #{1:d}".format(nick, game_id)) return 'Game #{0:d} started.'.format(game_id) @@ -386,7 +386,7 @@ class Storycraft(Module): else: return 'Game #{0:d} does not exist.'.format(game_id) - def storycraft_showline(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_showline(self, event, nick, userhost, what, admin_unlocked): """List the line to continue with, if queryer is the assignee.""" match = self.showlinere.search(what) @@ -428,7 +428,7 @@ class Storycraft(Module): else: return 'Game #{0:d} does not exist.'.format(game_id) - def storycraft_addline(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_addline(self, event, nick, userhost, what, admin_unlocked): """Add a line to an in progress game.""" match = self.addlinere.search(what) @@ -473,7 +473,7 @@ class Storycraft(Module): return_msg = 'Line logged. Please add another. ' + progress_str else: # notify the new owner, too - self.sendmsg(connection, player.nick, 'You have a new line in storycraft #{0:d}: \'{1:s}\' {2:s}'.format(game_id, last_line.line, progress_str)) + self.new_sendmsg(player.nick, 'You have a new line in storycraft #{0:d}: \'{1:s}\' {2:s}'.format(game_id, last_line.line, progress_str)) # get the default settings settings = self._get_storycraft_settings() @@ -482,10 +482,10 @@ class Storycraft(Module): self.log.debug("{0:s} added a line to #{1:d}".format(nick, game_id)) # log output if game.status == 'IN PROGRESS': - self.sendmsg(connection, master_channel, '{0:s} added a line to storycraft #{1:d}! - next player is {2:s}'.format(nick, game_id, player.nick)) + self.new_sendmsg(master_channel, '{0:s} added a line to storycraft #{1:d}! - next player is {2:s}'.format(nick, game_id, player.nick)) return return_msg else: - self.sendmsg(connection, master_channel, '{0:s} finished storycraft #{1:d}!'.format(nick, game_id)) + self.new_sendmsg(master_channel, '{0:s} finished storycraft #{1:d}!'.format(nick, game_id)) return 'Line logged (and game completed).' else: return 'Failed to assign game to next player.' @@ -504,7 +504,7 @@ class Storycraft(Module): else: return 'Game #{0:d} does not exist.'.format(game_id) - def storycraft_export(self, connection, event, nick, userhost, what, admin_unlocked): + def storycraft_export(self, event, nick, userhost, what, admin_unlocked): """Provide the story for access outside of the bot.""" match = self.exportre.search(what) diff --git a/modules/Twitter.py b/modules/Twitter.py index e6788a9..b411661 100644 --- a/modules/Twitter.py +++ b/modules/Twitter.py @@ -74,9 +74,6 @@ class Twitter(Module): if self._verify_credentials(): # self.twit is set - # ugly, but the best place to track it. store the connection for later use - self.connection = irc.server - # print timeline stuff. this will set up the appropriate timer self._check_self_timeline() @@ -128,25 +125,25 @@ class Twitter(Module): """Attempt to do twitter things.""" if self.getstatusre.search(what): - return self.irc.reply(event, self.twitter_getstatus(connection, event, nick, + return self.irc.reply(event, self.twitter_getstatus(event, nick, userhost, what, admin_unlocked)) elif self.getuserstatusre.search(what): - return self.irc.reply(event, self.twitter_getuserstatus(connection, event, nick, + return self.irc.reply(event, self.twitter_getuserstatus(event, nick, userhost, what, admin_unlocked)) elif self.tweetre.search(what): - return self.irc.reply(event, self.twitter_tweet(connection, event, nick, + return self.irc.reply(event, self.twitter_tweet(event, nick, userhost, what, admin_unlocked)) elif self.replytore.search(what): - return self.irc.reply(event, self.twitter_replyto(connection, event, nick, + return self.irc.reply(event, self.twitter_replyto(event, nick, userhost, what, admin_unlocked)) elif self.gettokenre.search(what): - return self.irc.reply(event, self.twitter_gettoken(connection, event, nick, + return self.irc.reply(event, self.twitter_gettoken(event, nick, userhost, what, admin_unlocked)) elif self.authre.search(what): - return self.irc.reply(event, self.twitter_auth(connection, event, nick, + return self.irc.reply(event, self.twitter_auth(event, nick, userhost, what, admin_unlocked)) - def twitter_getstatus(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_getstatus(self, event, nick, userhost, what, admin_unlocked): """Get a status by tweet ID.""" match = self.getstatusre.search(what) @@ -164,7 +161,7 @@ class Twitter(Module): except twitter.TwitterError as e: return "Couldn't obtain status: " + str(e) - def twitter_getuserstatus(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_getuserstatus(self, event, nick, userhost, what, admin_unlocked): """Get a status for a user. Allows for getting one other than the most recent.""" match = self.getuserstatusre.search(what) @@ -201,7 +198,7 @@ class Twitter(Module): except ValueError as e: return "Couldn't obtain status: " + str(e) - def twitter_tweet(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_tweet(self, event, nick, userhost, what, admin_unlocked): """Tweet. Needs authentication.""" match = self.tweetre.search(what) @@ -220,7 +217,7 @@ class Twitter(Module): except twitter.TwitterError as e: return "Couldn't tweet: " + str(e) - def twitter_replyto(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_replyto(self, event, nick, userhost, what, admin_unlocked): """Reply to a tweet, in the twitter in_reply_to_status_id sense. Needs authentication.""" match = self.replytore.search(what) @@ -245,7 +242,7 @@ class Twitter(Module): except twitter.TwitterError as e: return "Couldn't tweet: " + str(e) - def twitter_gettoken(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_gettoken(self, event, nick, userhost, what, admin_unlocked): """Get an oauth token, so that the user may authenticate the bot.""" match = self.gettokenre.search(what) @@ -260,7 +257,7 @@ class Twitter(Module): # have the user auth return "Go to the following link in your browser: %s?oauth_token=%s and then send me the pin." % (self.authorize_url, self.request_token['oauth_token']) - def twitter_auth(self, connection, event, nick, userhost, what, admin_unlocked): + def twitter_auth(self, event, nick, userhost, what, admin_unlocked): """Authenticate, given a PIN (following gettoken).""" match = self.authre.search(what) @@ -281,9 +278,6 @@ class Twitter(Module): self._log_in_to_twitter(access_token['oauth_token'], access_token['oauth_token_secret']) if self._verify_credentials() is not None: - # ugly, but the best place to track it. store the connection for later use - self.connection = connection - # print timeline stuff. this will set up the appropriate timer self._check_self_timeline() @@ -334,7 +328,7 @@ class Twitter(Module): #tweets.reverse() #for tweet in tweets: # tweet_text = self._return_tweet_or_retweet_text(tweet=tweet, print_source=True) - # self.sendmsg(self.connection, output_channel.encode('utf-8', 'ignore'), tweet_text) + # self.new_sendmsg(output_channel.encode('utf-8', 'ignore'), tweet_text) # ## friends timeline printed, find the latest id #new_since_id = self._get_latest_tweet_id(tweets, since_id) @@ -343,7 +337,7 @@ class Twitter(Module): tweets.reverse() for tweet in tweets: tweet_text = self._return_tweet_or_retweet_text(tweet=tweet, print_source=True) - self.sendmsg(self.connection, output_channel.encode('utf-8', 'ignore'), tweet_text) + self.new_sendmsg(output_channel.encode('utf-8', 'ignore'), tweet_text) # mentions printed, find the latest id new_since_id = self._get_latest_tweet_id(tweets, new_since_id)