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.

97 righe
3.8 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 re
  23. from earwigbot.irc import IRCConnection, Data
  24. __all__ = ["Frontend"]
  25. class Frontend(IRCConnection):
  26. """
  27. EarwigBot's 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 BaseCommand in classes/base_command.py. All command classes
  31. are automatically imported by commands/__init__.py if they are in
  32. commands/.
  33. """
  34. sender_regex = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  35. def __init__(self, bot):
  36. self.bot = bot
  37. self.logger = bot.logger.getChild("frontend")
  38. cf = bot.config.irc["frontend"]
  39. base = super(Frontend, self)
  40. base.__init__(cf["host"], cf["port"], cf["nick"], cf["ident"],
  41. cf["realname"])
  42. self._connect()
  43. def _process_message(self, line):
  44. """Process a single message from IRC."""
  45. line = line.strip().split()
  46. data = Data(self.bot, line)
  47. if line[1] == "JOIN":
  48. data.nick, data.ident, data.host = self.sender_regex.findall(line[0])[0]
  49. data.chan = line[2]
  50. data.parse_args()
  51. self.bot.commands.check("join", data)
  52. elif line[1] == "PRIVMSG":
  53. data.nick, data.ident, data.host = self.sender_regex.findall(line[0])[0]
  54. data.msg = " ".join(line[3:])[1:]
  55. data.chan = line[2]
  56. data.parse_args()
  57. if data.chan == self.bot.config.irc["frontend"]["nick"]:
  58. # This is a privmsg to us, so set 'chan' as the nick of the
  59. # sender, then check for private-only command hooks:
  60. data.chan = data.nick
  61. self.bot.commands.check("msg_private", data)
  62. else:
  63. # Check for public-only command hooks:
  64. self.bot.commands.check("msg_public", data)
  65. # Check for command hooks that apply to all messages:
  66. self.bot.commands.check("msg", data)
  67. elif line[0] == "PING": # If we are pinged, pong back
  68. self.pong(line[1])
  69. elif line[1] == "376": # On successful connection to the server
  70. # If we're supposed to auth to NickServ, do that:
  71. try:
  72. username = self.bot.config.irc["frontend"]["nickservUsername"]
  73. password = self.bot.config.irc["frontend"]["nickservPassword"]
  74. except KeyError:
  75. pass
  76. else:
  77. msg = "IDENTIFY {0} {1}".format(username, password)
  78. self.say("NickServ", msg)
  79. # Join all of our startup channels:
  80. for chan in self.bot.config.irc["frontend"]["channels"]:
  81. self.join(chan)