A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

frontend.py 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. """
  3. EarwigBot's IRC Frontend Component
  4. The IRC frontend runs on a normal IRC server and expects users to interact with
  5. it and give it commands. Commands are stored as "command classes", subclasses
  6. of BaseCommand in irc/base_command.py. All command classes are automatically
  7. imported by irc/command_handler.py if they are in irc/commands.
  8. """
  9. import re
  10. import config
  11. import commands
  12. from classes import Connection, Data, BrokenSocketException
  13. __all__ = ["get_connection", "startup", "main"]
  14. connection = None
  15. sender_regex = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  16. def get_connection():
  17. """Return a new Connection() instance with information about our server
  18. connection, but don't actually connect yet."""
  19. cf = config.irc["frontend"]
  20. connection = Connection(cf["host"], cf["port"], cf["nick"], cf["ident"],
  21. cf["realname"])
  22. return connection
  23. def startup(conn):
  24. """Accept a single arg, a Connection() object, and set our global variable
  25. 'connection' to it. Load all command classes in irc/commands with
  26. command_handler, and then establish a connection with the IRC server."""
  27. global connection
  28. connection = conn
  29. commands.load(connection)
  30. connection.connect()
  31. def main():
  32. """Main loop for the frontend component.
  33. get_connection() and startup() should have already been called before this.
  34. """
  35. read_buffer = str()
  36. while 1:
  37. try:
  38. read_buffer = read_buffer + connection.get()
  39. except BrokenSocketException:
  40. print "Socket has broken on front-end; restarting bot..."
  41. return
  42. lines = read_buffer.split("\n")
  43. read_buffer = lines.pop()
  44. for line in lines:
  45. _process_message(line)
  46. def _process_message(line):
  47. """Process a single message from IRC."""
  48. line = line.strip().split()
  49. data = Data(line) # new Data instance to store info about this line
  50. if line[1] == "JOIN":
  51. data.nick, data.ident, data.host = sender_regex.findall(line[0])[0]
  52. data.chan = line[2][1:]
  53. # Check for 'join' hooks in our commands:
  54. commands.check("join", data)
  55. elif line[1] == "PRIVMSG":
  56. data.nick, data.ident, data.host = sender_regex.findall(line[0])[0]
  57. data.msg = ' '.join(line[3:])[1:]
  58. data.chan = line[2]
  59. if data.chan == config.irc["frontend"]["nick"]:
  60. # This is a privmsg to us, so set 'chan' as the nick of the, sender
  61. # then check for private-only command hooks:
  62. data.chan = data.nick
  63. commands.check("msg_private", data)
  64. else:
  65. # Check for public-only command hooks:
  66. commands.check("msg_public", data)
  67. # Check for command hooks that apply to all messages:
  68. commands.check("msg", data)
  69. # Hardcode the !restart command (we can't restart from within an
  70. # ordinary command):
  71. if data.msg in ["!restart", ".restart"]:
  72. if data.host in config.irc["permissions"]["owners"]:
  73. print "Restarting bot per owner request..."
  74. return
  75. # If we are pinged, pong back to the server:
  76. if line[0] == "PING":
  77. connection.send("PONG %s" % line[1])
  78. # On successful connection to the server:
  79. if line[1] == "376":
  80. # If we're supposed to auth to NickServ, do that:
  81. try:
  82. username = config.irc["frontend"]["nickservUsername"]
  83. password = config.irc["frontend"]["nickservPassword"]
  84. except KeyError:
  85. pass
  86. else:
  87. msg = " ".join(("IDENTIFY", username, password))
  88. connection.say("NickServ", msg)
  89. # Join all of our startup channels:
  90. for chan in config.irc["frontend"]["channels"]:
  91. connection.join(chan)