2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
Provides fun commands that require a database to operate.
|
|
|
|
"""
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-11 23:03:02 +02:00
|
|
|
import re
|
2003-08-22 08:08:55 +02:00
|
|
|
import sets
|
2003-08-31 10:42:07 +02:00
|
|
|
import time
|
2003-03-12 07:26:59 +01:00
|
|
|
import string
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
import sqlite
|
2003-10-21 15:23:26 +02:00
|
|
|
import getopt
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import conf
|
2003-10-11 23:03:02 +02:00
|
|
|
import debug
|
2003-08-22 23:34:33 +02:00
|
|
|
import ircdb
|
2003-08-22 10:10:58 +02:00
|
|
|
import utils
|
2003-08-31 10:42:51 +02:00
|
|
|
import world
|
2003-03-12 07:26:59 +01:00
|
|
|
import ircmsgs
|
|
|
|
import ircutils
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
dbFilename = os.path.join(conf.dataDir, 'FunDB.db')
|
|
|
|
|
2003-09-01 07:51:32 +02:00
|
|
|
tableCreateStatements = {
|
2003-09-01 06:45:26 +02:00
|
|
|
'larts': ("""CREATE TABLE larts (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-10-02 18:07:53 +02:00
|
|
|
lart TEXT,
|
|
|
|
added_by TEXT
|
2003-09-01 06:45:26 +02:00
|
|
|
)""",),
|
|
|
|
'praises': ("""CREATE TABLE praises (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-10-02 18:07:53 +02:00
|
|
|
praise TEXT,
|
|
|
|
added_by TEXT
|
2003-09-01 06:45:26 +02:00
|
|
|
)""",),
|
|
|
|
'insults': ("""CREATE TABLE insults (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-10-02 18:07:53 +02:00
|
|
|
insult TEXT,
|
|
|
|
added_by TEXT
|
2003-09-01 06:45:26 +02:00
|
|
|
)""",),
|
|
|
|
'excuses': ("""CREATE TABLE excuses (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-10-02 18:07:53 +02:00
|
|
|
excuse TEXT,
|
|
|
|
added_by TEXT
|
2003-09-01 06:45:26 +02:00
|
|
|
)""",),
|
|
|
|
'words': ("""CREATE TABLE words (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word TEXT UNIQUE ON CONFLICT IGNORE,
|
|
|
|
sorted_word_id INTEGER
|
|
|
|
)""",
|
2003-09-01 07:51:32 +02:00
|
|
|
"""CREATE INDEX sorted_word_id ON words (sorted_word_id)""",
|
|
|
|
"""CREATE TABLE sorted_words (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word TEXT UNIQUE ON CONFLICT IGNORE
|
|
|
|
)""",
|
|
|
|
"""CREATE INDEX sorted_words_word ON sorted_words (word)"""),
|
2003-09-01 06:45:26 +02:00
|
|
|
}
|
|
|
|
|
2003-09-01 07:51:32 +02:00
|
|
|
def makeDb(dbfilename, replace=False):
|
|
|
|
if os.path.exists(dbfilename):
|
|
|
|
if replace:
|
|
|
|
os.remove(dbfilename)
|
|
|
|
db = sqlite.connect(dbfilename)
|
|
|
|
cursor = db.cursor()
|
|
|
|
for table in tableCreateStatements:
|
|
|
|
try:
|
|
|
|
cursor.execute("""SELECT * FROM %s LIMIT 1""" % table)
|
|
|
|
except sqlite.DatabaseError: # The table doesn't exist.
|
|
|
|
for sql in tableCreateStatements[table]:
|
|
|
|
cursor.execute(sql)
|
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
|
|
|
def addWord(db, word, commit=False):
|
|
|
|
word = word.strip().lower()
|
|
|
|
L = list(word)
|
|
|
|
L.sort()
|
|
|
|
sorted = ''.join(L)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO sorted_words VALUES (NULL, %s)""", sorted)
|
|
|
|
cursor.execute("""INSERT INTO words VALUES (NULL, %s,
|
|
|
|
(SELECT id FROM sorted_words
|
|
|
|
WHERE word=%s))""", word, sorted)
|
|
|
|
if commit:
|
2003-09-01 06:45:26 +02:00
|
|
|
db.commit()
|
2003-09-01 07:51:32 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
class FunDB(callbacks.Privmsg):
|
|
|
|
"""
|
|
|
|
Contains the 'fun' commands that require a database. Currently includes
|
|
|
|
database-backed commands for crossword puzzle solving, anagram searching,
|
2003-09-01 09:44:08 +02:00
|
|
|
larting, praising, excusing, and insulting.
|
2003-03-12 07:26:59 +01:00
|
|
|
"""
|
2003-08-31 10:42:07 +02:00
|
|
|
_tables = sets.Set(['lart', 'insult', 'excuse', 'praise'])
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
self.db = makeDb(dbFilename)
|
2003-08-31 10:43:56 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def die(self):
|
2003-08-15 05:00:10 +02:00
|
|
|
self.db.commit()
|
2003-03-12 07:26:59 +01:00
|
|
|
self.db.close()
|
2003-08-25 22:13:04 +02:00
|
|
|
del self.db
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def insult(self, irc, msg, args):
|
2003-04-20 08:26:17 +02:00
|
|
|
"""<nick>
|
|
|
|
|
|
|
|
Insults <nick>.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
nick = privmsgs.getArgs(args)
|
2003-10-23 14:35:43 +02:00
|
|
|
if not nick:
|
|
|
|
raise callbacks.ArgumentError
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""SELECT id, insult FROM insults
|
2003-04-05 12:24:40 +02:00
|
|
|
WHERE insult NOT NULL
|
2003-03-12 07:26:59 +01:00
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 1""")
|
2003-08-23 07:21:45 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-08-22 23:34:33 +02:00
|
|
|
irc.error(msg, 'There are currently no available insults.')
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-10-23 14:35:43 +02:00
|
|
|
(id, insult) = cursor.fetchone()
|
|
|
|
nick = nick.strip()
|
2003-10-31 19:43:08 +01:00
|
|
|
nick = re.sub(r'\bme\b', msg.nick, nick)
|
2003-10-29 21:27:09 +01:00
|
|
|
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
|
|
|
|
insultee = nick
|
2003-10-23 14:35:43 +02:00
|
|
|
insult = insult.replace("$who", insultee)
|
2003-08-27 09:45:48 +02:00
|
|
|
s = '%s: %s (#%s)' % (insultee, insult, id)
|
2003-10-23 14:35:43 +02:00
|
|
|
irc.reply(msg, s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def crossword(self, irc, msg, args):
|
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Gives the possible crossword completions for <word>; use underscores
|
|
|
|
('_') to denote blank spaces.
|
|
|
|
"""
|
|
|
|
word = privmsgs.getArgs(args).lower()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
if '%' in word:
|
|
|
|
irc.error(msg, '"%" isn\'t allowed in the word.')
|
|
|
|
return
|
|
|
|
cursor.execute("""SELECT word FROM words
|
|
|
|
WHERE word LIKE %s
|
|
|
|
ORDER BY word""", word)
|
|
|
|
words = map(lambda t: t[0], cursor.fetchall())
|
|
|
|
irc.reply(msg, ', '.join(words))
|
|
|
|
|
|
|
|
def excuse(self, irc, msg, args):
|
2003-10-22 21:52:05 +02:00
|
|
|
"""[<id>]
|
2003-04-20 08:26:17 +02:00
|
|
|
|
2003-10-22 21:52:05 +02:00
|
|
|
Gives you a standard, random BOFH excuse or the excuse with the given
|
|
|
|
<id>.
|
2003-04-20 08:26:17 +02:00
|
|
|
"""
|
2003-10-22 21:52:05 +02:00
|
|
|
id = privmsgs.getArgs(args, needed=0, optional=1)
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor = self.db.cursor()
|
2003-10-22 21:52:05 +02:00
|
|
|
if id:
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.error(msg, 'The <id> argument must be an integer.')
|
|
|
|
return
|
|
|
|
cursor.execute("""SELECT id, excuse FROM excuses WHERE id=%s""",
|
|
|
|
id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such excuse.')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
cursor.execute("""SELECT id, excuse FROM excuses
|
|
|
|
WHERE excuse NOTNULL
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 1""")
|
2003-08-23 07:21:45 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-08-22 23:34:33 +02:00
|
|
|
irc.error(msg, 'There are currently no available excuses.')
|
2003-08-23 07:21:45 +02:00
|
|
|
else:
|
|
|
|
(id, excuse) = cursor.fetchone()
|
|
|
|
irc.reply(msg, '%s (#%s)' % (excuse, id))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def add(self, irc, msg, args):
|
2003-08-22 07:36:53 +02:00
|
|
|
"""<lart|excuse|insult|praise> <text>
|
2003-04-20 08:26:17 +02:00
|
|
|
|
2003-08-25 22:13:04 +02:00
|
|
|
Adds another record to the data referred to in the first argument. For
|
|
|
|
commands that will later respond with an ACTION (lart and praise), $who
|
|
|
|
should be in the message to show who should be larted or praised. I.e.
|
|
|
|
'dbadd lart slices $who in half with a free AOL cd' would make the bot,
|
|
|
|
when it used that lart against, say, jemfinch, to say '/me slices
|
|
|
|
jemfinch in half with a free AOL cd'
|
2003-04-20 08:26:17 +02:00
|
|
|
"""
|
2003-08-22 07:36:53 +02:00
|
|
|
(table, s) = privmsgs.getArgs(args, needed=2)
|
2003-08-22 23:34:33 +02:00
|
|
|
table = table.lower()
|
|
|
|
try:
|
2003-09-12 22:06:58 +02:00
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
2003-08-22 23:34:33 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:56 +02:00
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
2003-08-22 23:34:33 +02:00
|
|
|
return
|
2003-08-22 07:36:53 +02:00
|
|
|
if table == "lart" or table == "praise":
|
|
|
|
if '$who' not in s:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, 'There must be a $who in the lart/praise '\
|
|
|
|
'somewhere.')
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
|
|
|
elif table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '"%s" is not valid. Valid values include %s.' % \
|
2003-08-22 09:04:47 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor = self.db.cursor()
|
2003-09-16 20:11:16 +02:00
|
|
|
sql = """INSERT INTO %ss VALUES (NULL, %%s, %%s)""" % table
|
2003-08-31 10:42:07 +02:00
|
|
|
cursor.execute(sql, s, name)
|
2003-03-12 07:26:59 +01:00
|
|
|
self.db.commit()
|
2003-08-22 10:10:58 +02:00
|
|
|
sql = """SELECT id FROM %ss WHERE %s=%%s""" % (table, table)
|
|
|
|
cursor.execute(sql, s)
|
|
|
|
id = cursor.fetchone()[0]
|
2003-09-18 09:26:21 +02:00
|
|
|
response = '%s (%s #%s)' % (conf.replySuccess, table, id)
|
|
|
|
irc.reply(msg, response)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def remove(self, irc, msg, args):
|
2003-08-22 07:36:53 +02:00
|
|
|
"""<lart|excuse|insult|praise> <id>
|
2003-04-20 08:26:17 +02:00
|
|
|
|
2003-08-22 07:36:53 +02:00
|
|
|
Removes the data, referred to in the first argument, with the id
|
|
|
|
number <id> from the database.
|
2003-04-20 08:26:17 +02:00
|
|
|
"""
|
2003-08-22 07:36:53 +02:00
|
|
|
(table, id) = privmsgs.getArgs(args, needed=2)
|
2003-08-22 23:34:33 +02:00
|
|
|
table = table.lower()
|
|
|
|
try:
|
2003-09-12 22:06:58 +02:00
|
|
|
ircdb.users.getUser(msg.prefix).name
|
2003-08-22 23:34:33 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:56 +02:00
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
2003-08-22 23:34:33 +02:00
|
|
|
return
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
2003-09-16 18:49:17 +02:00
|
|
|
irc.error(msg, 'The <id> argument must be an integer.')
|
2003-03-12 07:26:59 +01:00
|
|
|
return
|
2003-08-22 07:36:53 +02:00
|
|
|
if table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '"%s" is not valid. Valid values include %s.' % \
|
2003-08-22 09:04:47 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor = self.db.cursor()
|
2003-08-22 09:04:47 +02:00
|
|
|
sql = """DELETE FROM %ss WHERE id=%%s""" % table
|
|
|
|
cursor.execute(sql, id)
|
2003-03-12 07:26:59 +01:00
|
|
|
self.db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def change(self, irc, msg, args):
|
2003-09-16 18:49:17 +02:00
|
|
|
"""<lart|excuse|insult|praise> <id> <regexp>
|
|
|
|
|
|
|
|
Changes the data, referred to in the first argument, with the id
|
|
|
|
number <id> according to the regular expression <regexp>. <id> is the
|
|
|
|
zero-based index into the db; <regexp> is a regular expression of the
|
|
|
|
form s/regexp/replacement/flags.
|
|
|
|
"""
|
|
|
|
(table, id, regexp) = privmsgs.getArgs(args, needed=3)
|
|
|
|
table = table.lower()
|
|
|
|
try:
|
2003-09-16 20:53:09 +02:00
|
|
|
name = ircdb.users.getUser(msg.prefix).name
|
2003-09-16 18:49:17 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.error(msg, 'The <id> argument must be an integer.')
|
|
|
|
return
|
|
|
|
if table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '"%s" is not valid. Valid values include %s.' % \
|
2003-09-16 18:49:17 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
replacer = utils.perlReToReplacer(regexp)
|
|
|
|
except ValueError, e:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, 'The regexp wasn\'t valid: %s.' % e.args[0])
|
2003-09-16 18:49:17 +02:00
|
|
|
except re.error, e:
|
|
|
|
irc.error(msg, debug.exnToString(e))
|
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table)
|
|
|
|
cursor.execute(sql, id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such %s.' % table)
|
|
|
|
else:
|
|
|
|
old_entry = cursor.fetchone()[0]
|
|
|
|
new_entry = replacer(old_entry)
|
2003-10-02 18:07:53 +02:00
|
|
|
sql = """UPDATE %ss SET %s=%%s, added_by=%%s WHERE id=%%s""" % \
|
|
|
|
(table, table)
|
2003-09-16 20:53:09 +02:00
|
|
|
cursor.execute(sql, new_entry, name, id)
|
2003-09-16 18:49:17 +02:00
|
|
|
self.db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def num(self, irc, msg, args):
|
2003-08-22 07:36:53 +02:00
|
|
|
"""<lart|excuse|insult|praise>
|
2003-04-05 12:24:40 +02:00
|
|
|
|
2003-08-22 07:36:53 +02:00
|
|
|
Returns the number of records, of the type specified, currently in
|
|
|
|
the database.
|
2003-04-20 08:26:17 +02:00
|
|
|
"""
|
2003-08-22 07:36:53 +02:00
|
|
|
table = privmsgs.getArgs(args)
|
2003-08-22 23:34:33 +02:00
|
|
|
table = table.lower()
|
2003-08-22 07:36:53 +02:00
|
|
|
if table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '%r is not valid. Valid values include %s.' % \
|
2003-08-22 09:04:47 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
2003-04-05 12:24:40 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-08-22 09:04:47 +02:00
|
|
|
sql = """SELECT count(*) FROM %ss""" % table
|
|
|
|
cursor.execute(sql)
|
2003-10-02 18:07:53 +02:00
|
|
|
total = int(cursor.fetchone()[0])
|
|
|
|
irc.reply(msg, 'There %s currently %s in my database.' % \
|
2003-09-05 08:49:14 +02:00
|
|
|
(utils.be(total), utils.nItems(total, table)))
|
2003-08-22 07:36:53 +02:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def get(self, irc, msg, args):
|
2003-08-22 07:36:53 +02:00
|
|
|
"""<lart|excuse|insult|praise> <id>
|
|
|
|
|
2003-08-22 08:45:10 +02:00
|
|
|
Gets the record with id <id> from the table specified.
|
2003-08-22 07:36:53 +02:00
|
|
|
"""
|
|
|
|
(table, id) = privmsgs.getArgs(args, needed=2)
|
2003-08-22 08:45:10 +02:00
|
|
|
table = table.lower()
|
2003-08-22 07:36:53 +02:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
2003-09-16 18:49:17 +02:00
|
|
|
irc.error(msg, 'The <id> argument must be an integer.')
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
|
|
|
if table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '"%s" is not valid. Valid values include %s.' % \
|
2003-08-22 08:45:10 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
2003-08-22 08:45:10 +02:00
|
|
|
sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table)
|
|
|
|
cursor.execute(sql, id)
|
2003-08-22 07:36:53 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-08-22 09:04:47 +02:00
|
|
|
irc.error(msg, 'There is no such %s.' % table)
|
2003-08-22 07:36:53 +02:00
|
|
|
else:
|
2003-08-22 23:34:33 +02:00
|
|
|
reply = cursor.fetchone()[0]
|
|
|
|
irc.reply(msg, reply)
|
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def info(self, irc, msg, args):
|
2003-08-22 23:34:33 +02:00
|
|
|
"""<lart|excuse|insult|praise> <id>
|
|
|
|
|
|
|
|
Gets the info for the record with id <id> from the table specified.
|
|
|
|
"""
|
|
|
|
(table, id) = privmsgs.getArgs(args, needed=2)
|
|
|
|
table = table.lower()
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
2003-09-16 19:16:11 +02:00
|
|
|
irc.error(msg, 'The <id> argument must be an integer.')
|
2003-08-22 23:34:33 +02:00
|
|
|
return
|
|
|
|
if table not in self._tables:
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, '"%s" is not valid. Valid values include %s.' % \
|
2003-08-22 23:34:33 +02:00
|
|
|
(table, utils.commaAndify(self._tables)))
|
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
2003-09-16 20:11:16 +02:00
|
|
|
sql = """SELECT added_by FROM %ss WHERE id=%%s""" % table
|
2003-08-22 23:34:33 +02:00
|
|
|
cursor.execute(sql, id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such %s.' % table)
|
|
|
|
else:
|
2003-09-16 20:11:16 +02:00
|
|
|
add = cursor.fetchone()[0]
|
|
|
|
reply = '%s #%s: Created by %s.' % (table, id, add)
|
2003-08-22 23:34:33 +02:00
|
|
|
irc.reply(msg, reply)
|
2003-04-05 12:24:40 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def lart(self, irc, msg, args):
|
2003-10-21 17:36:39 +02:00
|
|
|
"""[<id>] <text> [for <reason>]
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-21 15:23:26 +02:00
|
|
|
Uses a lart on <text> (giving the reason, if offered). Will use lart
|
|
|
|
number <id> from the database when <id> is given.
|
2003-03-12 07:26:59 +01:00
|
|
|
"""
|
2003-10-21 17:36:39 +02:00
|
|
|
(id, nick) = privmsgs.getArgs(args, optional=1)
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
if id < 1:
|
|
|
|
irc.error(msg, 'There is no such lart.')
|
|
|
|
return
|
|
|
|
except ValueError:
|
|
|
|
nick = ' '.join([id, nick]).strip()
|
|
|
|
id = 0
|
|
|
|
if not nick:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
|
2003-09-01 08:11:02 +02:00
|
|
|
try:
|
2003-09-05 02:16:28 +02:00
|
|
|
(nick, reason) = map(' '.join,
|
2003-10-21 17:36:39 +02:00
|
|
|
utils.itersplit('for'.__eq__, nick.split(), 1))
|
2003-09-01 08:11:02 +02:00
|
|
|
except ValueError:
|
2003-09-18 09:26:21 +02:00
|
|
|
reason = ''
|
2003-10-21 17:36:39 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
cursor = self.db.cursor()
|
2003-10-21 17:36:39 +02:00
|
|
|
if id:
|
|
|
|
cursor.execute("""SELECT id, lart FROM larts WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such lart.')
|
|
|
|
return
|
2003-10-21 15:23:26 +02:00
|
|
|
else:
|
|
|
|
cursor.execute("""SELECT id, lart FROM larts
|
|
|
|
WHERE lart NOTNULL
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 1""")
|
2003-08-23 07:21:45 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-08-22 23:34:33 +02:00
|
|
|
irc.error(msg, 'There are currently no available larts.')
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2003-08-23 07:21:45 +02:00
|
|
|
(id, lart) = cursor.fetchone()
|
2003-10-31 19:43:08 +01:00
|
|
|
nick = re.sub(r'\bme\b', msg.nick, nick)
|
|
|
|
reason = re.sub(r'\bme\b', msg.nick, reason)
|
2003-10-29 21:27:09 +01:00
|
|
|
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
|
|
|
|
reason = re.sub(r'\bmy\b', '%s\'s' % msg.nick, reason)
|
|
|
|
lartee = nick
|
2003-08-23 07:21:45 +02:00
|
|
|
lart = lart.replace("$who", lartee)
|
2003-09-01 08:11:02 +02:00
|
|
|
if len(reason) > 0:
|
2003-09-18 09:26:21 +02:00
|
|
|
s = '%s for %s (#%s)' % (lart, reason, id)
|
2003-09-01 08:11:02 +02:00
|
|
|
else:
|
2003-09-18 09:26:21 +02:00
|
|
|
s = '%s (#%s)' % (lart, id)
|
|
|
|
irc.reply(msg, s, action=True)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-08-22 23:34:33 +02:00
|
|
|
def praise(self, irc, msg, args):
|
2003-10-21 17:36:39 +02:00
|
|
|
"""[<id>] <text> [for <reason>]
|
2003-08-22 23:34:33 +02:00
|
|
|
|
2003-10-21 15:23:26 +02:00
|
|
|
Uses a praise on <text> (giving the reason, if offered). Will use
|
|
|
|
praise number <id> from the database when <id> is given.
|
2003-08-22 23:34:33 +02:00
|
|
|
"""
|
2003-10-21 17:36:39 +02:00
|
|
|
(id, nick) = privmsgs.getArgs(args, optional=1)
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
if id < 1:
|
|
|
|
irc.error(msg, 'There is no such praise.')
|
|
|
|
return
|
|
|
|
except ValueError:
|
|
|
|
nick = ' '.join([id, nick]).strip()
|
|
|
|
id = 0
|
|
|
|
if not nick:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
|
2003-09-01 08:11:02 +02:00
|
|
|
try:
|
2003-09-05 02:16:28 +02:00
|
|
|
(nick, reason) = map(' '.join,
|
2003-10-21 17:36:39 +02:00
|
|
|
utils.itersplit('for'.__eq__, nick.split(), 1))
|
2003-09-01 08:11:02 +02:00
|
|
|
except ValueError:
|
2003-09-18 09:26:21 +02:00
|
|
|
reason = ''
|
2003-10-21 17:36:39 +02:00
|
|
|
|
2003-08-22 23:34:33 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-10-21 17:36:39 +02:00
|
|
|
if id:
|
|
|
|
cursor.execute("""SELECT id, praise FROM praises WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'There is no such praise.')
|
|
|
|
return
|
2003-10-21 15:23:26 +02:00
|
|
|
else:
|
|
|
|
cursor.execute("""SELECT id, praise FROM praises
|
|
|
|
WHERE praise NOTNULL
|
|
|
|
ORDER BY random()
|
|
|
|
LIMIT 1""")
|
2003-10-21 17:36:39 +02:00
|
|
|
|
2003-08-23 07:21:45 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-08-22 23:34:33 +02:00
|
|
|
irc.error(msg, 'There are currently no available praises.')
|
|
|
|
else:
|
2003-08-23 07:21:45 +02:00
|
|
|
(id, praise) = cursor.fetchone()
|
2003-10-31 19:43:08 +01:00
|
|
|
nick = re.sub(r'\bme\b', msg.nick, nick)
|
|
|
|
reason = re.sub(r'\bme\b', msg.nick, reason)
|
2003-10-29 21:27:09 +01:00
|
|
|
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
|
|
|
|
reason = re.sub(r'\bmy\b', '%s\'s' % msg.nick, reason)
|
|
|
|
praisee = nick
|
2003-08-23 07:21:45 +02:00
|
|
|
praise = praise.replace("$who", praisee)
|
2003-09-01 08:11:02 +02:00
|
|
|
if len(reason) > 0:
|
2003-09-18 09:26:21 +02:00
|
|
|
s = '%s for %s (#%s)' % (praise, reason, id)
|
2003-09-01 08:11:02 +02:00
|
|
|
else:
|
2003-09-18 09:26:21 +02:00
|
|
|
s = '%s (#%s)' % (praise, id)
|
|
|
|
irc.reply(msg, s, action=True)
|
2003-08-22 23:34:33 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def addword(self, irc, msg, args):
|
2003-04-20 08:26:17 +02:00
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Adds a word to the database of words. This database is used for the
|
|
|
|
anagram and crossword commands.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
word = privmsgs.getArgs(args)
|
|
|
|
if word.translate(string.ascii, string.ascii_letters) != '':
|
2003-10-02 18:07:53 +02:00
|
|
|
irc.error(msg, 'Word must contain only letters.')
|
2003-03-12 07:26:59 +01:00
|
|
|
addWord(self.db, word, commit=True)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
|
|
|
def anagram(self, irc, msg, args):
|
2003-04-20 08:26:17 +02:00
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Using the words database, determines if a word has any anagrams.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
word = privmsgs.getArgs(args).strip().lower()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""SELECT words.word FROM words
|
|
|
|
WHERE sorted_word_id=(
|
|
|
|
SELECT sorted_word_id FROM words
|
|
|
|
WHERE word=%s)""", word)
|
|
|
|
words = map(lambda t: t[0], cursor.fetchall())
|
|
|
|
try:
|
|
|
|
words.remove(word)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if words:
|
|
|
|
irc.reply(msg, ', '.join(words))
|
|
|
|
else:
|
|
|
|
irc.reply(msg, 'That word has no anagrams that I know of.')
|
2003-08-14 12:51:42 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
Class = FunDB
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if len(sys.argv) < 3:
|
2003-08-22 23:34:33 +02:00
|
|
|
print 'Usage: %s <words|larts|excuses|insults|zipcodes> file'\
|
|
|
|
' [<console>]' % sys.argv[0]
|
2003-03-12 07:26:59 +01:00
|
|
|
sys.exit(-1)
|
|
|
|
category = sys.argv[1]
|
|
|
|
filename = sys.argv[2]
|
2003-08-22 23:34:33 +02:00
|
|
|
if len(sys.argv) == 4:
|
|
|
|
added_by = sys.argv[3]
|
|
|
|
else:
|
|
|
|
added_by = '<console>'
|
2003-03-12 07:26:59 +01:00
|
|
|
db = makeDb(dbFilename)
|
|
|
|
cursor = db.cursor()
|
|
|
|
for line in open(filename, 'r'):
|
|
|
|
line = line.rstrip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if category == 'words':
|
|
|
|
cursor.execute("""PRAGMA cache_size = 50000""")
|
|
|
|
addWord(db, line)
|
|
|
|
elif category == 'larts':
|
2003-08-11 19:16:03 +02:00
|
|
|
if '$who' in line:
|
2003-08-22 23:34:33 +02:00
|
|
|
cursor.execute("""INSERT INTO larts VALUES (NULL, %s,
|
2003-09-16 20:11:16 +02:00
|
|
|
%s)""", line, added_by)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
print 'Invalid lart: %s' % line
|
2003-08-22 23:34:33 +02:00
|
|
|
elif category == 'praises':
|
|
|
|
if '$who' in line:
|
|
|
|
cursor.execute("""INSERT INTO praises VALUES (NULL, %s,
|
2003-09-16 20:11:16 +02:00
|
|
|
%s)""", line, added_by)
|
2003-08-22 23:34:33 +02:00
|
|
|
else:
|
|
|
|
print 'Invalid praise: %s' % line
|
2003-03-12 07:26:59 +01:00
|
|
|
elif category == 'insults':
|
2003-09-16 20:11:16 +02:00
|
|
|
cursor.execute("""INSERT INTO insults VALUES (NULL, %s, %s
|
|
|
|
)""", line, added_by)
|
2003-03-12 07:26:59 +01:00
|
|
|
elif category == 'excuses':
|
2003-09-16 20:11:16 +02:00
|
|
|
cursor.execute("""INSERT INTO excuses VALUES (NULL, %s, %s
|
|
|
|
)""", line, added_by)
|
2003-03-12 07:26:59 +01:00
|
|
|
db.commit()
|
|
|
|
db.close()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|