From c23d205928499b578e0742363dbfd8dc0116f394 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 26 May 2010 18:33:03 +0200 Subject: Replaced module's __call__ with a keyword mechanism. --- modules/anidb.py | 26 ++++++++++++++------------ modules/google_search.py | 26 +++++++++++++------------- modules/quotes.py | 9 +++------ modules/tracking.py | 14 ++++++-------- 4 files changed, 36 insertions(+), 39 deletions(-) (limited to 'modules') diff --git a/modules/anidb.py b/modules/anidb.py index 7a5a761..3c8302e 100644 --- a/modules/anidb.py +++ b/modules/anidb.py @@ -20,7 +20,8 @@ for option in ('username', 'password'): class Module: def __init__(self, bot): self.irc = bot - self.db = PgSQL.connect(database = 'fot') + self.db = PgSQL.connect(database = 'fot', host = 'komachi.') + self.irc.register_keyword('!anidb', self) def get_aid(self, msg, short = False): if msg.isdigit(): @@ -85,9 +86,11 @@ class Module: aid) def get_anime(self, msg): - short = msg.startswith('-s ') + short = msg.startswith('-s') if short: msg = ' '.join(msg.split()[1:]) + if not len(msg): + return 'Usage: !anidb [-s] search|aid' aid = self.get_aid(msg, short) if len(aid) == 1: return self.get_info(aid[0][0]) @@ -97,16 +100,15 @@ class Module: else: return 'anidb: %s%s' % ('%d/%d results: ' % (5, len(aid)) if len(aid) > 5 else '', ', '.join(['%s (%d)' % (x[1], x[0]) for x in aid[:5]])) - def __call__(self, nick, channel, msg): - if msg.startswith('!anidb'): - target = channel if not channel == self.irc.nickname else nick.split('!')[0] - args = msg.split() - if len(args) == 1: - self.irc.msg(target, 'Usage: !anidb [-s] search|aid') - return - info = self.get_anime(' '.join(args[1:])) - if info: - self.irc.msg(target, info) + def keyword(self, nick, channel, kw, msg): + target = channel if not channel == self.irc.nickname else nick.split('!')[0] + args = msg.split() + if len(args) == 0: + self.irc.msg(target, 'Usage: !anidb [-s] search|aid') + return + info = self.get_anime(' '.join(args)) + if info: + self.irc.msg(target, info) if __name__ == '__main__': import sys diff --git a/modules/google_search.py b/modules/google_search.py index 625cdf2..8af2c62 100644 --- a/modules/google_search.py +++ b/modules/google_search.py @@ -9,6 +9,7 @@ import urllib, urllib2, simplejson class Module: def __init__(self, bot): self.irc = bot + self.irc.register_keyword('!g', self) def search(self, s): try: @@ -24,19 +25,18 @@ class Module: return return (response['results'][0]['titleNoFormatting'], response['results'][0]['url']) - def __call__(self, nick, channel, msg): - if msg.startswith('!g'): - target = channel if not channel == self.irc.nickname else nick.split('!')[0] - args = msg.split() - if len(args) == 1: - self.irc.msg(target, 'Usage: !g search') - return - results = self.search(' '.join(args[1:])) - if results: - results = '\002%s\002 %s' % results - self.irc.msg(target, results.encode('utf-8')) - else: - self.irc.msg(target, 'No results.') + def keyword(self, nick, channel, kw, msg): + target = channel if not channel == self.irc.nickname else nick.split('!')[0] + args = msg.split() + if len(args) == 0: + self.irc.msg(target, 'Usage: !g search') + return + results = self.search(' '.join(args)) + if results: + results = '\002%s\002 %s' % results + self.irc.msg(target, results.encode('utf-8')) + else: + self.irc.msg(target, 'No results.') if __name__ == '__main__': import sys diff --git a/modules/quotes.py b/modules/quotes.py index a5060cd..54cacd9 100644 --- a/modules/quotes.py +++ b/modules/quotes.py @@ -16,7 +16,7 @@ class Quotes: return q def connect(self, db): - self.db = PgSQL.connect(database = 'fot') + self.db = PgSQL.connect(database = 'fot', host = 'komachi.') self.updatecount() def updatecount(self): @@ -201,13 +201,10 @@ quote_handler = IRCHandler() class Module: def __init__(self, bot): self.irc = bot + self.irc.register_keyword('!quote', self) - def __call__(self, nick, channel, msg): - if not msg.startswith('!quote'): - return + def keyword(self, nick, channel, kw, msg): args = msg.split(' ') - if msg.startswith('!quote'): - args = args[1:] cmd = args[0] if len(args) and len(args[0].strip()) else 'random' args = args[1:] diff --git a/modules/tracking.py b/modules/tracking.py index 94b8d86..aa33980 100644 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -54,6 +54,7 @@ class Module: def __init__(self, bot): self.setup_db() self.irc = bot + self.irc.register_keyword('!track', self) self.tracking = [] self.lc = LoopingCall(self.lc_callback) if bot: @@ -200,21 +201,18 @@ class Module: session.close() return msg - def __call__(self, nick, channel, msg): - if not msg.startswith('!track'): - return - + def keyword(self, nick, channel, kw, msg): args = msg.split() - if len(args) < 2: + if len(args) < 1: self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'Usage: !track (start|stop|status TRACKINGNO)|list') return - mode = args[1] + mode = args[0] if mode.lower() in ('start', 'stop', 'status'): - if len(args) != 3 and mode.lower() != 'status': + if len(args) != 2 and mode.lower() != 'status': self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'Usage: !track (start|stop|status TRACKINGNO)|list') return - code = args[2] if len(args) == 3 else '' + code = args[1] if len(args) == 2 else '' msg = None if mode.lower() == 'start': -- cgit v1.2.3