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.

87 lines
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. from earwigbot.irc import IRCConnection, Data
  23. __all__ = ["Frontend"]
  24. class Frontend(IRCConnection):
  25. """
  26. **EarwigBot: IRC Frontend Component**
  27. The IRC frontend runs on a normal IRC server and expects users to interact
  28. with it and give it commands. Commands are stored as "command classes",
  29. subclasses of :py:class:`~earwigbot.commands.Command`. All command classes
  30. are automatically imported by :py:meth:`commands.load()
  31. <earwigbot.managers._ResourceManager.load>` if they are in
  32. :py:mod:`earwigbot.commands` or the bot's custom command directory
  33. (explained in the :doc:`documentation </customizing>`).
  34. """
  35. def __init__(self, bot):
  36. self.bot = bot
  37. cf = bot.config.irc["frontend"]
  38. base = super(Frontend, self)
  39. base.__init__(cf["host"], cf["port"], cf["nick"], cf["ident"],
  40. cf["realname"], bot.logger.getChild("frontend"))
  41. self._connect()
  42. def __repr__(self):
  43. """Return the canonical string representation of the Frontend."""
  44. res = "Frontend(host={0!r}, port={1!r}, nick={2!r}, ident={3!r}, realname={4!r}, bot={5!r})"
  45. return res.format(self.host, self.port, self.nick, self.ident,
  46. self.realname, self.bot)
  47. def __str__(self):
  48. """Return a nice string representation of the Frontend."""
  49. res = "<Frontend {0}!{1} at {2}:{3}>"
  50. return res.format(self.nick, self.ident, self.host, self.port)
  51. def _process_message(self, line):
  52. """Process a single message from IRC."""
  53. if line[1] == "JOIN":
  54. data = Data(self.bot, self.nick, line, msgtype="JOIN")
  55. self.bot.commands.call("join", data)
  56. elif line[1] == "PRIVMSG":
  57. data = Data(self.bot, self.nick, line, msgtype="PRIVMSG")
  58. if data.is_private:
  59. self.bot.commands.call("msg_private", data)
  60. else:
  61. self.bot.commands.call("msg_public", data)
  62. self.bot.commands.call("msg", data)
  63. elif line[1] == "376": # On successful connection to the server
  64. # If we're supposed to auth to NickServ, do that:
  65. try:
  66. username = self.bot.config.irc["frontend"]["nickservUsername"]
  67. password = self.bot.config.irc["frontend"]["nickservPassword"]
  68. except KeyError:
  69. pass
  70. else:
  71. msg = "IDENTIFY {0} {1}".format(username, password)
  72. self.say("NickServ", msg, hidelog=True)
  73. # Join all of our startup channels:
  74. for chan in self.bot.config.irc["frontend"]["channels"]:
  75. self.join(chan)