A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

348 rindas
13 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 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. from collections import OrderedDict
  23. from getpass import getpass
  24. from hashlib import sha256
  25. import logging
  26. import logging.handlers
  27. from os import mkdir, path
  28. import stat
  29. from Crypto.Cipher import Blowfish
  30. import bcrypt
  31. import yaml
  32. from earwigbot.config.formatter import BotFormatter
  33. from earwigbot.config.node import ConfigNode
  34. from earwigbot.config.ordered_yaml import OrderedLoader
  35. from earwigbot.config.permissions import PermissionsDB
  36. from earwigbot.config.script import ConfigScript
  37. from earwigbot.exceptions import NoConfigError
  38. __all__ = ["BotConfig"]
  39. class BotConfig(object):
  40. """
  41. **EarwigBot: YAML Config File Manager**
  42. This handles all tasks involving reading and writing to our config file,
  43. including encrypting and decrypting passwords and making a new config file
  44. from scratch at the inital bot run.
  45. BotConfig has a few attributes and methods, including the following:
  46. - :py:attr:`root_dir`: bot's working directory; contains
  47. :file:`config.yml`, :file:`logs/`
  48. - :py:attr:`path`: path to the bot's config file
  49. - :py:attr:`components`: enabled components
  50. - :py:attr:`wiki`: information about wiki-editing
  51. - :py:attr:`irc`: information about IRC
  52. - :py:attr:`commands`: information about IRC commands
  53. - :py:attr:`tasks`: information for bot tasks
  54. - :py:attr:`metadata`: miscellaneous information
  55. - :py:meth:`schedule`: tasks scheduled to run at a given time
  56. BotConfig also has some methods used in config loading:
  57. - :py:meth:`load`: loads (or reloads) and parses our config file
  58. - :py:meth:`decrypt`: decrypts an object in the config tree
  59. """
  60. def __init__(self, bot, root_dir, level):
  61. self._bot = bot
  62. self._root_dir = root_dir
  63. self._logging_level = level
  64. self._config_path = path.join(self.root_dir, "config.yml")
  65. self._log_dir = path.join(self.root_dir, "logs")
  66. perms_file = path.join(self.root_dir, "permissions.db")
  67. self._permissions = PermissionsDB(perms_file)
  68. self._decryption_cipher = None
  69. self._data = None
  70. self._components = ConfigNode()
  71. self._wiki = ConfigNode()
  72. self._irc = ConfigNode()
  73. self._commands = ConfigNode()
  74. self._tasks = ConfigNode()
  75. self._metadata = ConfigNode()
  76. self._nodes = [self._components, self._wiki, self._irc, self._commands,
  77. self._tasks, self._metadata]
  78. self._decryptable_nodes = [ # Default nodes to decrypt
  79. (self._wiki, ("password",)),
  80. (self._wiki, ("search", "credentials", "key")),
  81. (self._wiki, ("search", "credentials", "secret")),
  82. (self._irc, ("frontend", "nickservPassword")),
  83. (self._irc, ("watcher", "nickservPassword")),
  84. ]
  85. def __repr__(self):
  86. """Return the canonical string representation of the BotConfig."""
  87. res = "BotConfig(root_dir={0!r}, level={1!r})"
  88. return res.format(self.root_dir, self.logging_level)
  89. def __str__(self):
  90. """Return a nice string representation of the BotConfig."""
  91. return "<BotConfig at {0}>".format(self.root_dir)
  92. def _handle_missing_config(self):
  93. print "Config file missing or empty:", self._config_path
  94. msg = "Would you like to create a config file now? [Y/n] "
  95. choice = raw_input(msg)
  96. if choice.lower().startswith("n"):
  97. raise NoConfigError()
  98. else:
  99. try:
  100. ConfigScript(self).make_new()
  101. except KeyboardInterrupt:
  102. raise NoConfigError()
  103. def _load(self):
  104. """Load data from our JSON config file (config.yml) into self._data."""
  105. filename = self._config_path
  106. with open(filename, 'r') as fp:
  107. try:
  108. self._data = yaml.load(fp, OrderedLoader)
  109. except yaml.YAMLError:
  110. print "Error parsing config file {0}:".format(filename)
  111. raise
  112. def _setup_logging(self):
  113. """Configures the logging module so it works the way we want it to."""
  114. log_dir = self._log_dir
  115. logger = logging.getLogger("earwigbot")
  116. logger.handlers = [] # Remove any handlers already attached to us
  117. logger.setLevel(logging.DEBUG)
  118. color_formatter = BotFormatter(color=True)
  119. formatter = BotFormatter()
  120. if self.metadata.get("enableLogging"):
  121. hand = logging.handlers.TimedRotatingFileHandler
  122. logfile = lambda f: path.join(log_dir, f)
  123. if not path.isdir(log_dir):
  124. if not path.exists(log_dir):
  125. mkdir(log_dir, stat.S_IWUSR|stat.S_IRUSR|stat.S_IXUSR)
  126. else:
  127. msg = "log_dir ({0}) exists but is not a directory!"
  128. print msg.format(log_dir)
  129. return
  130. main_handler = hand(logfile("bot.log"), "midnight", 1, 7)
  131. error_handler = hand(logfile("error.log"), "W6", 1, 4)
  132. debug_handler = hand(logfile("debug.log"), "H", 1, 6)
  133. main_handler.setLevel(logging.INFO)
  134. error_handler.setLevel(logging.WARNING)
  135. debug_handler.setLevel(logging.DEBUG)
  136. for h in (main_handler, error_handler, debug_handler):
  137. h.setFormatter(formatter)
  138. logger.addHandler(h)
  139. self._stream_handler = stream = logging.StreamHandler()
  140. stream.setLevel(self._logging_level)
  141. stream.setFormatter(color_formatter)
  142. logger.addHandler(stream)
  143. def _decrypt(self, node, nodes):
  144. """Try to decrypt the contents of a config node. Use self.decrypt()."""
  145. try:
  146. node._decrypt(self._decryption_cipher, nodes[:-1], nodes[-1])
  147. except ValueError:
  148. print "Error decrypting passwords:"
  149. raise
  150. @property
  151. def bot(self):
  152. """The config's Bot object."""
  153. return self._bot
  154. @property
  155. def root_dir(self):
  156. """The bot's root directory containing its config file and more."""
  157. return self._root_dir
  158. @property
  159. def logging_level(self):
  160. """The minimum logging level for messages logged via stdout."""
  161. return self._logging_level
  162. @logging_level.setter
  163. def logging_level(self, level):
  164. self._logging_level = level
  165. self._stream_handler.setLevel(level)
  166. @property
  167. def path(self):
  168. """The path to the bot's config file."""
  169. return self._config_path
  170. @property
  171. def log_dir(self):
  172. """The directory containing the bot's logs."""
  173. return self._log_dir
  174. @property
  175. def data(self):
  176. """The entire config file as a decoded JSON object."""
  177. return self._data
  178. @property
  179. def components(self):
  180. """A dict of enabled components."""
  181. return self._components
  182. @property
  183. def wiki(self):
  184. """A dict of information about wiki-editing."""
  185. return self._wiki
  186. @property
  187. def irc(self):
  188. """A dict of information about IRC."""
  189. return self._irc
  190. @property
  191. def commands(self):
  192. """A dict of information for IRC commands."""
  193. return self._commands
  194. @property
  195. def tasks(self):
  196. """A dict of information for bot tasks."""
  197. return self._tasks
  198. @property
  199. def metadata(self):
  200. """A dict of miscellaneous information."""
  201. return self._metadata
  202. def is_loaded(self):
  203. """Return ``True`` if our config file has been loaded, or ``False``."""
  204. return self._data is not None
  205. def is_encrypted(self):
  206. """Return ``True`` if passwords are encrypted, otherwise ``False``."""
  207. return self.metadata.get("encryptPasswords", False)
  208. def load(self):
  209. """Load, or reload, our config file.
  210. First, check if we have a valid config file, and if not, notify the
  211. user. If there is no config file at all, offer to make one, otherwise
  212. exit.
  213. Data from the config file is stored in six
  214. :py:class:`~earwigbot.config.ConfigNode`\ s (:py:attr:`components`,
  215. :py:attr:`wiki`, :py:attr:`irc`, :py:attr:`commands`, :py:attr:`tasks`,
  216. :py:attr:`metadata`) for easy access (as well as the lower-level
  217. :py:attr:`data` attribute). If passwords are encrypted, we'll use
  218. :py:func:`~getpass.getpass` for the key and then decrypt them. If the
  219. config is being reloaded, encrypted items will be automatically
  220. decrypted if they were decrypted earlier.
  221. """
  222. if not path.exists(self._config_path):
  223. self._handle_missing_config()
  224. self._load()
  225. if not self._data:
  226. self._handle_missing_config()
  227. self._load()
  228. self.components._load(self._data.get("components", OrderedDict()))
  229. self.wiki._load(self._data.get("wiki", OrderedDict()))
  230. self.irc._load(self._data.get("irc", OrderedDict()))
  231. self.commands._load(self._data.get("commands", OrderedDict()))
  232. self.tasks._load(self._data.get("tasks", OrderedDict()))
  233. self.metadata._load(self._data.get("metadata", OrderedDict()))
  234. self._setup_logging()
  235. if self.is_encrypted():
  236. if not self._decryption_cipher:
  237. key = getpass("Enter key to decrypt bot passwords: ")
  238. self._decryption_cipher = Blowfish.new(sha256(key).digest())
  239. signature = self.metadata["signature"]
  240. if bcrypt.hashpw(key, signature) != signature:
  241. raise RuntimeError("Incorrect password.")
  242. for node, nodes in self._decryptable_nodes:
  243. self._decrypt(node, nodes)
  244. if self.irc:
  245. self.irc["permissions"] = self._permissions
  246. self._permissions.load()
  247. def decrypt(self, node, *nodes):
  248. """Decrypt an object in our config tree.
  249. :py:attr:`_decryption_cipher` is used as our key, retrieved using
  250. :py:func:`~getpass.getpass` in :py:meth:`load` if it wasn't already
  251. specified. If this is called when passwords are not encrypted (check
  252. with :py:meth:`is_encrypted`), nothing will happen. We'll also keep
  253. track of this node if :py:meth:`load` is called again (i.e. to reload)
  254. and automatically decrypt it.
  255. Example usage::
  256. >>> config.decrypt(config.irc, "frontend", "nickservPassword")
  257. # decrypts config.irc["frontend"]["nickservPassword"]
  258. """
  259. signature = (node, nodes)
  260. if signature in self._decryptable_nodes:
  261. return # Already decrypted
  262. self._decryptable_nodes.append(signature)
  263. if self.is_encrypted():
  264. self._decrypt(node, nodes)
  265. def schedule(self, minute, hour, month_day, month, week_day):
  266. """Return a list of tasks scheduled to run at the specified time.
  267. The schedule data comes from our config file's ``schedule`` field,
  268. which is stored as :py:attr:`self.data["schedule"] <data>`.
  269. """
  270. # Tasks to run this turn, each as a list of either [task_name, kwargs],
  271. # or just the task_name:
  272. tasks = []
  273. now = {"minute": minute, "hour": hour, "month_day": month_day,
  274. "month": month, "week_day": week_day}
  275. data = self._data.get("schedule", [])
  276. for event in data:
  277. do = True
  278. for key, value in now.items():
  279. try:
  280. requirement = event[key]
  281. except KeyError:
  282. continue
  283. if requirement != value:
  284. do = False
  285. break
  286. if do:
  287. try:
  288. tasks.extend(event["tasks"])
  289. except KeyError:
  290. pass
  291. return tasks