Switched the togglesnarfers to the new Toggleable mixin

This commit is contained in:
James Vega 2003-10-29 06:06:56 +00:00
parent f0b850118b
commit 535cdde13c
6 changed files with 51 additions and 219 deletions

View File

@ -91,59 +91,24 @@ def configure(onStart, afterConnect, advanced):
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')
onStart.append('Bugzilla toggle bug off')
class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
class Bugzilla(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
"""Show a link to a bug report with a brief description"""
threaded = True
regexps = ['bzSnarfer']
toggles = plugins.ToggleDictionary({'bug' : True})
def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self)
#plugins.Toggleabel.__init__(self)
self.entre = re.compile('&(\S*?);')
self.db = makeDb(dbfilename)
self.snarfers = {'bug' : True}
def die(self):
self.db.close()
del self.db
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)))
_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):
"""shorthand url description
Add a bugzilla to the list of defined bugzillae.
@ -204,7 +169,7 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
return
def bzSnarfer(self, irc, msg, match):
r"(.*)/show_bug.cgi\?id=([0-9]+)"
if not self.snarfers['bug']:
if not self.toggles.get('bug', channel=msg.args[0]):
return
queryurl = '%s/xml.cgi?id=%s' % (match.group(1), match.group(2))
try:

View File

@ -43,6 +43,7 @@ import plugins
import conf
import debug
import utils
import plugins
import ircutils
import privmsgs
import callbacks
@ -61,22 +62,24 @@ def configure(onStart, afterConnect, advanced):
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')
onStart.append('Ebay toggle auction off')
example = utils.wrapLines("""
Add an example IRC session using this module here.
""")
class Ebay(callbacks.PrivmsgCommandAndRegexp):
class Ebay(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
"""
Module for eBay stuff. Currently contains a URL snarfer and a command to
get info about an auction.
"""
threaded = True
regexps = ['ebaySnarfer']
toggles = plugins.ToggleDictionary({'auction' : True})
def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.snarfers = {'auction' : True}
#plugins.Toggleable.__init__(self)
_reopts = re.I | re.S
_info = re.compile(r'<title>eBay item (\d+) \([^)]+\) - ([^<]+)</title>',
@ -115,43 +118,6 @@ 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 _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)))
_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):
"""[--link] <item>
@ -174,7 +140,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.snarfers['auction']:
if not self.toggles.get('auction', channel=msg.args[0]):
return
url = match.group(0)
self._getResponse(irc, msg, url, snarf = True)

View File

