from django.conf import settings

from ircbot import SingleServerIRCBot

class IrcMissionBot(SingleServerIRCBot):

    def __init__(self):
        SingleServerIRCBot.__init__(self, [settings.IRC_MISSION_SERVER],
            settings.IRC_MISSIONBOT_NICK, settings.IRC_MISSIONBOT_REALNAME)
        self.channel = settings.IRC_MISSION_CHANNEL

    def on_nicknameinuse(self, conn, event):
        conn.nick(conn.get_nickname() + '_')

    def on_welcome(self, conn, event):
        conn.join(self.channel)

@@ -25,16 +25,114 @@
 settings
+%0Afrom mysite.missions.models import IrcMissionSession%0Afrom mysite.missions.base import controllers
 %0A%0Afrom i
@@ -594,8 +594,1375 @@
 hannel)%0A
+        IrcMissionSession.objects.all().delete()%0A%0A    def setup_session(self, nick, conn):%0A        # Someone has joined the channel.%0A        password = controllers.make_password()%0A        IrcMissionSession(nick=nick, password=password).save()%0A        conn.notice(nick,%0A          'Hello, %25(nick)s! To start the mission, here are the words to type into the mission page: %25(password)s'%0A            %25 %7B'nick': nick, 'password': password%7D)%0A%0A    def on_join(self, conn, event):%0A        nick = event.source().split('!')%5B0%5D%0A        channel = event.target()%0A        if channel == self.channel and nick != conn.get_nickname():%0A            self.setup_session(nick, conn)%0A%0A    def on_namreply(self, conn, event):%0A        channel = event.arguments()%5B1%5D%0A        nicks = event.arguments()%5B2%5D.split()%0A        for nick in nicks:%0A            if nick%5B0%5D in '@+':%0A                nick = nick%5B1:%5D  # remove op/voice prefix%0A            self.setup_session(nick, conn)%0A%0A    def on_part(self, conn, event):%0A        nick = event.source().split('!')%5B0%5D%0A        channel = event.target()%0A        if channel == self.channel:%0A            IrcMissionSession.objects.filter(nick=nick).delete()%0A%0A    def on_kick(self, conn, event):%0A        nick = event.arguments()%5B0%5D%0A        channel = event.target()%0A        if channel == self.channel:%0A            IrcMissionSession.objects.filter(nick=nick).delete()%0A

Make the bot track nicks in the channel and maintain exactly one IrcMissionSession per nick.