A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 imp
  23. from earwigbot.irc import IRCConnection, RC
  24. __all__ = ["Watcher"]
  25. class Watcher(IRCConnection):
  26. """
  27. EarwigBot's IRC Watcher Component
  28. The IRC watcher runs on a wiki recent-changes server and listens for
  29. edits. Users cannot interact with this part of the bot. When an event
  30. occurs, we run it through some rules stored in our config, which can result
  31. in wiki bot tasks being started or messages being sent to channels on the
  32. IRC frontend.
  33. """
  34. def __init__(self, bot):
  35. self.bot = bot
  36. self.logger = bot.logger.getChild("watcher")
  37. cf = bot.config.irc["watcher"]
  38. base = super(Watcher, self)
  39. base.__init__(cf["host"], cf["port"], cf["nick"], cf["ident"],
  40. cf["realname"])
  41. self._prepare_process_hook()
  42. self._connect()
  43. def _process_message(self, line):
  44. """Process a single message from IRC."""
  45. line = line.strip().split()
  46. if line[1] == "PRIVMSG":
  47. chan = line[2]
  48. # Ignore messages originating from channels not in our list, to
  49. # prevent someone PMing us false data:
  50. if chan not in self.bot.config.irc["watcher"]["channels"]:
  51. return
  52. msg = " ".join(line[3:])[1:]
  53. rc = RC(msg) # New RC object to store this event's data
  54. rc.parse() # Parse a message into pagenames, usernames, etc.
  55. self._process_rc_event(rc)
  56. # If we are pinged, pong back:
  57. elif line[0] == "PING":
  58. self.pong(line[1])
  59. # When we've finished starting up, join all watcher channels:
  60. elif line[1] == "376":
  61. for chan in self.bot.config.irc["watcher"]["channels"]:
  62. self.join(chan)
  63. def _prepare_process_hook(self):
  64. """Create our RC event process hook from information in config.
  65. This will get put in the function self._process_hook, which takes the
  66. Bot object and an RC object and returns a list of frontend channels to
  67. report this event to.
  68. """
  69. # Set a default RC process hook that does nothing:
  70. self._process_hook = lambda rc: ()
  71. try:
  72. rules = self.bot.config.data["rules"]
  73. except KeyError:
  74. return
  75. module = imp.new_module("_rc_event_processing_rules")
  76. path = self.bot.config.path
  77. try:
  78. exec compile(rules, path, "exec") in module.__dict__
  79. except Exception:
  80. e = "Could not compile config file's RC event rules:"
  81. self.logger.exception(e)
  82. return
  83. self._process_hook_module = module
  84. try:
  85. self._process_hook = module.process
  86. except AttributeError:
  87. e = "RC event rules compiled correctly, but no process(bot, rc) function was found"
  88. self.logger.error(e)
  89. return
  90. def _process_rc_event(self, rc):
  91. """Process a recent change event from IRC (or, an RC object).
  92. The actual processing is configurable, so we don't have that hard-coded
  93. here. We simply call our process hook (self._process_hook), created by
  94. self._prepare_process_hook() from information in the "rules" section of
  95. our config.
  96. """
  97. chans = self._process_hook(self.bot, rc)
  98. with self.bot.component_lock:
  99. frontend = self.bot.frontend
  100. if chans and frontend and not frontend.is_stopped():
  101. pretty = rc.prettify()
  102. for chan in chans:
  103. frontend.say(chan, pretty)