Module class, convenience for implementing new features. meant to be inherited

This commit is contained in:
Brian S. Stephan 2010-07-25 20:09:02 -05:00
parent 9f6ae529fe
commit c25af51088
1 changed files with 87 additions and 0 deletions

View File

@ -12,6 +12,90 @@ from dateutil.relativedelta import *
from dateutil.tz import *
import irclib
class Module:
"""Base class used for creating classes that have real functionality.
"""
def __init__(self, config, server):
"""Constructor for 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.
"""
self.config = config
self.register_handlers(server)
def register_handlers(self, server):
"""This is called by __init__ and sets up server.add_global_handlers. Classes
inheriting from Module should implement this and set up the appropriate handlers,
e.g.:
server.add_global_handler('privmsg', self.on_privmsg)
Module.on_pubmsg and Module.on_privmsg are defined so far, the rest, you're on your
own.
"""
print "looks like someone forgot to implement register_handlers!"
server.add_global_handler('pubmsg', self.on_pubmsg)
server.add_global_handler('privmsg', self.on_privmsg)
def on_pubmsg(self, connection, event):
"""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.
"""
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:
if userhost == self.config.get('admin', 'userhost'):
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)
if not addressed_re.match(what):
return
else:
what = addressed_re.sub('', what)
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def on_privmsg(self, connection, event):
"""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.
"""
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = nick
what = event.arguments()[0]
admin_unlocked = False
try:
if userhost == self.config.get('admin', 'userhost'):
admin_unlocked = True
except NoOptionError: pass
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""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
"""
print "looks like someone forgot to implement do!"
#####
# sub_join_channel
# join a channel when told to by an admin
@ -382,6 +466,9 @@ server.add_global_handler("welcome", on_connect)
server.add_global_handler('privmsg', on_privmsg)
server.add_global_handler('pubmsg', on_pubmsg)
# test
module = Module(config, server)
# loop forever
irc.process_forever()