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
|
|
|
|
2010-07-29 22:36:08 -05:00
|
|
|
def __init__(self, config, server, modlist):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Construct a feature module. Inheritors should not do anything special
|
|
|
|
here, instead they should implement register_handlers and do, or else this will
|
|
|
|
be a very uneventful affair.
|
|
|
|
|
|
|
|
Classes that are interested in allowing an indirect call to their do routine
|
|
|
|
should add themselves to modlist inside their __init__. This will allow other
|
|
|
|
modules to call do and see if anything can handle text they may have seen (such
|
|
|
|
as in recursive commands).
|
|
|
|
"""
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
self.config = config
|
2010-07-29 22:36:08 -05:00
|
|
|
self.server = server
|
2010-07-27 20:35:01 -05:00
|
|
|
self.modlist = modlist
|
2010-07-29 22:36:08 -05:00
|
|
|
|
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 22:36:08 -05:00
|
|
|
# add self to the object list
|
|
|
|
modlist.append(self)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
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):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Hook handler functions into the IRC library. This is called by __init__ and sets
|
2010-07-30 18:54:57 -05:00
|
|
|
up server.add_global_handlers. Classes inheriting from Module could implement this
|
2010-07-30 13:34:51 -05:00
|
|
|
and set up the appropriate handlers, e.g.:
|
|
|
|
|
|
|
|
server.add_global_handler('privmsg', self.on_privmsg)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
|
|
|
|
own.
|
|
|
|
"""
|
|
|
|
|
2010-07-30 18:50:56 -05:00
|
|
|
server.add_global_handler('pubmsg', self.on_pubmsg)
|
|
|
|
server.add_global_handler('privmsg', self.on_privmsg)
|
2010-07-29 22:36:08 -05:00
|
|
|
|
|
|
|
def unregister_handlers(self):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Unhook handler functions from the IRC library. Inverse of the above.
|
|
|
|
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
|
|
|
|
|
|
|
server.remove_global_handler('privmsg', self.on_privmsg)
|
|
|
|
"""
|
2010-07-29 22:36:08 -05:00
|
|
|
|
2010-07-30 18:53:58 -05:00
|
|
|
self.server.remove_global_handler('pubmsg', self.on_pubmsg)
|
|
|
|
self.server.remove_global_handler('privmsg', self.on_privmsg)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-12-24 10:41:12 -06:00
|
|
|
def shutdown(self):
|
|
|
|
"""
|
|
|
|
Do pre-deletion type cleanup.
|
|
|
|
|
|
|
|
Implement this to close databases, write to disk, etc.
|
|
|
|
"""
|
|
|
|
|
2010-07-29 00:04:01 -05:00
|
|
|
def on_pubmsg(self, connection, event):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Handle pubmsg events. Does some variable setup and initial sanity checking before
|
|
|
|
calling Module.do, which should be implemented by subclasses and what can be
|
|
|
|
ultimately responsible for the work.
|
|
|
|
|
|
|
|
Of course, you are free to reimplement on_pubmsg on your own too.
|
|
|
|
"""
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
replypath = event.target()
|
|
|
|
what = event.arguments()[0]
|
|
|
|
|
|
|
|
admin_unlocked = False
|
|
|
|
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
if userhost == self.config.get('dr.botzo', 'admin_userhost'):
|
2010-07-27 20:35:01 -05:00
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
|
|
|
# only do commands if the bot has been addressed directly
|
|
|
|
addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+'
|
|
|
|
addressed_re = re.compile(addressed_pattern)
|
|
|
|
|
2010-12-16 21:06:20 -06:00
|
|
|
# see if we should just skip msg stuff entirely (common for stuff run via alias)
|
|
|
|
internal_only = True
|
|
|
|
try:
|
|
|
|
internal_only = self.config.getboolean(self.__class__.__name__, 'meta.internal_only')
|
|
|
|
except NoOptionError: pass
|
|
|
|
except NoSectionError: pass
|
|
|
|
if internal_only and replypath is not None:
|
|
|
|
return
|
|
|
|
|
2010-08-01 11:55:14 -05:00
|
|
|
need_prefix = True
|
|
|
|
try:
|
|
|
|
need_prefix = self.config.getboolean(self.__class__.__name__, 'meta.pubmsg_needs_bot_prefix')
|
|
|
|
except NoOptionError: pass
|
|
|
|
except NoSectionError: pass
|
|
|
|
|
2010-09-08 19:32:06 -05:00
|
|
|
ignore_prefix = False
|
|
|
|
try:
|
|
|
|
ignore_prefix = self.config.getboolean(self.__class__.__name__, 'meta.pubmsg_ignore_bot_prefix')
|
|
|
|
except NoOptionError: pass
|
|
|
|
except NoSectionError: pass
|
|
|
|
|
2010-09-08 19:49:56 -05:00
|
|
|
strip_bot_name_from_input = True
|
|
|
|
try:
|
|
|
|
strip_bot_name_from_input = self.config.getboolean(self.__class__.__name__, 'meta.strip_bot_name_from_input')
|
|
|
|
except NoOptionError: pass
|
|
|
|
except NoSectionError: pass
|
|
|
|
|
2010-08-01 11:55:14 -05:00
|
|
|
if not addressed_re.match(what) and need_prefix:
|
2010-07-27 20:35:01 -05:00
|
|
|
return
|
2010-09-08 19:32:06 -05:00
|
|
|
elif addressed_re.match(what) and ignore_prefix:
|
|
|
|
return
|
2010-09-08 19:49:56 -05:00
|
|
|
elif strip_bot_name_from_input:
|
2010-07-27 20:35:01 -05:00
|
|
|
what = addressed_re.sub('', what)
|
2010-07-28 13:25:49 -05:00
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
|
2010-07-29 00:04:01 -05:00
|
|
|
def on_privmsg(self, connection, event):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Handle privmsg events. Does some variable setup and initial sanity checking before
|
|
|
|
calling Module.do, which should be implemented by subclasses and what can be
|
|
|
|
ultimately responsible for the work.
|
|
|
|
|
|
|
|
Of course, you are free to reimplement on_privmsg on your own too.
|
|
|
|
"""
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
replypath = nick
|
|
|
|
what = event.arguments()[0]
|
|
|
|
|
|
|
|
admin_unlocked = False
|
|
|
|
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
if userhost == self.config.get('dr.botzo', 'admin_userhost'):
|
2010-07-27 20:35:01 -05:00
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
2010-12-16 21:06:20 -06:00
|
|
|
# see if we should just skip msg stuff entirely (common for stuff run via alias)
|
|
|
|
internal_only = True
|
|
|
|
try:
|
|
|
|
internal_only = self.config.getboolean(self.__class__.__name__, 'meta.internal_only')
|
|
|
|
except NoOptionError: pass
|
|
|
|
except NoSectionError: pass
|
|
|
|
if internal_only and replypath is not None:
|
|
|
|
return
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
|
|
|
|
|
2010-12-15 20:43:14 -06:00
|
|
|
def reload(self):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""Reload this module's code and then create a new object of it, removing the old."""
|
|
|
|
|
2010-12-15 20:43:14 -06:00
|
|
|
# re-read and re-compile module from source on disk
|
|
|
|
reload(sys.modules[self.__module__])
|
2010-07-30 06:56:37 -05:00
|
|
|
|
2010-12-15 20:43:14 -06:00
|
|
|
# find the class declaration for the current module
|
|
|
|
for name, obj in inspect.getmembers(sys.modules[self.__module__]):
|
|
|
|
if inspect.isclass(obj) and str(obj).find(self.__module__) > 0:
|
|
|
|
# remove existing object from in-memory stuff
|
|
|
|
self.modlist.remove(self)
|
|
|
|
self.unregister_handlers()
|
2010-07-30 06:56:37 -05:00
|
|
|
|
2010-12-15 20:43:14 -06:00
|
|
|
# create new object, like how we did initially
|
|
|
|
obj(self.config, self.server, self.modlist)
|
2010-12-15 20:55:04 -06:00
|
|
|
self.register_handlers(self.server)
|
2010-07-29 22:36:08 -05:00
|
|
|
|
2010-07-30 00:34:57 -05:00
|
|
|
def reply(self, connection, replypath, replystr):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Reply over IRC to replypath or return a string with the reply.
|
|
|
|
Utility method to do the proper type of reply (either to IRC, or as a return
|
|
|
|
to caller) depending on the target. Pretty simple, and included in the base
|
|
|
|
class for convenience. It should be the last step for callers:
|
|
|
|
|
|
|
|
return self.reply(connection, replypath, 'hello')
|
|
|
|
"""
|
|
|
|
|
2010-12-08 22:18:46 -06:00
|
|
|
if replystr is not None:
|
|
|
|
if replypath is None:
|
|
|
|
return replystr
|
|
|
|
else:
|
|
|
|
connection.privmsg(replypath, replystr)
|
2010-07-30 00:34:57 -05:00
|
|
|
|
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')
|
|
|
|
# if this option has been provided, don't print it
|
|
|
|
try:
|
|
|
|
self.config.get(self.__class__.__name__, 'meta.pubmsg_needs_bot_prefix')
|
2010-08-01 12:19:15 -05:00
|
|
|
list.remove('meta.pubmsg_needs_bot_prefix')
|
2010-08-01 12:13:38 -05:00
|
|
|
except NoOptionError: pass
|
2010-09-08 19:58:29 -05:00
|
|
|
try:
|
|
|
|
self.config.get(self.__class__.__name__, 'meta.pubmsg_ignore_bot_prefix')
|
|
|
|
list.remove('meta.pubmsg_ignore_bot_prefix')
|
|
|
|
except NoOptionError: pass
|
|
|
|
try:
|
|
|
|
self.config.get(self.__class__.__name__, 'meta.strip_bot_name_from_input')
|
|
|
|
list.remove('meta.strip_bot_name_from_input')
|
|
|
|
except NoOptionError: pass
|
2010-08-01 12:13:38 -05:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2010-07-29 00:04:01 -05:00
|
|
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
Do the primary thing this module was intended to do.
|
|
|
|
Implement this method in your subclass to have a fairly-automatic hook into
|
|
|
|
IRC functionality. This is called by the default on_pubmsg and on_privmsg
|
|
|
|
"""
|
|
|
|
|
2010-07-27 20:35:01 -05: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;
|