2003-03-12 07:26:59 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-03-12 07:26:59 +01: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-03-25 07:53:51 +01:00
|
|
|
"""
|
2004-04-30 04:13:42 +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-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
|
|
|
|
2004-10-24 03:14:27 +02:00
|
|
|
import supybot.dbi as dbi
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.ircdb as ircdb
|
2004-10-24 00:07:50 +02:00
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.plugins as plugins
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2004-01-20 13:27:48 +01:00
|
|
|
|
|
|
|
conf.registerPlugin('Factoids')
|
|
|
|
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Factoids, 'learnSeparator',
|
|
|
|
registry.String('as', """Determines what separator must be used in the
|
|
|
|
learn command. Defaults to 'as' -- learn <key> as <value>. Users might
|
|
|
|
feel more comfortable with 'is' or something else, so it's
|
|
|
|
configurable."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Factoids,
|
|
|
|
'showFactoidIfOnlyOneMatch', registry.Boolean(True, """Determines whether
|
|
|
|
the bot will reply with the single matching factoid if only one factoid
|
|
|
|
matches when using the search command."""))
|
2004-02-13 22:50:23 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Factoids,
|
|
|
|
'replyWhenInvalidCommand', registry.Boolean(True, """Determines whether
|
|
|
|
the bot will reply to invalid commands by searching for a factoid;
|
|
|
|
basically making the whatis unnecessary when you want all factoids for a
|
|
|
|
given key."""))
|
2004-07-02 15:58:29 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Factoids,
|
|
|
|
'factoidPrefix', registry.StringWithSpaceOnRight('could be ', """Determines
|
|
|
|
the string that factoids will be introduced by."""))
|
2004-01-20 13:27:48 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
class MultiKeyError(KeyError):
|
|
|
|
pass
|
2004-01-01 20:07:38 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
class LockError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SqliteFactoidsDB(object):
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.dbs = ircutils.IrcDict()
|
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
for db in self.dbs.itervalues():
|
|
|
|
db.close()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def _getDb(self, channel):
|
|
|
|
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/>'
|
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
|
|
|
if filename in self.dbs:
|
|
|
|
return self.dbs[filename]
|
2003-03-12 07:26:59 +01:00
|
|
|
if os.path.exists(filename):
|
2004-10-24 00:07:50 +02:00
|
|
|
self.dbs[filename] = sqlite.connect(filename)
|
|
|
|
return self.dbs[filename]
|
2003-03-12 07:26:59 +01:00
|
|
|
db = sqlite.connect(filename)
|
2004-10-24 00:07:50 +02:00
|
|
|
self.dbs[filename] = db
|
2003-03-12 07:26:59 +01:00
|
|
|
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
|
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def add(self, channel, key, factoid, name):
|
|
|
|
db = self._getDb(channel)
|
2003-03-12 07:26:59 +01:00
|
|
|
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
|
|
|
if not locked:
|
|
|
|
cursor.execute("""INSERT INTO factoids VALUES
|
|
|
|
(NULL, %s, %s, %s, %s)""",
|
|
|
|
id, name, int(time.time()), factoid)
|
|
|
|
db.commit()
|
|
|
|
else:
|
2004-10-24 00:07:50 +02:00
|
|
|
raise LockError
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def get(self, channel, key):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-02-13 22:50:23 +01:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT factoids.fact FROM factoids, keys
|
|
|
|
WHERE keys.key LIKE %s AND factoids.key_id=keys.id
|
|
|
|
ORDER BY factoids.id
|
|
|
|
LIMIT 20""", key)
|
|
|
|
return [t[0] for t in cursor.fetchall()]
|
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def lock(self, channel, key):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE %s", key)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def unlock(self, channel, key):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE %s", key)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def remove(self, channel, key, number):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT keys.id, factoids.id
|
|
|
|
FROM keys, factoids
|
|
|
|
WHERE key LIKE %s AND
|
|
|
|
factoids.key_id=keys.id""", key)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
elif cursor.rowcount == 1 or number is True:
|
|
|
|
(id, _) = cursor.fetchone()
|
|
|
|
cursor.execute("""DELETE FROM factoids WHERE key_id=%s""", id)
|
|
|
|
cursor.execute("""DELETE FROM keys WHERE key LIKE %s""", key)
|
|
|
|
db.commit()
|
|
|
|
else:
|
|
|
|
if number is not None:
|
|
|
|
results = cursor.fetchall()
|
|
|
|
try:
|
|
|
|
(_, id) = results[number]
|
|
|
|
except IndexError:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
cursor.execute("DELETE FROM factoids WHERE id=%s", id)
|
|
|
|
db.commit()
|
|
|
|
else:
|
|
|
|
raise MultiKeyError, cursor.rowcount
|
|
|
|
|
|
|
|
def random(self, channel):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT fact, key_id FROM factoids
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 3""")
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
L = []
|
|
|
|
for (factoid, id) in cursor.fetchall():
|
|
|
|
cursor.execute("""SELECT key FROM keys WHERE id=%s""", id)
|
|
|
|
(key,) = cursor.fetchone()
|
|
|
|
L.append((key, factoid))
|
|
|
|
return L
|
|
|
|
|
|
|
|
def info(self, channel, key):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s", key)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
(id, locked) = imap(int, cursor.fetchone())
|
|
|
|
cursor.execute("""SELECT added_by, added_at FROM factoids
|
|
|
|
WHERE key_id=%s
|
|
|
|
ORDER BY id""", id)
|
|
|
|
return (locked, cursor.fetchall())
|
|
|
|
|
|
|
|
_sqlTrans = string.maketrans('*?', '%_')
|
|
|
|
def select(self, channel, values, predicates, globs):
|
2004-11-01 05:00:24 +01:00
|
|
|
db = self._getDb(channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
tables = ['keys']
|
|
|
|
criteria = []
|
|
|
|
predicateName = 'p'
|
|
|
|
formats = []
|
|
|
|
if not values:
|
|
|
|
target = 'keys.key'
|
|
|
|
else:
|
|
|
|
target = 'factoids.fact'
|
|
|
|
if 'factoids' not in tables:
|
|
|
|
tables.append('factoids')
|
|
|
|
criteria.append('factoids.key_id=keys.id')
|
|
|
|
for glob in globs:
|
|
|
|
criteria.append('TARGET LIKE %s')
|
|
|
|
formats.append(glob.translate(self._sqlTrans))
|
2004-11-01 05:00:24 +01:00
|
|
|
for r in predicates:
|
2004-10-24 00:07:50 +02:00
|
|
|
criteria.append('%s(TARGET)' % predicateName)
|
2004-11-01 05:00:24 +01:00
|
|
|
def p(s, r=r):
|
|
|
|
return int(bool(r(s)))
|
2004-10-24 00:07:50 +02:00
|
|
|
db.create_function(predicateName, 1, p)
|
|
|
|
predicateName += 'p'
|
|
|
|
sql = """SELECT keys.key FROM %s WHERE %s""" % \
|
|
|
|
(', '.join(tables), ' AND '.join(criteria))
|
|
|
|
sql = sql.replace('TARGET', target)
|
|
|
|
cursor.execute(sql, formats)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
elif cursor.rowcount == 1 and \
|
|
|
|
conf.supybot.plugins.Factoids.showFactoidIfOnlyOneMatch.get(channel)():
|
|
|
|
return cursor.fetchone()[0]
|
|
|
|
elif cursor.rowcount > 100:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
2004-10-24 03:14:27 +02:00
|
|
|
FactoidsDB = plugins.DB('Factoids', {'sqlite': SqliteFactoidsDB})
|
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
class Factoids(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
|
|
|
self.__parent = super(Factoids, self)
|
|
|
|
self.__parent.__init__()
|
|
|
|
self.db = FactoidsDB()
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self.__parent.die()
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
def learn(self, irc, msg, args, channel, text):
|
|
|
|
"""[<channel>] <key> as <value>
|
|
|
|
|
|
|
|
Associates <key> with <value>. <channel> is only necessary if the
|
|
|
|
message isn't sent on the channel itself. The word 'as' is necessary
|
|
|
|
to separate the key from the value. It can be changed to another
|
|
|
|
word via the learnSeparator registry value.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
separator = conf.supybot.plugins.Factoids. \
|
|
|
|
learnSeparator.get(channel)()
|
|
|
|
i = text.index(separator)
|
|
|
|
except ValueError:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
text.pop(i)
|
|
|
|
key = ' '.join(text[:i])
|
|
|
|
factoid = ' '.join(text[i:])
|
|
|
|
try:
|
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
|
|
|
except KeyError:
|
|
|
|
name = msg.nick
|
|
|
|
try:
|
2004-10-31 16:17:33 +01:00
|
|
|
self.db.add(channel, key, factoid, name)
|
2004-10-24 00:07:50 +02:00
|
|
|
irc.replySuccess()
|
|
|
|
except LockError:
|
|
|
|
irc.error('That factoid is locked.')
|
2004-10-31 16:17:33 +01:00
|
|
|
learn = wrap(learn, ['channeldb', many('something')])
|
2004-10-24 00:07:50 +02:00
|
|
|
|
2004-07-02 16:08:19 +02:00
|
|
|
def _replyFactoids(self, irc, channel, key, factoids, number=0, error=True):
|
2004-02-13 22:50:23 +01:00
|
|
|
if factoids:
|
|
|
|
if number:
|
|
|
|
try:
|
2004-10-24 00:07:50 +02:00
|
|
|
irc.reply(factoids[number])
|
2004-02-13 22:50:23 +01:00
|
|
|
except IndexError:
|
2004-10-25 10:00:13 +02:00
|
|
|
irc.errorInvalid('number for that key')
|
2004-02-13 22:50:23 +01:00
|
|
|
else:
|
2004-07-02 15:58:29 +02:00
|
|
|
intro = self.registryValue('factoidPrefix', channel)
|
2004-10-24 00:07:50 +02:00
|
|
|
prefix = '%s %s' % (utils.quoted(key), intro)
|
2004-07-02 15:58:29 +02:00
|
|
|
if len(factoids) == 1:
|
|
|
|
irc.reply(prefix + factoids[0])
|
|
|
|
else:
|
|
|
|
factoidsS = []
|
|
|
|
counter = 1
|
|
|
|
for factoid in factoids:
|
|
|
|
factoidsS.append('(#%s) %s' % (counter, factoid))
|
|
|
|
counter += 1
|
|
|
|
irc.replies(factoidsS, prefixer=prefix,
|
|
|
|
joiner=', or ', onlyPrefixFirst=True)
|
2004-02-16 09:30:59 +01:00
|
|
|
elif error:
|
2004-02-13 22:50:23 +01:00
|
|
|
irc.error('No factoid matches that key.')
|
|
|
|
|
2004-09-16 12:54:42 +02:00
|
|
|
def tokenizedCommand(self, irc, msg, tokens):
|
2004-02-13 22:50:23 +01:00
|
|
|
if ircutils.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
|
|
|
if self.registryValue('replyWhenInvalidCommand', channel):
|
|
|
|
key = ' '.join(tokens)
|
2004-10-24 00:07:50 +02:00
|
|
|
factoids = self.db.get(channel, key)
|
2004-07-02 16:08:19 +02:00
|
|
|
self._replyFactoids(irc, channel, key, factoids, error=False)
|
2004-02-13 22:50:23 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def whatis(self, irc, msg, args, channel, number, key):
|
2003-08-29 08:32:49 +02:00
|
|
|
"""[<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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
factoids = self.db.get(channel, key)
|
2004-07-02 16:08:19 +02:00
|
|
|
self._replyFactoids(irc, channel, key, factoids, number)
|
2004-10-25 10:00:13 +02:00
|
|
|
whatis = wrap(whatis, ['channeldb', reverse(optional('positiveInt', 0)),
|
|
|
|
'something'])
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def lock(self, irc, msg, args, channel, key):
|
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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
self.db.lock(channel, key)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.replySuccess()
|
2004-11-01 05:00:24 +01:00
|
|
|
lock = wrap(lock, ['channeldb', ('checkChannelCapability', 'op'),
|
|
|
|
'something'])
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def unlock(self, irc, msg, args, channel, key):
|
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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
self.db.unlock(channel, key)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.replySuccess()
|
2004-11-01 05:00:24 +01:00
|
|
|
unlock = wrap(unlock, ['channeldb', ('checkChannelCapability', 'op'),
|
|
|
|
'something'])
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def forget(self, irc, msg, args, channel, number, key):
|
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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
if number == '*':
|
2003-09-04 14:14:12 +02:00
|
|
|
number = True
|
2003-08-29 08:32:49 +02:00
|
|
|
else:
|
2004-10-24 00:07:50 +02:00
|
|
|
number -= 1
|
|
|
|
key = ' '.join(key)
|
|
|
|
try:
|
|
|
|
self.db.remove(channel, key, number)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.replySuccess()
|
2004-10-24 00:07:50 +02:00
|
|
|
except dbi.NoRecordError:
|
|
|
|
irc.error('There is no such factoid.')
|
|
|
|
except MultiKeyError, e:
|
|
|
|
irc.error('%s factoids have that key. '
|
|
|
|
'Please specify which one to remove, '
|
|
|
|
'or use * to designate all of them.' % str(e))
|
|
|
|
forget = wrap(forget, ['channeldb',
|
2004-10-24 05:53:56 +02:00
|
|
|
reverse(first('positiveInt', ('literal', '*'))),
|
2004-10-25 10:00:13 +02:00
|
|
|
'something'])
|
2004-10-24 00:07:50 +02:00
|
|
|
|
|
|
|
def random(self, irc, msg, args, channel):
|
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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
try:
|
|
|
|
L = ['"%s": %s' % (ircutils.bold(k), v)
|
|
|
|
for (k, v) in self.db.random(channel)]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('; '.join(L))
|
2004-10-24 00:07:50 +02:00
|
|
|
except dbi.NoRecordError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I couldn\'t find a factoid.')
|
2004-10-24 00:07:50 +02:00
|
|
|
random = wrap(random, ['channeldb'])
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def info(self, irc, msg, args, channel, key):
|
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.
|
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
try:
|
|
|
|
(locked, factoids) = self.db.info(channel, key)
|
|
|
|
except dbi.NoRecordError:
|
|
|
|
irc.error('No factoid matches that key.', Raise=True)
|
2003-03-12 07:26:59 +01:00
|
|
|
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
|
2004-01-18 08:58:26 +01:00
|
|
|
added_at = time.strftime(conf.supybot.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)
|
2004-10-24 00:07:50 +02:00
|
|
|
s = 'Key %s is %s and has %s associated with it: %s' % \
|
|
|
|
(utils.quoted(key), locked and 'locked' or 'not locked',
|
2003-12-12 16:41:33 +01:00
|
|
|
utils.nItems('factoid', counter), factoids)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2004-10-25 10:00:13 +02:00
|
|
|
info = wrap(info, ['channeldb', 'something'])
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-01-08 22:49:10 +01:00
|
|
|
def change(self, irc, msg, args):
|
2003-10-30 05:36:40 +01:00
|
|
|
"""[<channel>] <key> <number> <regexp>
|
|
|
|
|
|
|
|
Changes the factoid #<number> associated with <key> according to
|
|
|
|
<regexp>.
|
|
|
|
"""
|
2004-01-08 22:49:10 +01:00
|
|
|
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:
|
2004-09-17 07:09:14 +02:00
|
|
|
irc.error('Invalid regexp: %s' % e)
|
|
|
|
return
|
2003-10-30 05:36:40 +01:00
|
|
|
try:
|
|
|
|
number = int(number)
|
|
|
|
if number <= 0:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
2004-09-17 07:09:14 +02:00
|
|
|
irc.error('Invalid key id.')
|
|
|
|
return
|
2003-10-30 05:36:40 +01:00
|
|
|
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:
|
2004-10-24 00:07:50 +02:00
|
|
|
irc.error('I couldn\'t find any key %s' % utils.quoted(key))
|
2003-10-30 05:36:40 +01:00
|
|
|
return
|
|
|
|
elif cursor.rowcount < number:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That\'s not a valid key id.')
|
2003-10-30 05:36:40 +01:00
|
|
|
return
|
|
|
|
(id, fact) = cursor.fetchall()[number-1]
|
|
|
|
newfact = replacer(fact)
|
|
|
|
cursor.execute("UPDATE factoids SET fact=%s WHERE id=%s", newfact, id)
|
|
|
|
db.commit()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-10-30 05:36:40 +01:00
|
|
|
|
2004-10-24 00:07:50 +02:00
|
|
|
def search(self, irc, msg, args, channel, optlist, globs):
|
2004-04-30 20:24:35 +02:00
|
|
|
"""[<channel>] [--values] [--{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.
|
2004-04-30 20:24:35 +02:00
|
|
|
If --values is given, search the value space instead of the keyspace.
|
2003-09-05 03:30:02 +02:00
|
|
|
"""
|
2004-10-24 00:07:50 +02:00
|
|
|
if not optlist and not globs:
|
2003-10-10 07:44:41 +02:00
|
|
|
raise callbacks.ArgumentError
|
2004-10-24 00:07:50 +02:00
|
|
|
values = False
|
2004-11-01 05:00:24 +01:00
|
|
|
predicates = []
|
2003-10-04 12:16:57 +02:00
|
|
|
for (option, arg) in optlist:
|
2004-10-24 00:07:50 +02:00
|
|
|
if option == 'values':
|
|
|
|
values = True
|
|
|
|
elif option == 'regexp':
|
2004-11-01 05:00:24 +01:00
|
|
|
predicates.append(arg.search)
|
2004-10-24 00:07:50 +02:00
|
|
|
L = []
|
|
|
|
for glob in globs:
|
2003-10-22 17:15:50 +02:00
|
|
|
if '*' not in glob and '?' not in glob:
|
|
|
|
glob = '*%s*' % glob
|
2004-10-24 00:07:50 +02:00
|
|
|
L.append(glob)
|
|
|
|
try:
|
|
|
|
factoids = self.db.select(channel, values, predicates, L)
|
|
|
|
if isinstance(factoids, basestring):
|
|
|
|
self.whatis(irc, msg, factoids)
|
|
|
|
elif factoids is None:
|
|
|
|
irc.reply('More than 100 keys matched that query; '
|
|
|
|
'please narrow your query.')
|
|
|
|
else:
|
|
|
|
keys = [repr(t[0]) for t in factoids]
|
|
|
|
s = utils.commaAndify(keys)
|
|
|
|
irc.reply(s)
|
|
|
|
except dbi.NoRecordError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('No keys matched that query.')
|
2004-10-24 00:07:50 +02:00
|
|
|
search = wrap(search, ['channeldb',
|
|
|
|
getopts({'values':'', 'regexp':'regexpMatcher'}),
|
2004-11-01 05:00:24 +01:00
|
|
|
any('glob')])
|
2003-09-12 22:06:58 +02:00
|
|
|
|
2004-06-19 02:10:04 +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:
|