irclib -- Internet Relay Chat (IRC) protocol client library

testbot.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #! /usr/bin/env python
  2. #
  3. # Example program using ircbot.py.
  4. #
  5. # Joel Rosdahl <joel@rosdahl.net>
  6. """A simple example bot.
  7. This is an example bot that uses the SingleServerIRCBot class from
  8. ircbot.py. The bot enters a channel and listens for commands in
  9. private messages and channel traffic. Commands in channel messages
  10. are given by prefixing the text by the bot name followed by a colon.
  11. It also responds to DCC CHAT invitations and echos data sent in such
  12. sessions.
  13. The known commands are:
  14. stats -- Prints some channel information.
  15. disconnect -- Disconnect the bot. The bot will try to reconnect
  16. after 60 seconds.
  17. die -- Let the bot cease to exist.
  18. dcc -- Let the bot invite you to a DCC CHAT connection.
  19. """
  20. from ircbot import SingleServerIRCBot
  21. from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
  22. class TestBot(SingleServerIRCBot):
  23. def __init__(self, channel, nickname, server, port=6667):
  24. SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
  25. self.channel = channel
  26. def on_nicknameinuse(self, c, e):
  27. c.nick(c.get_nickname() + "_")
  28. def on_welcome(self, c, e):
  29. c.join(self.channel)
  30. def on_privmsg(self, c, e):
  31. self.do_command(e, e.arguments()[0])
  32. def on_pubmsg(self, c, e):
  33. a = e.arguments()[0].split(":", 1)
  34. if len(a) > 1 and irc_lower(a[0]) == irc_lower(self.connection.get_nickname()):
  35. self.do_command(e, a[1].strip())
  36. return
  37. def on_dccmsg(self, c, e):
  38. c.privmsg("You said: " + e.arguments()[0])
  39. def on_dccchat(self, c, e):
  40. if len(e.arguments()) != 2:
  41. return
  42. args = e.arguments()[1].split()
  43. if len(args) == 4:
  44. try:
  45. address = ip_numstr_to_quad(args[2])
  46. port = int(args[3])
  47. except ValueError:
  48. return
  49. self.dcc_connect(address, port)
  50. def do_command(self, e, cmd):
  51. nick = nm_to_n(e.source())
  52. c = self.connection
  53. if cmd == "disconnect":
  54. self.disconnect()
  55. elif cmd == "die":
  56. self.die()
  57. elif cmd == "stats":
  58. for chname, chobj in self.channels.items():
  59. c.notice(nick, "--- Channel statistics ---")
  60. c.notice(nick, "Channel: " + chname)
  61. users = chobj.users()
  62. users.sort()
  63. c.notice(nick, "Users: " + ", ".join(users))
  64. opers = chobj.opers()
  65. opers.sort()
  66. c.notice(nick, "Opers: " + ", ".join(opers))
  67. voiced = chobj.voiced()
  68. voiced.sort()
  69. c.notice(nick, "Voiced: " + ", ".join(voiced))
  70. elif cmd == "dcc":
  71. dcc = self.dcc_listen()
  72. c.ctcp("DCC", nick, "CHAT chat %s %d" % (
  73. ip_quad_to_numstr(dcc.localaddress),
  74. dcc.localport))
  75. else:
  76. c.notice(nick, "Not understood: " + cmd)
  77. def main():
  78. import sys
  79. if len(sys.argv) != 4:
  80. print "Usage: testbot <server[:port]> <channel> <nickname>"
  81. sys.exit(1)
  82. s = sys.argv[1].split(":", 1)
  83. server = s[0]
  84. if len(s) == 2:
  85. try:
  86. port = int(s[1])
  87. except ValueError:
  88. print "Error: Erroneous port."
  89. sys.exit(1)
  90. else:
  91. port = 6667
  92. channel = sys.argv[2]
  93. nickname = sys.argv[3]
  94. bot = TestBot(channel, nickname, server, port)
  95. bot.start()
  96. if __name__ == "__main__":
  97. main()