Module: don't pass DrBotServerConnection to init

another "this is unnecessary" change, obviously impacting all the
modules that override __init__ as well as the base class. again, they
can use the DrBotIRC instance for anything, which is (with one
exception) only for add/remove_global_handler, which i'm planning on
working my way off of anyway
tmp/79b4e7d7e0424f8155715d7c458859a1/head
Brian S. Stephan 10 years ago
parent 3e76f75bba
commit 8d6d66333b

@ -610,7 +610,7 @@ class DrBotIRC(irclib.IRC):
modstr = 'modules.'+modname
__import__(modstr)
module = sys.modules[modstr]
botmod = eval('module.' + modname + '(self, self.config, self.server)')
botmod = eval('module.' + modname + '(self, self.config)')
self.modlist.append(botmod)
botmod.register_handlers()

@ -32,7 +32,7 @@ class Module(object):
def priority(self):
return 50
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Construct a feature module.
Inheritors can do special things here, but should be sure to call
@ -41,13 +41,11 @@ class Module(object):
Args:
irc DrBotIRC object for the running bot
config ConfigParser object, the entire config file
server DrBotServerConnection object
"""
self.irc = irc
self.config = config
self.server = server
# reload logging config every time
logging.config.fileConfig('logging.cfg')
@ -66,7 +64,7 @@ class Module(object):
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.:
self.server.add_global_handler('welcome', self.on_connect)
self.irc.server.add_global_handler('welcome', self.on_connect)
By default, a module attaches to pubmsg/privmsg, which sets up some
common variables and then calls do(). You are free to implement do()
@ -74,9 +72,9 @@ class Module(object):
"""
self.server.add_global_handler('pubmsg', self.on_pub_or_privmsg,
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg,
self.priority())
self.server.add_global_handler('privmsg', self.on_pub_or_privmsg,
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg,
self.priority())
def unregister_handlers(self):
@ -87,12 +85,12 @@ class Module(object):
been added via register_handlers). Classes inheriting from Module
could reimplement this, e.g.:
self.server.remove_global_handler('welcome', self.on_connect)
self.irc.server.remove_global_handler('welcome', self.on_connect)
"""
self.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
def on_pub_or_privmsg(self, connection, event):
"""Do a default thing on a pubmsg or privmsg.
@ -187,7 +185,7 @@ class Module(object):
"""
self.server._handle_event(event)
self.irc.server._handle_event(event)
def get_db(self):
"""Get a database connection to mdb.

@ -45,7 +45,7 @@ class Achievements(Module):
pass
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up trigger regexes."""
# TODO
@ -59,7 +59,7 @@ class Achievements(Module):
self.infore = re.compile(infopattern)
self.rankre = re.compile(rankpattern)
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
self.connection = None
self.next_achievements_scan = 0
@ -145,38 +145,38 @@ class Achievements(Module):
def register_handlers(self):
"""Handle all sorts of things to track."""
self.server.add_global_handler('pubmsg', self.track_irc_event)
self.server.add_global_handler('pubnotice', self.track_irc_event)
self.server.add_global_handler('privmsg', self.track_irc_event)
self.server.add_global_handler('privnotice', self.track_irc_event)
self.server.add_global_handler('join', self.track_irc_event)
self.server.add_global_handler('kick', self.track_irc_event)
self.server.add_global_handler('mode', self.track_irc_event)
self.server.add_global_handler('part', self.track_irc_event)
self.server.add_global_handler('quit', self.track_irc_event)
self.server.add_global_handler('invite', self.track_irc_event)
self.server.add_global_handler('action', self.track_irc_event)
self.server.add_global_handler('topic', self.track_irc_event)
self.server.add_global_handler('pubmsg', self.on_pub_or_privmsg)
self.server.add_global_handler('privmsg', self.on_pub_or_privmsg)
self.irc.server.add_global_handler('pubmsg', self.track_irc_event)
self.irc.server.add_global_handler('pubnotice', self.track_irc_event)
self.irc.server.add_global_handler('privmsg', self.track_irc_event)
self.irc.server.add_global_handler('privnotice', self.track_irc_event)
self.irc.server.add_global_handler('join', self.track_irc_event)
self.irc.server.add_global_handler('kick', self.track_irc_event)
self.irc.server.add_global_handler('mode', self.track_irc_event)
self.irc.server.add_global_handler('part', self.track_irc_event)
self.irc.server.add_global_handler('quit', self.track_irc_event)
self.irc.server.add_global_handler('invite', self.track_irc_event)
self.irc.server.add_global_handler('action', self.track_irc_event)
self.irc.server.add_global_handler('topic', self.track_irc_event)
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg)
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg)
def unregister_handlers(self):
self.server.remove_global_handler('pubmsg', self.track_irc_event)
self.server.remove_global_handler('pubnotice', self.track_irc_event)
self.server.remove_global_handler('privmsg', self.track_irc_event)
self.server.remove_global_handler('privnotice', self.track_irc_event)
self.server.remove_global_handler('join', self.track_irc_event)
self.server.remove_global_handler('kick', self.track_irc_event)
self.server.remove_global_handler('mode', self.track_irc_event)
self.server.remove_global_handler('part', self.track_irc_event)
self.server.remove_global_handler('quit', self.track_irc_event)
self.server.remove_global_handler('invite', self.track_irc_event)
self.server.remove_global_handler('action', self.track_irc_event)
self.server.remove_global_handler('topic', self.track_irc_event)
self.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('pubmsg', self.track_irc_event)
self.irc.server.remove_global_handler('pubnotice', self.track_irc_event)
self.irc.server.remove_global_handler('privmsg', self.track_irc_event)
self.irc.server.remove_global_handler('privnotice', self.track_irc_event)
self.irc.server.remove_global_handler('join', self.track_irc_event)
self.irc.server.remove_global_handler('kick', self.track_irc_event)
self.irc.server.remove_global_handler('mode', self.track_irc_event)
self.irc.server.remove_global_handler('part', self.track_irc_event)
self.irc.server.remove_global_handler('quit', self.track_irc_event)
self.irc.server.remove_global_handler('invite', self.track_irc_event)
self.irc.server.remove_global_handler('action', self.track_irc_event)
self.irc.server.remove_global_handler('topic', self.track_irc_event)
self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
def track_irc_event(self, connection, event):
"""Put events in the database."""

@ -65,7 +65,7 @@ class Acro(Module):
acronyms.
"""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up the game tracking and such."""
# build regexes
@ -85,7 +85,7 @@ class Acro(Module):
# game state
self.game = self.AcroGame()
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
def do(self, connection, event, nick, userhost, what, admin_unlocked):
"""Handle commands and inputs."""

@ -32,10 +32,10 @@ class Dice(Module):
tokens = ['NUMBER', 'TEXT', 'ROLLSEP']
literals = ['#', '/', '+', '-', 'd']
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up XML-RPC methods."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
irc.xmlrpc_register_function(self.do_roll, "dice_roll")

@ -25,10 +25,10 @@ class EightBall(Module):
"""Return a random answer when asked a question."""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Initialize the list of self.responses."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
self.responses = []

@ -30,10 +30,10 @@ class Facts(Module):
Facts are categorized by a name, which may allow for random selection and so on.
"""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up XML-RPC methods."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
irc.xmlrpc_register_function(self._get_fact, "facts_get")

