Converted to Configurable. Also reverted the _urlRe back to the old version, which works better. I don't care that parentheses are valid URL characters, they simply never happen in practice. Practicality beats Purity.

This commit is contained in:
Jeremy Fincher 2003-11-08 09:26:33 +00:00
parent c434925798
commit 3275859597
2 changed files with 31 additions and 33 deletions

View File

@ -62,15 +62,19 @@ def configure(onStart, afterConnect, advanced):
from questions import expect, anything, something, yn
onStart.append('load URL')
class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
toggles = plugins.ToggleDictionary({'tinysnarf':True,
'tinyreply':True})
_maxUrlLen = 46
class URL(callbacks.Privmsg, plugins.Configurable, plugins.ChannelDBHandler):
configurables = plugins.ConfigurableDictionary(
[('tinyurl-snarfer', plugins.ConfigurableTypes.bool, True,
"""Determines whether the bot will output shorter versions of URLs
longer than the tinyurl-minimum-length config variable."""),
('tinyurl-minimum-length', plugins.ConfigurableTypes.int, 46,
"""The minimum length a URL must be before the tinyurl-snarfer will
snarf it and offer a tinyurl replacement."""),]
)
def __init__(self):
self.nextMsgs = {}
callbacks.Privmsg.__init__(self)
plugins.ChannelDBHandler.__init__(self)
plugins.Toggleable.__init__(self)
def makeDb(self, filename):
if os.path.exists(filename):
@ -97,7 +101,7 @@ class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
db.commit()
return db
_urlRe = re.compile(r"([-a-z0-9+.]+://[-\w=#!*()',$;&/@:%?.~]+)", re.I)
_urlRe = re.compile(r"([^\[<(\s]+://[^\])>\s]+)", re.I)
def doPrivmsg(self, irc, msg):
channel = msg.args[0]
db = self.getDb(channel)
@ -125,20 +129,23 @@ class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
(NULL, %s, %s, %s, %s, %s, '', %s, %s, %s)""",
url, added, addedBy, msg.args[1], previousMsg,
protocol, site, filename)
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
len(url) > self._maxUrlLen:
channel = msg.args[0]
snarf = self.configurables.get('tinyurl-snarfer', channel)
minlen = self.configurables.get('tinyurl-minimum-length', channel)
if snarf and len(url) > minlen:
cursor.execute("""SELECT id FROM urls WHERE url=%s AND
added=%s AND added_by=%s""", url, added, addedBy)
added=%s AND added_by=%s""",
url, added, addedBy)
if cursor.rowcount != 0:
#debug.printf(url)
tinyurl = self._getTinyUrl(url)
if tinyurl:
id = int(cursor.fetchone()[0])
cursor.execute("""INSERT INTO tinyurls VALUES
(NULL, %s, %s)""", id, tinyurl)
if self.toggles.get('tinyreply', channel=msg.args[0]):
irc.queueMsg(callbacks.reply(msg, '%s (was <%s>)' %
(ircutils.bold(tinyurl), url), prefixName=False))
cursor.execute("""INSERT INTO tinyurls
VALUES (NULL, %s, %s)""",id, tinyurl)
tinyurl = ircutils.bold(tinyurl)
s = '%s (was <%s>)' % (tinyurl, url)
irc.queueMsg(callbacks.reply(msg, s, prefixName=False))
key = (msg.nick, channel)
self.nextMsgs.setdefault(key, []).append((url, added))
db.commit()
@ -193,13 +200,13 @@ class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
Returns a TinyURL.com version of <url>
"""
url = privmsgs.getArgs(args)
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
self.toggles.get('tinyreply', channel=msg.args[0]):
if self.configurables.get('tinyurl-snarfer', channel=msg.args[0]):
return
url = self._getTinyUrl(url, cmd=True)
if not url:
irc.error(msg, 'Could not parse the TinyURL.com results page. '\
'(%s)' % conf.replyPossibleBug)
s = 'Could not parse the TinyURL.com results page. (%s)' % \
conf.replyPossibleBug
irc.error(msg, s)
else:
irc.reply(msg, url)

View File

@ -75,8 +75,7 @@ if sqlite is not None:
class URLTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('URL',)
def test(self):
self.assertNotError('toggle tinyreply off')
self.assertNotError('toggle tinysnarf off')
self.assertNotError('config tinyurlsnarfer off')
counter = 0
self.assertNotError('url random')
for url in urls:
@ -93,38 +92,30 @@ if sqlite is not None:
self.assertNotError('url random')
def testDefaultNotFancy(self):
self.assertNotError('url toggle tinyreply off')
self.assertNotError('url toggle tinysnarf off')
self.assertNotError('url config tinyurlsnarfer off')
self.feedMsg(urls[0])
self.assertResponse('url last', urls[0])
def testAction(self):
self.assertNotError('url toggle tinyreply off')
self.assertNotError('url toggle tinysnarf off')
self.assertNotError('url config tinyurlsnarfer off')
self.irc.feedMsg(ircmsgs.action(self.channel, urls[1]))
self.assertNotRegexp('url last', '\\x01')
def testTinyurl(self):
self.assertNotError('url toggle tinyreply on')
self.assertNotError('url toggle tinysnarf off')
self.assertNotError('url config tinyurlsnarfer off')
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
'func=add&group_id=58965&atid=489447',
r'http://tinyurl.com/\w{4}')
self.assertNotError('url toggle tinysnarf on')
self.assertNotError('url config tinyurlsnarfer on')
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
'func=add&group_id=58965&atid=489447',
r'http://tinyurl.com/\w{4}')
self.assertNotError('toggle tinyreply off')
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
'func=add&group_id=58965&atid=489447',
r'http://tinyurl.com/\w{4}')
def testTinysnarf(self):
self.assertNotError('url toggle tinyreply off')
self.assertNotError('url toggle tinysnarf on')
self.assertNoResponse('http://sourceforge.net/tracker/?'\
'func=add&group_id=58965&atid=489447')
self.assertNotError('url toggle tinyreply on')
self.assertNotError('url config tinyurlsnarfer on')
self.assertRegexp('http://sourceforge.net/tracker/?'\
'func=add&group_id=58965&atid=489447',
r'http://tinyurl.com/\w{4}.* \(was')