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
|
|
|
|
|
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-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
|
|
|
|
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-05-07 13:34:56 +02:00
|
|
|
conf.registerGlobalValue(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."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Infobot, 'boringDunno',
|
|
|
|
registry.String('Dunno.', """Determines what boring dunno should be given
|
|
|
|
if supybot.plugins.Infobot.personality is False."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Infobot,
|
|
|
|
'snarfUnaddressedDefinitions', 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.registerGlobalValue(conf.supybot.plugins.Infobot,
|
|
|
|
'answerUnaddressedQuestions', 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."""))
|
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-08-29 19:05:14 +02:00
|
|
|
initialIs = {'who': '<reply>',
|
|
|
|
'what': '<reply>',
|
|
|
|
'when': '<reply>',
|
|
|
|
'where': '<reply>',
|
|
|
|
'why': '<reply>',
|
|
|
|
'it': '<reply>',
|
|
|
|
}
|
|
|
|
initialAre = {'who': '<reply>',
|
|
|
|
'what': '<reply>',
|
|
|
|
'when': '<reply>',
|
|
|
|
'where': '<reply>',
|
|
|
|
'why': '<reply>',
|
|
|
|
'it': '<reply>',
|
|
|
|
'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):
|
2004-08-29 19:05:14 +02:00
|
|
|
self._changes = 0
|
|
|
|
self._responses = 0
|
2004-09-11 22:24:52 +02:00
|
|
|
self.filename = filename
|
2004-05-06 17:54:26 +02:00
|
|
|
try:
|
2004-09-11 22:24:52 +02:00
|
|
|
fd = file(self.filename)
|
2004-05-06 17:54:26 +02:00
|
|
|
except EnvironmentError:
|
|
|
|
self._is = utils.InsensitivePreservingDict()
|
|
|
|
self._are = utils.InsensitivePreservingDict()
|
2004-08-29 19:05:14 +02:00
|
|
|
for (k, v) in initialIs.iteritems():
|
|
|
|
self.setIs(k, v)
|
|
|
|
for (k, v) in initialAre.iteritems():
|
|
|
|
self.setAre(k, v)
|
|
|
|
self._changes = 0
|
2004-05-06 17:54:26 +02:00
|
|
|
else:
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-09-03 15:55:03 +02:00
|
|
|
(self._is, self._are) = pickle.load(fd)
|
|
|
|
except cPickle.UnpicklingError, e:
|
|
|
|
raise dbi.InvalidDBError, str(e)
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def flush(self):
|
2004-09-11 22:24:52 +02:00
|
|
|
fd = utils.transactionalFile(self.filename, 'wb')
|
2004-05-06 17:54:26 +02:00
|
|
|
pickle.dump((self._is, self._are), fd)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.flush()
|
|
|
|
|
2004-08-06 04:42:20 +02:00
|
|
|
def changeIs(self, factoid, replacer):
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
|
|
|
old = self._is[factoid]
|
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-06 04:42:20 +02:00
|
|
|
if replacer is not None:
|
|
|
|
self._is[factoid] = replacer(old)
|
|
|
|
self.flush()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
2004-08-06 04:42:20 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def getIs(self, factoid):
|
|
|
|
ret = self._is[factoid]
|
|
|
|
self._responses += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def setIs(self, fact, oid):
|
|
|
|
self._is[fact] = oid
|
|
|
|
self.flush()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def delIs(self, factoid):
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
|
|
|
del self._is[factoid]
|
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-05-06 17:54:26 +02:00
|
|
|
self.flush()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def hasIs(self, factoid):
|
|
|
|
return factoid in self._is
|
|
|
|
|
2004-08-06 04:42:20 +02:00
|
|
|
def changeAre(self, factoid, replacer):
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
|
|
|
old = self._are[factoid]
|
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-08-06 04:42:20 +02:00
|
|
|
if replacer is not None:
|
|
|
|
self._are[factoid] = replacer(old)
|
|
|
|
self._changes += 1
|
|
|
|
self.flush()
|
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def getAre(self, factoid):
|
|
|
|
ret = self._are[factoid]
|
|
|
|
self._responses += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def hasAre(self, factoid):
|
|
|
|
return factoid in self._are
|
|
|
|
|
|
|
|
def setAre(self, fact, oid):
|
|
|
|
self._are[fact] = oid
|
|
|
|
self.flush()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
2004-05-06 17:54:26 +02:00
|
|
|
|
|
|
|
def delAre(self, factoid):
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
|
|
|
del self._are[factoid]
|
|
|
|
except KeyError:
|
|
|
|
raise dbi.NoRecordError
|
2004-05-06 17:54:26 +02:00
|
|
|
self.flush()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
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-05-06 17:54:26 +02:00
|
|
|
def getChangeCount(self):
|
|
|
|
return self._changes
|
|
|
|
|
|
|
|
def getResponseCount(self):
|
|
|
|
return self._responses
|
|
|
|
|
2004-08-29 19:05:14 +02:00
|
|
|
def getNumFacts(self):
|
|
|
|
return len(self._are.keys()) + len(self._is.keys())
|
|
|
|
|
2004-08-17 07:16:28 +02:00
|
|
|
class SqliteInfobotDB(object):
|
2004-09-11 22:24:52 +02:00
|
|
|
def __init__(self, filename):
|
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 07:16:28 +02:00
|
|
|
self._changes = 0
|
|
|
|
self._responses = 0
|
2004-09-11 22:24:52 +02:00
|
|
|
self.filename = filename
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
2004-09-11 22:24:52 +02:00
|
|
|
if os.path.exists(self.filename):
|
|
|
|
self.db = sqlite.connect(self.filename)
|
2004-09-03 15:55:03 +02:00
|
|
|
return self.db
|
2004-08-17 22:01:23 +02:00
|
|
|
#else:
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db = sqlite.connect(self.filename)
|
2004-09-03 15:55:03 +02:00
|
|
|
cursor = self.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-09-03 15:55:03 +02:00
|
|
|
self.db.commit()
|
2004-08-29 19:05:14 +02:00
|
|
|
for (k, v) in initialIs.iteritems():
|
|
|
|
self.setIs(k, v)
|
|
|
|
for (k, v) in initialAre.iteritems():
|
|
|
|
self.setAre(k, v)
|
|
|
|
self._changes = 0
|
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-09-11 22:24:52 +02:00
|
|
|
self.db.close()
|
2004-08-17 07:16:28 +02:00
|
|
|
|
|
|
|
def changeIs(self, factoid, replacer):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT value FROM isFacts WHERE key=%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:
|
|
|
|
cursor.execute("""UPDATE isFacts SET value=%s WHERE key=%s""",
|
|
|
|
replacer(old), factoid)
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def getIs(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT value FROM isFacts WHERE key=%s""", factoid)
|
|
|
|
ret = cursor.fetchone()[0]
|
|
|
|
self._responses += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def setIs(self, fact, oid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""INSERT INTO isFacts VALUES (%s, %s)""", fact, oid)
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def delIs(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""DELETE FROM isFacts WHERE key=%s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def hasIs(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT * FROM isFacts WHERE key=%s""", factoid)
|
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
|
|
|
def changeAre(self, factoid, replacer):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT value FROM areFacts WHERE key=%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:
|
|
|
|
cursor.execute("""UPDATE areFacts SET value=%s WHERE key=%s""",
|
|
|
|
replacer(old), factoid)
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def getAre(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT value FROM areFacts WHERE key=%s""", factoid)
|
|
|
|
ret = cursor.fetchone()[0]
|
|
|
|
self._responses += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def setAre(self, fact, oid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""INSERT INTO areFacts VALUES (%s, %s)""", fact, oid)
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def delAre(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""DELETE FROM areFacts WHERE key=%s""", factoid)
|
2004-08-17 22:01:23 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
2004-09-11 22:24:52 +02:00
|
|
|
self.db.commit()
|
2004-08-17 07:16:28 +02:00
|
|
|
self._changes += 1
|
|
|
|
|
|
|
|
def hasAre(self, factoid):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.db.cursor()
|
2004-08-17 07:16:28 +02:00
|
|
|
cursor.execute("""SELECT * FROM areFacts WHERE key=%s""", factoid)
|
|
|
|
return cursor.rowcount == 1
|
|
|
|
|
|
|
|
def getDunno(self):
|
|
|
|
return random.choice(dunnos) + random.choice(ends)
|
|
|
|
|
|
|
|
def getConfirm(self):
|
|
|
|
return random.choice(confirms) + random.choice(ends)
|
|
|
|
|
|
|
|
def getChangeCount(self):
|
|
|
|
return self._changes
|
|
|
|
|
|
|
|
def getResponseCount(self):
|
|
|
|
return self._responses
|
|
|
|
|
2004-08-29 19:05:14 +02:00
|
|
|
def getNumFacts(self):
|
2004-09-11 22:24:52 +02:00
|
|
|
cursor = self.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-08-06 04:42:20 +02:00
|
|
|
regexps = ['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-05-07 13:34:56 +02:00
|
|
|
try:
|
|
|
|
self.db = InfobotDB()
|
|
|
|
except Exception:
|
2004-09-11 22:24:52 +02:00
|
|
|
self.log.exception('Error loading %s:', self.filename)
|
2004-05-07 13:34:56 +02:00
|
|
|
raise # So it doesn't get loaded without its database.
|
2004-05-06 17:54:26 +02:00
|
|
|
self.irc = None
|
|
|
|
self.msg = None
|
|
|
|
self.force = False
|
2004-08-10 08:14:31 +02:00
|
|
|
self.replied = True
|
2004-08-01 16:38:37 +02:00
|
|
|
self.badForce = False
|
2004-05-06 17:54:26 +02:00
|
|
|
self.addressed = False
|
2004-09-23 00:22:38 +02:00
|
|
|
self.calledDoPrivmsg = 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-08-17 22:01:23 +02:00
|
|
|
def _error(self, s):
|
|
|
|
if self.addressed:
|
|
|
|
self.irc.error(s)
|
|
|
|
else:
|
|
|
|
self.log.warning(s)
|
|
|
|
|
2004-07-31 01:08:16 +02:00
|
|
|
def reply(self, s, irc=None, msg=None, action=False):
|
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-07-31 08:46:19 +02:00
|
|
|
irc.reply(plugins.standardSubstitute(irc, msg, 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'):
|
|
|
|
self.reply(self.db.getConfirm(), irc=irc, msg=msg)
|
|
|
|
else:
|
|
|
|
assert self.irc is not None
|
2004-08-30 02:56:00 +02:00
|
|
|
self.reply(conf.supybot.replies.success())
|
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'):
|
|
|
|
self.reply(self.db.getDunno(), irc=irc, msg=msg)
|
|
|
|
else:
|
|
|
|
self.reply(self.registryValue('boringDunno'), irc=irc, msg=msg)
|
2004-05-06 17:54:26 +02:00
|
|
|
|
2004-07-31 14:09:41 +02:00
|
|
|
def factoid(self, key, irc=None, msg=None, dunno=True, prepend=''):
|
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
|
|
|
|
isAre = None
|
2004-08-17 22:01:23 +02:00
|
|
|
try:
|
|
|
|
if self.db.hasIs(key):
|
|
|
|
isAre = 'is'
|
|
|
|
value = self.db.getIs(key)
|
|
|
|
elif self.db.hasAre(key):
|
|
|
|
isAre = 'are'
|
|
|
|
value = self.db.getAre(key)
|
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:
|
|
|
|
if self.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-05-06 17:54:26 +02:00
|
|
|
def normalize(self, s):
|
|
|
|
s = ircutils.stripFormatting(s)
|
|
|
|
s = s.strip() # After stripFormatting for formatted spaces.
|
|
|
|
s = utils.normalizeWhitespace(s)
|
|
|
|
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-09-07 22:28:24 +02:00
|
|
|
_karmaRe = re.compile(r'^(?:\S+|\(.+\))(?:\+\+|--)(?:\s+)?$')
|
2004-05-06 17:54:26 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2004-09-14 17:09:01 +02:00
|
|
|
if msg.repliedTo:
|
2004-09-23 00:22:38 +02:00
|
|
|
self.log.debug('Returning early from doPrivmsg: msg.repliedTo.')
|
|
|
|
return
|
|
|
|
if ircmsgs.isCtcp(msg):
|
|
|
|
self.log.debug('Returning early from doPrivmsg: isCtcp(msg).')
|
|
|
|
return
|
|
|
|
if self.calledDoPrivmsg:
|
|
|
|
self.log.debug('Returning early from doPrivmsg: calledDoPrivmsg.')
|
|
|
|
self.calledDoPrivmsg = False
|
2004-09-14 17:09:01 +02:00
|
|
|
return
|
2003-04-01 11:07:14 +02:00
|
|
|
try:
|
2004-07-31 06:52:50 +02:00
|
|
|
maybeAddressed = callbacks.addressed(irc.nick, msg,
|
|
|
|
whenAddressedByNick=True)
|
|
|
|
if maybeAddressed:
|
|
|
|
self.addressed = True
|
|
|
|
payload = maybeAddressed
|
|
|
|
else:
|
|
|
|
payload = msg.args[1]
|
2004-08-27 18:05:33 +02:00
|
|
|
if self._karmaRe.search(payload):
|
|
|
|
self.log.debug('Not snarfing a karma adjustment.')
|
|
|
|
return
|
2004-07-31 06:52:50 +02:00
|
|
|
payload = self.normalize(payload)
|
|
|
|
maybeForced = self._forceRe.sub('', payload)
|
|
|
|
if maybeForced != payload:
|
|
|
|
self.force = True
|
2004-08-01 16:38:37 +02:00
|
|
|
# Infobot requires that forces have the form "no, botname, ..."
|
|
|
|
# We think that's stupid to require the bot name if the bot is
|
|
|
|
# being directly addressed. The following makes sure both
|
|
|
|
# "botname: no, botname, ..." and "botname: no, ..." work the
|
|
|
|
# same and non-addressed forms require the bots nick.
|
|
|
|
nick = irc.nick.lower()
|
|
|
|
if not self.addressed:
|
|
|
|
if not maybeForced.lower().startswith(nick):
|
|
|
|
self.badForce = True
|
|
|
|
self.force = False
|
|
|
|
if maybeForced.lower().startswith(nick):
|
|
|
|
maybeForced = maybeForced[len(nick):].lstrip(', ')
|
2004-07-31 06:52:50 +02:00
|
|
|
payload = maybeForced
|
|
|
|
# Let's make sure we dump out of Infobot if the privmsg is an
|
|
|
|
# actual command otherwise we could get multiple responses.
|
2004-09-23 00:22:38 +02:00
|
|
|
if self.addressed and '=~' not in payload:
|
|
|
|
payload += '?'
|
2004-07-31 06:52:50 +02:00
|
|
|
if payload.endswith(irc.nick):
|
|
|
|
self.addressed = True
|
|
|
|
payload = payload[:-len(irc.nick)]
|
2004-09-01 06:55:08 +02:00
|
|
|
payload = payload.strip(', ') # Strip punctuation before nick.
|
2004-07-31 06:52:50 +02:00
|
|
|
payload += '?' # So doUnknown gets called.
|
2004-08-28 23:12:44 +02:00
|
|
|
if not payload.strip():
|
|
|
|
self.log.debug('Bailing since we received an empty msg.')
|
|
|
|
return
|
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.force = False
|
|
|
|
self.replied = False
|
2004-08-01 16:38:37 +02:00
|
|
|
self.badForce = False
|
2004-05-06 17:54:26 +02:00
|
|
|
self.addressed = False
|
2003-04-01 10:36:13 +02:00
|
|
|
|
2004-09-23 00:22:38 +02:00
|
|
|
def tokenizedCommand(self, irc, msg, tokens):
|
|
|
|
self.doPrivmsg(irc, msg)
|
|
|
|
self.calledDoPrivmsg = True
|
|
|
|
|
2004-09-06 10:44:45 +02:00
|
|
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
|
|
|
#print '***', name, utils.stackTrace()
|
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:
|
|
|
|
method(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()
|
|
|
|
else:
|
|
|
|
# XXX: Should this be genericified?
|
2004-08-30 02:56:00 +02:00
|
|
|
self.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
|
2003-08-20 18:26:23 +02:00
|
|
|
|
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:
|
|
|
|
if self.addressed:
|
|
|
|
irc.error('Invalid regexp: %s' % regexp)
|
|
|
|
return
|
|
|
|
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
|
|
|
|
for method in [self.db.changeIs, self.db.changeAre]:
|
|
|
|
try:
|
|
|
|
method(fact, r)
|
|
|
|
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:
|
|
|
|
# XXX: Should this be genericified?
|
2004-08-30 02:56:00 +02:00
|
|
|
self.reply('I\'ve never heard of %s, %s!' % (fact, msg.nick))
|
2004-08-06 04:42:20 +02:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def doUnknown(self, irc, msg, match):
|
2004-08-17 22:01:23 +02:00
|
|
|
r"^(.+?)\s*\?[?!. ]*$"
|
2004-05-06 17:54:26 +02:00
|
|
|
key = match.group(1)
|
2004-05-07 13:34:56 +02:00
|
|
|
if self.addressed or self.registryValue('answerUnaddressedQuestions'):
|
2004-09-06 00:57:15 +02:00
|
|
|
# Does the dunno'ing for us itself.
|
|
|
|
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-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-05-07 13:34:56 +02:00
|
|
|
if self.addressed or \
|
|
|
|
self.registryValue('answerUnaddressedQuestions'):
|
2004-09-06 00:57:15 +02:00
|
|
|
self.factoid(value, prepend=random.choice(starts))
|
2004-05-07 13:34:56 +02:00
|
|
|
return
|
|
|
|
if not self.addressed and \
|
|
|
|
not self.registryValue('snarfUnaddressedDefinitions'):
|
2004-05-06 17:54:26 +02:00
|
|
|
return
|
|
|
|
isAre = isAre.lower()
|
|
|
|
if isAre in ('was', 'is', 'am'):
|
|
|
|
if self.db.hasIs(key):
|
2004-07-23 23:27:16 +02:00
|
|
|
if also:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Adding %r to %r.', key, value)
|
2004-07-23 23:27:16 +02:00
|
|
|
value = '%s or %s' % (self.db.getIs(key), value)
|
|
|
|
elif self.force:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Forcing %r to %r.', key, value)
|
2004-08-01 16:38:37 +02:00
|
|
|
elif self.badForce:
|
|
|
|
value = self.db.getIs(key)
|
|
|
|
self.reply('... but %s is %s, %s ...' % (key, value,
|
|
|
|
msg.nick))
|
|
|
|
return
|
2004-07-23 23:27:16 +02:00
|
|
|
elif self.addressed:
|
2004-05-06 17:54:26 +02:00
|
|
|
value = self.db.getIs(key)
|
2004-07-23 23:27:16 +02:00
|
|
|
self.reply('But %s is %s, %s.' % (key, value, msg.nick))
|
2004-05-06 17:54:26 +02:00
|
|
|
return
|
2004-08-01 16:38:37 +02:00
|
|
|
else:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Already have a %r key.', key)
|
2004-08-01 16:38:37 +02:00
|
|
|
return
|
2004-05-06 17:54:26 +02:00
|
|
|
self.db.setIs(key, value)
|
|
|
|
else:
|
|
|
|
if self.db.hasAre(key):
|
2004-07-23 23:27:16 +02:00
|
|
|
if also:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Adding %r to %r.', key, value)
|
2004-07-23 23:27:16 +02:00
|
|
|
value = '%s or %s' % (self.db.getAre(key), value)
|
|
|
|
elif self.force:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Forcing %r to %r.', key, value)
|
2004-08-01 16:38:37 +02:00
|
|
|
elif self.badForce:
|
|
|
|
value = self.db.getAre(key)
|
|
|
|
self.reply('... but %s are %s, %s ...' % (key, value,
|
|
|
|
msg.nick))
|
|
|
|
return
|
2004-07-23 23:27:16 +02:00
|
|
|
elif self.addressed:
|
2004-05-06 17:54:26 +02:00
|
|
|
value = self.db.getAre(key)
|
2004-07-23 23:27:16 +02:00
|
|
|
self.reply('But %s are %s, %s.' % (key, value, msg.nick))
|
2004-05-06 17:54:26 +02:00
|
|
|
return
|
2004-08-01 16:38:37 +02:00
|
|
|
else:
|
2004-08-10 08:14:31 +02:00
|
|
|
self.log.debug('Already have a %r key.', key)
|
2004-08-01 16:38:37 +02:00
|
|
|
return
|
2004-05-06 17:54:26 +02:00
|
|
|
self.db.setAre(key, value)
|
2004-09-06 00:57:15 +02:00
|
|
|
if self.addressed or self.force:
|
2004-05-06 17:54:26 +02:00
|
|
|
self.confirm()
|
2003-03-28 10:07:21 +01:00
|
|
|
|
2004-05-06 17:54:26 +02:00
|
|
|
def stats(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the number of changes and requests made to the Infobot database
|
|
|
|
since the plugin was loaded.
|
|
|
|
"""
|
2004-08-29 19:05:14 +02:00
|
|
|
changes = self.db.getChangeCount()
|
|
|
|
responses = self.db.getResponseCount()
|
|
|
|
now = time.time()
|
|
|
|
diff = int(now - world.startedAt)
|
|
|
|
mode = {True: 'optional', False: 'require'}
|
|
|
|
answer = self.registryValue('answerUnaddressedQuestions')
|
|
|
|
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)),
|
|
|
|
utils.nItems('factoid',self.db.getNumFacts()),
|
|
|
|
mode[answer]))
|
|
|
|
status=stats
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-07-31 08:46:19 +02:00
|
|
|
def tell(self, irc, msg, args):
|
|
|
|
"""<nick> [about] <factoid>
|
|
|
|
|
|
|
|
Tells <nick> about <factoid>.
|
|
|
|
"""
|
|
|
|
if len(args) < 2:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
if args[1] == 'about':
|
|
|
|
del args[1]
|
|
|
|
(nick, factoid) = privmsgs.getArgs(args, required=2)
|
|
|
|
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 '
|
|
|
|
'do the telling.' % nick)
|
2004-07-31 08:46:19 +02:00
|
|
|
return
|
|
|
|
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()
|
|
|
|
|
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:
|