Updated to use the registry.

This commit is contained in:
Jeremy Fincher 2004-01-27 18:09:35 +00:00
parent a53819a00d
commit 64f4013a2f

View File

@ -52,8 +52,8 @@ import ircmsgs
import webutils import webutils
import ircutils import ircutils
import privmsgs import privmsgs
import registry
import callbacks import callbacks
import configurable
try: try:
import sqlite import sqlite
@ -61,65 +61,53 @@ except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \ raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
'plugin. Download it at <http://pysqlite.sf.net/>' 'plugin. Download it at <http://pysqlite.sf.net/>'
def configure(onStart, afterConnect, advanced): def configure(onStart):
# This will be called by setup.py to configure this module. onStart and
# afterConnect are both lists. Append to onStart the commands you would
# like to be run when the bot is started; append to afterConnect the
# 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 URL') conf.registerPlugin('URL', True)
if yn("""This plugin offers a snarfer that will go to tinyurl.com and get if yn("""This plugin offers a snarfer that will go to tinyurl.com and get
a shorter version of long URLs that are sent to the channel. a shorter version of long URLs that are sent to the channel.
Would you like this snarfer to be enabled?""") == 'y': Would you like this snarfer to be enabled?"""):
onStart.append('url config tinyurl-snarfer on') conf.supybot.plugins.URL.tinyurlSnarfer.setValue(True)
if advanced: if yn("""This plugin also offers a snarfer that will try to fetch the
x = anything("""What would you like to be the minimum URL length title of URLs that it sees in the channel. Would like you this
that will trigger this snarfer?""") snarfer to be enabled?"""):
if not x: conf.supybot.plugins.URL.titleSnarfer.setValue(True)
x = '46'
while True: conf.registerPlugin('URL')
try: conf.registerChannelValue(conf.supybot.plugins.URL, 'tinyurlSnarfer',
i = int(x) registry.Boolean(False, """Determines whether the
onStart.append('url config tinyurl-minimum-length %s' % i) tinyurl snarfer is enabled. This snarfer will watch for URLs in the
return channel, and if they're sufficiently long (as determined by
except ValueError: supybot.plugins.URL.tinyurlSnarfer.minimumLength) it will post a smaller
print 'That\'s not a valid integer.' from tinyurl.com."""))
x = anything("""What would you like to be the minimum URL conf.registerChannelValue(conf.supybot.plugins.URL, 'tinyurlMaximumLength',
length that will trigger this snarfer?""") registry.PositiveInteger(48, """The minimum length a URL must be before the
tinyurl snarfer will snarf it."""))
conf.registerChannelValue(conf.supybot.plugins.URL, 'titleSnarfer',
registry.Boolean(False, """Determines whether the bot will output the HTML
title of URLs it sees in the channel."""))
conf.registerChannelValue(conf.supybot.plugins.URL, 'titleSnarferIncludesUrl',
registry.Boolean(True, """Determines whether the bot will include the
snarfed URL in its message. This is particularly useful when people have a
habit of putting multiple URLs in a message."""))
conf.registerChannelValue(conf.supybot.plugins.URL, 'nonSnarfingRegexp',
registry.Regexp(None, """Determines what URLs are to be snarfed and stored
in the database in the channel; URLs matchin the regexp given will not be
snarfed. Give the empty string if you have no URLs that you'd like to
exclude from being snarfed."""))
class URL(callbacks.PrivmsgCommandAndRegexp, class URL(callbacks.PrivmsgCommandAndRegexp,
configurable.Mixin,
plugins.ChannelDBHandler): plugins.ChannelDBHandler):
regexps = ['tinyurlSnarfer', 'titleSnarfer'] regexps = ['tinyurlSnarfer', 'titleSnarfer']
configurables = configurable.Dictionary(
[('tinyurl-snarfer', configurable.BoolType, False,
"""Determines whether the bot will output shorter versions of URLs
longer than the tinyurl-minimum-length config variable."""),
('tinyurl-minimum-length', configurable.IntType, 46,
"""The minimum length a URL must be before the tinyurl-snarfer will
snarf it and offer a tinyurl replacement."""),
('title-snarfer', configurable.BoolType, False,
"""Determines whether the bot will output the HTML title of URLs it
sees in the channel."""),
('title-snarfer-includes-url', configurable.BoolType, True,
"""Determines whether the bot will include the snarfed URL in its
title-snarfer response. This is particularly useful when people
have a habit of putting multiple URLs in a message."""),
('non-snarfing-regexp', configurable.RegexpStrType, None,
"""A regular expression that should match URLs that should not be
snarfed. Give an empty string to have no regular expression."""),]
)
_titleRe = re.compile('<title>(.*?)</title>', re.I) _titleRe = re.compile('<title>(.*?)</title>', re.I)
maxSize = 4096 maxSize = 4096
def __init__(self): def __init__(self):
self.nextMsgs = {} self.nextMsgs = {}
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
configurable.Mixin.__init__(self)
plugins.ChannelDBHandler.__init__(self) plugins.ChannelDBHandler.__init__(self)
def die(self): def die(self):
callbacks.PrivmsgCommandAndRegexp.die(self) callbacks.PrivmsgCommandAndRegexp.die(self)
configurable.Mixin.die(self)
plugins.ChannelDBHandler.die(self) plugins.ChannelDBHandler.die(self)
def makeDb(self, filename): def makeDb(self, filename):
@ -162,7 +150,7 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
else: else:
text = msg.args[1] text = msg.args[1]
for url in webutils.urlRe.findall(text): for url in webutils.urlRe.findall(text):
r = self.configurables.get('non-snarfing-regexp', channel) r = self.registryValue('nonSnarfingRegexp', channel)
if r and r.search(url): if r and r.search(url):
continue continue
(protocol, site, filename, _, _, _) = urlparse.urlparse(url) (protocol, site, filename, _, _, _) = urlparse.urlparse(url)
@ -187,9 +175,9 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
if not ircutils.isChannel(msg.args[0]): if not ircutils.isChannel(msg.args[0]):
return return
channel = msg.args[0] channel = msg.args[0]
if self.configurables.get('tinyurl-snarfer', channel): if self.registryValue('tinyurlSnarfer', channel):
url = match.group(0) url = match.group(0)
minlen = self.configurables.get('tinyurl-minimum-length', channel) minlen = self.registryValue('tinyurlMaximumLength', channel)
if len(url) >= minlen: if len(url) >= minlen:
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
@ -208,13 +196,13 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
if not ircutils.isChannel(msg.args[0]): if not ircutils.isChannel(msg.args[0]):
return return
channel = msg.args[0] channel = msg.args[0]
if self.configurables.get('title-snarfer', channel): if self.registryValue('titleSnarfer', channel):
url = match.group(0) url = match.group(0)
text = webutils.getUrl(url, size=self.maxSize) text = webutils.getUrl(url, size=self.maxSize)
m = self._titleRe.search(text) m = self._titleRe.search(text)
if m is not None: if m is not None:
s = 'Title: %s' % utils.htmlToText(m.group(1).strip()) s = 'Title: %s' % utils.htmlToText(m.group(1).strip())
if self.configurables.get('titlesnarferincludesurl', channel): if self.registryValue('titleSnarferIncludesUrl', channel):
s += ' (<%s>)' % url s += ' (<%s>)' % url
irc.reply(s, prefixName=False) irc.reply(s, prefixName=False)
titleSnarfer = privmsgs.urlSnarfer(titleSnarfer) titleSnarfer = privmsgs.urlSnarfer(titleSnarfer)
@ -293,9 +281,8 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
'Stop being a lazy-biotch and type the URL yourself.') 'Stop being a lazy-biotch and type the URL yourself.')
return return
channel = msg.args[0] channel = msg.args[0]
snarf = self.configurables.get('tinyurl-snarfer', channel=msg.args[0]) snarf = self.registryValue('tinyurlSnarfer', channel)
minlen = self.configurables.get('tinyurl-minimum-length', minlen = self.registryValue('tinyurlMinimumLength', channel)
channel=channel)
if snarf and len(url) >= minlen: if snarf and len(url) >= minlen:
return return
(tinyurl, updateDb) = self._getTinyUrl(url, channel, cmd=True) (tinyurl, updateDb) = self._getTinyUrl(url, channel, cmd=True)