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.

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