diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-24 21:54:36 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-24 21:54:36 +0200 |
commit | 751ea18be99b24e5da1fb350c8eecc356ae09c32 (patch) | |
tree | 1ca2acbf852a112d9b31a2712005f017413f2fac | |
parent | 257ab677963bf235b617e984ade0b97df8c5af63 (diff) |
Added config to modules, and an optional stop method to Module classes.
-rwxr-xr-x | fot.py | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -4,7 +4,6 @@ from twisted.words.protocols import irc from twisted.internet import reactor, protocol from twisted.python import log from twisted.protocols.basic import LineReceiver -from twisted.manhole.telnet import Shell import os @@ -33,6 +32,7 @@ def load_modules(): else: modules[m] = __import__('modules.' + m, globals(), locals(), ['Module']) mod = modules[m] + mod.config = config print '%s by %s' % (mod.info['title'], mod.info['author']) del mod except: @@ -55,6 +55,7 @@ def refresh_modules(): class Bot(irc.IRCClient): def __init__(self): bots.append(self) + self.modules = {} def __del__(self): bots.remove(self) @@ -63,6 +64,10 @@ class Bot(irc.IRCClient): return '<Bot %s@%s>' % (self.nickname, self.factory.server) def apply_modules(self): + # Call stop() for each module if neccessary (can't rely on __del__ here) + for m in self.modules.itervalues(): + if hasattr(m, 'stop'): + m.stop() self.modules = {} for m in modules.keys(): if config.has_option(self.factory.server, m) and config.get(self.factory.server, m).strip(): @@ -85,23 +90,34 @@ class Bot(irc.IRCClient): def kickedFrom(self, channel, kicker, message): self.join(channel) + def connectionLost(self, reason): + for m in self.modules.itervalues(): + if hasattr(m, 'stop'): + m.stop() + self.modules = {} + irc.IRCClient.connectionLost(self, reason) + class BotFactory(protocol.ReconnectingClientFactory): protocol = Bot def __init__(self, server, nickname): self.server = server self.nickname = nickname + self.maxDelay = 120 def buildProtocol(self, addr): self.resetDelay() return protocol.ReconnectingClientFactory.buildProtocol(self, addr) + def startedConnecting(self, connector): + print 'Connecting to', connector.host + def clientConnectionFailed(self, connector, reason): - print 'Connection failed: ', reason + print 'Connection failed:', reason protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) def clientConnectionLost(self, connector, reason): - print 'Connection lost: ', reason + print 'Connection lost:', reason protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason) print 'Starting per-network instances...' |