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
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
from extlib import irclib
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
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:
|
|
|
|
db.execute('''
|
|
|
|
CREATE TABLE facts_facts (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
category TEXT NOT NULL,
|
|
|
|
fact TEXT NOT NULL,
|
|
|
|
who TEXT NOT NULL,
|
|
|
|
userhost TEXT NOT NULL,
|
|
|
|
time TEXT DEFAULT CURRENT_TIMESTAMP
|
|
|
|
)
|
|
|
|
''')
|
|
|
|
db.execute('INSERT INTO drbotzo_modules VALUES (?,?)', (self.__class__.__name__, 1))
|
|
|
|
db.commit()
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
db.rollback()
|
|
|
|
print("sqlite error: " + str(e))
|
|
|
|
raise
|
|
|
|
|
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."""
|
|
|
|
|
2011-01-07 20:37:24 -06:00
|
|
|
try:
|
2010-10-29 00:00:55 -05:00
|
|
|
db = self.get_db()
|
2011-01-07 20:37:24 -06:00
|
|
|
cur = db.cursor()
|
|
|
|
|
|
|
|
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)
|
|
|
|
VALUES (?, ?, ?, ?)''', (category, fact, nick, userhost))
|
|
|
|
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)
|
|
|
|
category_facts = cur.execute('SELECT * FROM facts_facts WHERE category=? AND fact REGEXP ?', (category, regex))
|
|
|
|
facts = category_facts.fetchall()
|
2010-10-29 00:00:55 -05:00
|
|
|
if len(facts) > 0:
|
|
|
|
fact = facts[random.randint(1,len(facts))-1]
|
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, fact['fact'].rstrip().encode('utf-8', 'ignore'))
|
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)
|
|
|
|
category_facts = cur.execute('SELECT * FROM facts_facts WHERE category=?', (category,))
|
|
|
|
facts = category_facts.fetchall()
|
|
|
|
if len(facts) > 0:
|
|
|
|
fact = facts[random.randint(1,len(facts))-1]
|
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, fact['fact'].rstrip().encode('utf-8', 'ignore'))
|
2010-10-29 00:00:55 -05:00
|
|
|
|
2011-01-07 20:37:24 -06:00
|
|
|
except sqlite3.Error as e:
|
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, "sqlite error: " + str(e))
|
2010-10-29 00:00:55 -05:00
|
|
|
|
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|