2005-04-18 18:13:53 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2003-2005, Daniel DiPaolo
|
|
|
|
# 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
|
2014-08-05 11:18:38 +02:00
|
|
|
import sys
|
2005-04-18 18:13:53 +02:00
|
|
|
import time
|
|
|
|
import string
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.utils as utils
|
2012-08-04 14:00:23 +02:00
|
|
|
import supybot.shlex as shlex
|
2005-04-18 18:13:53 +02:00
|
|
|
from supybot.commands import *
|
2015-08-11 16:50:23 +02:00
|
|
|
import supybot.utils.minisix as minisix
|
2005-04-18 18:13:53 +02:00
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-17 16:59:21 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('MoobotFactoids')
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
class OptionList(object):
|
2012-08-04 14:00:23 +02:00
|
|
|
separators = '|()'
|
2005-04-18 18:13:53 +02:00
|
|
|
def _insideParens(self, lexer):
|
|
|
|
ret = []
|
|
|
|
while True:
|
|
|
|
token = lexer.get_token()
|
|
|
|
if not token:
|
|
|
|
return '(%s' % ''.join(ret) #)
|
|
|
|
elif token == ')':
|
2005-10-24 05:06:29 +02:00
|
|
|
if '|' in ret:
|
2014-01-21 10:57:38 +01:00
|
|
|
L = list(map(''.join,
|
2005-10-24 05:06:29 +02:00
|
|
|
utils.iter.split('|'.__eq__, ret,
|
2014-01-21 10:57:38 +01:00
|
|
|
yieldEmpty=True)))
|
2005-10-24 05:06:29 +02:00
|
|
|
return utils.iter.choice(L)
|
2005-04-18 18:13:53 +02:00
|
|
|
else:
|
2005-10-24 05:06:29 +02:00
|
|
|
return '(%s)' % ''.join(ret)
|
2005-04-18 18:13:53 +02:00
|
|
|
elif token == '(':
|
|
|
|
ret.append(self._insideParens(lexer))
|
|
|
|
elif token == '|':
|
|
|
|
ret.append(token)
|
|
|
|
else:
|
|
|
|
ret.append(token)
|
|
|
|
|
|
|
|
def tokenize(self, s):
|
2015-08-10 17:55:25 +02:00
|
|
|
lexer = shlex.shlex(minisix.io.StringIO(s))
|
2005-04-18 18:13:53 +02:00
|
|
|
lexer.commenters = ''
|
|
|
|
lexer.quotes = ''
|
|
|
|
lexer.whitespace = ''
|
2012-08-04 14:00:23 +02:00
|
|
|
lexer.separators += self.separators
|
2005-04-18 18:13:53 +02:00
|
|
|
ret = []
|
|
|
|
while True:
|
|
|
|
token = lexer.get_token()
|
|
|
|
if not token:
|
|
|
|
break
|
|
|
|
elif token == '(':
|
|
|
|
ret.append(self._insideParens(lexer))
|
|
|
|
else:
|
|
|
|
ret.append(token)
|
|
|
|
return ''.join(ret)
|
|
|
|
|
|
|
|
def pickOptions(s):
|
|
|
|
return OptionList().tokenize(s)
|
|
|
|
|
|
|
|
class SqliteMoobotDB(object):
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
self.dbs = ircutils.IrcDict()
|
|
|
|
|
|
|
|
def close(self):
|
2015-08-08 22:20:14 +02:00
|
|
|
for db in self.dbs.values():
|
2005-04-18 18:13:53 +02:00
|
|
|
db.close()
|
|
|
|
self.dbs.clear()
|
|
|
|
|
|
|
|
def _getDb(self, channel):
|
2013-07-13 17:28:21 +02:00
|
|
|
import sqlite3
|
2010-03-21 07:25:11 +01:00
|
|
|
|
2005-04-18 18:13:53 +02:00
|
|
|
if channel in self.dbs:
|
|
|
|
return self.dbs[channel]
|
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
2010-03-21 07:25:11 +01:00
|
|
|
|
2005-04-18 18:13:53 +02:00
|
|
|
if os.path.exists(filename):
|
2011-07-02 11:50:31 +02:00
|
|
|
db = sqlite3.connect(filename, check_same_thread=False)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2014-08-05 11:18:38 +02:00
|
|
|
db.text_factory = str
|
2010-03-21 07:25:11 +01:00
|
|
|
self.dbs[channel] = db
|
|
|
|
return db
|
2011-07-02 11:50:31 +02:00
|
|
|
db = sqlite3.connect(filename, check_same_thread=False)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2014-08-05 11:18:38 +02:00
|
|
|
db.text_factory = str
|
2005-04-18 18:13:53 +02:00
|
|
|
self.dbs[channel] = db
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE factoids (
|
|
|
|
key TEXT PRIMARY KEY,
|
|
|
|
created_by INTEGER,
|
|
|
|
created_at TIMESTAMP,
|
|
|
|
modified_by INTEGER,
|
|
|
|
modified_at TIMESTAMP,
|
|
|
|
locked_at TIMESTAMP,
|
|
|
|
locked_by INTEGER,
|
|
|
|
last_requested_by TEXT,
|
|
|
|
last_requested_at TIMESTAMP,
|
|
|
|
fact TEXT,
|
|
|
|
requested_count INTEGER
|
|
|
|
)""")
|
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
|
|
|
def getFactoid(self, channel, key):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT fact FROM factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
WHERE key LIKE ?""", (key,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-18 18:13:53 +02:00
|
|
|
return None
|
|
|
|
else:
|
2010-03-21 07:25:11 +01:00
|
|
|
return results[0]
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def getFactinfo(self, channel, key):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT created_by, created_at,
|
|
|
|
modified_by, modified_at,
|
|
|
|
last_requested_by, last_requested_at,
|
|
|
|
requested_count, locked_by, locked_at
|
|
|
|
FROM factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
WHERE key LIKE ?""", (key,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-18 18:13:53 +02:00
|
|
|
return None
|
|
|
|
else:
|
2010-03-21 07:25:11 +01:00
|
|
|
return results[0]
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def randomFactoid(self, channel):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT fact, key FROM factoids
|
|
|
|
ORDER BY random() LIMIT 1""")
|
2010-03-21 07:25:11 +01:00
|
|
|
results = cursor.fetchall()
|
|
|
|
if len(results) == 0:
|
2005-04-18 18:13:53 +02:00
|
|
|
return None
|
|
|
|
else:
|
2010-03-21 07:25:11 +01:00
|
|
|
return results[0]
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def addFactoid(self, channel, key, value, creator_id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO factoids VALUES
|
2010-03-21 07:25:11 +01:00
|
|
|
(?, ?, ?, NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, ?, 0)""",
|
|
|
|
(key, creator_id, int(time.time()), value))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def updateFactoid(self, channel, key, newvalue, modifier_id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
SET fact=?, modified_by=?,
|
|
|
|
modified_at=? WHERE key LIKE ?""",
|
|
|
|
(newvalue, modifier_id, int(time.time()), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def updateRequest(self, channel, key, hostmask):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE factoids SET
|
2010-03-21 07:25:11 +01:00
|
|
|
last_requested_by = ?,
|
|
|
|
last_requested_at = ?,
|
2005-04-18 18:13:53 +02:00
|
|
|
requested_count = requested_count + 1
|
2010-03-21 07:25:11 +01:00
|
|
|
WHERE key = ?""",
|
|
|
|
(hostmask, int(time.time()), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def removeFactoid(self, channel, key):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2010-03-21 07:25:11 +01:00
|
|
|
cursor.execute("""DELETE FROM factoids WHERE key LIKE ?""",
|
|
|
|
(key,))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def locked(self, channel, key):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute ("""SELECT locked_by FROM factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
WHERE key LIKE ?""", (key,))
|
2005-04-18 18:13:53 +02:00
|
|
|
if cursor.fetchone()[0] is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def lock(self, channel, key, locker_id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
SET locked_by=?, locked_at=?
|
|
|
|
WHERE key LIKE ?""",
|
|
|
|
(locker_id, int(time.time()), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def unlock(self, channel, key):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
SET locked_by=?, locked_at=?
|
|
|
|
WHERE key LIKE ?""", (None, None, key))
|
2005-04-18 18:13:53 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def mostAuthored(self, channel, limit):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT created_by, count(key) FROM factoids
|
|
|
|
GROUP BY created_by
|
2010-03-21 07:25:11 +01:00
|
|
|
ORDER BY count(key) DESC LIMIT ?""", (limit,))
|
2005-04-18 18:13:53 +02:00
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
def mostRecent(self, channel, limit):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT key FROM factoids
|
2010-03-21 07:25:11 +01:00
|
|
|
ORDER BY created_at DESC LIMIT ?""", (limit,))
|
2005-04-18 18:13:53 +02:00
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
def mostPopular(self, channel, limit):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT key, requested_count FROM factoids
|
|
|
|
WHERE requested_count > 0
|
2010-03-21 07:25:11 +01:00
|
|
|
ORDER BY requested_count DESC LIMIT ?""", (limit,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
return results
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def getKeysByAuthor(self, channel, authorId):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2010-03-21 07:25:11 +01:00
|
|
|
cursor.execute("""SELECT key FROM factoids WHERE created_by=?
|
|
|
|
ORDER BY key""", (authorId,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
return results
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def getKeysByGlob(self, channel, glob):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
glob = '%%%s%%' % glob
|
2010-03-21 07:25:11 +01:00
|
|
|
cursor.execute("""SELECT key FROM factoids WHERE key LIKE ?
|
|
|
|
ORDER BY key""", (glob,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
return results
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def getKeysByValueGlob(self, channel, glob):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
glob = '%%%s%%' % glob
|
2010-03-21 07:25:11 +01:00
|
|
|
cursor.execute("""SELECT key FROM factoids WHERE fact LIKE ?
|
|
|
|
ORDER BY key""", (glob,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
return results
|
2005-04-18 18:13:53 +02:00
|
|
|
|
2011-07-24 19:39:36 +02:00
|
|
|
MoobotDB = plugins.DB('MoobotFactoids', {'sqlite3': SqliteMoobotDB})
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
class MoobotFactoids(callbacks.Plugin):
|
2014-11-30 21:07:41 +01:00
|
|
|
"""An alternative to the Factoids plugin, this plugin keeps factoids in
|
|
|
|
your bot."""
|
2005-04-18 18:13:53 +02:00
|
|
|
callBefore = ['Dunno']
|
|
|
|
def __init__(self, irc):
|
|
|
|
self.db = MoobotDB()
|
|
|
|
self.__parent = super(MoobotFactoids, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self.__parent.die()
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
_replyTag = '<reply>'
|
|
|
|
_actionTag = '<action>'
|
|
|
|
def _parseFactoid(self, irc, msg, fact):
|
|
|
|
type = 'define' # Default is to just spit the factoid back as a
|
|
|
|
# definition of what the key is (i.e., "foo is bar")
|
|
|
|
newfact = pickOptions(fact)
|
|
|
|
if newfact.startswith(self._replyTag):
|
|
|
|
newfact = newfact[len(self._replyTag):]
|
|
|
|
type = 'reply'
|
|
|
|
elif newfact.startswith(self._actionTag):
|
|
|
|
newfact = newfact[len(self._actionTag):]
|
|
|
|
type = 'action'
|
|
|
|
newfact = newfact.strip()
|
|
|
|
newfact = ircutils.standardSubstitute(irc, msg, newfact)
|
|
|
|
return (type, newfact)
|
|
|
|
|
|
|
|
def invalidCommand(self, irc, msg, tokens):
|
|
|
|
if '=~' in tokens:
|
|
|
|
self.changeFactoid(irc, msg, tokens)
|
|
|
|
elif tokens and tokens[0] in ('no', 'no,'):
|
|
|
|
self.replaceFactoid(irc, msg, tokens)
|
|
|
|
elif ['is', 'also'] in utils.seq.window(tokens, 2):
|
|
|
|
self.augmentFactoid(irc, msg, tokens)
|
|
|
|
else:
|
|
|
|
key = ' '.join(tokens)
|
|
|
|
key = self._sanitizeKey(key)
|
|
|
|
channel = plugins.getChannel(msg.args[0])
|
|
|
|
fact = self.db.getFactoid(channel, key)
|
|
|
|
if fact:
|
|
|
|
self.db.updateRequest(channel, key, msg.prefix)
|
|
|
|
# getFactoid returns "all results", so we need to extract the
|
|
|
|
# first one.
|
|
|
|
fact = fact[0]
|
|
|
|
# Update the requested count/requested by for this key
|
|
|
|
hostmask = msg.prefix
|
|
|
|
# Now actually get the factoid and respond accordingly
|
|
|
|
(type, text) = self._parseFactoid(irc, msg, fact)
|
|
|
|
if type == 'action':
|
|
|
|
irc.reply(text, action=True)
|
|
|
|
elif type == 'reply':
|
2005-06-01 23:08:30 +02:00
|
|
|
irc.reply(text, prefixNick=False)
|
2005-04-18 18:13:53 +02:00
|
|
|
elif type == 'define':
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.reply(format(_('%s is %s'), key, text),
|
|
|
|
prefixNick=False)
|
2005-04-18 18:13:53 +02:00
|
|
|
else:
|
|
|
|
assert False, 'Spurious type from _parseFactoid'
|
|
|
|
else:
|
|
|
|
if 'is' in tokens or '_is_' in tokens:
|
|
|
|
self.addFactoid(irc, msg, tokens)
|
|
|
|
|
|
|
|
def _getUserId(self, irc, prefix):
|
|
|
|
try:
|
|
|
|
return ircdb.users.getUserId(prefix)
|
|
|
|
except KeyError:
|
|
|
|
irc.errorNotRegistered(Raise=True)
|
|
|
|
|
|
|
|
def _sanitizeKey(self, key):
|
|
|
|
return key.rstrip('!? ')
|
|
|
|
|
|
|
|
def _checkNotLocked(self, irc, channel, key):
|
|
|
|
if self.db.locked(channel, key):
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Factoid %q is locked.'), key), Raise=True)
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def _getFactoid(self, irc, channel, key):
|
|
|
|
fact = self.db.getFactoid(channel, key)
|
|
|
|
if fact is not None:
|
|
|
|
return fact
|
|
|
|
else:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Factoid %q not found.'), key), Raise=True)
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def _getKeyAndFactoid(self, tokens):
|
|
|
|
if '_is_' in tokens:
|
|
|
|
p = '_is_'.__eq__
|
|
|
|
elif 'is' in tokens:
|
|
|
|
p = 'is'.__eq__
|
|
|
|
else:
|
|
|
|
self.log.debug('Invalid tokens for {add,replace}Factoid: %s.',
|
|
|
|
tokens)
|
2010-10-17 16:59:21 +02:00
|
|
|
s = _('Missing an \'is\' or \'_is_\'.')
|
2014-01-20 15:43:55 +01:00
|
|
|
raise ValueError(s)
|
2014-01-21 10:57:38 +01:00
|
|
|
(key, newfact) = list(map(' '.join, utils.iter.split(p, tokens, maxsplit=1)))
|
2005-04-18 18:13:53 +02:00
|
|
|
key = self._sanitizeKey(key)
|
|
|
|
return (key, newfact)
|
|
|
|
|
|
|
|
def addFactoid(self, irc, msg, tokens):
|
|
|
|
# First, check and see if the entire message matches a factoid key
|
|
|
|
channel = plugins.getChannel(msg.args[0])
|
|
|
|
id = self._getUserId(irc, msg.prefix)
|
|
|
|
try:
|
|
|
|
(key, fact) = self._getKeyAndFactoid(tokens)
|
2014-01-20 15:49:15 +01:00
|
|
|
except ValueError as e:
|
2005-04-18 18:13:53 +02:00
|
|
|
irc.error(str(e), Raise=True)
|
|
|
|
# Check and make sure it's not in the DB already
|
|
|
|
if self.db.getFactoid(channel, key):
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Factoid %q already exists.'), key), Raise=True)
|
2005-04-18 18:13:53 +02:00
|
|
|
self.db.addFactoid(channel, key, fact, id)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
|
|
|
def changeFactoid(self, irc, msg, tokens):
|
|
|
|
id = self._getUserId(irc, msg.prefix)
|
2014-01-21 10:57:38 +01:00
|
|
|
(key, regexp) = list(map(' '.join,
|
|
|
|
utils.iter.split('=~'.__eq__, tokens, maxsplit=1)))
|
2005-04-18 18:13:53 +02:00
|
|
|
channel = plugins.getChannel(msg.args[0])
|
|
|
|
# Check and make sure it's in the DB
|
|
|
|
fact = self._getFactoid(irc, channel, key)
|
|
|
|
self._checkNotLocked(irc, channel, key)
|
|
|
|
# It's fair game if we get to here
|
|
|
|
try:
|
|
|
|
r = utils.str.perlReToReplacer(regexp)
|
2014-01-20 15:49:15 +01:00
|
|
|
except ValueError as e:
|
2005-04-18 18:13:53 +02:00
|
|
|
irc.errorInvalid('regexp', regexp, Raise=True)
|
|
|
|
fact = fact[0]
|
|
|
|
new_fact = r(fact)
|
|
|
|
self.db.updateFactoid(channel, key, new_fact, id)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
|
|
|
def augmentFactoid(self, irc, msg, tokens):
|
|
|
|
# Must be registered!
|
|
|
|
id = self._getUserId(irc, msg.prefix)
|
|
|
|
pairs = list(utils.seq.window(tokens, 2))
|
|
|
|
isAlso = pairs.index(['is', 'also'])
|
|
|
|
key = ' '.join(tokens[:isAlso])
|
|
|
|
new_text = ' '.join(tokens[isAlso+2:])
|
|
|
|
channel = plugins.getChannel(msg.args[0])
|
|
|
|
fact = self._getFactoid(irc, channel, key)
|
|
|
|
self._checkNotLocked(irc, channel, key)
|
|
|
|
# It's fair game if we get to here
|
|
|
|
fact = fact[0]
|
2010-10-17 16:59:21 +02:00
|
|
|
new_fact = format(_('%s, or %s'), fact, new_text)
|
2005-04-18 18:13:53 +02:00
|
|
|
self.db.updateFactoid(channel, key, new_fact, id)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
|
|
|
def replaceFactoid(self, irc, msg, tokens):
|
|
|
|
# Must be registered!
|
|
|
|
channel = plugins.getChannel(msg.args[0])
|
|
|
|
id = self._getUserId(irc, msg.prefix)
|
|
|
|
del tokens[0] # remove the "no,"
|
|
|
|
try:
|
|
|
|
(key, fact) = self._getKeyAndFactoid(tokens)
|
2014-01-20 15:49:15 +01:00
|
|
|
except ValueError as e:
|
2005-04-18 18:13:53 +02:00
|
|
|
irc.error(str(e), Raise=True)
|
|
|
|
_ = self._getFactoid(irc, channel, key)
|
|
|
|
self._checkNotLocked(irc, channel, key)
|
|
|
|
self.db.removeFactoid(channel, key)
|
|
|
|
self.db.addFactoid(channel, key, fact, id)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def literal(self, irc, msg, args, channel, key):
|
|
|
|
"""[<channel>] <factoid key>
|
|
|
|
|
|
|
|
Returns the literal factoid for the given factoid key. No parsing of
|
|
|
|
the factoid value is done as it is with normal retrieval. <channel>
|
|
|
|
is only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
fact = self._getFactoid(irc, channel, key)
|
|
|
|
fact = fact[0]
|
|
|
|
irc.reply(fact)
|
|
|
|
literal = wrap(literal, ['channeldb', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def factinfo(self, irc, msg, args, channel, key):
|
|
|
|
"""[<channel>] <factoid key>
|
|
|
|
|
|
|
|
Returns the various bits of info on the factoid for the given key.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
|
|
|
# Start building the response string
|
|
|
|
s = key + ': '
|
|
|
|
# Next, get all the info and build the response piece by piece
|
|
|
|
info = self.db.getFactinfo(channel, key)
|
|
|
|
if not info:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('No such factoid: %q'), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
(created_by, created_at, modified_by, modified_at, last_requested_by,
|
|
|
|
last_requested_at, requested_count, locked_by, locked_at) = info
|
|
|
|
# First, creation info.
|
|
|
|
# Map the integer created_by to the username
|
|
|
|
created_by = plugins.getUserName(created_by)
|
|
|
|
created_at = time.strftime(conf.supybot.reply.format.time(),
|
|
|
|
time.localtime(int(created_at)))
|
2010-10-17 16:59:21 +02:00
|
|
|
s += format(_('Created by %s on %s.'), created_by, created_at)
|
2005-04-18 18:13:53 +02:00
|
|
|
# Next, modification info, if any.
|
|
|
|
if modified_by is not None:
|
|
|
|
modified_by = plugins.getUserName(modified_by)
|
|
|
|
modified_at = time.strftime(conf.supybot.reply.format.time(),
|
|
|
|
time.localtime(int(modified_at)))
|
2010-10-17 16:59:21 +02:00
|
|
|
s += format(_(' Last modified by %s on %s.'), modified_by,
|
|
|
|
modified_at)
|
2005-04-18 18:13:53 +02:00
|
|
|
# Next, last requested info, if any
|
|
|
|
if last_requested_by is not None:
|
|
|
|
last_by = last_requested_by # not an int user id
|
|
|
|
last_at = time.strftime(conf.supybot.reply.format.time(),
|
|
|
|
time.localtime(int(last_requested_at)))
|
|
|
|
req_count = requested_count
|
2010-10-17 16:59:21 +02:00
|
|
|
s += format(_(' Last requested by %s on %s, requested %n.'),
|
2005-04-18 18:13:53 +02:00
|
|
|
last_by, last_at, (requested_count, 'time'))
|
|
|
|
# Last, locked info
|
|
|
|
if locked_at is not None:
|
|
|
|
lock_at = time.strftime(conf.supybot.reply.format.time(),
|
|
|
|
time.localtime(int(locked_at)))
|
|
|
|
lock_by = plugins.getUserName(locked_by)
|
2010-10-17 16:59:21 +02:00
|
|
|
s += format(_(' Locked by %s on %s.'), lock_by, lock_at)
|
2005-04-18 18:13:53 +02:00
|
|
|
irc.reply(s)
|
|
|
|
factinfo = wrap(factinfo, ['channeldb', 'text'])
|
|
|
|
|
|
|
|
def _lock(self, irc, msg, channel, user, key, locking=True):
|
|
|
|
#self.log.debug('in _lock')
|
2005-05-18 06:18:35 +02:00
|
|
|
#self.log.debug('id: %s', id)
|
2005-04-18 18:13:53 +02:00
|
|
|
id = user.id
|
|
|
|
info = self.db.getFactinfo(channel, key)
|
|
|
|
if not info:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('No such factoid: %q'), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
2013-08-24 11:28:29 +02:00
|
|
|
(created_by, a, a, a, a, a, a, locked_by, a) = info
|
2005-04-18 18:13:53 +02:00
|
|
|
# Don't perform redundant operations
|
|
|
|
if locking and locked_by is not None:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Factoid %q is already locked.'), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
if not locking and locked_by is None:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Factoid %q is not locked.'), key))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
# Can only lock/unlock own factoids unless you're an admin
|
2005-05-18 06:18:35 +02:00
|
|
|
#self.log.debug('admin?: %s', ircdb.checkCapability(id, 'admin'))
|
|
|
|
#self.log.debug('created_by: %s', created_by)
|
2005-04-18 18:13:53 +02:00
|
|
|
if not (ircdb.checkCapability(id, 'admin') or created_by == id):
|
|
|
|
if locking:
|
|
|
|
s = 'lock'
|
|
|
|
else:
|
|
|
|
s = 'unlock'
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(format(_('Cannot %s someone else\'s factoid unless you '
|
|
|
|
'are an admin.'), s))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
# Okay, we're done, ready to lock/unlock
|
|
|
|
if locking:
|
|
|
|
self.db.lock(channel, key, id)
|
|
|
|
else:
|
|
|
|
self.db.unlock(channel, key)
|
|
|
|
irc.replySuccess()
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def lock(self, irc, msg, args, channel, user, key):
|
|
|
|
"""[<channel>] <factoid key>
|
|
|
|
|
|
|
|
Locks the factoid with the given factoid key. Requires that the user
|
|
|
|
be registered and have created the factoid originally. <channel> is
|
|
|
|
only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
self._lock(irc, msg, channel, user, key, True)
|
|
|
|
lock = wrap(lock, ['channeldb', 'user', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def unlock(self, irc, msg, args, channel, user, key):
|
|
|
|
"""[<channel>] <factoid key>
|
|
|
|
|
|
|
|
Unlocks the factoid with the given factoid key. Requires that the
|
|
|
|
user be registered and have locked the factoid. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
self._lock(irc, msg, channel, user, key, False)
|
|
|
|
unlock = wrap(unlock, ['channeldb', 'user', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def most(self, irc, msg, args, channel, method):
|
|
|
|
"""[<channel>] {popular|authored|recent}
|
|
|
|
|
|
|
|
Lists the most {popular|authored|recent} factoids. "popular" lists the
|
|
|
|
most frequently requested factoids. "authored" lists the author with
|
|
|
|
the most factoids. "recent" lists the most recently created factoids.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
|
|
|
method = method.capitalize()
|
|
|
|
method = getattr(self, '_most%s' % method, None)
|
|
|
|
if method is None:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
limit = self.registryValue('mostCount', channel)
|
|
|
|
method(irc, channel, limit)
|
|
|
|
most = wrap(most, ['channeldb',
|
|
|
|
('literal', ('popular', 'authored', 'recent'))])
|
|
|
|
|
|
|
|
def _mostAuthored(self, irc, channel, limit):
|
|
|
|
results = self.db.mostAuthored(channel, limit)
|
|
|
|
L = ['%s (%s)' % (plugins.getUserName(t[0]), int(t[1]))
|
|
|
|
for t in results]
|
|
|
|
if L:
|
2010-10-29 12:03:08 +02:00
|
|
|
author = _('author')
|
2005-04-18 18:13:53 +02:00
|
|
|
if len(L) != 1:
|
2010-10-29 12:03:08 +02:00
|
|
|
author = _('authors')
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.reply(format(_('Most prolific %s: %L'), author, L))
|
2005-04-18 18:13:53 +02:00
|
|
|
else:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(_('There are no factoids in my database.'))
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def _mostRecent(self, irc, channel, limit):
|
|
|
|
results = self.db.mostRecent(channel, limit)
|
|
|
|
L = [format('%q', t[0]) for t in results]
|
|
|
|
if L:
|
2010-10-17 16:59:21 +02:00
|
|
|
if len(L) < 2:
|
|
|
|
latest = _('latest factoid')
|
|
|
|
else:
|
|
|
|
latest = _('latest factoids')
|
2013-01-05 19:13:37 +01:00
|
|
|
irc.reply(format(_('%i %s: %L'), len(L), latest, L))
|
2005-04-18 18:13:53 +02:00
|
|
|
else:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(_('There are no factoids in my database.'))
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
def _mostPopular(self, irc, channel, limit):
|
|
|
|
results = self.db.mostPopular(channel, limit)
|
|
|
|
L = [format('%q (%s)', t[0], t[1]) for t in results]
|
|
|
|
if L:
|
2010-10-17 16:59:21 +02:00
|
|
|
if len(L) < 2:
|
|
|
|
requested = _('requested factoid')
|
|
|
|
else:
|
|
|
|
requested = _('requested factoids')
|
2013-01-05 19:13:37 +01:00
|
|
|
irc.reply(format(_('Top %i %s: %L'), len(L), requested, L))
|
2005-04-18 18:13:53 +02:00
|
|
|
else:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(_('No factoids have been requested from my database.'))
|
2005-04-18 18:13:53 +02:00
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def listauth(self, irc, msg, args, channel, author):
|
|
|
|
"""[<channel>] <author name>
|
|
|
|
|
|
|
|
Lists the keys of the factoids with the given author. Note that if an
|
|
|
|
author has an integer name, you'll have to use that author's id to use
|
|
|
|
this function (so don't use integer usernames!). <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
id = ircdb.users.getUserId(author)
|
|
|
|
except KeyError:
|
|
|
|
irc.errorNoUser(name=author, Raise=True)
|
|
|
|
results = self.db.getKeysByAuthor(channel, id)
|
|
|
|
if not results:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.reply(format(_('No factoids by %q found.'), author))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
keys = [format('%q', t[0]) for t in results]
|
2010-10-17 16:59:21 +02:00
|
|
|
s = format(_('Author search for %q (%i found): %L'),
|
2005-04-18 18:13:53 +02:00
|
|
|
author, len(keys), keys)
|
|
|
|
irc.reply(s)
|
|
|
|
listauth = wrap(listauth, ['channeldb', 'something'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def listkeys(self, irc, msg, args, channel, search):
|
|
|
|
"""[<channel>] <text>
|
|
|
|
|
|
|
|
Lists the keys of the factoids whose key contains the provided text.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
|
|
|
results = self.db.getKeysByGlob(channel, search)
|
|
|
|
if not results:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.reply(format(_('No keys matching %q found.'), search))
|
2005-04-18 18:13:53 +02:00
|
|
|
elif len(results) == 1 and \
|
|
|
|
self.registryValue('showFactoidIfOnlyOneMatch', channel):
|
|
|
|
key = results[0][0]
|
|
|
|
self.invalidCommand(irc, msg, [key])
|
|
|
|
else:
|
|
|
|
keys = [format('%q', tup[0]) for tup in results]
|
2010-10-17 16:59:21 +02:00
|
|
|
s = format(_('Key search for %q (%i found): %L'),
|
2005-04-18 18:13:53 +02:00
|
|
|
search, len(keys), keys)
|
|
|
|
irc.reply(s)
|
|
|
|
listkeys = wrap(listkeys, ['channeldb', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def listvalues(self, irc, msg, args, channel, search):
|
|
|
|
"""[<channel>] <text>
|
|
|
|
|
|
|
|
Lists the keys of the factoids whose value contains the provided text.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
|
|
|
results = self.db.getKeysByValueGlob(channel, search)
|
|
|
|
if not results:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.reply(format(_('No values matching %q found.'), search))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
keys = [format('%q', tup[0]) for tup in results]
|
2010-10-17 16:59:21 +02:00
|
|
|
s = format(_('Value search for %q (%i found): %L'),
|
2005-04-18 18:13:53 +02:00
|
|
|
search, len(keys), keys)
|
|
|
|
irc.reply(s)
|
|
|
|
listvalues = wrap(listvalues, ['channeldb', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def remove(self, irc, msg, args, channel, _, key):
|
|
|
|
"""[<channel>] <factoid key>
|
|
|
|
|
|
|
|
Deletes the factoid with the given key. <channel> is only necessary
|
|
|
|
if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
_ = self._getFactoid(irc, channel, key)
|
|
|
|
self._checkNotLocked(irc, channel, key)
|
|
|
|
self.db.removeFactoid(channel, key)
|
|
|
|
irc.replySuccess()
|
|
|
|
remove = wrap(remove, ['channeldb', 'user', 'text'])
|
|
|
|
|
2010-10-17 16:59:21 +02:00
|
|
|
@internationalizeDocstring
|
2005-04-18 18:13:53 +02:00
|
|
|
def random(self, irc, msg, args, channel):
|
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Displays a random factoid (along with its key) from the database.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
|
|
|
results = self.db.randomFactoid(channel)
|
|
|
|
if not results:
|
2010-10-17 16:59:21 +02:00
|
|
|
irc.error(_('No factoids in the database.'))
|
2005-04-18 18:13:53 +02:00
|
|
|
return
|
|
|
|
(fact, key) = results
|
|
|
|
irc.reply(format('Random factoid: %q is %q', key, fact))
|
|
|
|
random = wrap(random, ['channeldb'])
|
2010-10-26 09:32:12 +02:00
|
|
|
MoobotFactoids = internationalizeDocstring(MoobotFactoids)
|
2005-04-18 18:13:53 +02:00
|
|
|
|
|
|
|
Class = MoobotFactoids
|
|
|
|
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=8 expandtab textwidth=78:
|