2005-04-17 21:43:09 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
2010-02-06 00:50:03 +01:00
|
|
|
# Copyright (c) 2009-2010, James Vega
|
2005-04-17 21:43:09 +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.
|
|
|
|
###
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import string
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
|
|
|
|
2010-03-21 07:25:11 +01:00
|
|
|
#try:
|
|
|
|
#import sqlite3 as sqlite
|
|
|
|
#except ImportError:
|
|
|
|
#raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
|
|
|
|
#'plugin. Download it at ' \
|
|
|
|
#'<http://code.google.com/p/pysqlite/>'
|
|
|
|
|
2005-04-17 21:43:09 +02:00
|
|
|
try:
|
2010-03-21 07:25:11 +01:00
|
|
|
import sqlite3
|
2005-04-17 21:43:09 +02:00
|
|
|
except ImportError:
|
2010-03-21 07:25:11 +01:00
|
|
|
from pysqlite2 import dbapi2 as sqlite3 # for python2.4
|
|
|
|
|
|
|
|
# these are needed cuz we are overriding getdb
|
|
|
|
import threading
|
|
|
|
import supybot.world as world
|
2005-04-17 21:43:09 +02:00
|
|
|
|
|
|
|
def getFactoid(irc, msg, args, state):
|
|
|
|
assert not state.channel
|
|
|
|
callConverter('channel', irc, msg, args, state)
|
|
|
|
separator = state.cb.registryValue('learnSeparator', state.channel)
|
|
|
|
try:
|
|
|
|
i = args.index(separator)
|
|
|
|
except ValueError:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
args.pop(i)
|
|
|
|
key = []
|
|
|
|
value = []
|
|
|
|
for (j, s) in enumerate(args[:]):
|
|
|
|
if j < i:
|
|
|
|
key.append(args.pop(0))
|
|
|
|
else:
|
|
|
|
value.append(args.pop(0))
|
2010-02-06 00:50:03 +01:00
|
|
|
if not key or not value:
|
|
|
|
raise callbacks.ArgumentError
|
2005-04-17 21:43:09 +02:00
|
|
|
state.args.append(' '.join(key))
|
|
|
|
state.args.append(' '.join(value))
|
|
|
|
|
2005-04-17 23:38:17 +02:00
|
|
|
def getFactoidId(irc, msg, args, state):
|
|
|
|
Type = 'key id'
|
|
|
|
p = lambda i: i > 0
|
|
|
|
callConverter('int', irc, msg, args, state, Type, p)
|
|
|
|
|
2005-04-17 21:43:09 +02:00
|
|
|
addConverter('factoid', getFactoid)
|
2005-04-17 23:38:17 +02:00
|
|
|
addConverter('factoidId', getFactoidId)
|
2005-04-17 21:43:09 +02:00
|
|
|
|
|
|
|
class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
|
2009-04-06 23:30:53 +02:00
|
|
|
def __init__(self, irc):
|
|
|
|
callbacks.Plugin.__init__(self, irc)
|
|
|
|
plugins.ChannelDBHandler.__init__(self)
|
|
|
|
|
2005-04-17 21:43:09 +02:00
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
2010-03-21 07:25:11 +01:00
|
|
|
db = sqlite3.connect(filename)
|
|
|
|
db.text_factory = str
|
|
|
|
return db
|
|
|
|
db = sqlite3.connect(filename)
|
|
|
|
db.text_factory = str
|
2005-04-17 21:43:09 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE keys (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
key TEXT UNIQUE ON CONFLICT IGNORE,
|
|
|
|
locked BOOLEAN
|
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE factoids (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
key_id INTEGER,
|
|
|
|
added_by TEXT,
|
|
|
|
added_at TIMESTAMP,
|
2010-03-05 19:03:08 +01:00
|
|
|
usage_count INTEGER,
|
2005-04-17 21:43:09 +02:00
|
|
|
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
|
|
|
|
|
2010-03-21 07:25:11 +01:00
|
|
|
# override this because sqlite3 doesn't have autocommit
|
|
|
|
# use isolation_level instead.
|
|
|
|
def getDb(self, channel):
|
|
|
|
"""Use this to get a database for a specific channel."""
|
|
|
|
currentThread = threading.currentThread()
|
|
|
|
if channel not in self.dbCache and currentThread == world.mainThread:
|
|
|
|
self.dbCache[channel] = self.makeDb(self.makeFilename(channel))
|
|
|
|
if currentThread != world.mainThread:
|
|
|
|
db = self.makeDb(self.makeFilename(channel))
|
|
|
|
else:
|
|
|
|
db = self.dbCache[channel]
|
|
|
|
db.isolation_level = None
|
|
|
|
return db
|
|
|
|
|
2009-05-18 17:09:18 +02:00
|
|
|
def getCommandHelp(self, command, simpleSyntax=None):
|
2006-08-28 15:14:21 +02:00
|
|
|
method = self.getCommandMethod(command)
|
|
|
|
if method.im_func.func_name == 'learn':
|
2009-04-28 14:03:21 +02:00
|
|
|
chan = None
|
|
|
|
if dynamic.msg is not None:
|
|
|
|
chan = dynamic.msg.args[0]
|
|
|
|
s = self.registryValue('learnSeparator', chan)
|
2008-12-22 03:21:07 +01:00
|
|
|
help = callbacks.getHelp
|
2009-05-18 17:09:18 +02:00
|
|
|
if simpleSyntax is None:
|
|
|
|
simpleSyntax = conf.get(conf.supybot.reply.showSimpleSyntax,
|
|
|
|
chan)
|
|
|
|
if simpleSyntax:
|
2008-12-22 03:21:07 +01:00
|
|
|
help = callbacks.getSyntax
|
|
|
|
return help(method,
|
|
|
|
doc=method._fake__doc__ % (s, s),
|
|
|
|
name=callbacks.formatCommand(command))
|
2009-05-18 17:09:18 +02:00
|
|
|
return super(Factoids, self).getCommandHelp(command, simpleSyntax)
|
2005-04-17 21:43:09 +02:00
|
|
|
|
2006-08-28 15:14:21 +02:00
|
|
|
def learn(self, irc, msg, args, channel, key, factoid):
|
2005-04-17 21:43:09 +02:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
|
2009-11-18 08:03:44 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("""INSERT INTO keys VALUES (NULL, ?, 0)""", (key,))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
|
2009-11-18 08:03:44 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
(id, locked) = map(int, results[0])
|
2005-04-17 21:43:09 +02:00
|
|
|
capability = ircdb.makeChannelCapability(channel, 'factoids')
|
|
|
|
if not locked:
|
|
|
|
if ircdb.users.hasUser(msg.prefix):
|
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
|
|
|
else:
|
|
|
|
name = msg.nick
|
|
|
|
cursor.execute("""INSERT INTO factoids VALUES
|
2010-03-21 07:25:11 +01:00
|
|
|
(NULL, ?, ?, ?, ?, ?)""",
|
|
|
|
(id, name, int(time.time()), 0, factoid))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
|
|
|
irc.error('That factoid is locked.')
|
|
|
|
learn = wrap(learn, ['factoid'])
|
2006-08-28 15:14:21 +02:00
|
|
|
learn._fake__doc__ = """[<channel>] <key> %s <value>
|
|
|
|
|
|
|
|
Associates <key> with <value>. <channel> is only
|
|
|
|
necessary if the message isn't sent on the channel
|
|
|
|
itself. The word '%s' is necessary to separate the
|
|
|
|
key from the value. It can be changed to another word
|
|
|
|
via the learnSeparator registry value.
|
|
|
|
"""
|
|
|
|
|
2005-04-17 21:43:09 +02:00
|
|
|
|
|
|
|
def _lookupFactoid(self, channel, key):
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2010-03-05 19:03:08 +01:00
|
|
|
cursor.execute("""SELECT factoids.fact, factoids.id FROM factoids, keys
|
2009-11-18 07:48:11 +01:00
|
|
|
WHERE keys.key LIKE ? AND factoids.key_id=keys.id
|
2005-04-17 21:43:09 +02:00
|
|
|
ORDER BY factoids.id
|
2009-11-18 07:48:11 +01:00
|
|
|
LIMIT 20""", (key,))
|
2010-03-05 19:03:08 +01:00
|
|
|
return cursor.fetchall()
|
|
|
|
#return [t[0] for t in cursor.fetchall()]
|
|
|
|
|
2010-03-23 20:46:22 +01:00
|
|
|
def _searchFactoid(self, channel, key):
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
key = '%' + key + '%'
|
|
|
|
cursor.execute("""SELECT key FROM keys
|
|
|
|
WHERE key LIKE ?
|
|
|
|
LIMIT 20""", (key,))
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
|
2010-03-05 19:03:08 +01:00
|
|
|
def _updateRank(self, channel, factoids):
|
|
|
|
if self.registryValue('keepRankInfo', channel):
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
for (fact,id) in factoids:
|
|
|
|
cursor.execute("""SELECT factoids.usage_count
|
|
|
|
FROM factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
WHERE factoids.id=?""", (id,))
|
2010-03-05 19:03:08 +01:00
|
|
|
old_count = cursor.fetchall()[0][0]
|
2010-03-21 07:25:11 +01:00
|
|
|
cursor.execute("UPDATE factoids SET usage_count=? WHERE id=?", (old_count + 1, id,))
|
2010-03-05 19:03:08 +01:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def _replyFactoids(self, irc, msg, key, channel, factoids,
|
2005-04-17 23:38:17 +02:00
|
|
|
number=0, error=True):
|
2005-04-17 21:43:09 +02:00
|
|
|
if factoids:
|
|
|
|
if number:
|
|
|
|
try:
|
2010-03-05 19:03:08 +01:00
|
|
|
irc.reply(factoids[number-1][0])
|
|
|
|
self._updateRank(channel, [factoids[number-1]])
|
2005-04-17 21:43:09 +02:00
|
|
|
except IndexError:
|
|
|
|
irc.error('That\'s not a valid number for that key.')
|
|
|
|
return
|
|
|
|
else:
|
2009-10-16 04:37:59 +02:00
|
|
|
env = {'key': key}
|
|
|
|
def prefixer(v):
|
|
|
|
env['value'] = v
|
|
|
|
formatter = self.registryValue('format', msg.args[0])
|
|
|
|
return ircutils.standardSubstitute(irc, msg,
|
|
|
|
formatter, env)
|
2005-04-17 21:43:09 +02:00
|
|
|
if len(factoids) == 1:
|
2010-03-05 19:03:08 +01:00
|
|
|
irc.reply(prefixer(factoids[0][0]))
|
2005-04-17 21:43:09 +02:00
|
|
|
else:
|
|
|
|
factoidsS = []
|
|
|
|
counter = 1
|
|
|
|
for factoid in factoids:
|
2010-03-05 19:03:08 +01:00
|
|
|
factoidsS.append(format('(#%i) %s', counter, factoid[0]))
|
2005-04-17 21:43:09 +02:00
|
|
|
counter += 1
|
2009-10-16 04:37:59 +02:00
|
|
|
irc.replies(factoidsS, prefixer=prefixer,
|
2005-04-17 21:43:09 +02:00
|
|
|
joiner=', or ', onlyPrefixFirst=True)
|
2010-03-05 19:03:08 +01:00
|
|
|
self._updateRank(channel, factoids)
|
2005-04-17 21:43:09 +02:00
|
|
|
elif error:
|
|
|
|
irc.error('No factoid matches that key.')
|
|
|
|
|
2005-04-17 23:38:17 +02:00
|
|
|
def invalidCommand(self, irc, msg, tokens):
|
2005-04-17 21:43:09 +02:00
|
|
|
if irc.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
|
|
|
if self.registryValue('replyWhenInvalidCommand', channel):
|
|
|
|
key = ' '.join(tokens)
|
|
|
|
factoids = self._lookupFactoid(channel, key)
|
2010-03-23 20:46:22 +01:00
|
|
|
if factoids:
|
|
|
|
self._replyFactoids(irc, msg, key, channel, factoids, error=False)
|
|
|
|
else:
|
|
|
|
if self.registryValue('replyWhenInvalidCommandSearchKeys'):
|
|
|
|
factoids = self._searchFactoid(channel, key)
|
|
|
|
#print 'searchfactoids result:', factoids, '>'
|
|
|
|
if factoids:
|
|
|
|
keylist = ["'%s'" % (fact[0],) for fact in factoids]
|
|
|
|
keylist = ', '.join(keylist)
|
|
|
|
irc.reply("I do not know about '%s', but I do know about these similar topics: %s" % (key, keylist))
|
2005-04-17 21:43:09 +02:00
|
|
|
|
2005-04-18 03:58:57 +02:00
|
|
|
def whatis(self, irc, msg, args, channel, words):
|
2005-04-17 21:43:09 +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.
|
|
|
|
"""
|
2005-04-18 03:58:57 +02:00
|
|
|
number = None
|
|
|
|
if len(words) > 1:
|
|
|
|
if words[-1].isdigit():
|
|
|
|
number = int(words.pop())
|
|
|
|
if number <= 0:
|
|
|
|
irc.errorInvalid('key id')
|
|
|
|
key = ' '.join(words)
|
2005-04-17 21:43:09 +02:00
|
|
|
factoids = self._lookupFactoid(channel, key)
|
2010-03-05 19:03:08 +01:00
|
|
|
self._replyFactoids(irc, msg, key, channel, factoids, number)
|
2005-04-18 03:58:57 +02:00
|
|
|
whatis = wrap(whatis, ['channel', many('something')])
|
2005-04-17 21:43:09 +02:00
|
|
|
|
2010-03-23 18:34:50 +01:00
|
|
|
def rank(self, irc, msg, args, channel):
|
2010-03-05 19:03:08 +01:00
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns a list of top-ranked factoid keys, sorted by usage count
|
|
|
|
(rank). The number of factoid keys returned is set by the
|
|
|
|
rankListLength registry value. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
numfacts = self.registryValue('rankListLength', channel)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT keys.key, factoids.usage_count
|
|
|
|
FROM keys, factoids
|
|
|
|
WHERE factoids.key_id=keys.id
|
|
|
|
ORDER BY factoids.usage_count DESC
|
2010-03-21 07:25:11 +01:00
|
|
|
LIMIT ?""", (numfacts,))
|
2010-03-05 19:03:08 +01:00
|
|
|
factkeys = cursor.fetchall()
|
2010-03-16 04:12:35 +01:00
|
|
|
s = [ "#%d %s (%d)" % (i+1, key[0], key[1]) for i, key in enumerate(factkeys) ]
|
2010-03-05 19:03:08 +01:00
|
|
|
irc.reply(", ".join(s))
|
2010-03-23 18:34:50 +01:00
|
|
|
rank = wrap(rank, ['channel'])
|
2010-03-05 19:03:08 +01:00
|
|
|
|
2005-04-17 21:43:09 +02:00
|
|
|
def lock(self, irc, msg, args, channel, key):
|
|
|
|
"""[<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.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE ?", (key,))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
lock = wrap(lock, ['channel', 'text'])
|
|
|
|
|
|
|
|
def unlock(self, irc, msg, args, channel, key):
|
|
|
|
"""[<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.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE ?", (key,))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
unlock = wrap(unlock, ['channel', 'text'])
|
|
|
|
|
2005-04-18 03:58:57 +02:00
|
|
|
def forget(self, irc, msg, args, channel, words):
|
2005-04-17 21:43:09 +02:00
|
|
|
"""[<channel>] <key> [<number>|*]
|
|
|
|
|
|
|
|
Removes the factoid <key> from the factoids database. If there are
|
|
|
|
more than one factoid with such a key, a number is necessary to
|
|
|
|
determine which one should be removed. A * can be used to remove all
|
|
|
|
factoids associated with a key. <channel> is only necessary if
|
|
|
|
the message isn't sent in the channel itself.
|
|
|
|
"""
|
2005-04-18 03:58:57 +02:00
|
|
|
number = None
|
|
|
|
if len(words) > 1:
|
|
|
|
if words[-1].isdigit():
|
|
|
|
number = int(words.pop())
|
|
|
|
if number <= 0:
|
|
|
|
irc.errorInvalid('key id')
|
|
|
|
elif words[-1] == '*':
|
|
|
|
words.pop()
|
|
|
|
number = True
|
|
|
|
key = ' '.join(words)
|
2005-04-17 21:43:09 +02:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT keys.id, factoids.id
|
|
|
|
FROM keys, factoids
|
2009-11-18 07:48:11 +01:00
|
|
|
WHERE key LIKE ? AND
|
|
|
|
factoids.key_id=keys.id""", (key,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.error('There is no such factoid.')
|
2009-11-18 07:48:11 +01:00
|
|
|
elif len(results) == 1 or number is True:
|
|
|
|
(id, _) = results[0]
|
|
|
|
cursor.execute("""DELETE FROM factoids WHERE key_id=?""", (id,))
|
|
|
|
cursor.execute("""DELETE FROM keys WHERE key LIKE ?""", (key,))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
|
|
|
if number is not None:
|
2009-11-18 07:48:11 +01:00
|
|
|
#results = cursor.fetchall()
|
2005-04-17 21:43:09 +02:00
|
|
|
try:
|
2005-04-17 23:38:17 +02:00
|
|
|
(_, id) = results[number-1]
|
2005-04-17 21:43:09 +02:00
|
|
|
except IndexError:
|
|
|
|
irc.error('Invalid factoid number.')
|
|
|
|
return
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("DELETE FROM factoids WHERE id=?", (id,))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
|
|
|
irc.error('%s factoids have that key. '
|
|
|
|
'Please specify which one to remove, '
|
|
|
|
'or use * to designate all of them.' %
|
2009-11-18 08:03:44 +01:00
|
|
|
len(results))
|
2005-04-18 03:58:57 +02:00
|
|
|
forget = wrap(forget, ['channel', many('something')])
|
2005-04-17 21:43:09 +02:00
|
|
|
|
|
|
|
def random(self, irc, msg, args, channel):
|
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns a random factoid from the database for <channel>. <channel>
|
|
|
|
is only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT fact, key_id FROM factoids
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 3""")
|
2009-11-18 08:03:44 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) != 0:
|
2005-04-17 21:43:09 +02:00
|
|
|
L = []
|
2009-11-18 08:03:44 +01:00
|
|
|
for (factoid, id) in results:
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("""SELECT key FROM keys WHERE id=?""", (id,))
|
2005-04-17 21:43:09 +02:00
|
|
|
(key,) = cursor.fetchone()
|
|
|
|
L.append('"%s": %s' % (ircutils.bold(key), factoid))
|
|
|
|
irc.reply('; '.join(L))
|
|
|
|
else:
|
|
|
|
irc.error('I couldn\'t find a factoid.')
|
|
|
|
random = wrap(random, ['channel'])
|
|
|
|
|
|
|
|
def info(self, irc, msg, args, channel, key):
|
|
|
|
"""[<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.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
|
2009-11-18 08:03:44 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.error('No factoid matches that key.')
|
|
|
|
return
|
2009-11-18 08:03:44 +01:00
|
|
|
(id, locked) = map(int, results[0])
|
2010-04-01 06:51:25 +02:00
|
|
|
cursor.execute("""SELECT added_by, added_at, usage_count FROM factoids
|
2009-11-18 07:48:11 +01:00
|
|
|
WHERE key_id=?
|
|
|
|
ORDER BY id""", (id,))
|
2005-04-17 21:43:09 +02:00
|
|
|
factoids = cursor.fetchall()
|
|
|
|
L = []
|
|
|
|
counter = 0
|
2010-04-01 06:51:25 +02:00
|
|
|
for (added_by, added_at, usage_count) in factoids:
|
2005-04-17 21:43:09 +02:00
|
|
|
counter += 1
|
2009-02-27 20:17:05 +01:00
|
|
|
added_at = time.strftime(conf.supybot.reply.format.time(),
|
|
|
|
time.localtime(int(added_at)))
|
2010-04-01 06:51:25 +02:00
|
|
|
L.append(format('#%i was added by %s at %s, and has been recalled '
|
|
|
|
'%n',
|
|
|
|
counter, added_by, added_at, (usage_count, 'time')))
|
2005-04-17 21:43:09 +02:00
|
|
|
factoids = '; '.join(L)
|
|
|
|
s = format('Key %q is %s and has %n associated with it: %s',
|
|
|
|
key, locked and 'locked' or 'not locked',
|
|
|
|
(counter, 'factoid'), factoids)
|
|
|
|
irc.reply(s)
|
|
|
|
info = wrap(info, ['channel', 'text'])
|
|
|
|
|
|
|
|
def change(self, irc, msg, args, channel, key, number, replacer):
|
|
|
|
"""[<channel>] <key> <number> <regexp>
|
|
|
|
|
|
|
|
Changes the factoid #<number> associated with <key> according to
|
|
|
|
<regexp>.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT factoids.id, factoids.fact
|
|
|
|
FROM keys, factoids
|
2009-11-18 07:48:11 +01:00
|
|
|
WHERE keys.key LIKE ? AND
|
|
|
|
keys.id=factoids.key_id""", (key,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.error(format('I couldn\'t find any key %q', key))
|
|
|
|
return
|
2009-11-18 07:48:11 +01:00
|
|
|
elif len(results) < number:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.errorInvalid('key id')
|
2009-11-18 07:48:11 +01:00
|
|
|
(id, fact) = results[number-1]
|
2005-04-17 21:43:09 +02:00
|
|
|
newfact = replacer(fact)
|
2009-11-18 07:48:11 +01:00
|
|
|
cursor.execute("UPDATE factoids SET fact=? WHERE id=?", (newfact, id))
|
2005-04-17 21:43:09 +02:00
|
|
|
db.commit()
|
|
|
|
irc.replySuccess()
|
|
|
|
change = wrap(change, ['channel', 'something',
|
2005-04-17 23:38:17 +02:00
|
|
|
'factoidId', 'regexpReplacer'])
|
2005-04-17 21:43:09 +02:00
|
|
|
|
|
|
|
_sqlTrans = string.maketrans('*?', '%_')
|
|
|
|
def search(self, irc, msg, args, channel, optlist, globs):
|
|
|
|
"""[<channel>] [--values] [--{regexp} <value>] [<glob> ...]
|
|
|
|
|
|
|
|
Searches the keyspace for keys matching <glob>. If --regexp is given,
|
|
|
|
it associated value is taken as a regexp and matched against the keys.
|
|
|
|
If --values is given, search the value space instead of the keyspace.
|
|
|
|
"""
|
|
|
|
if not optlist and not globs:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
tables = ['keys']
|
|
|
|
formats = []
|
|
|
|
criteria = []
|
|
|
|
target = 'keys.key'
|
|
|
|
predicateName = 'p'
|
|
|
|
db = self.getDb(channel)
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'values':
|
|
|
|
target = 'factoids.fact'
|
|
|
|
if 'factoids' not in tables:
|
|
|
|
tables.append('factoids')
|
|
|
|
criteria.append('factoids.key_id=keys.id')
|
|
|
|
elif option == 'regexp':
|
|
|
|
criteria.append('%s(TARGET)' % predicateName)
|
|
|
|
def p(s, r=arg):
|
|
|
|
return int(bool(r.search(s)))
|
|
|
|
db.create_function(predicateName, 1, p)
|
|
|
|
predicateName += 'p'
|
|
|
|
for glob in globs:
|
2009-11-18 07:48:11 +01:00
|
|
|
criteria.append('TARGET LIKE ?')
|
2005-04-17 21:43:09 +02:00
|
|
|
formats.append(glob.translate(self._sqlTrans))
|
|
|
|
cursor = db.cursor()
|
|
|
|
sql = """SELECT keys.key FROM %s WHERE %s""" % \
|
|
|
|
(', '.join(tables), ' AND '.join(criteria))
|
2010-03-05 20:51:25 +01:00
|
|
|
sql = sql + " ORDER BY keys.key"
|
2005-04-17 21:43:09 +02:00
|
|
|
sql = sql.replace('TARGET', target)
|
|
|
|
cursor.execute(sql, formats)
|
2009-11-18 08:03:44 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.reply('No keys matched that query.')
|
2009-11-18 08:03:44 +01:00
|
|
|
elif len(results) == 1 and \
|
2005-04-17 21:43:09 +02:00
|
|
|
self.registryValue('showFactoidIfOnlyOneMatch', channel):
|
2009-11-18 18:04:52 +01:00
|
|
|
self.whatis(irc, msg, [results[0][0]])
|
2009-11-18 08:03:44 +01:00
|
|
|
elif len(results) > 100:
|
2005-04-17 21:43:09 +02:00
|
|
|
irc.reply('More than 100 keys matched that query; '
|
|
|
|
'please narrow your query.')
|
|
|
|
else:
|
2009-11-18 18:04:52 +01:00
|
|
|
keys = [repr(t[0]) for t in results]
|
2005-04-17 21:43:09 +02:00
|
|
|
s = format('%L', keys)
|
|
|
|
irc.reply(s)
|
|
|
|
search = wrap(search, ['channel',
|
|
|
|
getopts({'values': '', 'regexp': 'regexpMatcher'}),
|
|
|
|
any('glob')])
|
|
|
|
|
|
|
|
|
|
|
|
Class = Factoids
|
|
|
|
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|