From 1e4879dfb1e265a2f3f8816729f84845252b0f3e Mon Sep 17 00:00:00 2001 From: James Vega Date: Mon, 27 Oct 2003 22:57:05 +0000 Subject: [PATCH] Reworked the togglesnarfer mechanism --- plugins/Bugzilla.py | 61 ++++++++++++++++++++++++++++++++------------- plugins/Ebay.py | 59 +++++++++++++++++++++++++++++++------------ 2 files changed, 87 insertions(+), 33 deletions(-) diff --git a/plugins/Bugzilla.py b/plugins/Bugzilla.py index d7c1c990d..40be81109 100644 --- a/plugins/Bugzilla.py +++ b/plugins/Bugzilla.py @@ -84,12 +84,13 @@ class BugError(Exception): def configure(onStart, afterConnect, advanced): from questions import expect, anything, yn onStart.append('load Bugzilla') - print 'The Bugzilla plugin has the functionality to watch for URLs' - print 'that match a specific pattern (we call this a snarfer). When' - print 'supybot sees such a URL, he will parse the web page for information' - print 'and reply with the results.\n' - if yn('Do you want the Bugzilla snarfer enabled by default?') == 'n': - onStart.append('Bugzilla togglesnarfer') + if advanced: + print 'The Bugzilla plugin has the functionality to watch for URLs' + print 'that match a specific pattern (we call this a snarfer). When' + print 'supybot sees such a URL, he will parse the web page for' + print 'information and reply with the results.\n' + if yn('Do you want the Bugzilla snarfer enabled by default?') == 'n': + onStart.append('Bugzilla togglesnarfer bug off') class Bugzilla(callbacks.PrivmsgCommandAndRegexp): """Show a link to a bug report with a brief description""" @@ -99,23 +100,49 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp): callbacks.PrivmsgCommandAndRegexp.__init__(self) self.entre = re.compile('&(\S*?);') self.db = makeDb(dbfilename) - self.snarfer = True + self.snarfers = {'bug' : True} def die(self): self.db.close() del self.db - def togglesnarfer(self, irc, msg, args): - """takes no argument + def _toggleHelper(self, irc, msg, state, snarfer): + if not state: + self.snarfers[snarfer] = not self.snarfers[snarfer] + elif state in self._enable: + self.snarfers[snarfer] = True + elif state in self._disable: + self.snarfers[snarfer] = False + resp = [] + for k in self.snarfers: + if self.snarfers[k]: + resp.append('%s%s: On' % (k[0].upper(), k[1:])) + else: + resp.append('%s%s: Off' % (k[0].upper(), k[1:])) + irc.reply(msg, '%s (%s)' % (conf.replySuccess, '; '.join(resp))) - Disables the snarfer that responds to all Bugzilla links + _enable = ('on', 'enable') + _disable = ('off', 'disable') + def togglesnarfer(self, irc, msg, args): + """ [] + + Toggles the snarfer that responds to Bugzilla-style bug links. If + nothing is specified, all snarfers will have their states + toggled (on -> off, off -> on). If only a state is specified, all + snarfers will have their state set to the specified state. If a + specific snarfer is specified, the changes will apply only to that + snarfer. """ - self.snarfer = not self.snarfer - if self.snarfer: - irc.reply(msg, '%s (Snarfer is enabled)' % conf.replySuccess) - else: - irc.reply(msg, '%s (Snarfer is disabled)' % conf.replySuccess) - togglesnarfer=privmsgs.checkCapability(togglesnarfer,'admin') + (snarfer, state) = privmsgs.getArgs(args, optional=1) + snarfer = snarfer.lower() + state = state.lower() + if snarfer not in self.snarfers: + raise callbacks.ArgumentError + if state and state not in self._enable and state not in self._disable: + raise callbacks.ArgumentError + self._toggleHelper(irc, msg, state, snarfer) + togglesnarfer=privmsgs.checkCapability(togglesnarfer, 'admin') + def addzilla(self, irc, msg, args): """shorthand url description Add a bugzilla to the list of defined bugzillae. @@ -176,7 +203,7 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp): return def bzSnarfer(self, irc, msg, match): r"(.*)/show_bug.cgi\?id=([0-9]+)" - if not self.snarfer: + if not self.snarfers['bug']: return queryurl = '%s/xml.cgi?id=%s' % (match.group(1), match.group(2)) try: diff --git a/plugins/Ebay.py b/plugins/Ebay.py index 82ab33187..c17ed1b92 100644 --- a/plugins/Ebay.py +++ b/plugins/Ebay.py @@ -55,12 +55,13 @@ def configure(onStart, afterConnect, advanced): # commands you would like to be run when the bot has finished connecting. from questions import expect, anything, something, yn onStart.append('load Ebay') - print 'The Ebay plugin has the functionality to watch for URLs' - print 'that match a specific pattern (we call this a snarfer). When' - print 'supybot sees such a URL, he will parse the web page for information' - print 'and reply with the results.\n' - if yn('Do you want the Ebay snarfer enabled by default?') == 'n': - onStart.append('Ebay togglesnarfer') + if advanced: + print 'The Ebay plugin has the functionality to watch for URLs' + print 'that match a specific pattern (we call this a snarfer). When' + print 'supybot sees such a URL, he will parse the web page for' + print 'information and reply with the results.\n' + if yn('Do you want the Ebay snarfer enabled by default?') == 'n': + onStart.append('Ebay togglesnarfer auction off') example = utils.wrapLines(""" Add an example IRC session using this module here. @@ -75,7 +76,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp): regexps = ['ebaySnarfer'] def __init__(self): callbacks.PrivmsgCommandAndRegexp.__init__(self) - self.snarfer = True + self.snarfers = {'auction' : True} _reopts = re.I | re.S _info = re.compile(r'eBay item (\d+) \([^)]+\) - ([^<]+)', @@ -114,16 +115,42 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp): _getSeller = lambda self, s: '%s: %s (%s)' % (ircutils.bold('Seller'), self._seller.search(s).group(1), self._seller.search(s).group(2)) - def togglesnarfer(self, irc, msg, args): - """takes no argument + def _toggleHelper(self, irc, msg, state, snarfer): + if not state: + self.snarfers[snarfer] = not self.snarfers[snarfer] + elif state in self._enable: + self.snarfers[snarfer] = True + elif state in self._disable: + self.snarfers[snarfer] = False + resp = [] + for k in self.snarfers: + if self.snarfers[k]: + resp.append('%s%s: On' % (k[0].upper(), k[1:])) + else: + resp.append('%s%s: Off' % (k[0].upper(), k[1:])) + irc.reply(msg, '%s (%s)' % (conf.replySuccess, '; '.join(resp))) - Disables the snarfer that responds to all Sourceforge Tracker links + _enable = ('on', 'enable') + _disable = ('off', 'disable') + def togglesnarfer(self, irc, msg, args): + """ [] + + Toggles the snarfer that responds to eBay auction links. If + nothing is specified, all snarfers will have their states + toggled (on -> off, off -> on). If only a state is specified, all + snarfers will have their state set to the specified state. If a + specific snarfer is specified, the changes will apply only to that + snarfer. """ - self.snarfer = not self.snarfer - if self.snarfer: - irc.reply(msg, '%s (Snarfer is enabled)' % conf.replySuccess) - else: - irc.reply(msg, '%s (Snarfer is disabled)' % conf.replySuccess) + (snarfer, state) = privmsgs.getArgs(args, optional=1) + snarfer = snarfer.lower() + state = state.lower() + if snarfer not in self.snarfers: + raise callbacks.ArgumentError + if state and state not in self._enable and state not in self._disable: + raise callbacks.ArgumentError + self._toggleHelper(irc, msg, state, snarfer) + togglesnarfer=privmsgs.checkCapability(togglesnarfer, 'admin') def ebay(self, irc, msg, args): """[--link] @@ -147,7 +174,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp): def ebaySnarfer(self, irc, msg, match): r"http://cgi\.ebay\.com/ws/eBayISAPI\.dll\?ViewItem(?:&item=\d+|"\ "&category=\d+)+" - if not self.snarfer: + if not self.snarfers['auction']: return url = match.group(0) self._getResponse(irc, msg, url, snarf = True)