move common reply functionality into a method in Module

This commit is contained in:
Brian S. Stephan 2010-07-30 00:34:57 -05:00
parent 48e5c513ae
commit ae4c1fa726
7 changed files with 26 additions and 56 deletions

View File

@ -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:

View File

@ -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

View File

@ -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;

View File

@ -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')

View File

@ -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;

View File

@ -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):

View File

@ -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