2003-03-12 07:26:59 +01: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-03-25 07:53:51 +01:00
|
|
|
"""
|
2003-08-20 18:26:23 +02:00
|
|
|
Handles "factoids," little tidbits of information held in a database and
|
2003-03-25 07:53:51 +01:00
|
|
|
available on demand via several commands.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import time
|
2003-10-04 12:16:57 +02:00
|
|
|
import getopt
|
2003-10-04 14:29:58 +02:00
|
|
|
import string
|
2003-03-12 07:26:59 +01:00
|
|
|
import os.path
|
2003-11-15 05:46:09 +01:00
|
|
|
from itertools import imap
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import conf
|
2003-08-29 09:01:05 +02:00
|
|
|
import utils
|
2003-03-12 07:26:59 +01:00
|
|
|
import ircdb
|
2003-08-29 08:45:51 +02:00
|
|
|
import ircutils
|
2003-03-12 07:26:59 +01:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
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/>'
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg):
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2003-10-05 14:56:56 +02:00
|
|
|
plugins.ChannelDBHandler.__init__(self)
|
2003-03-12 07:26:59 +01:00
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
|
|
|
return sqlite.connect(filename)
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE keys (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-04-29 15:25:40 +02:00
|
|
|
key TEXT UNIQUE ON CONFLICT IGNORE,
|
2003-03-12 07:26:59 +01:00
|
|
|
locked BOOLEAN
|
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE factoids (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
key_id INTEGER,
|
|
|
|
added_by TEXT,
|
|
|
|
added_at TIMESTAMP,
|
|
|
|
fact TEXT
|
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TRIGGER remove_factoids
|
|
|
|
BEFORE DELETE ON keys
|
|
|
|
BEGIN
|
|
|
|
DELETE FROM factoids WHERE key_id = old.id;
|
|
|
|
END
|
|
|
|
""")
|
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
2003-08-29 08:32:49 +02:00
|
|
|
def learn(self, irc, msg, args):
|
2003-04-29 15:25:40 +02:00
|
|
|
"""[<channel>] <key> as <value>
|
|
|
|
|
|
|
|
Associates <key> with <value>. <channel> is only necessary if the
|
|
|
|
message isn't sent on the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-08-29 08:32:49 +02:00
|
|
|
try:
|
|
|
|
i = args.index('as')
|
|
|
|
except ValueError:
|
2003-04-29 15:25:40 +02:00
|
|
|
raise callbacks.ArgumentError
|
2003-08-29 08:32:49 +02:00
|
|
|
args.pop(i)
|
|
|
|
key = ' '.join(args[:i])
|
|
|
|
factoid = ' '.join(args[i:])
|
2003-03-12 07:26:59 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-08-29 08:45:51 +02:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s", key)
|
2003-03-12 07:26:59 +01:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
cursor.execute("""INSERT INTO keys VALUES (NULL, %s, 0)""", key)
|
|
|
|
db.commit()
|
2003-08-29 08:45:51 +02:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s",key)
|
2003-11-15 05:46:09 +01:00
|
|
|
(id, locked) = imap(int, cursor.fetchone())
|
2003-03-12 07:26:59 +01:00
|
|
|
capability = ircdb.makeChannelCapability(channel, 'factoids')
|
|
|
|
if not locked:
|
|
|
|
if not ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
irc.error(msg, conf.replyNoCapability % capability)
|
|
|
|
return
|
|
|
|
if ircdb.users.hasUser(msg.prefix):
|
2003-09-12 22:06:58 +02:00
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
name = msg.nick
|
|
|
|
cursor.execute("""INSERT INTO factoids VALUES
|
|
|
|
(NULL, %s, %s, %s, %s)""",
|
|
|
|
id, name, int(time.time()), factoid)
|
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, 'That factoid is locked.')
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-08-29 08:32:49 +02:00
|
|
|
def whatis(self, irc, msg, args):
|
|
|
|
"""[<channel>] <key> [<number>]
|
|
|
|
|
|
|
|
Looks up the value of <key> in the factoid database. If given a
|
|
|
|
number, will return only that exact factoid. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-29 19:45:29 +01:00
|
|
|
if len(args) > 1 and args[-1].isdigit():
|
2003-10-02 08:10:54 +02:00
|
|
|
number = args.pop()
|
|
|
|
else:
|
|
|
|
number = ''
|
2003-08-29 08:32:49 +02:00
|
|
|
key = privmsgs.getArgs(args)
|
2003-10-02 08:10:54 +02:00
|
|
|
if number:
|
|
|
|
try:
|
|
|
|
number = int(number)
|
|
|
|
except ValueError:
|
|
|
|
irc.error(msg, '%s is not a valid number.' % number)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
number = 0
|
2003-03-12 07:26:59 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT factoids.fact FROM factoids, keys WHERE
|
2003-08-29 08:45:51 +02:00
|
|
|
keys.key LIKE %s AND factoids.key_id=keys.id
|
2003-08-29 08:32:49 +02:00
|
|
|
ORDER BY factoids.id
|
|
|
|
LIMIT 20""", key)
|
|
|
|
if cursor.rowcount == 0:
|
2003-03-12 07:26:59 +01:00
|
|
|
irc.error(msg, 'No factoid matches that key.')
|
|
|
|
else:
|
2003-10-02 08:10:54 +02:00
|
|
|
if not number:
|
|
|
|
factoids = []
|
|
|
|
counter = 1
|
|
|
|
for result in cursor.fetchall():
|
|
|
|
factoids.append('(#%s) %s' % (counter, result[0]))
|
|
|
|
counter += 1
|
|
|
|
irc.reply(msg,'%r could be %s' % (key, ', or '.join(factoids)))
|
|
|
|
else:
|
|
|
|
try:
|
2003-10-08 18:24:41 +02:00
|
|
|
irc.reply(msg, cursor.fetchall()[number-1][0])
|
2003-10-02 08:10:54 +02:00
|
|
|
except IndexError:
|
|
|
|
irc.error(msg, 'That\'s not a valid number for this key.')
|
|
|
|
return
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-29 15:25:40 +02:00
|
|
|
def lock(self, irc, msg, args):
|
2003-08-29 08:32:49 +02:00
|
|
|
"""[<channel>] <key>
|
|
|
|
|
|
|
|
Locks the factoid(s) associated with <key> so that they cannot be
|
|
|
|
removed or added to. <channel> is only necessary if the message isn't
|
|
|
|
sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
key = privmsgs.getArgs(args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
capability = ircdb.makeChannelCapability(channel, 'factoids')
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
cursor = db.cursor()
|
2003-08-29 08:45:51 +02:00
|
|
|
cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE %s", key)
|
2003-03-12 07:26:59 +01:00
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, conf.replyNoCapability % capability)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-29 15:25:40 +02:00
|
|
|
def unlock(self, irc, msg, args):
|
2003-08-29 08:32:49 +02:00
|
|
|
"""[<channel>] <key>
|
|
|
|
|
|
|
|
Unlocks the factoid(s) associated with <key> so that they can be
|
|
|
|
removed or added to. <channel> is only necessary if the message isn't
|
|
|
|
sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
key = privmsgs.getArgs(args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
capability = ircdb.makeChannelCapability(channel, 'factoids')
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
cursor = db.cursor()
|
2003-10-11 23:03:02 +02:00
|
|
|
cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE %s", key)
|
2003-03-12 07:26:59 +01:00
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, conf.replyNoCapability % capability)
|
|
|
|
|
2003-09-08 22:14:46 +02:00
|
|
|
def forget(self, irc, msg, args):
|
2003-09-04 14:14:12 +02:00
|
|
|
"""[<channel>] <key> [<number>|*]
|
2003-08-29 08:32:49 +02:00
|
|
|
|
|
|
|
Removes the factoid <key> from the factoids database. If there are
|
|
|
|
more than one factoid with such a key, a number is necessary to
|
2003-09-04 14:14:12 +02:00
|
|
|
determine which one should be removed. A * can be used to remove all
|
|
|
|
factoids associated with a key. <channel> is only necessary if
|
2003-08-29 08:32:49 +02:00
|
|
|
the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-08-29 08:45:51 +02:00
|
|
|
if args[-1].isdigit():
|
2003-08-29 08:32:49 +02:00
|
|
|
number = int(args.pop())
|
2003-10-02 08:10:54 +02:00
|
|
|
number -= 1
|
|
|
|
if number < 0:
|
|
|
|
irc.error(msg, 'Negative numbers aren\'t valid.')
|
|
|
|
return
|
2003-09-04 14:14:12 +02:00
|
|
|
elif args[-1] == '*':
|
|
|
|
del args[-1]
|
|
|
|
number = True
|
2003-08-29 08:32:49 +02:00
|
|
|
else:
|
|
|
|
number = None
|
2003-03-12 07:26:59 +01:00
|
|
|
key = privmsgs.getArgs(args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
capability = ircdb.makeChannelCapability(channel, 'factoids')
|
|
|
|
if ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
cursor = db.cursor()
|
2003-08-29 08:32:49 +02:00
|
|
|
cursor.execute("""SELECT keys.id, factoids.id
|
|
|
|
FROM keys, factoids
|
2003-08-29 08:45:51 +02:00
|
|
|
WHERE key LIKE %s AND
|
2003-08-29 08:32:49 +02:00
|
|
|
factoids.key_id=keys.id""", key)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such factoid.')
|
2003-09-04 14:14:12 +02:00
|
|
|
elif cursor.rowcount == 1 or number is True:
|
2003-08-29 08:32:49 +02:00
|
|
|
(id, _) = cursor.fetchone()
|
|
|
|
cursor.execute("""DELETE FROM factoids WHERE key_id=%s""", id)
|
2003-08-29 08:45:51 +02:00
|
|
|
cursor.execute("""DELETE FROM keys WHERE key LIKE %s""", key)
|
2003-08-29 08:32:49 +02:00
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
if number is not None:
|
|
|
|
results = cursor.fetchall()
|
|
|
|
try:
|
|
|
|
(_, id) = results[number]
|
|
|
|
except IndexError:
|
|
|
|
irc.error(msg, 'Invalid factoid number.')
|
|
|
|
return
|
|
|
|
cursor.execute("DELETE FROM factoids WHERE id=%s", id)
|
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, '%s factoids have that key. ' \
|
2003-09-04 14:14:12 +02:00
|
|
|
'Please specify which one to remove, ' \
|
|
|
|
'or use * to designate all of them.' % \
|
2003-08-29 08:32:49 +02:00
|
|
|
cursor.rowcount)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
irc.error(msg, conf.replyNoCapability % capability)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-21 07:19:54 +02:00
|
|
|
def random(self, irc, msg, args):
|
2003-08-29 08:32:49 +02:00
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns a random factoid from the database for <channel>. <channel>
|
|
|
|
is only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT fact, key_id FROM factoids
|
|
|
|
ORDER BY random()
|
2003-09-07 08:01:25 +02:00
|
|
|
LIMIT 3""")
|
2003-03-12 07:26:59 +01:00
|
|
|
if cursor.rowcount != 0:
|
2003-08-29 08:45:51 +02:00
|
|
|
L = []
|
|
|
|
for (factoid, id) in cursor.fetchall():
|
|
|
|
cursor.execute("""SELECT key FROM keys WHERE id=%s""", id)
|
|
|
|
(key,) = cursor.fetchone()
|
|
|
|
L.append('"%s": %s' % (ircutils.bold(key), factoid))
|
2003-09-07 08:01:25 +02:00
|
|
|
irc.reply(msg, '; '.join(L))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
irc.error(msg, 'I couldn\'t find a factoid.')
|
|
|
|
|
2003-10-21 07:19:54 +02:00
|
|
|
def info(self, irc, msg, args):
|
2003-08-29 08:32:49 +02:00
|
|
|
"""[<channel>] <key>
|
|
|
|
|
|
|
|
Gives information about the factoid(s) associated with <key>.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
key = privmsgs.getArgs(args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-08-29 08:45:51 +02:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s", key)
|
2003-03-12 07:26:59 +01:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'No factoid matches that key.')
|
|
|
|
return
|
2003-11-15 05:46:09 +01:00
|
|
|
(id, locked) = imap(int, cursor.fetchone())
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor.execute("""SELECT added_by, added_at FROM factoids
|
|
|
|
WHERE key_id=%s
|
|
|
|
ORDER BY id""", id)
|
|
|
|
factoids = cursor.fetchall()
|
|
|
|
L = []
|
2003-10-23 06:17:18 +02:00
|
|
|
counter = 0
|
2003-03-12 07:26:59 +01:00
|
|
|
for (added_by, added_at) in factoids:
|
2003-10-23 06:17:18 +02:00
|
|
|
counter += 1
|
2003-08-26 13:15:15 +02:00
|
|
|
added_at = time.strftime(conf.humanTimestampFormat,
|
2003-03-12 07:26:59 +01:00
|
|
|
time.localtime(int(added_at)))
|
|
|
|
L.append('#%s was added by %s at %s' % (counter,added_by,added_at))
|
|
|
|
factoids = '; '.join(L)
|
2003-10-23 07:14:41 +02:00
|
|
|
s = 'Key %r is %s and has %s associated with it: %s' % \
|
|
|
|
(key, locked and 'locked' or 'not locked',
|
2003-12-12 16:41:33 +01:00
|
|
|
utils.nItems('factoid', counter), factoids)
|
2003-03-12 07:26:59 +01:00
|
|
|
irc.reply(msg, s)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-30 05:36:40 +01:00
|
|
|
def change(self, irc, msg, args):
|
|
|
|
"""[<channel>] <key> <number> <regexp>
|
|
|
|
|
|
|
|
Changes the factoid #<number> associated with <key> according to
|
|
|
|
<regexp>.
|
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-11 14:20:06 +01:00
|
|
|
(key, number, regexp) = privmsgs.getArgs(args, required=3)
|
2003-10-30 05:36:40 +01:00
|
|
|
try:
|
|
|
|
replacer = utils.perlReToReplacer(regexp)
|
|
|
|
except ValueError, e:
|
|
|
|
irc.error(msg, 'Invalid regexp: %s' % e)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
number = int(number)
|
|
|
|
if number <= 0:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
|
|
|
irc.error(msg, 'Invalid key id.')
|
|
|
|
return
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT factoids.id, factoids.fact
|
|
|
|
FROM keys, factoids
|
|
|
|
WHERE keys.key LIKE %s AND
|
|
|
|
keys.id=factoids.key_id""", key)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'I couldn\'t find any key %r' % key)
|
|
|
|
return
|
|
|
|
elif cursor.rowcount < number:
|
|
|
|
irc.error(msg, 'That\'s not a valid key id.')
|
|
|
|
return
|
|
|
|
(id, fact) = cursor.fetchall()[number-1]
|
|
|
|
newfact = replacer(fact)
|
|
|
|
cursor.execute("UPDATE factoids SET fact=%s WHERE id=%s", newfact, id)
|
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
2003-09-05 03:30:02 +02:00
|
|
|
_sqlTrans = string.maketrans('*?', '%_')
|
2003-10-21 07:19:54 +02:00
|
|
|
def search(self, irc, msg, args):
|
2003-12-12 14:25:03 +01:00
|
|
|
"""[<channel>] [--{regexp}=<value>] [<glob>]
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-04 12:16:57 +02:00
|
|
|
Searches the keyspace for keys matching <glob>. If --regexp is given,
|
2003-12-12 14:25:03 +01:00
|
|
|
it associated value is taken as a regexp and matched against the keys.
|
2003-09-05 03:30:02 +02:00
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-12-12 14:25:03 +01:00
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['regexp='])
|
2003-10-10 07:44:41 +02:00
|
|
|
if not optlist and not rest:
|
|
|
|
raise callbacks.ArgumentError
|
2003-10-04 12:16:57 +02:00
|
|
|
criteria = []
|
|
|
|
formats = []
|
|
|
|
predicateName = 'p'
|
2003-09-05 03:30:02 +02:00
|
|
|
db = self.getDb(channel)
|
2003-10-04 12:16:57 +02:00
|
|
|
for (option, arg) in optlist:
|
2003-12-12 14:25:03 +01:00
|
|
|
if option == '--regexp':
|
2003-10-04 12:16:57 +02:00
|
|
|
criteria.append('%s(key)' % predicateName)
|
|
|
|
try:
|
|
|
|
r = utils.perlReToPythonRe(arg)
|
|
|
|
except ValueError, e:
|
|
|
|
irc.error(msg, 'Invalid regexp: %s' % e)
|
|
|
|
return
|
|
|
|
def p(s, r=r):
|
|
|
|
return int(bool(r.search(s)))
|
|
|
|
db.create_function(predicateName, 1, p)
|
|
|
|
predicateName += 'p'
|
|
|
|
for glob in rest:
|
2003-10-22 17:15:50 +02:00
|
|
|
if '*' not in glob and '?' not in glob:
|
|
|
|
glob = '*%s*' % glob
|
2003-10-04 12:16:57 +02:00
|
|
|
criteria.append('key LIKE %s')
|
|
|
|
formats.append(glob.translate(self._sqlTrans))
|
2003-09-05 03:30:02 +02:00
|
|
|
cursor = db.cursor()
|
2003-10-04 12:16:57 +02:00
|
|
|
sql = """SELECT key FROM keys WHERE %s""" % ' AND '.join(criteria)
|
|
|
|
cursor.execute(sql, formats)
|
2003-09-05 03:30:02 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.reply(msg, 'No keys matched that query.')
|
2003-11-18 18:49:10 +01:00
|
|
|
elif cursor.rowcount == 1:
|
|
|
|
self.whatis(irc, msg, [cursor.fetchone()[0]])
|
2003-09-05 03:30:02 +02:00
|
|
|
elif cursor.rowcount > 100:
|
|
|
|
irc.reply(msg, 'More than 100 keys matched that query; '
|
|
|
|
'please narrow your query.')
|
|
|
|
else:
|
|
|
|
keys = [repr(t[0]) for t in cursor.fetchall()]
|
|
|
|
s = utils.commaAndify(keys)
|
2003-10-04 12:16:57 +02:00
|
|
|
irc.reply(msg, s)
|
2003-09-12 22:06:58 +02:00
|
|
|
|
2003-09-05 03:30:02 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
Class = Factoids
|
2003-11-26 19:21:12 +01:00
|
|
|
|
2003-12-12 14:25:03 +01:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|