irc.py 1.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#encoding: utf-8
"""
A simplistic IRC client connecting to freenode network.
"""

from twisted.internet import reactor, protocol
from twisted.words.protocols import irc

class IrcClient(irc.IRCClient):
    def __init__(self, config):
        self.config = config
Mindiell's avatar
Mindiell committed
12
        self.nickname = config.nickname
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        self.password = config.password

    def connectionMade(self):
        irc.IRCClient.connectionMade(self)

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)

    def signedOn(self):
        for channel in self.config.channels:
            self.join(channel)


class IrcClientFactory(protocol.ClientFactory):
    def __init__(self, config):
        self.config = config

30
    def set_callbacks(self):
31 32 33 34 35
        pass

    def buildProtocol(self, addr):
        print("Building protocol")
        self.client = IrcClient(self.config)
36
        self.set_callbacks()
37 38 39 40 41 42 43 44 45
        return self.client

    def clientConnectionLost(self, connector, reason):
        """If we get disconnected, reconnect to server."""
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print("Connection failed: %s" % reason)
        reactor.stop()