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.

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