A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 line
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import logging
  23. import re
  24. from earwigbot.commands import command_manager
  25. from earwigbot.irc import IRCConnection, Data, BrokenSocketException
  26. __all__ = ["Frontend"]
  27. class Frontend(IRCConnection):
  28. """
  29. EarwigBot's IRC Frontend Component
  30. The IRC frontend runs on a normal IRC server and expects users to interact
  31. with it and give it commands. Commands are stored as "command classes",
  32. subclasses of BaseCommand in classes/base_command.py. All command classes
  33. are automatically imported by commands/__init__.py if they are in
  34. commands/.
  35. """
  36. sender_regex = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  37. def __init__(self, config):
  38. self.config = config
  39. self.logger = logging.getLogger("earwigbot.frontend")
  40. cf = config.irc["frontend"]
  41. base = super(Frontend, self)
  42. base.__init__(cf["host"], cf["port"], cf["nick"], cf["ident"],
  43. cf["realname"], self.logger)
  44. command_manager.load(self)
  45. self._connect()
  46. def _process_message(self, 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 = self.sender_regex.findall(line[0])[0]
  52. data.chan = line[2]
  53. # Check for 'join' hooks in our commands:
  54. command_manager.check("join", data)
  55. elif line[1] == "PRIVMSG":
  56. data.nick, data.ident, data.host = self.sender_regex.findall(line[0])[0]
  57. data.msg = " ".join(line[3:])[1:]
  58. data.chan = line[2]
  59. if data.chan == self.config.irc["frontend"]["nick"]:
  60. # This is a privmsg to us, so set 'chan' as the nick of the
  61. # sender, then check for private-only command hooks:
  62. data.chan = data.nick
  63. command_manager.check("msg_private", data)
  64. else:
  65. # Check for public-only command hooks:
  66. command_manager.check("msg_public", data)
  67. # Check for command hooks that apply to all messages:
  68. command_manager.check("msg", data)
  69. # If we are pinged, pong back:
  70. elif line[0] == "PING":
  71. self.pong(line[1])
  72. # On successful connection to the server:
  73. elif line[1] == "376":
  74. # If we're supposed to auth to NickServ, do that:
  75. try:
  76. username = self.config.irc["frontend"]["nickservUsername"]
  77. password = self.config.irc["frontend"]["nickservPassword"]
  78. except KeyError:
  79. pass
  80. else:
  81. msg = "IDENTIFY {0} {1}".format(username, password)
  82. self.say("NickServ", msg)
  83. # Join all of our startup channels:
  84. for chan in self.config.irc["frontend"]["channels"]:
  85. self.join(chan)