move common reply functionality into a method in Module
This commit is contained in:
parent
48e5c513ae
commit
ae4c1fa726
12
Module.py
12
Module.py
|
@ -134,6 +134,18 @@ class Module(object):
|
||||||
self.unregister_handlers()
|
self.unregister_handlers()
|
||||||
obj(self.config, self.server, self.modlist)
|
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
|
# 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
|
# that we should do what is basically a text replacement on. The intent is to
|
||||||
# allow things like the following:
|
# allow things like the following:
|
||||||
|
|
|
@ -51,18 +51,12 @@ class Countdown(Module):
|
||||||
|
|
||||||
self.config.set('countdown', item, target.astimezone(tzutc()).isoformat())
|
self.config.set('countdown', item, target.astimezone(tzutc()).isoformat())
|
||||||
replystr = 'added countdown item ' + whats[2]
|
replystr = 'added countdown item ' + whats[2]
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
elif whats[1] == 'remove':
|
elif whats[1] == 'remove':
|
||||||
try:
|
try:
|
||||||
if self.config.remove_option('countdown', whats[2]):
|
if self.config.remove_option('countdown', whats[2]):
|
||||||
replystr = 'removed countdown item ' + whats[2]
|
replystr = 'removed countdown item ' + whats[2]
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
except NoSectionError: pass
|
except NoSectionError: pass
|
||||||
elif whats[1] == 'list':
|
elif whats[1] == 'list':
|
||||||
try:
|
try:
|
||||||
|
@ -70,10 +64,7 @@ class Countdown(Module):
|
||||||
cdlist.remove('debug')
|
cdlist.remove('debug')
|
||||||
cdlist.sort()
|
cdlist.sort()
|
||||||
liststr = ', '.join(cdlist)
|
liststr = ', '.join(cdlist)
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, liststr)
|
||||||
return liststr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, liststr)
|
|
||||||
except NoSectionError: pass
|
except NoSectionError: pass
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
@ -94,10 +85,7 @@ class Countdown(Module):
|
||||||
if rdelta.seconds != 0:
|
if rdelta.seconds != 0:
|
||||||
relstr += str(rdelta.seconds) + ' seconds'
|
relstr += str(rdelta.seconds) + ' seconds'
|
||||||
#relstr += ' (' + timestr + ')'
|
#relstr += ' (' + timestr + ')'
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, relstr)
|
||||||
return relstr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, relstr)
|
|
||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
|
|
|
@ -101,10 +101,7 @@ class Dice(Module):
|
||||||
if t != times-1:
|
if t != times-1:
|
||||||
result += ', '
|
result += ', '
|
||||||
|
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, result)
|
||||||
return result
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, result)
|
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||||
|
|
|
@ -51,10 +51,7 @@ class FactFile(Module):
|
||||||
to_print = facts[random.randint(1, len(facts))-1]
|
to_print = facts[random.randint(1, len(facts))-1]
|
||||||
|
|
||||||
# return text
|
# return text
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, to_print.rstrip())
|
||||||
return to_print.rstrip()
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, to_print.rstrip())
|
|
||||||
else:
|
else:
|
||||||
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
print('filename in config file for \'' + whats[0] + '\' is wrong')
|
||||||
|
|
||||||
|
|
|
@ -65,10 +65,7 @@ class GoogleTranslate(Module):
|
||||||
translation = translation.replace('\\u0026gt;', '>')
|
translation = translation.replace('\\u0026gt;', '>')
|
||||||
translation = translation.replace('\\u0026#39;', '\'')
|
translation = translation.replace('\\u0026#39;', '\'')
|
||||||
|
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, translation)
|
||||||
return translation
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, translation)
|
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||||
|
|
|
@ -80,10 +80,7 @@ class IrcAdmin(Module):
|
||||||
if irclib.is_channel(channel):
|
if irclib.is_channel(channel):
|
||||||
connection.join(channel)
|
connection.join(channel)
|
||||||
replystr = 'joined ' + channel
|
replystr = 'joined ' + channel
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
|
|
||||||
def sub_part_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
def sub_part_channel(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
whats = what.split(' ')
|
whats = what.split(' ')
|
||||||
|
@ -92,10 +89,7 @@ class IrcAdmin(Module):
|
||||||
if irclib.is_channel(channel):
|
if irclib.is_channel(channel):
|
||||||
connection.part(channel, ' '.join(whats[2:]))
|
connection.part(channel, ' '.join(whats[2:]))
|
||||||
replystr = 'parted ' + channel
|
replystr = 'parted ' + channel
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
|
|
||||||
def sub_quit_irc(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
def sub_quit_irc(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
whats = what.split(' ')
|
whats = what.split(' ')
|
||||||
|
@ -118,10 +112,7 @@ class IrcAdmin(Module):
|
||||||
channelset.add(channel)
|
channelset.add(channel)
|
||||||
self.config.set('channels', 'autojoin', ','.join(channelset))
|
self.config.set('channels', 'autojoin', ','.join(channelset))
|
||||||
replystr = 'added ' + channel + ' to autojoin'
|
replystr = 'added ' + channel + ' to autojoin'
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
elif whats[1] == 'remove':
|
elif whats[1] == 'remove':
|
||||||
try:
|
try:
|
||||||
|
@ -132,10 +123,7 @@ class IrcAdmin(Module):
|
||||||
channelset.discard(channel)
|
channelset.discard(channel)
|
||||||
self.config.set('channels', 'autojoin', ','.join(channelset))
|
self.config.set('channels', 'autojoin', ','.join(channelset))
|
||||||
replystr = 'removed ' + channel + ' from autojoin'
|
replystr = 'removed ' + channel + ' from autojoin'
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
|
|
||||||
def sub_save_config(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
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:
|
with open('dr.botzo.cfg', 'w') as cfg:
|
||||||
self.config.write(cfg)
|
self.config.write(cfg)
|
||||||
replystr = 'saved config file'
|
replystr = 'saved config file'
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
|
|
||||||
def sub_change_nick(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
def sub_change_nick(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||||
whats = what.split(' ')
|
whats = what.split(' ')
|
||||||
|
@ -156,10 +141,7 @@ class IrcAdmin(Module):
|
||||||
connection.nick(newnick)
|
connection.nick(newnick)
|
||||||
self.config.set('IRC', 'nick', newnick)
|
self.config.set('IRC', 'nick', newnick)
|
||||||
replystr = 'changed nickname'
|
replystr = 'changed nickname'
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
|
|
||||||
# Save the config file.
|
# Save the config file.
|
||||||
def save_config(self):
|
def save_config(self):
|
||||||
|
|
|
@ -88,10 +88,7 @@ class Seen(Module):
|
||||||
seendata = self.config.get('seen', query).split('|:|')
|
seendata = self.config.get('seen', query).split('|:|')
|
||||||
converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc())
|
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] + '\''
|
replystr = 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\''
|
||||||
if replypath is None:
|
return self.reply(connection, replypath, replystr)
|
||||||
return replystr
|
|
||||||
else:
|
|
||||||
connection.privmsg(replypath, replystr)
|
|
||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
|
|
Loading…
Reference in New Issue