@ -42,6 +42,7 @@ import urllib2
import conf
import debug
import utils
import plugins
import ircutils
import privmsgs
import callbacks
@ -54,12 +55,17 @@ 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 Gameknot')
print 'The Gameknot 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 Gameknot snarfer enabled by default?') == 'n':
onStart.append('Gameknot togglesnarfer')
if advanced:
print 'The Gameknot 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 Gameknot stats snarfer enabled by default?') ==\
'n':
onStart.append('Gameknot toggle stat off')
if yn('Do you want the Gameknot Game links snarfer enabled by '\
'default?') == 'n':
onStart.append('Gameknot toggle stat off')
example = utils.wrapLines("""
@ -73,13 +79,14 @@ example = utils.wrapLines("""
<supybot> Challenge from ddipaolo: inkedmn (901; W-69, L-84, D-4) vs. ddipaolo (1159; W-135, L-136, D-8); inkedmn to move. <http://gameknot.com/chess.pl?bd=1038943>
""")
class Gameknot(callbacks.PrivmsgCommandAndRegexp):
class Gameknot(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
threaded = True
regexps = ['gameknotSnarfer', 'gameknotStatsSnarfer']
toggles = plugins.ToggleDictionary({'game' : True,
'stat' : True})
def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.snarfers = {'game' : True,
'stat' : True}
#plugins.Toggleable.__init__(self)
_gkrating = re.compile(r'<font color="#FFFF33">(\d+)</font>')
_gkgames = re.compile(r's:&nbsp;&nbsp;</td><td class=sml>(\d+)</td></tr>')
@ -164,43 +171,6 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp):
name = privmsgs.getArgs(args)
irc.reply(msg, self.getStats(name))
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)))
_enable = ('on', 'enable')
_disable = ('off', 'disable')
def togglesnarfer(self, irc, msg, args):
"""<game|stat> [<on|off>]
Toggles the snarfer that responds to Gameknot game links or stat 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')
_gkPlayer = re.compile(r"popd\('(Rating[^']+)'\).*?>([^<]+)<")
_gkRating = re.compile(r": (\d+)[^:]+:<br>(\d+)[^,]+, (\d+)[^,]+, (\d+)")
_gkGameTitle = re.compile(r"<p><b>(.*?)\s*</b>&nbsp;\s*<span.*?>\(started")
@ -208,7 +178,7 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp):
_gkReason = re.compile(r'won\s+\(\S+\s+(\S+)\)')
def gameknotSnarfer(self, irc, msg, match):
r"http://(?:www\.)?gameknot\.com/chess\.pl\?bd=\d+(&r=\d+)?"
if not self.snarfers['stat']:
if not self.toggles.get('game', channel=msg.args[0]):
return
#debug.printf('Got a GK URL from %s' % msg.prefix)
url = match.group(0)
@ -261,7 +231,7 @@ class Gameknot(callbacks.PrivmsgCommandAndRegexp):
def gameknotStatsSnarfer(self, irc, msg, match):
r"http://gameknot\.com/stats\.pl\?([^&]+)"
if not self.snarfers['game']:
if not self.toggles.get('stat', channel=msg.args[0]):
return
name = match.group(1)
s = self.getStats(name)

View File

@ -48,6 +48,7 @@ import conf
import debug
import utils
import ircmsgs
import plugins
import ircutils
import privmsgs
import callbacks
@ -88,10 +89,10 @@ def configure(onStart, afterConnect, advanced):
print 'snarfing and a google search snarfer.\n'
if yn('Do you want the Google Groups link snarfer enabled by '\
'default?') == 'n':
onStart.append('Google togglesnarfer groups off')
onStart.append('Google toggle groups off')
if yn('Do you want the Google search snarfer enabled by default?')\
== 'y':
onStart.append('Google togglesnarfer search on')
onStart.append('Google toggle search on')
else:
print 'You\'ll need to get a key before you can use this plugin.'
print 'You can apply for a key at http://www.google.com/apis/'
@ -135,15 +136,16 @@ def search(*args, **kwargs):
else:
raise
class Google(callbacks.PrivmsgCommandAndRegexp):
class Google(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
threaded = True
regexps = sets.Set(['googleSnarfer', 'googleGroups'])
toggles = plugins.ToggleDictionary({'groups' : True,
'search' : False})
def __init__(self):
super(self.__class__, self).__init__()
#plugins.Toggleable.__init__(self)
self.total = 0
self.totalTime = 0
self.snarfers = {'groups' : True,
'search' : False}
self.last24hours = structures.queue()
def formatData(self, data):
@ -174,43 +176,6 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
irc.reply(msg, conf.replySuccess)
licensekey = privmsgs.checkCapability(licensekey, 'admin')
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)))
_enable = ('on', 'enable')
_disable = ('off', 'disable')
def togglesnarfer(self, irc, msg, args):
"""<groups|search> [<on|off>]
Toggles the snarfer that responds to Google Groups links or Google
searches. 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 google(self, irc, msg, args):
"""<search> [--{language,restrict}=<value>] [--{notsafe,similar}]
@ -312,7 +277,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
def googleSnarfer(self, irc, msg, match):
r"^google\s+(.*)$"
if not self.snarfers['search']:
if not self.toggles.get('search', channel=msg.args[0]):
return
searchString = match.group(1)
try:
@ -329,7 +294,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
_ggGroup = re.compile(r'Newsgroups: <a[^>]+>([^<]+)</a>')
def googleGroups(self, irc, msg, match):
r"http://groups.google.com/[^\s]+"
if not self.snarfers['groups']:
if not self.toggles.get('groups', channel=msg.args[0]):
return
request = urllib2.Request(match.group(0), headers=\
{'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'})

