More cleanup of irc bot code, including having it cleanly disconnect from irc channel when destroyed.

This commit is contained in:
Griatch 2014-02-27 01:11:00 +01:00
parent 7bde8afe66
commit f126d30b36
3 changed files with 62 additions and 18 deletions

View file

@ -34,6 +34,7 @@ class IRCBot(irc.IRCClient, Session):
the game as a full session.
"""
self.join(self.channel)
self.stopping = False
self.factory.bot = self
self.init_session("ircbot", self.network, self.factory.sessionhandler)
# we link back to our bot and log in
@ -43,6 +44,15 @@ class IRCBot(irc.IRCClient, Session):
logger.log_infomsg("IRC bot '%s' connected to %s at %s:%s." % (self.nickname, self.channel,
self.network, self.port))
def disconnect(self, reason=None):
"""
Called by sessionhandler to disconnect this protocol
"""
print "irc disconnect called!"
self.sessionhandler.disconnect(self)
self.stopping = True
self.transport.loseConnection()
def privmsg(self, user, channel, msg):
"A message was sent to channel"
if not msg.startswith('***'):
@ -99,6 +109,13 @@ class IRCBotFactory(protocol.ReconnectingClientFactory):
"Tracks reconnections for debugging"
logger.log_infomsg("(re)connecting to %s" % self.channel)
def clientConnectionFailed(self, connector, reason):
self.retry(connector)
def clientConnectionLost(self, connector, reason):
if not self.bot.stopping:
self.retry(connector)
def start(self):
"Connect session to sessionhandler"
service = internet.TCPClient(self.network, self.port, self)