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/>.
|
2012-12-19 19:53:41 -06:00
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
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
|
|
|
|
2012-07-15 21:32:12 -05:00
|
|
|
import logging
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
import MySQLdb as mdb
|
|
|
|
|
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):
|
2012-12-19 19:53:41 -06:00
|
|
|
|
|
|
|
"""Provide module base class with convenience functionality/bootstrap."""
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2011-01-07 01:10:52 -06:00
|
|
|
def priority(self):
|
|
|
|
return 50
|
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
def __init__(self, irc, config):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Construct a feature module.
|
|
|
|
|
|
|
|
Inheritors can do special things here, but should be sure to call
|
|
|
|
Module.__init__.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
irc DrBotIRC object for the running bot
|
|
|
|
config ConfigParser object, the entire config file
|
|
|
|
|
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
|
2012-07-28 09:47:30 -05:00
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
# reload logging config every time
|
2012-07-28 09:47:30 -05:00
|
|
|
logging.config.fileConfig('logging.cfg')
|
2012-07-26 20:28:17 -05:00
|
|
|
self.log = logging.getLogger('drbotzo.'+self.__class__.__name__.lower())
|
2010-07-29 22:36:08 -05:00
|
|
|
|
2011-04-23 12:07:30 -05:00
|
|
|
self.is_shutdown = False
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
# set up database for this module
|
|
|
|
self.db_init()
|
|
|
|
|
2013-01-04 10:13:57 -06:00
|
|
|
# set up XML-RPC for this module
|
|
|
|
self.xmlrpc_init()
|
|
|
|
|
2012-07-26 20:09:57 -05:00
|
|
|
self.log.info("Loaded " + self.__class__.__name__)
|
2010-07-29 19:50:13 -05:00
|
|
|
|
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
|
|
|
def register_handlers(self):
|
2012-12-19 19:53:41 -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 and set up the appropriate handlers, e.g.:
|
2010-07-30 13:34:51 -05:00
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server.add_global_handler('welcome', self.on_connect)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
By default, a module attaches to pubmsg/privmsg, which sets up some
|
|
|
|
common variables and then calls do(). You are free to implement do()
|
|
|
|
(see below), or override this and do whatever you want.
|
|
|
|
|
2012-12-20 10:41:31 -06:00
|
|
|
Modules interested in doing so might also register XML-RPC stuff here.
|
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg,
|
2012-12-19 19:53:41 -06:00
|
|
|
self.priority())
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg,
|
2012-12-19 19:53:41 -06:00
|
|
|
self.priority())
|
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
|
|
|
|
2010-07-29 22:36:08 -05:00
|
|
|
def unregister_handlers(self):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Unhook handler functions from the IRC library.
|
|
|
|
|
|
|
|
Inverse of the above. This is called by unload, to remove the
|
|
|
|
soon-to-be old object from the server global handlers (or whatever has
|
|
|
|
been added via register_handlers). Classes inheriting from Module
|
|
|
|
could reimplement this, e.g.:
|
2010-07-30 13:34:51 -05:00
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server.remove_global_handler('welcome', self.on_connect)
|
2012-12-19 19:53:41 -06:00
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
2010-07-29 22:36:08 -05:00
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
|
|
|
|
self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
|
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
|
|
|
|
|
|
|
def on_pub_or_privmsg(self, connection, event):
|
|
|
|
"""Do a default thing on a pubmsg or privmsg.
|
|
|
|
|
|
|
|
Sets up a couple variables and then calls do(), which by default we
|
|
|
|
expect implementers to implement.
|
2012-12-19 19:53:41 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
connection the source connection for this event
|
|
|
|
event the event to handle
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The results of handling the event.
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
nick = irclib.nm_to_n(event.source())
|
|
|
|
userhost = irclib.nm_to_uh(event.source())
|
|
|
|
what = event.arguments()[0]
|
|
|
|
admin_unlocked = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
if userhost == self.config.get('dr.botzo', 'admin_userhost'):
|
|
|
|
admin_unlocked = True
|
|
|
|
except NoOptionError: pass
|
|
|
|
|
|
|
|
return self.do(connection, event, nick, userhost, what, admin_unlocked)
|
|
|
|
|
2013-02-09 15:11:38 -06:00
|
|
|
def sendmsg(self, target, msg):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Send a privmsg over IRC to target.
|
|
|
|
|
2013-01-04 10:14:37 -06:00
|
|
|
Args:
|
|
|
|
target destination on the network
|
|
|
|
msg the message to send
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if msg is not None:
|
|
|
|
if target is not None:
|
|
|
|
msgs = msg.split('\n')
|
|
|
|
for line in msgs:
|
|
|
|
self.irc.server.privmsg(target, line)
|
|
|
|
|
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.
|
2012-12-19 19:53:41 -06:00
|
|
|
|
2011-01-06 00:28:50 -06:00
|
|
|
"""
|
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
pass
|
|
|
|
|
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
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
Implement this to close databases, write to disk, etc. Note that
|
|
|
|
DrBotIRC will call save() before this, so implement appropriately.
|
|
|
|
|
2010-12-24 10:41:12 -06:00
|
|
|
"""
|
|
|
|
|
2011-04-23 12:07:30 -05:00
|
|
|
self.is_shutdown = True
|
2012-07-26 20:09:57 -05:00
|
|
|
self.log.info("Unloading " + self.__class__.__name__)
|
2011-04-23 12:07:30 -05:00
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
def remove_metaoptions(self, list_):
|
|
|
|
"""Remove metaoptions from provided list, which was probably from a
|
|
|
|
config file.
|
2010-08-01 12:13:38 -05:00
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
DEPRECATED
|
|
|
|
|
|
|
|
This is a convenience method that can go away once we get module stuff
|
|
|
|
out of the config file and into the database.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
list_ the list to prune known keywords from
|
2010-08-01 12:13:38 -05:00
|
|
|
|
2010-09-04 10:45:18 -05:00
|
|
|
"""
|
2012-12-19 19:53:41 -06:00
|
|
|
|
|
|
|
list_.remove('debug')
|
|
|
|
|
|
|
|
def retransmit_event(self, event):
|
|
|
|
"""Pretend that some event the bot has generated is rather an incoming
|
|
|
|
IRC event.
|
|
|
|
|
|
|
|
DEPRECATED
|
|
|
|
|
|
|
|
Why one would do this is unclear, but I wrote it and then realized
|
2010-09-04 10:45:18 -05:00
|
|
|
I didn't need it.
|
2012-12-19 19:53:41 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
event the event to replay
|
|
|
|
|
2010-09-04 10:45:18 -05:00
|
|
|
"""
|
|
|
|
|
2012-12-19 21:06:53 -06:00
|
|
|
self.irc.server._handle_event(event)
|
2010-09-04 10:45:18 -05:00
|
|
|
|
2010-10-24 09:46:41 -05:00
|
|
|
def get_db(self):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Get a database connection to mdb.
|
|
|
|
|
|
|
|
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.
|
2012-12-19 19:53:41 -06:00
|
|
|
|
2010-10-24 09:46:41 -05:00
|
|
|
"""
|
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
dbhost = self.config.get('dr.botzo', 'dbhost')
|
|
|
|
dbuser = self.config.get('dr.botzo', 'dbuser')
|
|
|
|
dbpass = self.config.get('dr.botzo', 'dbpass')
|
|
|
|
dbname = self.config.get('dr.botzo', 'dbname')
|
2013-05-03 15:38:46 -05:00
|
|
|
db = mdb.connect(dbhost, dbuser, dbpass, dbname, charset='utf8',
|
|
|
|
use_unicode=True)
|
2011-06-21 17:22:24 -05:00
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
return db
|
2010-10-24 09:46:41 -05:00
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
def db_module_registered(self, modulename):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Ask the database for a version number for a module.
|
|
|
|
|
|
|
|
Return that version number if the module has registered, or None if
|
|
|
|
not.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
modulename the name of the module to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The version number stored for the module in the database, as a int
|
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
"""
|
2010-10-27 17:59:01 -05:00
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
db = self.get_db()
|
2010-10-24 11:50:12 -05:00
|
|
|
version = None
|
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur = db.cursor()
|
|
|
|
cur.execute("SELECT version FROM drbotzo_modules WHERE module = %s",
|
|
|
|
(modulename,))
|
2010-10-24 11:50:12 -05:00
|
|
|
version = cur.fetchone()
|
|
|
|
if (version != None):
|
|
|
|
version = version[0]
|
2012-07-27 02:18:01 -05:00
|
|
|
except mdb.Error as e:
|
|
|
|
self.log.error("database error during module registered check")
|
|
|
|
self.log.exception(e)
|
|
|
|
raise
|
|
|
|
finally: cur.close()
|
2010-10-27 17:59:01 -05:00
|
|
|
|
2010-10-24 11:50:12 -05:00
|
|
|
return version
|
|
|
|
|
2011-05-01 09:29:09 -05:00
|
|
|
def db_register_module_version(self, modulename, version):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Enter the given module name and version number into the database.
|
|
|
|
|
|
|
|
The primary want for this is to record what the current revision the
|
|
|
|
tables are, to do upgrades.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
modulename the name of the module to update
|
|
|
|
version the version number to set as a int
|
|
|
|
|
|
|
|
"""
|
2011-05-01 09:29:09 -05:00
|
|
|
|
|
|
|
db = self.get_db()
|
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
|
|
|
cur.execute('INSERT IGNORE INTO drbotzo_modules (version, module) VALUES (%s, %s)',
|
|
|
|
(version, modulename))
|
2011-05-01 09:29:09 -05:00
|
|
|
db.commit()
|
2012-07-27 02:18:01 -05:00
|
|
|
except mdb.Error as e:
|
2011-05-01 09:29:09 -05:00
|
|
|
db.rollback()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.log.error("database error during register module version")
|
|
|
|
self.log.exception(e)
|
2011-05-01 09:29:09 -05:00
|
|
|
raise
|
2012-07-27 02:18:01 -05:00
|
|
|
finally: cur.close()
|
2011-05-01 09:29:09 -05:00
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
def db_init(self):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Set up the database tables and so on.
|
|
|
|
|
|
|
|
Modules interested in this should implement table setup here.
|
|
|
|
|
2010-10-27 17:59:01 -05:00
|
|
|
"""
|
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
pass
|
|
|
|
|
2013-01-04 10:13:57 -06:00
|
|
|
def xmlrpc_init(self):
|
|
|
|
"""Set up XML-RPC handlers.
|
|
|
|
|
|
|
|
Modules interested in this should implement registration here.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
def do(self, connection, event, nick, userhost, what, admin):
|
2011-01-07 21:04:33 -06:00
|
|
|
"""Do the primary thing this module was intended to do, in most cases.
|
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
Implement this method in your subclass to have a fairly-automatic hook
|
|
|
|
into IRC functionality. This is called by DrBotIRC during pubmsg and
|
|
|
|
privmsg events.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
connection the connection instance to handle
|
|
|
|
event the event to handle
|
|
|
|
nick the nick of the originator of the event
|
|
|
|
userhost the userhost of the originator of the event
|
|
|
|
what the message body of the event
|
|
|
|
admin if the event was created by an admin
|
|
|
|
|
2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
|
2012-12-19 19:53:41 -06:00
|
|
|
pass
|
2010-07-28 00:11:58 -05:00
|
|
|
|
2011-01-26 20:28:34 -06:00
|
|
|
def _unencode_xml(self, text):
|
2012-12-19 19:53:41 -06:00
|
|
|
"""Convert <, >, & to their real entities.
|
|
|
|
|
|
|
|
Convenience method for modules, I've been thinking about moving this.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text the text to clean up
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The provided text string, with the known entities replaced.
|
|
|
|
|
|
|
|
"""
|
2011-01-26 20:28:34 -06:00
|
|
|
|
|
|
|
text = text.replace('<', '<')
|
|
|
|
text = text.replace('>', '>')
|
|
|
|
text = text.replace('&', '&')
|
|
|
|
return text
|
|
|
|
|
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;
|