2010-10-29 00:00:55 -05:00
|
|
|
"""
|
|
|
|
Facts - display facts, from within a category, from the database
|
|
|
|
Copyright (C) 2010 Brian S. Stephan
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import random
|
|
|
|
import re
|
2012-07-27 02:18:01 -05:00
|
|
|
|
|
|
|
import MySQLdb as mdb
|
2010-10-29 00:00:55 -05:00
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
|
|
|
class Facts(Module):
|
2011-01-06 23:25:46 -06:00
|
|
|
|
|
|
|
"""Select a fact from the database.
|
|
|
|
|
|
|
|
Facts are categorized by a name, which may allow for random selection and so on.
|
2010-10-29 00:00:55 -05:00
|
|
|
"""
|
|
|
|
|
2012-03-30 10:19:16 -05:00
|
|
|
def __init__(self, irc, config, server):
|
|
|
|
"""Set up XML-RPC methods."""
|
|
|
|
|
|
|
|
Module.__init__(self, irc, config, server)
|
|
|
|
|
|
|
|
irc.xmlrpc_register_function(self._get_fact, "facts_get")
|
|
|
|
|
2010-10-29 00:00:55 -05:00
|
|
|
def db_init(self):
|
2011-01-06 23:25:46 -06:00
|
|
|
"""Initialize database tables."""
|
2010-10-29 00:00:55 -05:00
|
|
|
|
|
|
|
# init the database if module isn't registered
|
|
|
|
version = self.db_module_registered(self.__class__.__name__)
|
|
|
|
if version == None:
|
|
|
|
db = self.get_db()
|
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
version = 1
|
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
|
|
|
cur.execute('''
|
2010-10-29 00:00:55 -05:00
|
|
|
CREATE TABLE facts_facts (
|
2012-07-27 02:18:01 -05:00
|
|
|
id SERIAL,
|
|
|
|
category VARCHAR(64) NOT NULL,
|
|
|
|
fact LONGTEXT NOT NULL,
|
|
|
|
who VARCHAR(64) NOT NULL,
|
|
|
|
userhost VARCHAR(256) NOT NULL,
|
|
|
|
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
2012-07-27 14:57:41 -05:00
|
|
|
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin
|
2010-10-29 00:00:55 -05:00
|
|
|
''')
|
|
|
|
db.commit()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.db_register_module_version(self.__class__.__name__, version)
|
|
|
|
except mdb.Error as e:
|
2010-10-29 00:00:55 -05:00
|
|
|
db.rollback()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.log.error("database error trying to create tables")
|
|
|
|
self.log.exception(e)
|
2010-10-29 00:00:55 -05:00
|
|
|
raise
|
2012-07-27 02:18:01 -05:00
|
|
|
finally: cur.close()
|
2010-10-29 00:00:55 -05:00
|
|
|
|
2011-01-07 23:09:07 -06:00
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
2011-01-06 23:25:46 -06:00
|
|
|
"""Add or retrieve a fact from the database."""
|
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
db = self.get_db()
|
2011-01-07 20:37:24 -06:00
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
2011-01-07 20:37:24 -06:00
|
|
|
|
|
|
|
match = re.search('^!facts\s+add\s+(\S+)\s+(.*)$', what)
|
|
|
|
if match:
|
|
|
|
category = match.group(1)
|
|
|
|
fact = match.group(2)
|
|
|
|
cur.execute('''INSERT INTO facts_facts (category, fact, who, userhost)
|
2012-07-27 02:18:01 -05:00
|
|
|
VALUES (%s, %s, %s, %s)''', (category, fact, nick, userhost))
|
2011-01-07 20:37:24 -06:00
|
|
|
db.commit()
|
rewrite recursion/alias code for the 500th time.
more of a moving of the code, actually, it now exists in (an overridden)
_handle_event, so that recursions happen against irc events directly,
rather than an already partially interpreted object.
with this change, modules don't need to implement do() nor do we have a
need for the internal_bus, which was doing an additional walk of the
modules after the irc event was already handled and turned into text. now
the core event handler does the recursion scans.
to support this, we bring back the old replypath trick and use it again,
so we know when to send a privmsg reply and when to return text so that
it may be chained in recursion. this feels old hat by now, but if you
haven't been following along, you should really look at the diff.
that's the meat of the change. the rest is updating modules to use
self.reply() and reimplementing (un)register_handlers where appropriate
2011-02-17 01:08:45 -06:00
|
|
|
return self.reply(connection, event, category + ' added.')
|
2010-10-29 00:00:55 -05:00
|
|
|
|
2011-01-07 20:37:24 -06:00
|
|
|
match = re.search('^!facts\s+(\S+)\s+(.*)$', what)
|
|
|
|
if match:
|
|
|
|
category = match.group(1)
|
|
|
|
regex = match.group(2)
|
2012-03-30 10:18:40 -05:00
|
|
|
return self.reply(connection, event, self._get_fact(category, regex))
|
2010-10-29 00:00:55 -05:00
|
|
|
|
2011-01-07 20:37:24 -06:00
|
|
|
match = re.search('^!facts\s+(\S+)$', what)
|
|
|
|
if match:
|
|
|
|
category = match.group(1)
|
2012-03-30 10:18:40 -05:00
|
|
|
return self.reply(connection, event, self._get_fact(category))
|
2012-07-27 02:18:01 -05:00
|
|
|
except mdb.Error as e:
|
|
|
|
db.rollback()
|
|
|
|
self.log.error("database error during add/retrieve")
|
|
|
|
self.log.exception(e)
|
|
|
|
return self.reply(connection, event, "database error during add/retrieve fact")
|
|
|
|
finally: cur.close()
|
2010-10-29 00:00:55 -05:00
|
|
|
|
2012-03-30 10:18:40 -05:00
|
|
|
def _get_fact(self, category, search=""):
|
2012-03-30 11:14:31 -05:00
|
|
|
"""
|
|
|
|
Get a fact in the given category from the database.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
category - the fact category to query
|
|
|
|
search - the optional regex to match against within that category
|
|
|
|
"""
|
2012-03-30 10:18:40 -05:00
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
db = self.get_db()
|
2012-03-30 10:18:40 -05:00
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
2012-03-30 10:18:40 -05:00
|
|
|
|
|
|
|
if search == "":
|
2012-07-27 02:18:01 -05:00
|
|
|
cur.execute("SELECT * FROM facts_facts WHERE category = %s",
|
|
|
|
(category,))
|
|
|
|
facts = cur.fetchall()
|
2012-03-30 10:18:40 -05:00
|
|
|
else:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur.execute("SELECT * FROM facts_facts WHERE category = %s AND fact REGEXP %s",
|
|
|
|
(category, search))
|
|
|
|
facts = cur.fetchall()
|
2012-03-30 10:18:40 -05:00
|
|
|
|
|
|
|
if len(facts) > 0:
|
|
|
|
fact = facts[random.randint(1,len(facts))-1]
|
2012-07-27 16:34:57 -05:00
|
|
|
return fact['fact'].rstrip()
|
2012-07-27 02:18:01 -05:00
|
|
|
except mdb.Error as e:
|
|
|
|
self.log.error("database error in _get_fact")
|
|
|
|
self.log.exception(e)
|
2012-07-27 20:38:45 -05:00
|
|
|
raise
|
2012-07-27 02:18:01 -05:00
|
|
|
finally: cur.close()
|
2012-03-30 10:18:40 -05:00
|
|
|
|
2010-10-29 00:00:55 -05:00
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|