PageRenderTime 58ms CodeModel.GetById 51ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/ircbot.py

https://bitbucket.org/prologic/circuits/
Python | 98 lines | 34 code | 26 blank | 38 comment | 3 complexity | ee56338fd6fdba5583defbd77265cd4a MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""IRC Bot Example
 4
 5This example shows how to use several components in circuits as well
 6as one of the builtin networking protocols. This IRC Bot simply connects
 7to the FreeNode IRC Network and joins the #circuits channel. It will also
 8echo anything privately messages to it in response.
 9"""
10
11
12import sys
13
14
15from circuits import Component
16from circuits.net.sockets import TCPClient, connect
17from circuits.protocols.irc import IRC, PRIVMSG, USER, NICK, JOIN
18
19from circuits.protocols.irc import ERR_NICKNAMEINUSE
20from circuits.protocols.irc import RPL_ENDOFMOTD, ERR_NOMOTD
21
22
23class Bot(Component):
24
25    # Define a separate channel so we can create many instances of ``Bot``
26    channel = "ircbot"
27
28    def init(self, host="irc.freenode.net", port="6667", channel=channel):
29        self.host = host
30        self.port = int(port)
31
32        # Add TCPClient and IRC to the system.
33        TCPClient(channel=self.channel).register(self)
34        IRC(channel=self.channel).register(self)
35
36    def ready(self, component):
37        """Ready Event
38
39        This event is triggered by the underlying ``TCPClient`` Component
40        when it is ready to start making a new connection.
41        """
42
43        self.fire(connect(self.host, self.port))
44
45    def connected(self, host, port):
46        """connected Event
47
48        This event is triggered by the underlying ``TCPClient`` Component
49        when a successfully connection has been made.
50        """
51
52        self.fire(NICK("circuits"))
53        self.fire(USER("circuits", "circuits", host, "Test circuits IRC Bot"))
54
55    def disconnected(self):
56        """disconnected Event
57
58        This event is triggered by the underlying ``TCPClient`` Component
59        when the connection is lost.
60        """
61
62        raise SystemExit(0)
63
64    def numeric(self, source, numeric, *args):
65        """Numeric Event
66
67        This event is triggered by the ``IRC`` Protocol Component when we have
68        received an IRC Numberic Event from server we are connected to.
69        """
70
71        if numeric == ERR_NICKNAMEINUSE:
72            self.fire(NICK("{0:s}_".format(args[0])))
73        elif numeric in (RPL_ENDOFMOTD, ERR_NOMOTD):
74            self.fire(JOIN("#circuits"))
75
76    def privmsg(self, source, target, message):
77        """Message Event
78
79        This event is triggered by the ``IRC`` Protocol Component for each
80        message we receieve from the server.
81        """
82
83        if target.startswith("#"):
84            self.fire(PRIVMSG(target, message))
85        else:
86            self.fire(PRIVMSG(source[0], message))
87
88
89# Configure and run the system
90bot = Bot(*sys.argv[1:])
91
92from circuits import Debugger
93Debugger().register(bot)
94
95# To register a 2nd ``Bot`` instance. Simply use a separate channel.
96# Bot(*sys.argv[1:], channel="foo").register(bot)
97
98bot.run()