Ticket #1335: test_BUG_ircClient.py

File test_BUG_ircClient.py, 1.7 KB (added by gnuk, 9 years ago)
Line 
1#!/usr/bin/env python
2# -*- coding: UTF8 -*-
3
4from twisted.protocols import irc
5from twisted.internet import reactor, protocol
6from twisted.python import log
7
8import sys
9
10class LogBot(irc.IRCClient):
11    """A logging IRC bot."""
12    nickname = "ploper"
13
14    def connectionMade(self):
15        irc.IRCClient.connectionMade(self)
16   
17    def connectionLost(self, reason):
18        irc.IRCClient.connectionLost(self, reason)
19        quitmsg = 'Connexion lost.'
20
21# callbacks
22    def signedOn(self):
23        """Appeler quand le bot est connecte au server."""
24        self.join(self.factory.channel)
25
26    def action(self, user, channel, data):
27        """This will get called when the bot sees someone do an action."""
28        print "raw data from action : ",data
29   
30    def privmsg(self, user, channel, data):
31        """Message sur le canal."""
32        print "raw data from privmsg : ",data
33
34class LogBotFactory(protocol.ClientFactory):
35    """
36    A factory for LogBots.
37    A new protocol instance will be created each time we connect to the server.
38    """
39    # the class of the protocol to build when new connection is made
40    protocol = LogBot
41    def __init__(self, channel):
42        self.channel = channel
43
44    def clientConnectionLost(self, connector, reason):
45        """If we get disconnected, reconnect to server."""
46        connector.connect()
47
48    def clientConnectionFailed(self, connector, reason):
49        print "connection failed:", reason
50        reactor.stop()
51   
52if __name__ == '__main__':
53    # initialize logging
54    log.startLogging(sys.stdout)
55    # create factory protocol and application
56    BOTconnection = LogBotFactory("glop")
57    # connect factory to this host and port
58    reactor.connectTCP("irc.freenode.net", 6667, BOTconnection)
59    reactor.run() # run bot