/examples/ircbot.py

https://bitbucket.org/prologic/circuits/ · Python · 98 lines · 34 code · 26 blank · 38 comment · 4 complexity · ee56338fd6fdba5583defbd77265cd4a MD5 · raw file

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