2003-09-11 00:22:50 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-09-11 00:22:50 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import sqlite
|
|
|
|
|
|
|
|
import conf
|
|
|
|
import utils
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
|
|
|
|
def configure(onStart, afterConnect, advanced):
|
|
|
|
# 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
|
2003-10-21 08:31:57 +02:00
|
|
|
onStart.append('load Lookup')
|
2003-09-11 12:05:24 +02:00
|
|
|
print 'This module allows you to define commands that do a simple key'
|
2003-10-21 06:32:52 +02:00
|
|
|
print 'lookup and return some simple value. It has a command "add"'
|
2003-09-11 12:05:24 +02:00
|
|
|
print 'that takes a command name and a file in conf.dataDir and adds a'
|
|
|
|
print 'command with that name that responds with mapping from that file.'
|
|
|
|
print 'The file itself should be composed of lines of the form key:value.'
|
|
|
|
while yn('Would you like to add a file?') == 'y':
|
|
|
|
filename = something('What\'s the filename?')
|
2003-09-24 09:55:28 +02:00
|
|
|
try:
|
|
|
|
fd = file(os.path.join(conf.dataDir, filename))
|
2003-11-22 07:54:42 +01:00
|
|
|
except EnvironmentError, e:
|
2003-09-24 09:55:28 +02:00
|
|
|
print 'I couldn\'t open that file: %s' % e
|
|
|
|
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:
|
|
|
|
print 'That\'s not a valid file; line #%s is malformed.' % counter
|
|
|
|
continue
|
|
|
|
command = something('What would you like the command to be?')
|
2003-10-21 06:32:52 +02:00
|
|
|
onStart.append('lookup add %s %s' % (command, filename))
|
2003-09-11 12:05:24 +02:00
|
|
|
|
2003-09-11 00:22:50 +02:00
|
|
|
|
|
|
|
def getDb():
|
2003-10-21 08:31:57 +02:00
|
|
|
return sqlite.connect(os.path.join(conf.dataDir, 'Lookup.db'))
|
2003-09-11 00:22:50 +02:00
|
|
|
|
2003-10-21 06:32:52 +02:00
|
|
|
class Lookup(callbacks.Privmsg):
|
2003-09-11 12:05:24 +02:00
|
|
|
def die(self):
|
|
|
|
db = getDb()
|
|
|
|
db.commit()
|
|
|
|
db.close()
|
|
|
|
del db
|
|
|
|
|
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)
|
|
|
|
db = getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("""DROP TABLE %s""" % name)
|
|
|
|
db.commit()
|
|
|
|
cb = irc.getCallback('Alias')
|
|
|
|
if cb is not None:
|
|
|
|
cb.removeAlias(name, evenIfFrozen=True)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
except sqlite.DatabaseError:
|
|
|
|
irc.error(msg, 'No such lookup exists.')
|
2003-09-13 15:33:49 +02:00
|
|
|
|
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
|
|
|
|
for in conf.dataDir. Use 'lookup <name> <key>' to get the value of
|
|
|
|
the key in the file.
|
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
(name, filename) = privmsgs.getArgs(args, required=2)
|
2003-09-11 00:22:50 +02:00
|
|
|
db = getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("""SELECT * FROM %s LIMIT 1""" % name)
|
|
|
|
cb = irc.getCallback('Alias')
|
|
|
|
if cb is not None:
|
|
|
|
try:
|
2003-09-13 15:33:49 +02:00
|
|
|
cb.addAlias(irc, name, 'lookup %s @1' % name, freeze=True)
|
2003-09-11 00:22:50 +02:00
|
|
|
except sys.modules[cb.__module__].AliasError, e:
|
|
|
|
pass
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
except sqlite.DatabaseError:
|
|
|
|
# Good, there's no such database.
|
|
|
|
try:
|
|
|
|
filename = os.path.join(conf.dataDir, filename)
|
|
|
|
fd = file(filename)
|
2003-11-19 23:36:58 +01:00
|
|
|
except EnvironmentError, e:
|
|
|
|
irc.error('Could not open %s: %s' % (filename, e))
|
2003-09-11 00:22:50 +02:00
|
|
|
return
|
|
|
|
cursor.execute("""CREATE TABLE %s (key TEXT, value TEXT)""" % name)
|
|
|
|
sql = """INSERT INTO %s VALUES (%%s, %%s)""" % name
|
|
|
|
for line in fd:
|
|
|
|
line = line.rstrip('\r\n')
|
2003-11-19 23:39:01 +01:00
|
|
|
if not line or line.startswith('#'):
|
|
|
|
continue
|
2003-09-11 00:22:50 +02:00
|
|
|
try:
|
|
|
|
(key, value) = line.split(':', 1)
|
|
|
|
except ValueError:
|
|
|
|
irc.error(msg, 'Invalid line in %s: %r' % (filename, line))
|
2003-09-18 17:45:43 +02:00
|
|
|
return
|
2003-09-11 00:22:50 +02:00
|
|
|
cursor.execute(sql, key, value)
|
|
|
|
cursor.execute("CREATE INDEX %s_keys ON %s (key)" % (name, name))
|
|
|
|
db.commit()
|
|
|
|
cb = irc.getCallback('Alias')
|
|
|
|
if cb is not None:
|
|
|
|
try:
|
2003-09-13 15:33:49 +02:00
|
|
|
cb.addAlias(irc, name, 'lookup %s @1' % name, freeze=True)
|
2003-09-11 00:22:50 +02:00
|
|
|
except sys.modules[cb.__module__].AliasError, e:
|
|
|
|
irc.error(msg, str(e))
|
|
|
|
return
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
|
|
|
def lookup(self, irc, msg, args):
|
|
|
|
"""<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-09-11 00:22:50 +02:00
|
|
|
db = getDb()
|
|
|
|
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):
|
|
|
|
irc.error(msg, 'I don\'t have a domain %s' % name)
|
|
|
|
else:
|
|
|
|
irc.error(msg, str(e))
|
|
|
|
return
|
|
|
|
if cursor.rowcount == 0:
|
2003-10-30 03:33:04 +01:00
|
|
|
irc.error(msg, 'I couldn\'t find %s in %s' % (key, name))
|
2003-09-13 15:33:49 +02:00
|
|
|
elif cursor.rowcount == 1:
|
|
|
|
irc.reply(msg, 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()]
|
|
|
|
irc.reply(msg, '%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):
|
|
|
|
irc.error(msg, 'I don\'t have a domain %r' % name)
|
|
|
|
else:
|
|
|
|
irc.error(msg, str(e))
|
|
|
|
return
|
|
|
|
(key, value) = cursor.fetchone()
|
|
|
|
irc.reply(msg, '%s: %s' % (key, value))
|
2003-09-11 00:22:50 +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:
|