View File

@ -42,6 +42,7 @@ from itertools import ifilter
import conf
import debug
import utils
import plugins
import ircutils
import privmsgs
import callbacks
@ -60,7 +61,7 @@ def configure(onStart, afterConnect, advanced):
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 Sourceforge snarfer enabled by default?') =='n':
onStart.append('Sourceforge togglesnarfer tracker off')
onStart.append('Sourceforge toggle tracker off')
print 'The bugs and rfes commands of the Sourceforge plugin can be set'
print 'to query a default project when no project is specified. If this'
@ -87,7 +88,7 @@ in 0.71, Bug #820961: dock icon doesn't show up with..., Bug #820879: Cannot con
< supybot> jamessan|work: Improve CLI interface <http://sourceforge.net/tracker/index.php?func=detail&aid=720757&group_id=75946&atid=545548>
""")
class Sourceforge(callbacks.PrivmsgCommandAndRegexp):
class Sourceforge(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
"""
Module for Sourceforge stuff. Currently contains commands to query a
project's most recent bugs and rfes.
@ -109,10 +110,12 @@ class Sourceforge(callbacks.PrivmsgCommandAndRegexp):
'Submitted by':_submitted, 'Priority':_priority,
'Status':_status}
toggles = plugins.ToggleDictionary({'tracker' : True})
project = None
def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.snarfers = {'tracker' : True}
self.project = None
#plugins.Toggleable.__init__(self)
def _formatResp(self, num, text):
"""
@ -142,43 +145,6 @@ class Sourceforge(callbacks.PrivmsgCommandAndRegexp):
irc.reply(msg, conf.replySuccess)
setdefault = privmsgs.checkCapability(setdefault, 'admin')
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)))
_enable = ('on', 'enable')
_disable = ('off', 'disable')
def togglesnarfer(self, irc, msg, args):
"""<tracker> [<on|off>]
Toggles the snarfer that responds to Sourceforge Tracker 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 _getTrackerInfo(self, irc, msg, url, regex, num):
try:
fd = urllib2.urlopen(url)
@ -277,7 +243,7 @@ class Sourceforge(callbacks.PrivmsgCommandAndRegexp):
_linkType = re.compile(r'(\w+ \w+|\w+): Tracker Detailed View', re.I)
def sfSnarfer(self, irc, msg, match):
r"https?://(?:www\.)?(?:sourceforge|sf)\.net/tracker/(?:index\.php)?\?(?:&?func=detail|&?aid=\d+|&?group_id=\d+|&?atid=\d+){4}"
if not self.snarfers['tracker']:
if not self.toggles.get('tracker', channel=msg.args[0]):
return
url = match.group(0)
fd = urllib2.urlopen(url)

View File

@ -88,14 +88,14 @@ class SourceforgeTest(PluginTestCase, PluginDocumentation):
self.assertNoResponse('https://sourceforge.net/tracker/?'\
'group_id=58965&atid=489447')
def testDisablesfsnarfer(self):
def testToggle(self):
s = r'.*Status.*: \w+'
self.assertRegexp('http://sourceforge.net/tracker/index.php?'\
'func=detail&aid=540223&group_id=235&atid=300235', s)
self.assertNotError('togglesnarfer tracker off')
self.assertNotError('Sourceforge toggle tracker off')
self.assertNoResponse('http://sourceforge.net/tracker/index.php?'\
'func=detail&aid=540223&group_id=235&atid=300235')
self.assertNotError('togglesnarfer tracker on')
self.assertNotError('Sourceforge toggle tracker on')
self.assertRegexp('http://sourceforge.net/tracker/index.php?'\
'func=detail&aid=540223&group_id=235&atid=300235', s)