2004-01-20 08:15:37 +01:00
|
|
|
#!/usr/bin/env python
|
2003-10-28 02:46:58 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2003, 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-07-20 07:36:59 +02:00
|
|
|
The Dunno module is used to spice up the 'replyWhenNotCommand' behavior with
|
|
|
|
random 'I dunno'-like responses. If you want something spicier than '<x> is
|
|
|
|
not a valid command'-like responses, use this plugin.
|
2003-10-28 02:46:58 +01:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-05-06 18:33:43 +02:00
|
|
|
__author__ = "Daniel DiPaolo (Strike) <ddipaolo@users.sf.net>"
|
2003-11-25 09:23:47 +01:00
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
import os
|
|
|
|
import time
|
2003-10-28 02:46:58 +01:00
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
import conf
|
2003-10-28 02:46:58 +01:00
|
|
|
import utils
|
2003-12-04 00:48:00 +01:00
|
|
|
import ircdb
|
|
|
|
import plugins
|
2003-10-28 02:46:58 +01:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
2003-12-04 00:48:00 +01: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-01-18 08:58:26 +01:00
|
|
|
dbfilename = os.path.join(conf.supybot.directories.data(), 'Dunno.db')
|
2003-10-28 02:46:58 +01:00
|
|
|
|
|
|
|
class Dunno(callbacks.Privmsg):
|
2004-07-20 07:36:59 +02:00
|
|
|
"""This plugin was written initially to work with MoobotFactoids, the two
|
|
|
|
of them to provide a similar-to-moobot-and-blootbot interface for factoids.
|
|
|
|
Basically, it replaces the standard 'Error: <X> is not a valid command.'
|
|
|
|
messages with messages kept in a database, able to give more personable
|
|
|
|
responses."""
|
2003-10-28 06:31:51 +01:00
|
|
|
priority = 100
|
2003-10-28 02:46:58 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
self.makeDb(dbfilename)
|
|
|
|
|
|
|
|
def makeDb(self, filename):
|
|
|
|
"""create Dunno database and tables"""
|
|
|
|
if os.path.exists(filename):
|
|
|
|
self.db = sqlite.connect(filename)
|
|
|
|
return
|
|
|
|
self.db = sqlite.connect(filename, converters={'bool': bool})
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE dunnos (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
added_by INTEGER,
|
|
|
|
added_at TIMESTAMP,
|
|
|
|
dunno TEXT
|
|
|
|
)""")
|
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
def invalidCommand(self, irc, msg, tokens):
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""SELECT dunno
|
|
|
|
FROM dunnos
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 1""")
|
2003-10-29 23:05:53 +01:00
|
|
|
if cursor.rowcount != 0:
|
2003-10-28 07:09:05 +01:00
|
|
|
dunno = cursor.fetchone()[0]
|
2003-11-02 04:18:39 +01:00
|
|
|
dunno = plugins.standardSubstitute(irc, msg, dunno)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(dunno, prefixName=False)
|
2003-10-28 02:46:58 +01:00
|
|
|
|
|
|
|
def add(self, irc, msg, args):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Adds <text> as a "dunno" to be used as a random response when no
|
|
|
|
command or factoid key matches. Can optionally contain '$who', which
|
|
|
|
will be replaced by the user's name when the dunno is displayed.
|
|
|
|
"""
|
|
|
|
# Must be registered to use this
|
|
|
|
try:
|
2004-04-30 07:21:13 +02:00
|
|
|
user_id = ircdb.users.getUserId(msg.prefix)
|
2003-10-28 02:46:58 +01:00
|
|
|
except KeyError:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
2004-04-30 07:21:13 +02:00
|
|
|
text = privmsgs.getArgs(args)
|
2003-10-28 02:46:58 +01:00
|
|
|
cursor = self.db.cursor()
|
2004-04-30 07:21:13 +02:00
|
|
|
at = int(time.time())
|
|
|
|
cursor.execute("""INSERT INTO dunnos VALUES(NULL, %s, %s, %s)""",
|
|
|
|
user_id, at, text)
|
2003-10-28 02:46:58 +01:00
|
|
|
self.db.commit()
|
2004-04-30 07:21:13 +02:00
|
|
|
cursor.execute("""SELECT id FROM dunnos WHERE added_at=%s""", at)
|
|
|
|
id = cursor.fetchone()[0]
|
|
|
|
irc.replySuccess('Dunno #%s added.' % id)
|
2003-10-28 02:46:58 +01:00
|
|
|
|
|
|
|
def remove(self, irc, msg, args):
|
|
|
|
"""<id>
|
|
|
|
|
|
|
|
Removes dunno with the given <id>.
|
|
|
|
"""
|
|
|
|
# Must be registered to use this
|
|
|
|
try:
|
|
|
|
user_id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
2003-11-11 14:20:06 +01:00
|
|
|
dunno_id = privmsgs.getArgs(args, required=1)
|
2003-10-28 02:46:58 +01:00
|
|
|
cursor = self.db.cursor()
|
2004-04-30 07:21:13 +02:00
|
|
|
cursor.execute("""SELECT added_by, dunno FROM dunnos
|
|
|
|
WHERE id=%s""", dunno_id)
|
2003-10-28 02:46:58 +01:00
|
|
|
if cursor.rowcount == 0:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('No dunno with id #%s.' % dunno_id)
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
|
|
|
(added_by, dunno) = cursor.fetchone()
|
|
|
|
if not (ircdb.checkCapability(user_id, 'admin') or \
|
|
|
|
added_by == user_id):
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('Only admins and the dunno creator may delete a dunno.')
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
2004-04-30 07:21:13 +02:00
|
|
|
cursor.execute("""DELETE FROM dunnos WHERE id=%s""", dunno_id)
|
2003-10-28 02:46:58 +01:00
|
|
|
self.db.commit()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-10-28 02:46:58 +01:00
|
|
|
|
|
|
|
def search(self, irc, msg, args):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Search for dunno containing the given text. Returns the ids of the
|
|
|
|
dunnos with the text in them.
|
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
text = privmsgs.getArgs(args, required=1)
|
2003-10-28 02:46:58 +01:00
|
|
|
glob = "%" + text + "%"
|
|
|
|
cursor = self.db.cursor()
|
2004-04-30 07:21:13 +02:00
|
|
|
cursor.execute("""SELECT id FROM dunnos WHERE dunno LIKE %s""", glob)
|
2003-10-28 02:46:58 +01:00
|
|
|
if cursor.rowcount == 0:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('No dunnos with %r found.' % text)
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
2003-10-28 07:09:05 +01:00
|
|
|
ids = [str(t[0]) for t in cursor.fetchall()]
|
2004-04-30 07:21:13 +02:00
|
|
|
s = 'Dunno search for %r (%s found): %s.' % \
|
2003-10-28 02:46:58 +01:00
|
|
|
(text, len(ids), utils.commaAndify(ids))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-10-28 02:46:58 +01:00
|
|
|
|
2003-10-28 06:31:51 +01:00
|
|
|
def get(self, irc, msg, args):
|
2003-10-28 02:46:58 +01:00
|
|
|
"""<id>
|
|
|
|
|
|
|
|
Display the text of the dunno with the given id.
|
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
id = privmsgs.getArgs(args, required=1)
|
2003-10-28 02:46:58 +01:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('%r is not a valid dunno id.' % id)
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
2004-04-30 07:21:13 +02:00
|
|
|
cursor.execute("""SELECT dunno FROM dunnos WHERE id=%s""", id)
|
2003-10-28 02:46:58 +01:00
|
|
|
if cursor.rowcount == 0:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('No dunno found with id #%s.' % id)
|
2003-10-28 02:46:58 +01:00
|
|
|
return
|
|
|
|
dunno = cursor.fetchone()[0]
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.reply("Dunno #%s: %r." % (id, dunno))
|
2003-10-28 02:46:58 +01:00
|
|
|
|
2004-01-26 22:19:07 +01:00
|
|
|
def change(self, irc, msg, args):
|
|
|
|
"""<id> <regexp>
|
|
|
|
|
|
|
|
Alters the dunno with the given id according to the provided regexp.
|
|
|
|
"""
|
|
|
|
id, regexp = privmsgs.getArgs(args, required=2)
|
|
|
|
# Must be registered to use this
|
|
|
|
try:
|
|
|
|
user_id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.errorNotRegistered()
|
2004-01-26 22:19:07 +01:00
|
|
|
return
|
|
|
|
# Check id arg
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('%r is not a valid dunno id.' % id)
|
2004-01-26 22:19:07 +01:00
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""SELECT dunno FROM dunnos WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('There is no dunno #%s.' % id)
|
2004-01-26 22:19:07 +01:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
replacer = utils.perlReToReplacer(regexp)
|
|
|
|
except:
|
2004-04-30 07:21:13 +02:00
|
|
|
irc.error('%r is not a valid regular expression.' % regexp)
|
2004-01-26 22:19:07 +01:00
|
|
|
return
|
2004-02-05 07:00:44 +01:00
|
|
|
dunno = cursor.fetchone()[0]
|
2004-01-26 22:19:07 +01:00
|
|
|
new_dunno = replacer(dunno)
|
|
|
|
cursor.execute("""UPDATE dunnos SET dunno=%s WHERE id=%s""",
|
|
|
|
new_dunno, id)
|
|
|
|
self.db.commit()
|
2004-02-05 07:00:44 +01:00
|
|
|
irc.replySuccess()
|
2004-07-20 07:36:59 +02:00
|
|
|
|
|
|
|
def stats(self, irc, msg, args):
|
|
|
|
"""Returns the number of dunnos in the dunno database."""
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""SELECT COUNT(*) FROM dunnos""")
|
|
|
|
irc.reply(cursor.fetchone()[0])
|
2004-01-26 22:19:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-10-28 02:46:58 +01:00
|
|
|
Class = Dunno
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|