2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Module - dr.botzo modular functionality base class
|
|
|
|
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/>.
|
|
|
|
"""
|
2010-07-28 23:47:29 -05:00
|
|
|
|
2010-08-01 11:55:14 -05:00
|
|
|
from ConfigParser import NoSectionError, NoOptionError
|
2010-10-24 11:50:12 -05:00
|
|
|
|
2010-07-29 22:36:08 -05:00
|
|
|
import inspect
|
2010-07-27 20:35:01 -05:00
|
|
|
import re
|
2010-07-29 22:36:08 -05:00
|
|
|
import sys
|
2010-10-24 09:46:41 -05:00
|
|
|
import sqlite3
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-07-30 18:34:10 -05:00
|
|
|
from extlib import irclib
|
2010-07-29 00:18:20 -05:00
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
class Module(object):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""Declare a base class used for creating classes that have real functionality."""
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2011-01-07 01:10:52 -06:00
|
|
|
def priority(self):
|
|
|
|
return 50
|
|
|
|
|
migrate some code that became pivotal to the bot into DrBotIRC.
this is a big change. DrBotIrc is now in charge of module loading
and unloading, aliases, and recursion. the Alias module is no more,
and a bunch of functionality was moved out of IrcAdmin, including
also config file saving, the sigint handler, and quitting the bot.
additionally, a lot of stuff got caught in the wake. dr.botzo.py
is simpler now, and lets DrBotIRC do the dynamic loading stuff.
Module.__init__ changed, modules no longer get modlist and instead
get a reference to the DrBotIRC object. IrcAdmin still has the same
exposed methods, but now calls out to DrBotIRC to achieve some of
them.
naturally, a recursion/alias rewrite was included with this change.
it is clearer now (i think), but probably brittle somewhere.
additionally, currently any module that has registered a pubmsg
handler can potentially fire more than once on one input (without
recursion). this may be the next thing to fix. do() may need to
be split, or maybe it's time to stop having modules deal with
pubmsg/privmsg entirely. need to decide.
WEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
2011-01-07 17:38:26 -06:00
|
|
|
def __init__(self, irc, config, server):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Construct a feature module. Inheritors can do special things here, but
|
|
|
|
should be sure to call Module.__init__.
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
|
migrate some code that became pivotal to the bot into DrBotIRC.
this is a big change. DrBotIrc is now in charge of module loading
and unloading, aliases, and recursion. the Alias module is no more,
and a bunch of functionality was moved out of IrcAdmin, including
also config file saving, the sigint handler, and quitting the bot.
additionally, a lot of stuff got caught in the wake. dr.botzo.py
is simpler now, and lets DrBotIRC do the dynamic loading stuff.
Module.__init__ changed, modules no longer get modlist and instead
get a reference to the DrBotIRC object. IrcAdmin still has the same
exposed methods, but now calls out to DrBotIRC to achieve some of
them.
naturally, a recursion/alias rewrite was included with this change.
it is clearer now (i think), but probably brittle somewhere.
additionally, currently any module that has registered a pubmsg
handler can potentially fire more than once on one input (without
recursion). this may be the next thing to fix. do() may need to
be split, or maybe it's time to stop having modules deal with
pubmsg/privmsg entirely. need to decide.
WEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
2011-01-07 17:38:26 -06:00
|
|
|
self.irc = irc
|
2010-07-27 20:35:01 -05:00
|
|
|
self.config = config
|
2010-07-29 22:36:08 -05:00
|
|
|
self.server = server
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
# open database connection
|
|
|
|
dbfile = self.config.get('dr.botzo', 'database')
|
|
|
|
self.conn = sqlite3.connect(dbfile)
|
|
|
|
self.conn.row_factory = sqlite3.Row
|
|
|
|
|
2010-10-28 23:52:06 -05:00
|
|
|
# setup regexp function in sqlite
|
|
|
|
def regexp(expr, item):
|
2010-11-01 22:12:14 -05:00
|
|
|
reg = re.compile(expr, re.IGNORECASE)
|
2010-10-29 00:30:02 -05:00
|
|
|
return reg.search(item) is not None
|
2010-10-28 23:52:06 -05:00
|
|
|
self.conn.create_function('REGEXP', 2, regexp)
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
# set up database for this module
|
|
|
|
self.db_init()
|
|
|
|
|
2010-07-29 19:50:13 -05:00
|
|
|
# print what was loaded, for debugging
|
|
|
|
print("loaded " + self.__class__.__name__)
|
|
|
|
|
2010-07-29 00:04:01 -05:00
|
|
|
def register_handlers(self, server):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Hook handler functions into the IRC library. This is called when the
|
|
|
|
module is loaded. Classes with special stuff to do could implement this
|
2010-07-30 13:34:51 -05:00
|
|
|
and set up the appropriate handlers, e.g.:
|
|
|
|
|
2011-01-07 21:04:33 -06:00
|
|
|
self.server.add_global_handler('welcome', self.on_connect)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2011-01-07 21:04:33 -06:00
|
|
|
Unless you're sure what you're doing, don't register pubmsg or privmsg.
|
|
|
|
They are handled by the alias support in DrBotIRC, and will call your
|
|
|
|
module's do().
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
|
2010-07-29 22:36:08 -05:00
|
|
|
def unregister_handlers(self):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Unhook handler functions from the IRC library. Inverse of the above.
|
2010-07-30 13:34:51 -05:00
|
|
|
This is called by reload, to remove the soon-to-be old object from the server
|
|
|
|
global handlers (or whatever has been added via register_handlers). Classes
|
2010-07-30 18:53:58 -05:00
|
|
|
inheriting from Module could implement this, e.g.:
|
2010-07-30 13:34:51 -05:00
|
|
|
|
2011-01-07 21:04:33 -06:00
|
|
|
self.server.remove_global_handler('welcome', self.on_connect)
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
2010-07-29 22:36:08 -05:00
|
|
|
|
2011-01-06 00:28:50 -06:00
|
|
|
def save(self):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Save whatever the module may need to save. Sync files, etc.
|
2011-01-06 00:28:50 -06:00
|
|
|
|
|
|
|
Implement this if you need it.
|
|
|
|
"""
|
|
|
|
|
2010-12-24 10:41:12 -06:00
|
|
|
def shutdown(self):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Do pre-deletion type cleanup.
|
2010-12-24 10:41:12 -06:00
|
|
|
|
|
|
|
Implement this to close databases, write to disk, etc.
|
|
|
|
"""
|
|
|
|
|
2010-08-01 12:13:38 -05:00
|
|
|
def remove_metaoptions(self, list):
|
|
|
|
"""Remove metaoptions from provided list, which was probably from a config file."""
|
|
|
|
|
|
|
|
list.remove('debug')
|
|
|
|
|
2010-09-04 10:45:18 -05:00
|
|
|
def retransmit_event(self, event):
|
|
|
|
"""
|
|
|
|
Pretend that some event the bot has generated is rather an incoming IRC
|
|
|
|
event. Why one would do this is unclear, but I wrote it and then realized
|
|
|
|
I didn't need it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.server._handle_event(event)
|
|
|
|
|
2010-10-24 09:46:41 -05:00
|
|
|
def get_db(self):
|
|
|
|
"""
|
|
|
|
Get a database connection to sqlite3. Once grabbed, it should be closed
|
|
|
|
when work is done. Modules that need a database connection should
|
|
|
|
test for and create (or, eventually, alter) required table structure
|
|
|
|
in their __init__ IF that structure does not already exist. Well-behaved
|
|
|
|
modules should use a prefix in their table names (eg "karma_log" rather
|
|
|
|
than "log")
|
2010-10-24 11:50:12 -05:00
|
|
|
|
|
|
|
See also db_module_registered, below.
|
2010-10-24 09:46:41 -05:00
|
|
|
"""
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
return self.conn
|
2010-10-24 09:46:41 -05:00
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
def db_module_registered(self, modulename):
|
|
|
|
"""
|
|
|
|
ask the database for a version number for a module. Return that version
|
|
|
|
number if the module has registered, or None if not
|
|
|
|
"""
|
2010-10-27 17:59:01 -05:00
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
conn = self.get_db()
|
|
|
|
version = None
|
|
|
|
try:
|
|
|
|
cur = conn.cursor()
|
2010-10-24 13:05:16 -05:00
|
|
|
cur.execute("SELECT version FROM drbotzo_modules WHERE module = :name",
|
|
|
|
{'name': modulename})
|
2010-10-24 11:50:12 -05:00
|
|
|
version = cur.fetchone()
|
|
|
|
if (version != None):
|
|
|
|
version = version[0]
|
2010-10-27 17:59:01 -05:00
|
|
|
except sqlite3.Error as e:
|
|
|
|
print("sqlite error:" + str(e))
|
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
return version
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
def db_init(self):
|
|
|
|
"""
|
|
|
|
Set up the database tables and so on, if subclass is planning on using it.
|
|
|
|
"""
|
|
|
|
|
2011-01-07 23:09:07 -06:00
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Do the primary thing this module was intended to do, in most cases.
|
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
Implement this method in your subclass to have a fairly-automatic hook into
|
2011-01-07 21:04:33 -06:00
|
|
|
IRC functionality. This is called by DrBotIRC during pubmsg and privmsg
|
|
|
|
events.
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
|
2011-01-07 21:04:33 -06:00
|
|
|
print("looks like someone forgot to implement do!")
|
2010-07-28 00:11:58 -05:00
|
|
|
|
2010-07-28 23:48:47 -05:00
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
2010-07-28 00:11:58 -05:00
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|