diff --git a/Module.py b/Module.py index ef98c30..261cfe3 100644 --- a/Module.py +++ b/Module.py @@ -134,6 +134,18 @@ class Module(object): self.unregister_handlers() obj(self.config, self.server, self.modlist) + # Utility method to do the proper type of reply (either to IRC, or as a return + # to caller) depending on the target. Pretty simple, and included in the base + # class for convenience. It should be the last step for callers: + # + # return self.reply(connection, replypath, 'hello') + + def reply(self, connection, replypath, replystr): + if replypath is None: + return replystr + else: + connection.privmsg(replypath, replystr) + # Upon seeing a line intended for this module, see if there are subcommands # that we should do what is basically a text replacement on. The intent is to # allow things like the following: diff --git a/modules/Countdown.py b/modules/Countdown.py index caba75a..090631e 100644 --- a/modules/Countdown.py +++ b/modules/Countdown.py @@ -51,18 +51,12 @@ class Countdown(Module): self.config.set('countdown', item, target.astimezone(tzutc()).isoformat()) replystr = 'added countdown item ' + whats[2] - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) elif whats[1] == 'remove': try: if self.config.remove_option('countdown', whats[2]): replystr = 'removed countdown item ' + whats[2] - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) except NoSectionError: pass elif whats[1] == 'list': try: @@ -70,10 +64,7 @@ class Countdown(Module): cdlist.remove('debug') cdlist.sort() liststr = ', '.join(cdlist) - if replypath is None: - return liststr - else: - connection.privmsg(replypath, liststr) + return self.reply(connection, replypath, liststr) except NoSectionError: pass else: try: @@ -94,10 +85,7 @@ class Countdown(Module): if rdelta.seconds != 0: relstr += str(rdelta.seconds) + ' seconds' #relstr += ' (' + timestr + ')' - if replypath is None: - return relstr - else: - connection.privmsg(replypath, relstr) + return self.reply(connection, replypath, relstr) except NoOptionError: pass # vi:tabstop=4:expandtab:autoindent diff --git a/modules/Dice.py b/modules/Dice.py index 13de5de..a906c4e 100644 --- a/modules/Dice.py +++ b/modules/Dice.py @@ -101,10 +101,7 @@ class Dice(Module): if t != times-1: result += ', ' - if replypath is None: - return result - else: - connection.privmsg(replypath, result) + return self.reply(connection, replypath, result) # vi:tabstop=4:expandtab:autoindent # kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/modules/FactFile.py b/modules/FactFile.py index 9bbcb35..7f1e3a3 100644 --- a/modules/FactFile.py +++ b/modules/FactFile.py @@ -51,10 +51,7 @@ class FactFile(Module): to_print = facts[random.randint(1, len(facts))-1] # return text - if replypath is None: - return to_print.rstrip() - else: - connection.privmsg(replypath, to_print.rstrip()) + return self.reply(connection, replypath, to_print.rstrip()) else: print('filename in config file for \'' + whats[0] + '\' is wrong') diff --git a/modules/GoogleTranslate.py b/modules/GoogleTranslate.py index d62686c..d18d424 100644 --- a/modules/GoogleTranslate.py +++ b/modules/GoogleTranslate.py @@ -65,10 +65,7 @@ class GoogleTranslate(Module): translation = translation.replace('\\u0026gt;', '>') translation = translation.replace('\\u0026#39;', '\'') - if replypath is None: - return translation - else: - connection.privmsg(replypath, translation) + return self.reply(connection, replypath, translation) # vi:tabstop=4:expandtab:autoindent # kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/modules/IrcAdmin.py b/modules/IrcAdmin.py index 9ccb1a7..6bd8367 100644 --- a/modules/IrcAdmin.py +++ b/modules/IrcAdmin.py @@ -80,10 +80,7 @@ class IrcAdmin(Module): if irclib.is_channel(channel): connection.join(channel) replystr = 'joined ' + channel - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) def sub_part_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked): whats = what.split(' ') @@ -92,10 +89,7 @@ class IrcAdmin(Module): if irclib.is_channel(channel): connection.part(channel, ' '.join(whats[2:])) replystr = 'parted ' + channel - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) def sub_quit_irc(self, connection, event, nick, userhost, replypath, what, admin_unlocked): whats = what.split(' ') @@ -118,10 +112,7 @@ class IrcAdmin(Module): channelset.add(channel) self.config.set('channels', 'autojoin', ','.join(channelset)) replystr = 'added ' + channel + ' to autojoin' - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) except NoOptionError: pass elif whats[1] == 'remove': try: @@ -132,10 +123,7 @@ class IrcAdmin(Module): channelset.discard(channel) self.config.set('channels', 'autojoin', ','.join(channelset)) replystr = 'removed ' + channel + ' from autojoin' - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) except NoOptionError: pass def sub_save_config(self, connection, event, nick, userhost, replypath, what, admin_unlocked): @@ -144,10 +132,7 @@ class IrcAdmin(Module): with open('dr.botzo.cfg', 'w') as cfg: self.config.write(cfg) replystr = 'saved config file' - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) def sub_change_nick(self, connection, event, nick, userhost, replypath, what, admin_unlocked): whats = what.split(' ') @@ -156,10 +141,7 @@ class IrcAdmin(Module): connection.nick(newnick) self.config.set('IRC', 'nick', newnick) replystr = 'changed nickname' - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) # Save the config file. def save_config(self): diff --git a/modules/Seen.py b/modules/Seen.py index bf517d8..599fc99 100644 --- a/modules/Seen.py +++ b/modules/Seen.py @@ -88,10 +88,7 @@ class Seen(Module): seendata = self.config.get('seen', query).split('|:|') converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc()) replystr = 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'' - if replypath is None: - return replystr - else: - connection.privmsg(replypath, replystr) + return self.reply(connection, replypath, replystr) except NoOptionError: pass # vi:tabstop=4:expandtab:autoindent