dr.botzo/modules/Facts.py
Brian S. Stephan 0bd681c324 convert most modules to use ! commands.
okay, it's time. we got around for a while with all sorts of silly
config options and exceptions and common strings triggering bot
commands. but now it's time to man up and expect modules to be
more strict and less loosey-goosey.

convert the popular modules (i.e. the ones that still work) to
trigger on !pi rather than pi, etc. usually, this is achieved via
regex searches, although there are some weird bugs (ones i'm hoping
are caused by other recursion/alias bugs and not this commit).

more code around this will be gutted soon, but this, at least,
means you can't say 'tweet that shit, yo' and accidentally trigger
the bot.
2011-01-07 20:37:24 -06:00

99 lines
3.8 KiB
Python

"""
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):
"""Select a fact from the database.
Facts are categorized by a name, which may allow for random selection and so on.
"""
def db_init(self):
"""Initialize database tables."""
# 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
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""Add or retrieve a fact from the database."""
try:
db = self.get_db()
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()
return self.reply(connection, replypath, category + ' added.')
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()
if len(facts) > 0:
fact = facts[random.randint(1,len(facts))-1]
return self.reply(connection, replypath, fact['fact'].rstrip().encode('utf-8', 'ignore'))
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]
return self.reply(connection, replypath, fact['fact'].rstrip().encode('utf-8', 'ignore'))
except sqlite3.Error as e:
return self.reply(connection, replypath, "sqlite error: " + str(e))
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;