irc.py 1.34 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        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

    def set_privmsg(self):
        pass

    def buildProtocol(self, addr):
        print("Building protocol")
        self.client = IrcClient(self.config)
        self.set_privmsg()
        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()


if __name__ == '__main__':
    factory = IrcClientFactory({'nickname':'','password':''})
    reactor.connectTCP("irc.freenode.net", 6667, factory)
    reactor.run()