2003-03-28 10:07:21 +01:00
|
|
|
###
|
2004-05-06 17:54:26 +02:00
|
|
|
# Copyright (c) 2004, Jeremiah Fincher
|
2003-03-28 10:07:21 +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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-05-06 17:54:26 +02:00
|
|
|
Infobot compatibility, for the parts that we don't support already.
|
2003-03-28 10:07:21 +01:00
|
|
|
"""
|
|
|
|
|
2004-09-03 15:55:03 +02:00
|
|
|
import supybot
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
#deprecated = True
|
2004-12-10 04:43:43 +01:00
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-03 15:55:03 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2003-11-25 09:23:47 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
import os
|
2003-04-01 10:36:13 +02:00
|
|
|
import re
|
2004-08-29 19:05:14 +02:00
|
|
|
import time
|
2004-05-07 13:34:56 +02:00
|
|
|
import random
|
2004-05-06 17:54:26 +02:00
|
|
|
import cPickle as pickle
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-08-17 22:01:23 +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
|
2004-08-29 19:05:14 +02:00
|
|
|
import supybot.world as world
|
2004-12-29 07:18:43 +01:00
|
|
|
from supybot.commands import *
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
2004-12-29 21:34:19 +01:00
|
|
|
import supybot.webutils as webutils
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.callbacks as callbacks
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
conf.registerPlugin('Infobot')
|
2004-12-29 07:18:43 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Infobot, 'personality',
|
2004-07-23 23:27:16 +02:00
|
|
|
registry.Boolean(True, """Determines whether the bot will respond with
|
2004-05-07 13:34:56 +02:00
|
|
|
personable (Infobot-like) responses rather than its standard messages."""))
|
2004-12-29 07:18:43 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Infobot, 'boringDunno',
|
2004-05-07 13:34:56 +02:00
|
|
|
registry.String('Dunno.', """Determines what boring dunno should be given
|
|
|
|
if supybot.plugins.Infobot.personality is False."""))
|
2004-12-29 07:18:43 +01:00
|
|
|
|
|
|
|
conf.registerGroup(conf.supybot.plugins.Infobot, 'unaddressed')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Infobot.unaddressed,
|
|
|
|
'snarfDefinitions', registry.Boolean(True, """Determines whether the bot
|
|
|
|
will snarf definitions given in the channel that weren't directly
|
|
|
|
addressed to it. Of course, no confirmation will be given if the bot
|
|
|
|
isn't directly addressed."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Infobot.unaddressed,
|
|
|
|
'answerQuestions', registry.Boolean(True, """Determines whether the bot
|
|
|
|
will answer questions that weren't directly addressed to it. Of course,
|
|
|
|
if it doesn't have an answer, it will remain silent."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Infobot.unaddressed,
|
|
|
|
'replyExistingFactoid', registry.Boolean(False, """Determines whether the
|
|
|
|
bot will announce that a factoid already exists when it sees a definition
|
|
|
|
for a pre-existing factoid."""))
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def configure(advanced):
|
|
|
|
# This will be called by setup.py to configure this module. Advanced is
|
|
|
|
# a bool that specifies whether the user identified himself as an advanced
|
|
|
|
# user or not. You should effect your configuration by manipulating the
|
|
|
|
# registry as appropriate.
|
2004-07-25 20:24:51 +02:00
|
|
|
from supybot.questions import expect, anything, something, yn
|
2004-05-06 17:54:26 +02:00
|
|
|
conf.registerPlugin('Infobot', True)
|
|
|
|
|
2004-08-17 07:16:28 +02:00
|
|
|
ends = ['!',
|
|
|
|
'.',
|
|
|
|
', $who.',]
|
|
|
|
dunnos = ['Dunno',
|
|
|
|
'No idea',
|
|
|
|
'I don\'t know',
|
|
|
|
'I have no idea',
|
2004-09-06 00:57:15 +02:00
|
|
|
'I don\'t have a clue',
|
|
|
|
'Bugger all, I dunno',
|
|
|
|
'Wish I knew',]
|
|
|
|
starts = ['It has been said that ',
|
|
|
|
'I guess ',
|
|
|
|
'',
|
|
|
|
'hmm... ',
|
|
|
|
'Rumor has it ',
|
|
|
|
'Somebody said ',]
|
2004-08-17 07:16:28 +02:00
|
|
|
confirms = ['10-4',
|
|
|
|
'Okay',
|
|
|
|
'Got it',
|
|
|
|
'Gotcha',
|
|
|
|
'I hear ya']
|
2004-09-30 22:00:41 +02:00
|
|
|
NORESPONSE = '<reply>'
|
|
|
|
initialIs = {'who': NORESPONSE,
|
|
|
|
'what': NORESPONSE,
|
|
|
|
'when': NORESPONSE,
|
|
|
|
'where': NORESPONSE,
|
|
|
|
'why': NORESPONSE,
|
|
|
|
'it': NORESPONSE,
|
|
|
|
'that': NORESPONSE,
|
|
|
|
'this': NORESPONSE,
|
2004-08-29 19:05:14 +02:00
|
|
|
}
|
2004-09-30 22:00:41 +02:00
|
|
|
initialAre = {'who': NORESPONSE,
|
|
|
|
'what': NORESPONSE,
|
|
|
|
'when': NORESPONSE,
|
|
|
|
'where': NORESPONSE,
|
|
|
|
'why': NORESPONSE,
|
|
|
|
'it': NORESPONSE,
|
|
|
|
'they': NORESPONSE,
|
|
|
|
'these': NORESPONSE,
|
|
|
|
'those': NORESPONSE,
|
2004-08-29 19:05:14 +02:00
|
|
|
'roses': 'red',
|
|
|
|
'violets': 'blue',
|
|
|
|
}
|
2004-08-17 07:16:28 +02:00
|
|
|
|
|
|
|
class PickleInfobotDB(object):
|
2004-09-11 22:24:52 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
2004-12-29 07:18:43 +01:00
|
|
|
self.dbs = ircutils.IrcDict()
|
|
|
|
self.changes = ircutils.IrcDict()
|
|
|
|
self.responses = ircutils.IrcDict()
|
|
|
|
|
|
|
|
def _getDb(self, channel):
|
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
|
|
|
if filename in self.dbs:
|
|
|
|
pass
|
|
|
|
elif os.path.exists(filename):
|
|
|
|
fd = file(filename)
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
(Is, Are) = pickle.load(fd)
|
|
|
|
self.dbs[filename] = (Is, Are)
|
|
|
|
self.changes[filename] = 0
|
|
|
|
self.responses[filename] = 0
|
2004-09-03 15:55:03 +02:00
|
|
|
except cPickle.UnpicklingError, e:
|
2004-12-29 07:18:43 +01:00
|
|
|
fd.close()
|
2004-09-03 15:55:03 +02:00
|
|
|
raise dbi.InvalidDBError, str(e)
|
2004-12-29 07:18:43 +01:00
|
|
|
fd.close()
|
|
|
|
else:
|
|
|
|
self.dbs[filename] = (utils.InsensitivePreservingDict(),
|
|
|
|
utils.InsensitivePreservingDict())
|
|
|
|
for (k, v) in initialIs.iteritems():
|
|
|
|
self.setIs(channel, k, v)
|
|
|
|
for (k, v) in initialAre.iteritems():
|
|
|
|
self.setAre(channel, k, v)
|
|
|
|
self.changes[filename] = 0
|
|
|
|
self.responses[filename] = 0
|
|
|
|
return (self.dbs[filename], filename)
|
|
|
|
|
|
|
|
def flush(self, db=None, filename=None):
|
|
|
|
if db is None and filename is None:
|
|
|
|
for (filename, db) in self.dbs.iteritems():
|
|
|
|
fd = utils.transactionalFile(filename, 'wb')
|
|
|
|
pickle.dump(db, fd)
|
|
|
|
fd.close()
|
|
|
|
else:
|
|
|
|
fd = utils.transactionalFile(filename, 'wb')
|
|
|
|
pickle.dump(db, fd)
|
|
|
|
fd.close()
|
|
|
|
self.dbs[filename] = db
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.flush()
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def incChanges(self):
|
|
|
|
filename = dynamic.filename
|
|
|
|
print '*** self.changes: %s' % self.changes
|
|
|
|
self.changes[filename] += 1
|
|
|
|
|
|
|
|
def incResponses(self):
|
|
|
|
self.responses[dynamic.filename] += 1
|
|
|
|
|
|
|
|
def changeIs(self, channel, factoid, replacer):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
old = Is[factoid]
|
2004-08-17 22:01:23 +02:00
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-06 04:42:20 +02:00
|
|
|
if replacer is not None:
|
2004-12-29 07:18:43 +01:00
|
|
|
Is[factoid] = replacer(old)
|
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
|
|
|
|
|
|
|
def getIs(self, channel, factoid):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
|
|
|
ret = Is[factoid]
|
|
|
|
self.incResponses()
|
2004-05-06 17:54:26 +02:00
|
|
|
return ret
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def setIs(self, channel, key, value):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
|
|
|
Is[key] = value
|
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def delIs(self, channel, factoid):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
Is.pop(factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-12-29 07:18:43 +01:00
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def hasIs(self, channel, factoid):
|
|
|
|
((Is, Are), _) = self._getDb(channel)
|
|
|
|
return factoid in Is
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def changeAre(self, channel, factoid, replacer):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
old = Are[factoid]
|
2004-08-17 22:01:23 +02:00
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-06 04:42:20 +02:00
|
|
|
if replacer is not None:
|
2004-12-29 07:18:43 +01:00
|
|
|
Are[factoid] = replacer(old)
|
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
|
|
|
|
|
|
|
def getAre(self, channel, factoid):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
|
|
|
ret = Are[factoid]
|
|
|
|
self.incResponses()
|
2004-05-06 17:54:26 +02:00
|
|
|
return ret
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def hasAre(self, channel, factoid):
|
|
|
|
((Is, Are), _) = self._getDb(channel)
|
|
|
|
return factoid in Are
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def setAre(self, channel, key, value):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
|
|
|
Are[key] = value
|
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def delAre(self, channel, factoid):
|
|
|
|
((Is, Are), filename) = self._getDb(channel)
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
Are.pop(factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-12-29 07:18:43 +01:00
|
|
|
self.flush((Is, Are), filename)
|
|
|
|
self.incChanges()
|
2004-05-07 13:34:56 +02:00
|
|
|
|
|
|
|
def getDunno(self):
|
2004-08-17 07:16:28 +02:00
|
|
|
return random.choice(dunnos) + random.choice(ends)
|
2004-05-07 13:34:56 +02:00
|
|
|
|
|
|
|
def getConfirm(self):
|
2004-08-17 07:16:28 +02:00
|
|
|
return random.choice(confirms) + random.choice(ends)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getChangeCount(self, channel):
|
|
|
|
(_, filename) = self._getDb(channel)
|
|
|
|
return self.changes[filename]
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getResponseCount(self, channel):
|
|
|
|
(_, filename) = self._getDb(channel)
|
|
|
|
return self.responses[filename]
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getNumFacts(self, channel):
|
|
|
|
((Is, Are), _) = self._getDb(channel)
|
|
|
|
return len(Are.keys()) + len(Is.keys())
|
2004-08-29 19:05:14 +02:00
|
|
|
|
2004-08-17 07:16:28 +02:00
|
|
|
class SqliteInfobotDB(object):
|
2004-09-11 22:24:52 +02:00
|
|
|
def __init__(self, filename):
|
2004-12-29 07:18:43 +01:00
|
|
|
self.filename = filename
|
|
|
|
self.dbs = ircutils.IrcDict()
|
|
|
|
self.changes = ircutils.IrcDict()
|
|
|
|
self.responses = ircutils.IrcDict()
|
|
|
|
|
|
|
|
def _getDb(self, channel):
|
2004-09-06 00:57:15 +02: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/>'
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
|
|
|
if filename not in self.changes:
|
|
|
|
self.changes[filename] = 0
|
|
|
|
if filename not in self.responses:
|
|
|
|
self.responses[filename] = 0
|
|
|
|
if filename in self.dbs:
|
|
|
|
return (self.dbs[filename], filename)
|
|
|
|
if os.path.exists(filename):
|
|
|
|
self.dbs[filename] = sqlite.connect(filename)
|
|
|
|
return (self.dbs[filename], filename)
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
self.dbs[filename] = db
|
|
|
|
cursor = db.cursor()
|
2004-08-17 22:01:23 +02:00
|
|
|
cursor.execute("""CREATE TABLE isFacts (
|
|
|
|
key TEXT UNIQUE ON CONFLICT REPLACE,
|
|
|
|
value TEXT
|
|
|
|
);""")
|
|
|
|
cursor.execute("""CREATE TABLE areFacts (
|
|
|
|
key TEXT UNIQUE ON CONFLICT REPLACE,
|
|
|
|
value TEXT
|
|
|
|
);""")
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
2004-08-29 19:05:14 +02:00
|
|
|
for (k, v) in initialIs.iteritems():
|
2004-12-29 07:18:43 +01:00
|
|
|
self.setIs(channel, k, v)
|
2004-08-29 19:05:14 +02:00
|
|
|
for (k, v) in initialAre.iteritems():
|
2004-12-29 07:18:43 +01:00
|
|
|
self.setAre(channel, k, v)
|
|
|
|
self.changes[filename] = 0
|
|
|
|
self.responses[filename] = 0
|
|
|
|
return (db, filename)
|
2004-08-17 22:01:23 +02:00
|
|
|
except sqlite.DatabaseError, e:
|
|
|
|
raise dbi.InvalidDBError, str(e)
|
2004-08-17 07:16:28 +02:00
|
|
|
|
|
|
|
def close(self):
|
2004-12-29 07:18:43 +01:00
|
|
|
for db in self.dbs.itervalues():
|
|
|
|
db.close()
|
|
|
|
self.dbs.clear()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def incChanges(self):
|
|
|
|
self.changes[dynamic.filename] += 1
|
|
|
|
|
|
|
|
def incResponses(self):
|
|
|
|
self.changes[dynamic.filename] += 1
|
|
|
|
|
|
|
|
def changeIs(self, channel, factoid, replacer):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-17 07:16:28 +02:00
|
|
|
old = cursor.fetchone()[0]
|
|
|
|
if replacer is not None:
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""UPDATE isFacts SET value=%s WHERE key LIKE %s""",
|
2004-08-17 07:16:28 +02:00
|
|
|
replacer(old), factoid)
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getIs(self, channel, factoid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT value FROM isFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 07:16:28 +02:00
|
|
|
ret = cursor.fetchone()[0]
|
2004-12-29 07:18:43 +01:00
|
|
|
self.incResponses()
|
2004-08-17 07:16:28 +02:00
|
|
|
return ret
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def setIs(self, channel, fact, oid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""INSERT INTO isFacts VALUES (%s, %s)""", fact, oid)
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def delIs(self, channel, factoid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""DELETE FROM isFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def hasIs(self, channel, factoid):
|
|
|
|
(db, _) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT * FROM isFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 07:16:28 +02:00
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def changeAre(self, channel, factoid, replacer):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-17 07:16:28 +02:00
|
|
|
old = cursor.fetchone()[0]
|
|
|
|
if replacer is not None:
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""UPDATE areFacts SET value=%s WHERE key LIKE %s""",
|
2004-08-17 07:16:28 +02:00
|
|
|
replacer(old), factoid)
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getAre(self, channel, factoid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT value FROM areFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 07:16:28 +02:00
|
|
|
ret = cursor.fetchone()[0]
|
2004-12-29 07:18:43 +01:00
|
|
|
self.incResponses()
|
2004-08-17 07:16:28 +02:00
|
|
|
return ret
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def setAre(self, channel, fact, oid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""INSERT INTO areFacts VALUES (%s, %s)""", fact, oid)
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def delAre(self, channel, factoid):
|
|
|
|
(db, filename) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""DELETE FROM areFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-12-29 07:18:43 +01:00
|
|
|
db.commit()
|
|
|
|
self.incChanges()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def hasAre(self, channel, factoid):
|
|
|
|
(db, _) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-12-29 21:34:19 +01:00
|
|
|
cursor.execute("""SELECT * FROM areFacts WHERE key LIKE %s""", factoid)
|
2004-08-17 07:16:28 +02:00
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
|
|
|
def getDunno(self):
|
|
|
|
return random.choice(dunnos) + random.choice(ends)
|
|
|
|
|
|
|
|
def getConfirm(self):
|
|
|
|
return random.choice(confirms) + random.choice(ends)
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getChangeCount(self, channel):
|
|
|
|
(_, filename) = self._getDb(channel)
|
|
|
|
try:
|
|
|
|
return self.changes[filename]
|
|
|
|
except KeyError:
|
|
|
|
return 0
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getResponseCount(self, channel):
|
|
|
|
(_, filename) = self._getDb(channel)
|
|
|
|
try:
|
|
|
|
return self.responses[filename]
|
|
|
|
except KeyError:
|
|
|
|
return 0
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def getNumFacts(self, channel):
|
|
|
|
(db, _) = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-08-29 19:05:14 +02:00
|
|
|
cursor.execute("""SELECT COUNT(*) FROM areFacts""")
|
|
|
|
areFacts = int(cursor.fetchone()[0])
|
|
|
|
cursor.execute("""SELECT COUNT(*) FROM isFacts""")
|
|
|
|
isFacts = int(cursor.fetchone()[0])
|
|
|
|
return areFacts + isFacts
|
|
|
|
|
2004-09-11 22:24:52 +02:00
|
|
|
|
|
|
|
InfobotDB = plugins.DB('Infobot',
|
|
|
|
{'sqlite': SqliteInfobotDB,
|
|
|
|
'pickle': PickleInfobotDB})
|
2004-08-17 07:16:28 +02:00
|
|
|
|
2004-07-31 08:46:19 +02:00
|
|
|
class Dunno(Exception):
|
|
|
|
pass
|
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
class Infobot(callbacks.PrivmsgCommandAndRegexp):
|
2004-12-29 07:18:43 +01:00
|
|
|
regexps = ['doForce', 'doForget', 'doFactoid', 'doUnknown']
|
|
|
|
addressedRegexps = ['doForce', 'doForget', 'doChange', 'doFactoid', 'doUnknown']
|
2004-05-06 17:54:26 +02:00
|
|
|
def __init__(self):
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent = super(Infobot, self)
|
|
|
|
self.__parent.__init__()
|
2004-12-29 07:18:43 +01:00
|
|
|
self.db = InfobotDB()
|
2004-05-06 17:54:26 +02:00
|
|
|
self.irc = None
|
|
|
|
self.msg = None
|
2004-08-10 08:14:31 +02:00
|
|
|
self.replied = True
|
2004-12-29 07:18:43 +01:00
|
|
|
self.changed = False
|
|
|
|
self.added = False
|
2003-08-14 14:11:45 +02:00
|
|
|
|
|
|
|
def die(self):
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent.die()
|
2003-08-14 14:11:45 +02:00
|
|
|
self.db.close()
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def reset(self):
|
|
|
|
self.db.close()
|
|
|
|
|
2004-08-17 22:01:23 +02:00
|
|
|
def _error(self, s):
|
2004-12-29 07:18:43 +01:00
|
|
|
if msg.addressed:
|
2004-08-17 22:01:23 +02:00
|
|
|
self.irc.error(s)
|
|
|
|
else:
|
|
|
|
self.log.warning(s)
|
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def reply(self, s, irc=None, msg=None, action=False, substitute=True):
|
2004-05-06 17:54:26 +02:00
|
|
|
if self.replied:
|
|
|
|
self.log.debug('Already replied, not replying again.')
|
2003-08-14 14:11:45 +02:00
|
|
|
return
|
2004-05-06 17:54:26 +02:00
|
|
|
if irc is None:
|
|
|
|
assert self.irc is not None
|
|
|
|
irc = self.irc
|
|
|
|
if msg is None:
|
|
|
|
assert self.msg is not None
|
|
|
|
msg = self.msg
|
|
|
|
self.replied = True
|
2004-12-29 07:18:43 +01:00
|
|
|
if substitute:
|
|
|
|
s = ircutils.standardSubstitute(irc, msg, s)
|
|
|
|
irc.reply(s, prefixName=False, action=action, msg=msg)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def confirm(self, irc=None, msg=None):
|
2004-05-07 13:34:56 +02:00
|
|
|
if self.registryValue('personality'):
|
2004-12-29 17:28:46 +01:00
|
|
|
s = self.db.getConfirm()
|
2004-05-07 13:34:56 +02:00
|
|
|
else:
|
2004-12-29 17:28:46 +01:00
|
|
|
s = conf.supybot.replies.success()
|
|
|
|
self.reply(s, irc=irc, msg=msg)
|
|
|
|
|
|
|
|
def missing(self, fact, irc=None, msg=None):
|
|
|
|
if msg is None:
|
|
|
|
assert self.msg is not None
|
|
|
|
msg = self.msg
|
|
|
|
self.reply('I didn\'t have anything matching %s, %s.' %
|
|
|
|
(utils.quoted(fact), msg.nick),
|
|
|
|
irc=irc, msg=msg)
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def dunno(self, irc=None, msg=None):
|
2004-05-07 13:34:56 +02:00
|
|
|
if self.registryValue('personality'):
|
2004-12-29 17:28:46 +01:00
|
|
|
s = self.db.getDunno()
|
2004-05-07 13:34:56 +02:00
|
|
|
else:
|
2004-12-29 17:28:46 +01:00
|
|
|
s = self.registryValue('boringDunno')
|
|
|
|
self.reply(s, irc=irc, msg=msg)
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def factoid(self, key, irc=None, msg=None, dunno=True, prepend='',
|
|
|
|
isAre=None):
|
2004-05-06 17:54:26 +02:00
|
|
|
if irc is None:
|
|
|
|
assert self.irc is not None
|
|
|
|
irc = self.irc
|
|
|
|
if msg is None:
|
|
|
|
assert self.msg is not None
|
|
|
|
msg = self.msg
|
2004-12-29 07:18:43 +01:00
|
|
|
if isAre is not None:
|
|
|
|
isAre = isAre.lower()
|
|
|
|
channel = dynamic.channel
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
if isAre is None:
|
|
|
|
if self.db.hasIs(channel, key):
|
|
|
|
isAre = 'is'
|
|
|
|
value = self.db.getIs(channel, key)
|
|
|
|
elif self.db.hasAre(channel, key):
|
|
|
|
isAre = 'are'
|
|
|
|
value = self.db.getAre(channel, key)
|
|
|
|
elif isAre == 'is':
|
|
|
|
if not self.db.hasIs(channel, key):
|
|
|
|
isAre = None
|
|
|
|
else:
|
|
|
|
value = self.db.getIs(channel, key)
|
|
|
|
elif isAre == 'are':
|
|
|
|
if not self.db.hasAre(channel, key):
|
|
|
|
isAre = None
|
|
|
|
else:
|
|
|
|
value = self.db.getAre(channel, key)
|
2004-12-31 05:18:02 +01:00
|
|
|
else:
|
|
|
|
self.log.debug('Returning early: Got a bad isAre value.')
|
|
|
|
return
|
2004-09-06 10:44:45 +02:00
|
|
|
except dbi.InvalidDBError, e:
|
2004-08-17 22:01:23 +02:00
|
|
|
self._error('Unable to access db: %s' % e)
|
|
|
|
return
|
2004-05-06 17:54:26 +02:00
|
|
|
if isAre is None:
|
2004-12-29 07:18:43 +01:00
|
|
|
if msg.addressed:
|
2004-07-31 08:46:19 +02:00
|
|
|
if dunno:
|
|
|
|
self.dunno(irc=irc, msg=msg)
|
|
|
|
else:
|
|
|
|
raise Dunno
|
2003-08-14 14:11:45 +02:00
|
|
|
else:
|
2004-07-31 01:49:44 +02:00
|
|
|
value = random.choice(value.split('|'))
|
2004-07-31 01:08:16 +02:00
|
|
|
if value.startswith('<reply>'):
|
2004-08-01 16:38:37 +02:00
|
|
|
value = value[7:].strip()
|
|
|
|
if value:
|
|
|
|
self.reply(value, irc=irc, msg=msg)
|
|
|
|
else:
|
|
|
|
self.log.debug('Not sending empty factoid.')
|
2004-07-31 01:08:16 +02:00
|
|
|
elif value.startswith('<action>'):
|
2004-07-31 08:46:19 +02:00
|
|
|
self.reply(value[8:].strip(),
|
|
|
|
irc=irc, msg=msg, action=True)
|
2004-07-31 01:08:16 +02:00
|
|
|
else:
|
2004-07-31 14:09:41 +02:00
|
|
|
s = '%s %s %s, $who' % (key, isAre, value)
|
|
|
|
s = prepend + s
|
|
|
|
self.reply(s, irc=irc, msg=msg)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-12-29 17:28:46 +01:00
|
|
|
_iAm = (re.compile(r'^i am ', re.I), '%s is ')
|
|
|
|
_my = (re.compile(r'^my ', re.I), '%s\'s ')
|
|
|
|
_your = (re.compile(r'^your ', re.I), '%s\'s ')
|
|
|
|
def normalize(self, s, bot, nick):
|
2004-05-06 17:54:26 +02:00
|
|
|
s = ircutils.stripFormatting(s)
|
|
|
|
s = s.strip() # After stripFormatting for formatted spaces.
|
|
|
|
s = utils.normalizeWhitespace(s)
|
2004-12-29 17:28:46 +01:00
|
|
|
s = self._iAm[0].sub(self._iAm[1] % nick, s)
|
|
|
|
s = self._my[0].sub(self._my[1] % nick, s)
|
|
|
|
s = self._your[0].sub(self._your[1] % bot, s)
|
2004-05-06 17:54:26 +02:00
|
|
|
contractions = [('what\'s', 'what is'), ('where\'s', 'where is'),
|
2004-08-17 22:01:23 +02:00
|
|
|
('who\'s', 'who is'), ('wtf\'s', 'wtf is'),]
|
2004-05-06 17:54:26 +02:00
|
|
|
for (contraction, replacement) in contractions:
|
|
|
|
if s.startswith(contraction):
|
|
|
|
s = replacement + s[len(contraction):]
|
|
|
|
return s
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-05-06 22:09:16 +02:00
|
|
|
_forceRe = re.compile(r'^no[,: -]+', re.I)
|
2004-12-29 07:18:43 +01:00
|
|
|
_karmaRe = re.compile(r'(?:\+\+|--)(?:\s+)?$')
|
2004-05-06 17:54:26 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2003-04-01 11:07:14 +02:00
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
if msg.repliedTo:
|
|
|
|
self.replied = True
|
|
|
|
if ircmsgs.isCtcp(msg):
|
|
|
|
self.log.debug('Returning early from doPrivmsg: isCtcp(msg).')
|
2004-08-27 18:05:33 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
if self._karmaRe.search(msg.args[1]):
|
|
|
|
self.log.debug('Returning early from doPrivmsg: karma.')
|
2004-08-28 23:12:44 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
# For later dynamic scoping
|
|
|
|
channel = plugins.getChannel(msg.args[0])
|
2004-12-29 17:28:46 +01:00
|
|
|
s = callbacks.addressed(irc.nick, msg)
|
|
|
|
payload = self.normalize(s or msg.args[1], irc.nick, msg.nick)
|
|
|
|
if s:
|
|
|
|
msg.tag('addressed', payload)
|
2004-09-16 16:37:08 +02:00
|
|
|
msg = ircmsgs.IrcMsg(args=(msg.args[0], payload), msg=msg)
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent.doPrivmsg(irc, msg)
|
2004-05-06 17:54:26 +02:00
|
|
|
finally:
|
|
|
|
self.replied = False
|
2004-12-29 07:18:43 +01:00
|
|
|
self.changed = False
|
|
|
|
self.added = False
|
2004-09-23 00:22:38 +02:00
|
|
|
|
2004-09-06 10:44:45 +02:00
|
|
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
2004-05-06 17:54:26 +02:00
|
|
|
try:
|
|
|
|
self.irc = irc
|
|
|
|
self.msg = msg
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent.callCommand(name, irc, msg, *L, **kwargs)
|
2004-05-06 17:54:26 +02:00
|
|
|
finally:
|
|
|
|
self.irc = None
|
|
|
|
self.msg = None
|
2003-04-01 10:36:13 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def doForget(self, irc, msg, match):
|
|
|
|
r"^forget\s+(.+?)[?!. ]*$"
|
|
|
|
fact = match.group(1)
|
2004-07-23 23:27:16 +02:00
|
|
|
deleted = False
|
2004-05-06 17:54:26 +02:00
|
|
|
for method in [self.db.delIs, self.db.delAre]:
|
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
method(dynamic.channel, fact)
|
2004-07-23 23:27:16 +02:00
|
|
|
deleted = True
|
2004-08-17 22:01:23 +02:00
|
|
|
except dbi.NoRecordError:
|
2004-05-06 17:54:26 +02:00
|
|
|
pass
|
2004-07-23 23:27:16 +02:00
|
|
|
if deleted:
|
|
|
|
self.confirm()
|
2004-12-29 17:28:46 +01:00
|
|
|
elif msg.addressed:
|
|
|
|
self.missing(fact, irc=irc, msg=msg)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def doForce(self, irc, msg, match):
|
|
|
|
r"^no,\s+(\w+,\s+)?(.+?)\s+(?<!\\)(was|is|am|were|are)\s+(.+?)[?!. ]*$"
|
|
|
|
(nick, key, isAre, value) = match.groups()
|
|
|
|
if not msg.addressed:
|
|
|
|
if nick is None:
|
|
|
|
self.log.debug('Not forcing because we weren\'t addressed and '
|
|
|
|
'payload wasn\'t of the form: no, irc.nick, ..')
|
|
|
|
return
|
|
|
|
nick = nick.rstrip(' \t,')
|
|
|
|
if not ircutils.nickEqual(nick, irc.nick):
|
|
|
|
self.log.debug('Not forcing because the regexp nick didn\'t '
|
|
|
|
'match our nick.')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if nick is not None:
|
|
|
|
stripped = nick.rstrip(' \t,')
|
|
|
|
if not ircutils.nickEqual(stripped, irc.nick):
|
|
|
|
key = nick + key
|
|
|
|
isAre = isAre.lower()
|
|
|
|
if self.added:
|
|
|
|
return
|
2004-12-29 17:28:46 +01:00
|
|
|
channel = dynamic.channel
|
2004-12-29 07:18:43 +01:00
|
|
|
if isAre in ('was', 'is', 'am'):
|
2004-12-29 17:28:46 +01:00
|
|
|
if self.db.hasIs(channel, key):
|
|
|
|
oldValue = self.db.getIs(channel, key)
|
|
|
|
if oldValue.lower() == value.lower():
|
|
|
|
self.reply('I already had it that way, %s.' % msg.nick)
|
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
self.log.debug('Forcing %s to %s.',
|
|
|
|
utils.quoted(key), utils.quoted(value))
|
|
|
|
self.added = True
|
2004-12-29 17:28:46 +01:00
|
|
|
self.db.setIs(channel, key, value)
|
2004-12-29 07:18:43 +01:00
|
|
|
else:
|
2004-12-29 17:28:46 +01:00
|
|
|
if self.db.hasAre(channel, key):
|
|
|
|
oldValue = self.db.getAre(channel, key)
|
|
|
|
if oldValue.lower() == value.lower():
|
|
|
|
self.reply('I already had it that way, %s.' % msg.nick)
|
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
self.log.debug('Forcing %s to %s.',
|
|
|
|
utils.quoted(key), utils.quoted(value))
|
|
|
|
self.added = True
|
2004-12-29 17:28:46 +01:00
|
|
|
self.db.setAre(channel, key, value)
|
2004-12-29 07:18:43 +01:00
|
|
|
self.confirm()
|
|
|
|
|
2004-08-06 04:42:20 +02:00
|
|
|
def doChange(self, irc, msg, match):
|
|
|
|
r"^(.+)\s+=~\s+(.+)$"
|
|
|
|
(fact, regexp) = match.groups()
|
|
|
|
changed = False
|
|
|
|
try:
|
|
|
|
r = utils.perlReToReplacer(regexp)
|
|
|
|
except ValueError, e:
|
2004-12-29 07:18:43 +01:00
|
|
|
if msg.addressed:
|
|
|
|
irc.errorInvalid('regexp', regexp)
|
2004-08-06 04:42:20 +02:00
|
|
|
else:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Invalid regexp: %s' % regexp)
|
2004-08-06 04:42:20 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
if self.changed:
|
|
|
|
return
|
2004-08-06 04:42:20 +02:00
|
|
|
for method in [self.db.changeIs, self.db.changeAre]:
|
|
|
|
try:
|
2004-12-29 07:18:43 +01:00
|
|
|
method(dynamic.channel, fact, r)
|
|
|
|
self.changed = True
|
2004-08-17 22:01:23 +02:00
|
|
|
except dbi.NoRecordError:
|
2004-08-06 04:42:20 +02:00
|
|
|
pass
|
|
|
|
if changed:
|
|
|
|
self.confirm()
|
|
|
|
else:
|
2004-12-29 17:28:46 +01:00
|
|
|
self.missing(fact, irc=irc, msg=msg)
|
2004-08-06 04:42:20 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def doUnknown(self, irc, msg, match):
|
2004-12-29 07:18:43 +01:00
|
|
|
r"^(.+?)\s*(\?[?!. ]*)?$"
|
|
|
|
(key, question) = match.groups()
|
|
|
|
if not msg.addressed:
|
|
|
|
if question is None:
|
|
|
|
self.log.debug('Not answering question since we weren\'t '
|
|
|
|
'addressed and there was no question mark.')
|
|
|
|
return
|
|
|
|
if self.registryValue('unaddressed.answerQuestions'):
|
|
|
|
self.factoid(key, prepend=random.choice(starts))
|
|
|
|
else:
|
2004-09-06 00:57:15 +02:00
|
|
|
self.factoid(key, prepend=random.choice(starts))
|
2004-08-01 16:38:37 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def doFactoid(self, irc, msg, match):
|
2004-08-25 18:42:54 +02:00
|
|
|
r"^(.+?)\s+(?<!\\)(was|is|am|were|are)\s+(also\s+)?(.+?)[?!. ]*$"
|
2004-07-23 23:27:16 +02:00
|
|
|
(key, isAre, also, value) = match.groups()
|
2004-07-31 01:08:16 +02:00
|
|
|
key = key.replace('\\', '')
|
2004-12-31 05:18:02 +01:00
|
|
|
isAre = isAre.lower()
|
|
|
|
if isAre in ('was', 'is', 'am'):
|
|
|
|
isAre = 'is'
|
|
|
|
else:
|
|
|
|
isAre = 'are'
|
2004-08-17 22:01:23 +02:00
|
|
|
if key.lower() in ('where', 'what', 'who', 'wtf'):
|
2004-05-06 17:54:26 +02:00
|
|
|
# It's a question.
|
2004-12-29 07:18:43 +01:00
|
|
|
if msg.addressed or \
|
|
|
|
self.registryValue('unaddressed.answerQuestions'):
|
|
|
|
self.factoid(value, isAre=isAre, prepend=random.choice(starts))
|
2004-05-07 13:34:56 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
if not msg.addressed and \
|
|
|
|
not self.registryValue('unaddressed.snarfDefinitions'):
|
2004-05-06 17:54:26 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
if self.added:
|
|
|
|
return
|
2004-12-31 05:18:02 +01:00
|
|
|
if isAre == 'is':
|
2004-12-29 07:18:43 +01:00
|
|
|
if self.db.hasIs(dynamic.channel, key):
|
2004-12-29 17:28:46 +01:00
|
|
|
oldValue = self.db.getIs(dynamic.channel, key)
|
|
|
|
if oldValue.lower() == value.lower():
|
|
|
|
self.reply('I already had it that way, %s.' % msg.nick)
|
|
|
|
return
|
2004-07-23 23:27:16 +02:00
|
|
|
if also:
|
2004-10-24 00:07:50 +02:00
|
|
|
self.log.debug('Adding %s to %s.',
|
|
|
|
utils.quoted(key), utils.quoted(value))
|
2004-12-29 17:28:46 +01:00
|
|
|
value = '%s or %s' % (oldValue, value)
|
2004-12-29 07:18:43 +01:00
|
|
|
elif msg.addressed:
|
|
|
|
if initialIs.get(key) != value:
|
2004-12-29 17:28:46 +01:00
|
|
|
self.reply('... but %s is %s ...' %
|
|
|
|
(key, oldValue), substitute=False)
|
2004-12-29 07:18:43 +01:00
|
|
|
return
|
2004-08-01 16:38:37 +02:00
|
|
|
else:
|
2004-10-24 00:07:50 +02:00
|
|
|
self.log.debug('Already have a %s key.',
|
|
|
|
utils.quoted(key))
|
2004-08-01 16:38:37 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
self.added = True
|
|
|
|
self.db.setIs(dynamic.channel, key, value)
|
2004-05-06 17:54:26 +02:00
|
|
|
else:
|
2004-12-29 07:18:43 +01:00
|
|
|
if self.db.hasAre(dynamic.channel, key):
|
2004-12-29 17:28:46 +01:00
|
|
|
oldValue = self.db.getAre(dynamic.channel, key)
|
|
|
|
if oldValue.lower() == value.lower():
|
|
|
|
self.reply('I already had it that way, %s.' % msg.nick)
|
|
|
|
return
|
2004-07-23 23:27:16 +02:00
|
|
|
if also:
|
2004-10-24 00:07:50 +02:00
|
|
|
self.log.debug('Adding %s to %s.',
|
|
|
|
utils.quoted(key), utils.quoted(value))
|
2004-12-29 17:28:46 +01:00
|
|
|
value = '%s or %s' % (oldValue, value)
|
2004-12-29 07:18:43 +01:00
|
|
|
elif msg.addressed:
|
|
|
|
if initialAre.get(key) != value:
|
2004-12-29 17:28:46 +01:00
|
|
|
self.reply('... but %s are %s ...' %
|
|
|
|
(key, oldValue), substitute=False)
|
2004-12-29 07:18:43 +01:00
|
|
|
return
|
2004-08-01 16:38:37 +02:00
|
|
|
else:
|
2004-10-24 00:07:50 +02:00
|
|
|
self.log.debug('Already have a %s key.',
|
|
|
|
utils.quoted(key))
|
2004-08-01 16:38:37 +02:00
|
|
|
return
|
2004-12-29 07:18:43 +01:00
|
|
|
self.added = True
|
|
|
|
self.db.setAre(dynamic.channel, key, value)
|
|
|
|
if msg.addressed:
|
2004-05-06 17:54:26 +02:00
|
|
|
self.confirm()
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def stats(self, irc, msg, args, channel):
|
2004-12-29 21:34:19 +01:00
|
|
|
"""[<channel>]
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
Returns the number of changes and requests made to the Infobot database
|
2004-12-29 21:34:19 +01:00
|
|
|
since the plugin was loaded. <channel> is only necessary if the
|
|
|
|
message isn't in the channel itself.
|
2004-05-06 17:54:26 +02:00
|
|
|
"""
|
2004-12-29 07:18:43 +01:00
|
|
|
changes = self.db.getChangeCount(channel)
|
|
|
|
responses = self.db.getResponseCount(channel)
|
2004-08-29 19:05:14 +02:00
|
|
|
now = time.time()
|
|
|
|
diff = int(now - world.startedAt)
|
|
|
|
mode = {True: 'optional', False: 'require'}
|
2004-12-29 07:18:43 +01:00
|
|
|
answer = self.registryValue('unaddressed.answerQuestions')
|
2004-08-29 19:05:14 +02:00
|
|
|
irc.reply('Since %s, there %s been %s and %s. I have been awake for %s'
|
|
|
|
' this session, and currently reference %s. Addressing is in'
|
|
|
|
' %s mode.' % (time.ctime(world.startedAt),
|
|
|
|
utils.has(changes),
|
|
|
|
utils.nItems('modification', changes),
|
|
|
|
utils.nItems('question', responses),
|
|
|
|
utils.timeElapsed(int(now - world.startedAt)),
|
2004-12-29 07:18:43 +01:00
|
|
|
utils.nItems('factoid',
|
|
|
|
self.db.getNumFacts(channel)),
|
2004-08-29 19:05:14 +02:00
|
|
|
mode[answer]))
|
2004-12-29 07:18:43 +01:00
|
|
|
stats = wrap(stats, ['channeldb'])
|
2004-08-29 19:05:14 +02:00
|
|
|
status=stats
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-12-29 07:18:43 +01:00
|
|
|
def tell(self, irc, msg, args, channel, nick, _, factoid):
|
2004-12-29 21:34:19 +01:00
|
|
|
"""[<channel>] <nick> [about] <factoid>
|
2004-07-31 08:46:19 +02:00
|
|
|
|
2004-12-29 21:34:19 +01:00
|
|
|
Tells <nick> about <factoid>. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself.
|
2004-07-31 08:46:19 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
hostmask = irc.state.nickToHostmask(nick)
|
|
|
|
except KeyError:
|
2004-09-09 23:18:57 +02:00
|
|
|
irc.error('I haven\'t seen %s, I\'ll let you '
|
2004-12-29 07:18:43 +01:00
|
|
|
'do the telling.' % nick, Raise=True)
|
2004-07-31 08:46:19 +02:00
|
|
|
newmsg = ircmsgs.privmsg(irc.nick, factoid+'?', prefix=hostmask)
|
|
|
|
try:
|
2004-07-31 14:09:41 +02:00
|
|
|
prepend = '%s wants you to know that ' % msg.nick
|
|
|
|
self.factoid(factoid, msg=newmsg, prepend=prepend)
|
2004-07-31 08:46:19 +02:00
|
|
|
except Dunno:
|
|
|
|
self.dunno()
|
2004-12-29 07:18:43 +01:00
|
|
|
tell = wrap(tell, ['channeldb', 'something',
|
|
|
|
optional(('literal', 'about')), 'text'])
|
2004-07-31 08:46:19 +02:00
|
|
|
|
2004-12-29 21:34:19 +01:00
|
|
|
def update(self, irc, msg, args, channel, isAre, url):
|
|
|
|
"""[<channel>] {is,are} <url|file>
|
|
|
|
|
|
|
|
Updates the Infobot database using the dumped database at remote <url>
|
|
|
|
or local <file>. The first argument should be "is" or "are", and
|
|
|
|
determines whether the is or are database is updated.
|
|
|
|
"""
|
|
|
|
isAre = isAre.lower()
|
|
|
|
if isAre == 'is':
|
|
|
|
add = self.db.setIs
|
|
|
|
elif isAre == 'are':
|
|
|
|
add = self.db.setAre
|
|
|
|
count = 0
|
|
|
|
try:
|
|
|
|
fd = webutils.getUrlFd(url)
|
|
|
|
except webutils.WebError:
|
|
|
|
try:
|
|
|
|
fd = file(url)
|
|
|
|
except EnvironmentError:
|
|
|
|
irc.errorInvalid('url or file')
|
|
|
|
for line in fd:
|
|
|
|
line = line.rstrip('\r\n')
|
|
|
|
try:
|
|
|
|
(key, value) = line.split(' => ', 1)
|
|
|
|
except ValueError: #unpack list of wrong size
|
|
|
|
self.log.debug('Invalid line: %r', line)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
key = key.rstrip()
|
|
|
|
value = value.lstrip()
|
|
|
|
self.log.debug('Adding factoid %r with value %r.', key, value)
|
|
|
|
add(channel, key, value)
|
|
|
|
count += 1
|
|
|
|
fd.close()
|
|
|
|
irc.replySuccess('%s added.' % utils.nItems('factoid', count))
|
|
|
|
update = wrap(update,
|
|
|
|
['owner', 'channeldb', ('literal', ('is', 'are')),
|
|
|
|
first('url', 'text')])
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
Class = Infobot
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-28 10:07:21 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|