2003-09-11 00:22:50 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-09-11 00:22:50 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2003-10-21 08:31:57 +02:00
|
|
|
The Lookup plugin handles looking up various values by their key.
|
2003-09-11 00:22:50 +02:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-09-11 00:22:50 +02:00
|
|
|
|
|
|
|
import os
|
2003-11-25 10:08:06 +01:00
|
|
|
import re
|
2003-09-11 00:22:50 +02:00
|
|
|
import sys
|
2003-12-03 22:19:49 +01:00
|
|
|
import sets
|
2003-12-11 01:25:50 +01:00
|
|
|
import getopt
|
|
|
|
import string
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
|
|
|
|
'plugin. Download it at <http://pysqlite.sf.net/>'
|
|
|
|
|
2004-01-30 00:58:27 +01:00
|
|
|
def configure(advanced):
|
2004-07-25 20:24:51 +02:00
|
|
|
from supybot.questions import output, expect, anything, something, yn
|
2004-01-28 23:19:25 +01:00
|
|
|
conf.registerPlugin('Lookup', True)
|
2004-02-04 08:11:47 +01:00
|
|
|
lookups = conf.supybot.plugins.Lookup.lookups
|
2004-01-31 23:24:43 +01:00
|
|
|
output("""This module allows you to define commands that do a simple key
|
|
|
|
lookup and return some simple value. It has a command "add"
|
|
|
|
that takes a command name and a file from the data dir and adds a
|
|
|
|
command with that name that responds with the mapping from that
|
|
|
|
file. The file itself should be composed of lines
|
|
|
|
of the form key:value.""")
|
|
|
|
while yn('Would you like to add a file?'):
|
2003-09-11 12:05:24 +02:00
|
|
|
filename = something('What\'s the filename?')
|
2003-09-24 09:55:28 +02:00
|
|
|
try:
|
2004-08-25 19:32:49 +02:00
|
|
|
fd = file(filename)
|
2003-11-22 07:54:42 +01:00
|
|
|
except EnvironmentError, e:
|
2004-01-31 23:24:43 +01:00
|
|
|
output('I couldn\'t open that file: %s' % e)
|
2003-09-24 09:55:28 +02:00
|
|
|
continue
|
2003-09-11 12:05:24 +02:00
|
|
|
counter = 1
|
|
|
|
try:
|
|
|
|
for line in fd:
|
2003-11-22 07:57:03 +01:00
|
|
|
line = line.rstrip('\r\n')
|
|
|
|
if not line or line.startswith('#'):
|
|
|
|
continue
|
2003-09-11 12:05:24 +02:00
|
|
|
(key, value) = line.split(':', 1)
|
|
|
|
counter += 1
|
|
|
|
except ValueError:
|
2004-01-31 23:24:43 +01:00
|
|
|
output('That\'s not a valid file; '
|
|
|
|
'line #%s is malformed.' % counter)
|
2003-09-11 12:05:24 +02:00
|
|
|
continue
|
|
|
|
command = something('What would you like the command to be?')
|
2004-02-04 08:11:47 +01:00
|
|
|
conf.registerGlobalValue(lookups,command, registry.String(filename,''))
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2004-02-04 07:55:35 +01:00
|
|
|
conf.registerPlugin('Lookup')
|
|
|
|
conf.registerGroup(conf.supybot.plugins.Lookup, 'lookups')
|
|
|
|
|
2003-12-02 23:40:50 +01:00
|
|
|
class LookupDB(plugins.DBHandler):
|
|
|
|
def makeDb(self, filename):
|
|
|
|
return sqlite.connect(filename)
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2003-10-21 06:32:52 +02:00
|
|
|
class Lookup(callbacks.Privmsg):
|
2003-12-02 23:40:50 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-12-04 08:03:37 +01:00
|
|
|
self.lookupDomains = sets.Set()
|
2004-01-18 08:58:26 +01:00
|
|
|
dataDir = conf.supybot.directories.data()
|
|
|
|
self.dbHandler = LookupDB(name=os.path.join(dataDir, 'Lookup'))
|
2004-02-04 08:11:47 +01:00
|
|
|
for (name, value) in registry._cache.iteritems():
|
|
|
|
name = name.lower()
|
|
|
|
if name.startswith('supybot.plugins.lookup.lookups.'):
|
|
|
|
name = name[len('supybot.plugins.lookup.lookups.'):]
|
|
|
|
if '.' in name:
|
|
|
|
continue
|
2004-02-06 06:53:31 +01:00
|
|
|
self.addRegistryValue(name, value)
|
2004-02-04 08:11:47 +01:00
|
|
|
group = conf.supybot.plugins.Lookup.lookups
|
|
|
|
for (name, value) in group.getValues(fullNames=False):
|
|
|
|
name = name.lower() # Just in case.
|
|
|
|
filename = value()
|
2004-02-13 11:06:27 +01:00
|
|
|
try:
|
|
|
|
self.addDatabase(name, filename)
|
|
|
|
self.addCommand(name)
|
|
|
|
except Exception, e:
|
|
|
|
self.log.warning('Couldn\'t add lookup %s: %s', name, e)
|
2004-06-04 08:14:29 +02:00
|
|
|
|
2003-12-11 01:25:50 +01:00
|
|
|
def _shrink(self, s):
|
|
|
|
return utils.ellipsisify(s, 50)
|
|
|
|
|
2003-09-11 12:05:24 +02:00
|
|
|
def die(self):
|
2003-12-02 23:40:50 +01:00
|
|
|
self.dbHandler.die()
|
2003-09-11 12:05:24 +02:00
|
|
|
|
2003-10-21 06:32:52 +02:00
|
|
|
def remove(self, irc, msg, args):
|
2003-09-11 00:22:50 +02:00
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Removes the lookup for <name>.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2003-12-03 22:19:49 +01:00
|
|
|
name = callbacks.canonicalName(name)
|
2003-12-04 08:03:37 +01:00
|
|
|
if name not in self.lookupDomains:
|
2004-09-17 07:09:14 +02:00
|
|
|
irc.error('That\'s not a valid lookup to remove.')
|
|
|
|
return
|
2003-12-02 23:40:50 +01:00
|
|
|
db = self.dbHandler.getDb()
|
2003-09-11 00:22:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("""DROP TABLE %s""" % name)
|
|
|
|
db.commit()
|
2003-12-02 00:02:31 +01:00
|
|
|
delattr(self.__class__, name)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-09-11 00:22:50 +02:00
|
|
|
except sqlite.DatabaseError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('No such lookup exists.')
|
2003-12-03 22:19:49 +01:00
|
|
|
remove = privmsgs.checkCapability(remove, 'admin')
|
2003-09-13 15:33:49 +02:00
|
|
|
|
2003-11-25 10:08:06 +01:00
|
|
|
_splitRe = re.compile(r'(?<!\\):')
|
2003-10-21 06:32:52 +02:00
|
|
|
def add(self, irc, msg, args):
|
2003-09-11 00:22:50 +02:00
|
|
|
"""<name> <filename>
|
|
|
|
|
|
|
|
Adds a lookup for <name> with the key/value pairs specified in the
|
|
|
|
colon-delimited file specified by <filename>. <filename> is searched
|
2004-01-18 08:58:26 +01:00
|
|
|
for in conf.supybot.directories.data. If <name> is not singular, we
|
|
|
|
try to make it singular before creating the command.
|
2003-09-11 00:22:50 +02:00
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
(name, filename) = privmsgs.getArgs(args, required=2)
|
2003-12-02 20:02:53 +01:00
|
|
|
name = utils.depluralize(name)
|
2003-12-03 22:19:49 +01:00
|
|
|
name = callbacks.canonicalName(name)
|
2003-12-04 08:03:37 +01:00
|
|
|
if hasattr(self, name):
|
|
|
|
s = 'I already have a command in this plugin named %s' % name
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(s)
|
2003-12-04 08:03:37 +01:00
|
|
|
return
|
2003-12-02 23:40:50 +01:00
|
|
|
db = self.dbHandler.getDb()
|
2003-09-11 00:22:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("""SELECT * FROM %s LIMIT 1""" % name)
|
2003-12-02 00:02:31 +01:00
|
|
|
self.addCommand(name)
|
2003-09-11 00:22:50 +02:00
|
|
|
except sqlite.DatabaseError:
|
|
|
|
try:
|
2004-02-04 07:55:35 +01:00
|
|
|
self.addDatabase(name, filename)
|
2003-11-19 23:36:58 +01:00
|
|
|
except EnvironmentError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Could not open %s: %s' % (filename, e.args[1]))
|
2003-09-11 00:22:50 +02:00
|
|
|
return
|
2003-12-02 00:02:31 +01:00
|
|
|
self.addCommand(name)
|
2004-02-04 08:11:47 +01:00
|
|
|
self.addRegistryValue(name, filename)
|
|
|
|
irc.replySuccess('Lookup %s added.' % name)
|
2003-12-03 22:19:49 +01:00
|
|
|
add = privmsgs.checkCapability(add, 'admin')
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2004-02-04 08:11:47 +01:00
|
|
|
def addRegistryValue(self, name, filename):
|
|
|
|
v = registry.String(filename, '')
|
|
|
|
conf.supybot.plugins.Lookup.lookups.register(name, v)
|
2004-06-04 08:14:29 +02:00
|
|
|
|
2004-02-04 07:55:35 +01:00
|
|
|
def addDatabase(self, name, filename):
|
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
dataDir = conf.supybot.directories.data()
|
|
|
|
filename = os.path.join(dataDir, filename)
|
|
|
|
fd = file(filename)
|
|
|
|
try:
|
|
|
|
cursor.execute("""SELECT COUNT(*) FROM %s""" % name)
|
|
|
|
except sqlite.DatabaseError:
|
|
|
|
cursor.execute("CREATE TABLE %s (key TEXT, value TEXT)" % name)
|
|
|
|
sql = "INSERT INTO %s VALUES (%%s, %%s)" % name
|
|
|
|
for line in utils.nonCommentNonEmptyLines(fd):
|
|
|
|
line = line.rstrip('\r\n')
|
|
|
|
try:
|
|
|
|
(key, value) = self._splitRe.split(line, 1)
|
|
|
|
key = key.replace('\\:', ':')
|
|
|
|
except ValueError:
|
|
|
|
cursor.execute("""DROP TABLE %s""" % name)
|
|
|
|
s = 'Invalid line in %s: %r' % (filename, line)
|
|
|
|
raise callbacks.Error, s
|
|
|
|
cursor.execute(sql, key, value)
|
|
|
|
cursor.execute("CREATE INDEX %s_keys ON %s (key)" % (name, name))
|
|
|
|
db.commit()
|
|
|
|
|
2003-12-02 00:02:31 +01:00
|
|
|
def addCommand(self, name):
|
|
|
|
def f(self, irc, msg, args):
|
|
|
|
args.insert(0, name)
|
|
|
|
self._lookup(irc, msg, args)
|
2003-12-02 23:40:50 +01:00
|
|
|
db = self.dbHandler.getDb()
|
2003-12-02 19:37:39 +01:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT COUNT(*) FROM %s""" % name)
|
2003-12-02 19:50:46 +01:00
|
|
|
rows = int(cursor.fetchone()[0])
|
2003-12-02 19:37:39 +01:00
|
|
|
docstring = """[<key>]
|
|
|
|
|
|
|
|
If <key> is given, looks up <key> in the %s database. Otherwise,
|
|
|
|
returns a random key: value pair from the database. There are
|
|
|
|
%s in the database.
|
2003-12-12 15:01:16 +01:00
|
|
|
""" % (name, utils.nItems(name, rows))
|
2004-01-16 17:39:00 +01:00
|
|
|
f = utils.changeFunctionName(f, name, docstring)
|
2003-12-04 08:03:37 +01:00
|
|
|
self.lookupDomains.add(name)
|
2003-12-02 00:02:31 +01:00
|
|
|
setattr(self.__class__, name, f)
|
|
|
|
|
2003-12-11 01:25:50 +01:00
|
|
|
_sqlTrans = string.maketrans('*?', '%_')
|
|
|
|
def search(self, irc, msg, args):
|
2003-12-12 15:01:16 +01:00
|
|
|
"""[--{regexp}=<value>] [--values] <name> <glob>
|
2003-12-11 01:25:50 +01:00
|
|
|
|
|
|
|
Searches the domain <name> for lookups matching <glob>. If --regexp
|
|
|
|
is given, its associated value is taken as a regexp and matched
|
2003-12-12 15:01:16 +01:00
|
|
|
against the lookups. If --values is given, search the values rather
|
|
|
|
than the keys.
|
2003-12-11 01:25:50 +01:00
|
|
|
"""
|
2003-12-12 15:01:16 +01:00
|
|
|
column = 'key'
|
|
|
|
while '--values' in args:
|
|
|
|
column = 'value'
|
|
|
|
args.remove('--values')
|
|
|
|
(options, rest) = getopt.getopt(args, '', ['regexp='])
|
2003-12-11 01:25:50 +01:00
|
|
|
(name, globs) = privmsgs.getArgs(rest, optional=1)
|
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
criteria = []
|
|
|
|
formats = []
|
|
|
|
predicateName = 'p'
|
|
|
|
for (option, arg) in options:
|
2003-12-12 15:01:16 +01:00
|
|
|
if option == '--regexp':
|
|
|
|
criteria.append('%s(%s)' % (predicateName, column))
|
2003-12-11 01:25:50 +01:00
|
|
|
try:
|
|
|
|
r = utils.perlReToPythonRe(arg)
|
|
|
|
except ValueError, e:
|
2004-09-17 07:09:14 +02:00
|
|
|
irc.error('%r is not a valid regular expression' %
|
|
|
|
arg)
|
|
|
|
return
|
2003-12-11 01:25:50 +01:00
|
|
|
def p(s, r=r):
|
|
|
|
return int(bool(r.search(s)))
|
|
|
|
db.create_function(predicateName, 1, p)
|
|
|
|
predicateName += 'p'
|
|
|
|
for glob in globs.split():
|
|
|
|
if '?' not in glob and '*' not in glob:
|
|
|
|
glob = '*%s*' % glob
|
2003-12-12 15:01:16 +01:00
|
|
|
criteria.append('%s LIKE %%s' % column)
|
2003-12-11 01:25:50 +01:00
|
|
|
formats.append(glob.translate(self._sqlTrans))
|
2003-12-12 15:01:16 +01:00
|
|
|
if not criteria:
|
|
|
|
raise callbacks.ArgumentError
|
2003-12-11 01:25:50 +01:00
|
|
|
#print 'criteria: %s' % repr(criteria)
|
|
|
|
#print 'formats: %s' % repr(formats)
|
|
|
|
cursor = db.cursor()
|
2003-12-12 15:01:16 +01:00
|
|
|
sql = """SELECT key, value FROM %s WHERE %s""" % \
|
|
|
|
(name, ' AND '.join(criteria))
|
2003-12-11 01:25:50 +01:00
|
|
|
#print 'sql: %s' % sql
|
|
|
|
cursor.execute(sql, formats)
|
|
|
|
if cursor.rowcount == 0:
|
2004-02-24 12:22:53 +01:00
|
|
|
irc.reply('No entries in %s matched that query.' % name)
|
2003-12-11 01:25:50 +01:00
|
|
|
else:
|
|
|
|
lookups = ['%s: %s' % (item[0], self._shrink(item[1]))
|
|
|
|
for item in cursor.fetchall()]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(lookups))
|
2003-12-11 01:25:50 +01:00
|
|
|
|
2003-12-02 00:02:31 +01:00
|
|
|
def _lookup(self, irc, msg, args):
|
2003-09-11 00:22:50 +02:00
|
|
|
"""<name> <key>
|
|
|
|
|
|
|
|
Looks up the value of <key> in the domain <name>.
|
|
|
|
"""
|
2003-09-13 15:33:49 +02:00
|
|
|
(name, key) = privmsgs.getArgs(args, optional=1)
|
2003-12-02 23:40:50 +01:00
|
|
|
db = self.dbHandler.getDb()
|
2003-09-11 00:22:50 +02:00
|
|
|
cursor = db.cursor()
|
2003-09-13 15:33:49 +02:00
|
|
|
if key:
|
|
|
|
sql = """SELECT value FROM %s WHERE key LIKE %%s""" % name
|
|
|
|
try:
|
|
|
|
cursor.execute(sql, key)
|
|
|
|
except sqlite.DatabaseError, e:
|
|
|
|
if 'no such table' in str(e):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I don\'t have a domain %s' % name)
|
2003-09-13 15:33:49 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-09-13 15:33:49 +02:00
|
|
|
return
|
|
|
|
if cursor.rowcount == 0:
|
2004-02-24 12:22:53 +01:00
|
|
|
irc.error('I couldn\'t find %s in %s.' % (key, name))
|
2003-09-13 15:33:49 +02:00
|
|
|
elif cursor.rowcount == 1:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(cursor.fetchone()[0])
|
2003-09-11 00:22:50 +02:00
|
|
|
else:
|
2003-09-13 15:33:49 +02:00
|
|
|
values = [t[0] for t in cursor.fetchall()]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('%s could be %s' % (key, ', or '.join(values)))
|
2003-09-11 00:22:50 +02:00
|
|
|
else:
|
2003-09-13 15:33:49 +02:00
|
|
|
sql = """SELECT key, value FROM %s
|
|
|
|
ORDER BY random() LIMIT 1""" % name
|
|
|
|
try:
|
|
|
|
cursor.execute(sql)
|
|
|
|
except sqlite.DatabaseError, e:
|
|
|
|
if 'no such table' in str(e):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I don\'t have a domain %r' % name)
|
2003-09-13 15:33:49 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-09-13 15:33:49 +02:00
|
|
|
return
|
|
|
|
(key, value) = cursor.fetchone()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('%s: %s' % (key, value))
|
2004-06-04 08:14:29 +02:00
|
|
|
|
|
|
|
|
2003-10-21 06:32:52 +02:00
|
|
|
Class = Lookup
|
2003-09-11 00:22:50 +02:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|