mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-06 17:44:09 +01:00
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:
parent
c434925798
commit
3275859597
@ -62,15 +62,19 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
from questions import expect, anything, something, yn
|
from questions import expect, anything, something, yn
|
||||||
onStart.append('load URL')
|
onStart.append('load URL')
|
||||||
|
|
||||||
class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
|
class URL(callbacks.Privmsg, plugins.Configurable, plugins.ChannelDBHandler):
|
||||||
toggles = plugins.ToggleDictionary({'tinysnarf':True,
|
configurables = plugins.ConfigurableDictionary(
|
||||||
'tinyreply':True})
|
[('tinyurl-snarfer', plugins.ConfigurableTypes.bool, True,
|
||||||
_maxUrlLen = 46
|
"""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):
|
def __init__(self):
|
||||||
self.nextMsgs = {}
|
self.nextMsgs = {}
|
||||||
callbacks.Privmsg.__init__(self)
|
callbacks.Privmsg.__init__(self)
|
||||||
plugins.ChannelDBHandler.__init__(self)
|
plugins.ChannelDBHandler.__init__(self)
|
||||||
plugins.Toggleable.__init__(self)
|
|
||||||
|
|
||||||
def makeDb(self, filename):
|
def makeDb(self, filename):
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
@ -97,7 +101,7 @@ class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
|
|||||||
db.commit()
|
db.commit()
|
||||||
return db
|
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):
|
def doPrivmsg(self, irc, msg):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
db = self.getDb(channel)
|
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)""",
|
(NULL, %s, %s, %s, %s, %s, '', %s, %s, %s)""",
|
||||||
url, added, addedBy, msg.args[1], previousMsg,
|
url, added, addedBy, msg.args[1], previousMsg,
|
||||||
protocol, site, filename)
|
protocol, site, filename)
|
||||||
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
|
channel = msg.args[0]
|
||||||
len(url) > self._maxUrlLen:
|
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
|
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:
|
if cursor.rowcount != 0:
|
||||||
#debug.printf(url)
|
#debug.printf(url)
|
||||||
tinyurl = self._getTinyUrl(url)
|
tinyurl = self._getTinyUrl(url)
|
||||||
if tinyurl:
|
if tinyurl:
|
||||||
id = int(cursor.fetchone()[0])
|
id = int(cursor.fetchone()[0])
|
||||||
cursor.execute("""INSERT INTO tinyurls VALUES
|
cursor.execute("""INSERT INTO tinyurls
|
||||||
(NULL, %s, %s)""", id, tinyurl)
|
VALUES (NULL, %s, %s)""",id, tinyurl)
|
||||||
if self.toggles.get('tinyreply', channel=msg.args[0]):
|
tinyurl = ircutils.bold(tinyurl)
|
||||||
irc.queueMsg(callbacks.reply(msg, '%s (was <%s>)' %
|
s = '%s (was <%s>)' % (tinyurl, url)
|
||||||
(ircutils.bold(tinyurl), url), prefixName=False))
|
irc.queueMsg(callbacks.reply(msg, s, prefixName=False))
|
||||||
key = (msg.nick, channel)
|
key = (msg.nick, channel)
|
||||||
self.nextMsgs.setdefault(key, []).append((url, added))
|
self.nextMsgs.setdefault(key, []).append((url, added))
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -193,13 +200,13 @@ class URL(callbacks.Privmsg, plugins.Toggleable, plugins.ChannelDBHandler):
|
|||||||
Returns a TinyURL.com version of <url>
|
Returns a TinyURL.com version of <url>
|
||||||
"""
|
"""
|
||||||
url = privmsgs.getArgs(args)
|
url = privmsgs.getArgs(args)
|
||||||
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
|
if self.configurables.get('tinyurl-snarfer', channel=msg.args[0]):
|
||||||
self.toggles.get('tinyreply', channel=msg.args[0]):
|
|
||||||
return
|
return
|
||||||
url = self._getTinyUrl(url, cmd=True)
|
url = self._getTinyUrl(url, cmd=True)
|
||||||
if not url:
|
if not url:
|
||||||
irc.error(msg, 'Could not parse the TinyURL.com results page. '\
|
s = 'Could not parse the TinyURL.com results page. (%s)' % \
|
||||||
'(%s)' % conf.replyPossibleBug)
|
conf.replyPossibleBug
|
||||||
|
irc.error(msg, s)
|
||||||
else:
|
else:
|
||||||
irc.reply(msg, url)
|
irc.reply(msg, url)
|
||||||
|
|
||||||
|
@ -75,8 +75,7 @@ if sqlite is not None:
|
|||||||
class URLTestCase(ChannelPluginTestCase, PluginDocumentation):
|
class URLTestCase(ChannelPluginTestCase, PluginDocumentation):
|
||||||
plugins = ('URL',)
|
plugins = ('URL',)
|
||||||
def test(self):
|
def test(self):
|
||||||
self.assertNotError('toggle tinyreply off')
|
self.assertNotError('config tinyurlsnarfer off')
|
||||||
self.assertNotError('toggle tinysnarf off')
|
|
||||||
counter = 0
|
counter = 0
|
||||||
self.assertNotError('url random')
|
self.assertNotError('url random')
|
||||||
for url in urls:
|
for url in urls:
|
||||||
@ -93,38 +92,30 @@ if sqlite is not None:
|
|||||||
self.assertNotError('url random')
|
self.assertNotError('url random')
|
||||||
|
|
||||||
def testDefaultNotFancy(self):
|
def testDefaultNotFancy(self):
|
||||||
self.assertNotError('url toggle tinyreply off')
|
self.assertNotError('url config tinyurlsnarfer off')
|
||||||
self.assertNotError('url toggle tinysnarf off')
|
|
||||||
self.feedMsg(urls[0])
|
self.feedMsg(urls[0])
|
||||||
self.assertResponse('url last', urls[0])
|
self.assertResponse('url last', urls[0])
|
||||||
|
|
||||||
def testAction(self):
|
def testAction(self):
|
||||||
self.assertNotError('url toggle tinyreply off')
|
self.assertNotError('url config tinyurlsnarfer off')
|
||||||
self.assertNotError('url toggle tinysnarf off')
|
|
||||||
self.irc.feedMsg(ircmsgs.action(self.channel, urls[1]))
|
self.irc.feedMsg(ircmsgs.action(self.channel, urls[1]))
|
||||||
self.assertNotRegexp('url last', '\\x01')
|
self.assertNotRegexp('url last', '\\x01')
|
||||||
|
|
||||||
def testTinyurl(self):
|
def testTinyurl(self):
|
||||||
self.assertNotError('url toggle tinyreply on')
|
self.assertNotError('url config tinyurlsnarfer off')
|
||||||
self.assertNotError('url toggle tinysnarf off')
|
|
||||||
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
|
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
|
||||||
'func=add&group_id=58965&atid=489447',
|
'func=add&group_id=58965&atid=489447',
|
||||||
r'http://tinyurl.com/\w{4}')
|
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/?'\
|
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
|
||||||
'func=add&group_id=58965&atid=489447',
|
'func=add&group_id=58965&atid=489447',
|
||||||
r'http://tinyurl.com/\w{4}')
|
r'http://tinyurl.com/\w{4}')
|
||||||
self.assertNotError('toggle tinyreply off')
|
|
||||||
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
|
self.assertRegexp('url tiny http://sourceforge.net/tracker/?'\
|
||||||
'func=add&group_id=58965&atid=489447',
|
'func=add&group_id=58965&atid=489447',
|
||||||
r'http://tinyurl.com/\w{4}')
|
r'http://tinyurl.com/\w{4}')
|
||||||
|
|
||||||
def testTinysnarf(self):
|
def testTinysnarf(self):
|
||||||
self.assertNotError('url toggle tinyreply off')
|
self.assertNotError('url config tinyurlsnarfer on')
|
||||||
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.assertRegexp('http://sourceforge.net/tracker/?'\
|
self.assertRegexp('http://sourceforge.net/tracker/?'\
|
||||||
'func=add&group_id=58965&atid=489447',
|
'func=add&group_id=58965&atid=489447',
|
||||||
r'http://tinyurl.com/\w{4}.* \(was')
|
r'http://tinyurl.com/\w{4}.* \(was')
|
||||||
|
Loading…
Reference in New Issue
Block a user