original_testbot.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /usr/bin/env python
  2. #
  3. # Example program using irc.bot.
  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. irc.bot. 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. import irc.bot
  21. import irc.strings
  22. from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
  23. class TestBot(irc.bot.SingleServerIRCBot):
  24. def __init__(self, channel, nickname, server, port=6667):
  25. irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
  26. self.channel = channel
  27. def on_nicknameinuse(self, c, e):
  28. c.nick(c.get_nickname() + "_")
  29. def on_welcome(self, c, e):
  30. c.join(self.channel)
  31. def on_privmsg(self, c, e):
  32. self.do_command(e, e.arguments[0])
  33. def on_pubmsg(self, c, e):
  34. a = e.arguments[0].split(":", 1)
  35. if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
  36. self.do_command(e, a[1].strip())
  37. return
  38. def on_dccmsg(self, c, e):
  39. # non-chat DCC messages are raw bytes; decode as text
  40. text = e.arguments[0].decode('utf-8')
  41. c.privmsg("You said: " + text)
  42. def on_dccchat(self, c, e):
  43. if len(e.arguments) != 2:
  44. return
  45. args = e.arguments[1].split()
  46. if len(args) == 4:
  47. try:
  48. address = ip_numstr_to_quad(args[2])
  49. port = int(args[3])
  50. except ValueError:
  51. return
  52. self.dcc_connect(address, port)
  53. def do_command(self, e, cmd):
  54. nick = e.source.nick
  55. c = self.connection
  56. if cmd == "disconnect":
  57. self.disconnect()
  58. elif cmd == "die":
  59. self.die()
  60. elif cmd == "stats":
  61. for chname, chobj in self.channels.items():
  62. c.notice(nick, "--- Channel statistics ---")
  63. c.notice(nick, "Channel: " + chname)
  64. users = sorted(chobj.users())
  65. c.notice(nick, "Users: " + ", ".join(users))
  66. opers = sorted(chobj.opers())
  67. c.notice(nick, "Opers: " + ", ".join(opers))
  68. voiced = sorted(chobj.voiced())
  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()