A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

114 righe
4.8 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2016 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 time import sleep
  23. from earwigbot.irc import IRCConnection, Data
  24. __all__ = ["Frontend"]
  25. class Frontend(IRCConnection):
  26. """
  27. **EarwigBot: IRC Frontend Component**
  28. The IRC frontend runs on a normal IRC server and expects users to interact
  29. with it and give it commands. Commands are stored as "command classes",
  30. subclasses of :py:class:`~earwigbot.commands.Command`. All command classes
  31. are automatically imported by :py:meth:`commands.load()
  32. <earwigbot.managers._ResourceManager.load>` if they are in
  33. :py:mod:`earwigbot.commands` or the bot's custom command directory
  34. (explained in the :doc:`documentation </customizing>`).
  35. """
  36. NICK_SERVICES = "NickServ"
  37. def __init__(self, bot):
  38. self.bot = bot
  39. cf = bot.config.irc["frontend"]
  40. base = super(Frontend, self)
  41. base.__init__(cf["host"], cf["port"], cf["nick"], cf["ident"],
  42. cf["realname"], bot.logger.getChild("frontend"))
  43. self._auth_wait = False
  44. self._connect()
  45. def __repr__(self):
  46. """Return the canonical string representation of the Frontend."""
  47. res = "Frontend(host={0!r}, port={1!r}, nick={2!r}, ident={3!r}, realname={4!r}, bot={5!r})"
  48. return res.format(self.host, self.port, self.nick, self.ident,
  49. self.realname, self.bot)
  50. def __str__(self):
  51. """Return a nice string representation of the Frontend."""
  52. res = "<Frontend {0}!{1} at {2}:{3}>"
  53. return res.format(self.nick, self.ident, self.host, self.port)
  54. def _join_channels(self):
  55. """Join all startup channels as specified by the config file."""
  56. for chan in self.bot.config.irc["frontend"]["channels"]:
  57. self.join(chan)
  58. def _process_message(self, line):
  59. """Process a single message from IRC."""
  60. if line[1] == "JOIN":
  61. data = Data(self.nick, line, msgtype="JOIN")
  62. self.bot.commands.call("join", data)
  63. elif line[1] == "PART":
  64. data = Data(self.nick, line, msgtype="PART")
  65. self.bot.commands.call("part", data)
  66. elif line[1] == "PRIVMSG":
  67. data = Data(self.nick, line, msgtype="PRIVMSG")
  68. if data.is_private:
  69. self.bot.commands.call("msg_private", data)
  70. else:
  71. self.bot.commands.call("msg_public", data)
  72. self.bot.commands.call("msg", data)
  73. elif line[1] == "NOTICE":
  74. data = Data(self.nick, line, msgtype="NOTICE")
  75. if self._auth_wait and data.nick == self.NICK_SERVICES:
  76. if data.msg.startswith("This nickname is registered."):
  77. return
  78. self._auth_wait = False
  79. sleep(2) # Wait for hostname change to propagate
  80. self._join_channels()
  81. elif line[1] == "376": # On successful connection to the server
  82. # If we're supposed to auth to NickServ, do that:
  83. try:
  84. username = self.bot.config.irc["frontend"]["nickservUsername"]
  85. password = self.bot.config.irc["frontend"]["nickservPassword"]
  86. except KeyError:
  87. self._join_channels()
  88. else:
  89. self.logger.debug("Identifying with services")
  90. msg = "IDENTIFY {0} {1}".format(username, password)
  91. self.say(self.NICK_SERVICES, msg, hidelog=True)
  92. self._auth_wait = True
  93. elif line[1] == "401": # No such nickname
  94. if self._auth_wait and line[3] == self.NICK_SERVICES:
  95. # Services is down, or something...?
  96. self._auth_wait = False
  97. self._join_channels()