diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-26 18:33:03 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-26 18:33:03 +0200 |
commit | c23d205928499b578e0742363dbfd8dc0116f394 (patch) | |
tree | 3b28becd52bd3cee3af26b6bcbd484df8bc4dd9f | |
parent | b4b59bf076d8d4ec22b489174707ecda72a1c06e (diff) |
Replaced module's __call__ with a keyword mechanism.
-rwxr-xr-x | fot.py | 18 | ||||
-rw-r--r-- | modules/anidb.py | 26 | ||||
-rw-r--r-- | modules/google_search.py | 26 | ||||
-rw-r--r-- | modules/quotes.py | 9 | ||||
-rw-r--r-- | modules/tracking.py | 14 |
5 files changed, 51 insertions, 42 deletions
@@ -56,10 +56,16 @@ class Bot(irc.IRCClient): def __init__(self): bots.append(self) self.modules = {} + self.keywords = {} def __repr__(self): return '<Bot %s@%s>' % (self.nickname, self.factory.server) + def register_keyword(self, kw, mod): + if kw in self.keywords and self.keywords[kw] != mod: + raise Exception('Keyword "%s" already in use' % kw) + self.keywords[kw] = mod + def apply_modules(self): # Call stop() for each module if neccessary (can't rely on __del__ here) for m in self.modules.itervalues(): @@ -80,9 +86,15 @@ class Bot(irc.IRCClient): self.join(chan) def privmsg(self, nick, channel, msg): - for m in self.modules.keys(): - if (config.has_option(self.factory.server, m) and channel in config.get(self.factory.server, m)) or (channel == self.nickname and nick.split('!')[0] != self.nickname): - self.modules[m](nick, channel, msg) + msg = msg.split(None, 1) + if not len(msg): + return + elif len(msg) == 1: + kw, msg = msg[0], '' + else: + kw, msg = msg + if kw in self.keywords: + self.keywords[kw].keyword(nick, channel, kw, msg) def kickedFrom(self, channel, kicker, message): self.join(channel) 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': |