Reworked the togglesnarfer mechanism

This commit is contained in:
James Vega 2003-10-27 22:57:05 +00:00
parent 8ff643d540
commit 1e4879dfb1
2 changed files with 87 additions and 33 deletions

View File

@ -84,12 +84,13 @@ class BugError(Exception):
def configure(onStart, afterConnect, advanced): def configure(onStart, afterConnect, advanced):
from questions import expect, anything, yn from questions import expect, anything, yn
onStart.append('load Bugzilla') onStart.append('load Bugzilla')
if advanced:
print 'The Bugzilla plugin has the functionality to watch for URLs' print 'The Bugzilla plugin has the functionality to watch for URLs'
print 'that match a specific pattern (we call this a snarfer). When' 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 'supybot sees such a URL, he will parse the web page for'
print 'and reply with the results.\n' print 'information and reply with the results.\n'
if yn('Do you want the Bugzilla snarfer enabled by default?') == 'n': if yn('Do you want the Bugzilla snarfer enabled by default?') == 'n':
onStart.append('Bugzilla togglesnarfer') onStart.append('Bugzilla togglesnarfer bug off')
class Bugzilla(callbacks.PrivmsgCommandAndRegexp): class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
"""Show a link to a bug report with a brief description""" """Show a link to a bug report with a brief description"""
@ -99,23 +100,49 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.entre = re.compile('&(\S*?);') self.entre = re.compile('&(\S*?);')
self.db = makeDb(dbfilename) self.db = makeDb(dbfilename)
self.snarfer = True self.snarfers = {'bug' : True}
def die(self): def die(self):
self.db.close() self.db.close()
del self.db del self.db
def togglesnarfer(self, irc, msg, args): def _toggleHelper(self, irc, msg, state, snarfer):
"""takes no argument if not state:
self.snarfers[snarfer] = not self.snarfers[snarfer]
Disables the snarfer that responds to all Bugzilla links elif state in self._enable:
""" self.snarfers[snarfer] = True
self.snarfer = not self.snarfer elif state in self._disable:
if self.snarfer: self.snarfers[snarfer] = False
irc.reply(msg, '%s (Snarfer is enabled)' % conf.replySuccess) resp = []
for k in self.snarfers:
if self.snarfers[k]:
resp.append('%s%s: On' % (k[0].upper(), k[1:]))
else: else:
irc.reply(msg, '%s (Snarfer is disabled)' % conf.replySuccess) resp.append('%s%s: Off' % (k[0].upper(), k[1:]))
togglesnarfer=privmsgs.checkCapability(togglesnarfer,'admin') irc.reply(msg, '%s (%s)' % (conf.replySuccess, '; '.join(resp)))
_enable = ('on', 'enable')
_disable = ('off', 'disable')
def togglesnarfer(self, irc, msg, args):
"""<bug> [<on|off>]
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.
"""
(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): def addzilla(self, irc, msg, args):
"""shorthand url description """shorthand url description
Add a bugzilla to the list of defined bugzillae. Add a bugzilla to the list of defined bugzillae.
@ -176,7 +203,7 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
return return
def bzSnarfer(self, irc, msg, match): def bzSnarfer(self, irc, msg, match):
r"(.*)/show_bug.cgi\?id=([0-9]+)" r"(.*)/show_bug.cgi\?id=([0-9]+)"
if not self.snarfer: if not self.snarfers['bug']:
return return
queryurl = '%s/xml.cgi?id=%s' % (match.group(1), match.group(2)) queryurl = '%s/xml.cgi?id=%s' % (match.group(1), match.group(2))
try: try:

View File

@ -55,12 +55,13 @@ def configure(onStart, afterConnect, advanced):
# commands you would like to be run when the bot has finished connecting. # commands you would like to be run when the bot has finished connecting.
from questions import expect, anything, something, yn from questions import expect, anything, something, yn
onStart.append('load Ebay') onStart.append('load Ebay')
if advanced:
print 'The Ebay plugin has the functionality to watch for URLs' print 'The Ebay plugin has the functionality to watch for URLs'
print 'that match a specific pattern (we call this a snarfer). When' 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 'supybot sees such a URL, he will parse the web page for'
print 'and reply with the results.\n' print 'information and reply with the results.\n'
if yn('Do you want the Ebay snarfer enabled by default?') == 'n': if yn('Do you want the Ebay snarfer enabled by default?') == 'n':
onStart.append('Ebay togglesnarfer') onStart.append('Ebay togglesnarfer auction off')
example = utils.wrapLines(""" example = utils.wrapLines("""
Add an example IRC session using this module here. Add an example IRC session using this module here.
@ -75,7 +76,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp):
regexps = ['ebaySnarfer'] regexps = ['ebaySnarfer']
def __init__(self): def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.snarfer = True self.snarfers = {'auction' : True}
_reopts = re.I | re.S _reopts = re.I | re.S
_info = re.compile(r'<title>eBay item (\d+) \([^)]+\) - ([^<]+)</title>', _info = re.compile(r'<title>eBay item (\d+) \([^)]+\) - ([^<]+)</title>',
@ -114,16 +115,42 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp):
_getSeller = lambda self, s: '%s: %s (%s)' % (ircutils.bold('Seller'), _getSeller = lambda self, s: '%s: %s (%s)' % (ircutils.bold('Seller'),
self._seller.search(s).group(1), self._seller.search(s).group(2)) self._seller.search(s).group(1), self._seller.search(s).group(2))
def togglesnarfer(self, irc, msg, args): def _toggleHelper(self, irc, msg, state, snarfer):
"""takes no argument if not state:
self.snarfers[snarfer] = not self.snarfers[snarfer]
Disables the snarfer that responds to all Sourceforge Tracker links elif state in self._enable:
""" self.snarfers[snarfer] = True
self.snarfer = not self.snarfer elif state in self._disable:
if self.snarfer: self.snarfers[snarfer] = False
irc.reply(msg, '%s (Snarfer is enabled)' % conf.replySuccess) resp = []
for k in self.snarfers:
if self.snarfers[k]:
resp.append('%s%s: On' % (k[0].upper(), k[1:]))
else: else:
irc.reply(msg, '%s (Snarfer is disabled)' % conf.replySuccess) resp.append('%s%s: Off' % (k[0].upper(), k[1:]))
irc.reply(msg, '%s (%s)' % (conf.replySuccess, '; '.join(resp)))
_enable = ('on', 'enable')
_disable = ('off', 'disable')
def togglesnarfer(self, irc, msg, args):
"""<auction> [<on|off>]
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.
"""
(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): def ebay(self, irc, msg, args):
"""[--link] <item> """[--link] <item>
@ -147,7 +174,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp):
def ebaySnarfer(self, irc, msg, match): def ebaySnarfer(self, irc, msg, match):
r"http://cgi\.ebay\.com/ws/eBayISAPI\.dll\?ViewItem(?:&item=\d+|"\ r"http://cgi\.ebay\.com/ws/eBayISAPI\.dll\?ViewItem(?:&item=\d+|"\
"&category=\d+)+" "&category=\d+)+"
if not self.snarfer: if not self.snarfers['auction']:
return return
url = match.group(0) url = match.group(0)
self._getResponse(irc, msg, url, snarf = True) self._getResponse(irc, msg, url, snarf = True)