@ -25,12 +25,12 @@ __date__ = "2011-01-08"
class Help(Module):
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""
Upon creation, determine the save file location
"""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
self.helpre = re.compile('^!help(\s+(\S+)(\s+(.+))?)?\s*$')

@ -27,14 +27,14 @@ class IrcAdmin(Module):
"""Support miscellaneous IRC stuff --- joining channels, changing the nick, etc."""
def register_handlers(self):
self.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, self.priority())
self.server.add_global_handler('privmsg', self.on_pub_or_privmsg, self.priority())
self.server.add_global_handler('welcome', self.on_connect, self.priority())
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, self.priority())
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg, self.priority())
self.irc.server.add_global_handler('welcome', self.on_connect, self.priority())
def unregister_handlers(self):
self.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('welcome', self.on_connect)
self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('welcome', self.on_connect)
def on_connect(self, connection, event):
"""Set up handlers when the bot has connected to IRC."""

@ -28,12 +28,12 @@ __date__ = "$Oct 23, 2010 11:12:33 AM$"
class Karma(Module):
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""
Upon creation, determine the save file location
"""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
pattern = "(?:\((.+?)\)|(\S+))"
karmapattern = pattern + '(\+\+|--|\+-|-\+)' + '(\s+|$)'

@ -41,7 +41,7 @@ class Markov(Module):
http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/
"""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Create the Markov chainer, and learn text from a file if available."""
# set up some keywords for use in the chains --- don't change these
@ -60,7 +60,7 @@ class Markov(Module):
self.shut_up = False
self.lines_seen = []
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
self.next_shut_up_check = 0
self.next_chatter_check = 0
@ -129,16 +129,16 @@ class Markov(Module):
def register_handlers(self):
"""Handle pubmsg/privmsg, to learn and/or reply to IRC events."""
self.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, self.priority())
self.server.add_global_handler('privmsg', self.on_pub_or_privmsg, self.priority())
self.server.add_global_handler('pubmsg', self.learn_from_irc_event)
self.server.add_global_handler('privmsg', self.learn_from_irc_event)
self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, self.priority())
self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg, self.priority())
self.irc.server.add_global_handler('pubmsg', self.learn_from_irc_event)
self.irc.server.add_global_handler('privmsg', self.learn_from_irc_event)
def unregister_handlers(self):
self.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.server.remove_global_handler('pubmsg', self.learn_from_irc_event)
self.server.remove_global_handler('privmsg', self.learn_from_irc_event)
self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)
self.irc.server.remove_global_handler('pubmsg', self.learn_from_irc_event)
self.irc.server.remove_global_handler('privmsg', self.learn_from_irc_event)
def learn_from_irc_event(self, connection, event):
"""Learn from IRC events."""

@ -46,7 +46,7 @@ class Radio(Module):
connection is necessary or not.
"""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up the usual IRC regex-type stuff."""
# set up regexes, for replying to specific stuff
@ -67,7 +67,7 @@ class Radio(Module):
except (NoSectionError, NoOptionError):
pass
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
def do(self, connection, event, nick, userhost, what, admin_unlocked):
"""Handle commands and inputs from IRC events."""

@ -69,10 +69,10 @@ class Storycraft(Module):
pass
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up trigger regexes."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
rootcommand = '^!storycraft'
statuspattern = rootcommand + '\s+status$'

@ -32,10 +32,10 @@ class Twitter(Module):
"""Access Twitter via the bot as an authenticated client."""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Prepare for oauth stuff (but don't execute it yet)."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
# setup regexes
getstatuspattern = "^!twitter\s+getstatus(\s+nosource)?(\s+noid)?\s+(\S+)$"

@ -27,10 +27,10 @@ from Module import Module
class Weather(Module):
"""Provide weather lookup services to the bot."""
def __init__(self, irc, config, server):
def __init__(self, irc, config):
"""Set up regexes and wunderground API."""
Module.__init__(self, irc, config, server)
Module.__init__(self, irc, config)
self.weatherre = re.compile("^!weather\s+(.*)$")

Loading…
Cancel
Save