rotbot.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.bot import Channel
  23. from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
  24. class RotBot(irc.bot.SingleServerIRCBot):
  25. def __init__(self, channel, nickname, server, port=6667):
  26. print("Connecting")
  27. irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
  28. print("Joining channel")
  29. self.channel = channel
  30. def on_nicknameinuse(self, c, e):
  31. c.nick(c.get_nickname() + "_")
  32. def on_welcome(self, c, e):
  33. print("Joining channel")
  34. c.join(self.channel)
  35. def on_privmsg(self, connection, event):
  36. self.do_command(connection, event)
  37. def on_pubmsg(self, connection, event):
  38. self.do_command(connection, event)
  39. #def on_pubmsg(self, c, e):
  40. # a = e.arguments[0].split(":", 1)
  41. # if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
  42. # self.do_command(e, a[1].strip())
  43. # return
  44. def on_dccmsg(self, c, e):
  45. # non-chat DCC messages are raw bytes; decode as text
  46. text = e.arguments[0].decode('utf-8')
  47. c.privmsg("You said: " + text)
  48. def on_dccchat(self, c, e):
  49. if len(e.arguments) != 2:
  50. return
  51. args = e.arguments[1].split()
  52. if len(args) == 4:
  53. try:
  54. address = ip_numstr_to_quad(args[2])
  55. port = int(args[3])
  56. except ValueError:
  57. return
  58. self.dcc_connect(address, port)
  59. def do_command(self, connection, event):
  60. cmdchar = "!"
  61. helpchar = "@"
  62. trigger = event.arguments[0]
  63. #Check command prefix
  64. if trigger.startswith(cmdchar) or trigger.startswith(helpchar): #Do nothing if it's not a command.
  65. command = trigger[1:] #Without prefix.
  66. help = False
  67. if trigger.startswith(helpchar): #It's a help requist.
  68. help = True
  69. # Set replyto variable for output to multiple protocols
  70. if event.type == "pubmsg":
  71. replyto = event.target
  72. elif event.type == "privmsg":
  73. replyto = event.source.nick
  74. else:
  75. return
  76. if command == "cmd":
  77. if help:
  78. connection.privmsg(replyto, "Displays a list of commands.")
  79. else:
  80. connection.privmsg(replyto, "!cmd, !test, !msg")
  81. elif command == "test":
  82. if help:
  83. connection.privmsg(replyto, "Strictly for testing purposes only!")
  84. else:
  85. connection.privmsg(replyto, str(connection) + " | " + str(event))
  86. #elif command == "channel":
  87. #channel = Channel()
  88. #print("aaa")
  89. #print(replyto)
  90. #connection.privmsg(replyto, event.source.nick)
  91. #connection.privmsg(replyto, str(channel.users(replyto)))
  92. #connection.privmsg(replyto, is_admin(event.source.nick))
  93. #connection.privmsg(replyto, "A: " + self.channels[0].is_admin(event.source.nick))
  94. elif command.split(maxsplit=1)[0] == "msg":
  95. print(1)
  96. if help:
  97. print(2)
  98. connection.privmsg(replyto, "Message a user or channel.")
  99. connection.privmsg(replyto, "\x0314Usage: \x0300!msg \x1Dname message")
  100. else:
  101. try:
  102. connection.privmsg(command.split()[1], command.split(maxsplit=2)[2])
  103. except IndexError:
  104. connection.privmsg(replyto, "Nothing")
  105. #else: #Parrot mode
  106. # connection.privmsg("#[CP]Specifer", str(trigger.arguments[0].split(maxsplit=1)[0]))
  107. # def do_command(self, e, cmd):
  108. # nick = e.source.nick
  109. # c = self.connection
  110. #
  111. # if cmd == "disconnect":
  112. # self.disconnect()
  113. # elif cmd == "die":
  114. # self.die()
  115. # elif cmd == "stats":
  116. # for chname, chobj in self.channels.items():
  117. # c.notice(nick, "--- Channel statistics ---")
  118. # c.notice(nick, "Channel: " + chname)
  119. # users = sorted(chobj.users())
  120. # c.notice(nick, "Users: " + ", ".join(users))
  121. # opers = sorted(chobj.opers())
  122. # c.notice(nick, "Opers: " + ", ".join(opers))
  123. # voiced = sorted(chobj.voiced())
  124. # c.notice(nick, "Voiced: " + ", ".join(voiced))
  125. # elif cmd == "dcc":
  126. # dcc = self.dcc_listen()
  127. # c.ctcp("DCC", nick, "CHAT chat %s %d" % (
  128. # ip_quad_to_numstr(dcc.localaddress),
  129. # dcc.localport))
  130. # else:
  131. # c.notice(nick, "Not understood: " + cmd)
  132. def main():
  133. #import sys
  134. #if len(sys.argv) != 4:
  135. # print(sys.argv)
  136. # print("Usage: testbot <server[:port]> <channel> <nickname>")
  137. # sys.exit(1)
  138. #s = sys.argv[1].split(":", 1)
  139. #server = s[0]
  140. #if len(s) == 2:
  141. # try:
  142. # port = int(s[1])
  143. # except ValueError:
  144. # print("Error: Erroneous port.")
  145. # sys.exit(1)
  146. #else:
  147. # port = 6667
  148. #channel = sys.argv[2]
  149. #nickname = sys.argv[3]
  150. channel = "#[CP]Specifer"
  151. nickname = "pyRotBot"
  152. server = "irc.gtanet.com"
  153. port = 6667
  154. bot = RotBot(channel, nickname, server, port)
  155. print("Starting bot")
  156. bot.start()
  157. if __name__ == "__main__":
  158. main()