A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 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.

118 linhas
3.9 KiB

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