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.

383 righe
14 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. from getpass import getpass
  23. from hashlib import sha256
  24. import logging
  25. import logging.handlers
  26. from os import mkdir, path
  27. from Crypto.Cipher import Blowfish
  28. import yaml
  29. __all__ = ["BotConfig"]
  30. class BotConfig(object):
  31. """
  32. EarwigBot's YAML Config File Manager
  33. This handles all tasks involving reading and writing to our config file,
  34. including encrypting and decrypting passwords and making a new config file
  35. from scratch at the inital bot run.
  36. BotConfig has a few properties and functions, including the following:
  37. * config.root_dir - bot's working directory; contains config.yml, logs/
  38. * config.path - path to the bot's config file
  39. * config.components - enabled components
  40. * config.wiki - information about wiki-editing
  41. * config.tasks - information for bot tasks
  42. * config.irc - information about IRC
  43. * config.metadata - miscellaneous information
  44. * config.schedule() - tasks scheduled to run at a given time
  45. BotConfig also has some functions used in config loading:
  46. * config.load() - loads and parses our config file, returning True if
  47. passwords are stored encrypted or False otherwise;
  48. can also be used to easily reload config
  49. * config.decrypt() - given a key, decrypts passwords inside our config
  50. variables, and remembers to decrypt the password if
  51. config is reloaded; won't do anything if passwords
  52. aren't encrypted
  53. """
  54. def __init__(self, root_dir, level):
  55. self._root_dir = root_dir
  56. self._logging_level = level
  57. self._config_path = path.join(self._root_dir, "config.yml")
  58. self._log_dir = path.join(self._root_dir, "logs")
  59. self._decryption_cipher = None
  60. self._data = None
  61. self._components = _ConfigNode()
  62. self._wiki = _ConfigNode()
  63. self._tasks = _ConfigNode()
  64. self._irc = _ConfigNode()
  65. self._metadata = _ConfigNode()
  66. self._nodes = [self._components, self._wiki, self._tasks, self._irc,
  67. self._metadata]
  68. self._decryptable_nodes = [ # Default nodes to decrypt
  69. (self._wiki, ("password",)),
  70. (self._wiki, ("search", "credentials", "key")),
  71. (self._wiki, ("search", "credentials", "secret")),
  72. (self._irc, ("frontend", "nickservPassword")),
  73. (self._irc, ("watcher", "nickservPassword")),
  74. ]
  75. def _load(self):
  76. """Load data from our JSON config file (config.yml) into self._data."""
  77. filename = self._config_path
  78. with open(filename, 'r') as fp:
  79. try:
  80. self._data = yaml.load(fp)
  81. except yaml.YAMLError:
  82. print "Error parsing config file {0}:".format(filename)
  83. raise
  84. def _setup_logging(self):
  85. """Configures the logging module so it works the way we want it to."""
  86. log_dir = self._log_dir
  87. logger = logging.getLogger("earwigbot")
  88. logger.handlers = [] # Remove any handlers already attached to us
  89. logger.setLevel(logging.DEBUG)
  90. if self.metadata.get("enableLogging"):
  91. hand = logging.handlers.TimedRotatingFileHandler
  92. formatter = _BotFormatter()
  93. color_formatter = _BotFormatter(color=True)
  94. logfile = lambda f: path.join(log_dir, f)
  95. if not path.isdir(log_dir):
  96. if not path.exists(log_dir):
  97. mkdir(log_dir, 0700)
  98. else:
  99. msg = "log_dir ({0}) exists but is not a directory!"
  100. print msg.format(log_dir)
  101. return
  102. main_handler = hand(logfile("bot.log"), "midnight", 1, 7)
  103. error_handler = hand(logfile("error.log"), "W6", 1, 4)
  104. debug_handler = hand(logfile("debug.log"), "H", 1, 6)
  105. main_handler.setLevel(logging.INFO)
  106. error_handler.setLevel(logging.WARNING)
  107. debug_handler.setLevel(logging.DEBUG)
  108. for h in (main_handler, error_handler, debug_handler):
  109. h.setFormatter(formatter)
  110. logger.addHandler(h)
  111. self._stream_handler = stream = logging.StreamHandler()
  112. stream.setLevel(self._logging_level)
  113. stream.setFormatter(color_formatter)
  114. logger.addHandler(stream)
  115. def _decrypt(self, node, nodes):
  116. """Try to decrypt the contents of a config node. Use self.decrypt()."""
  117. try:
  118. node._decrypt(self._decryption_cipher, nodes[:-1], nodes[-1])
  119. except ValueError:
  120. print "Error decrypting passwords:"
  121. raise
  122. def _make_new(self):
  123. """Make a new config file based on the user's input."""
  124. #m = "Would you like to encrypt passwords stored in config.yml? [y/n] "
  125. #encrypt = raw_input(m)
  126. #if encrypt.lower().startswith("y"):
  127. # is_encrypted = True
  128. #else:
  129. # is_encrypted = False
  130. raise NotImplementedError()
  131. # yaml.dumps() config.yml file (self._config_path)
  132. # Create root_dir/, root_dir/commands/, root_dir/tasks/
  133. # Give a reasonable message after config has been created regarding
  134. # what to do next...
  135. @property
  136. def root_dir(self):
  137. return self._root_dir
  138. @property
  139. def logging_level(self):
  140. return self._logging_level
  141. @logging_level.setter
  142. def logging_level(self, level):
  143. self._logging_level = level
  144. self._stream_handler.setLevel(level)
  145. @property
  146. def path(self):
  147. return self._config_path
  148. @property
  149. def log_dir(self):
  150. return self._log_dir
  151. @property
  152. def data(self):
  153. """The entire config file."""
  154. return self._data
  155. @property
  156. def components(self):
  157. """A dict of enabled components."""
  158. return self._components
  159. @property
  160. def wiki(self):
  161. """A dict of information about wiki-editing."""
  162. return self._wiki
  163. @property
  164. def tasks(self):
  165. """A dict of information for bot tasks."""
  166. return self._tasks
  167. @property
  168. def irc(self):
  169. """A dict of information about IRC."""
  170. return self._irc
  171. @property
  172. def metadata(self):
  173. """A dict of miscellaneous information."""
  174. return self._metadata
  175. def is_loaded(self):
  176. """Return True if our config file has been loaded, otherwise False."""
  177. return self._data is not None
  178. def is_encrypted(self):
  179. """Return True if passwords are encrypted, otherwise False."""
  180. return self.metadata.get("encryptPasswords", False)
  181. def load(self):
  182. """Load, or reload, our config file.
  183. First, check if we have a valid config file, and if not, notify the
  184. user. If there is no config file at all, offer to make one, otherwise
  185. exit.
  186. Store data from our config file in five _ConfigNodes (components,
  187. wiki, tasks, irc, metadata) for easy access (as well as the internal
  188. _data variable).
  189. If config is being reloaded, encrypted items will be automatically
  190. decrypted if they were decrypted beforehand.
  191. """
  192. if not path.exists(self._config_path):
  193. print "Config file not found:", self._config_path
  194. choice = raw_input("Would you like to create a config file now? [y/n] ")
  195. if choice.lower().startswith("y"):
  196. self._make_new()
  197. else:
  198. exit(1) # TODO: raise an exception instead
  199. self._load()
  200. data = self._data
  201. self.components._load(data.get("components", {}))
  202. self.wiki._load(data.get("wiki", {}))
  203. self.tasks._load(data.get("tasks", {}))
  204. self.irc._load(data.get("irc", {}))
  205. self.metadata._load(data.get("metadata", {}))
  206. self._setup_logging()
  207. if self.is_encrypted():
  208. if not self._decryption_cipher:
  209. key = getpass("Enter key to decrypt bot passwords: ")
  210. self._decryption_cipher = Blowfish.new(sha256(key).digest())
  211. for node, nodes in self._decryptable_nodes:
  212. self._decrypt(node, nodes)
  213. def decrypt(self, node, *nodes):
  214. """Use self._decryption_cipher to decrypt an object in our config tree.
  215. If this is called when passwords are not encrypted (check with
  216. config.is_encrypted()), nothing will happen. We'll also keep track of
  217. this node if config.load() is called again (i.e. to reload) and
  218. automatically decrypt it.
  219. Example usage:
  220. config.decrypt(config.irc, "frontend", "nickservPassword")
  221. -> decrypts config.irc["frontend"]["nickservPassword"]
  222. """
  223. self._decryptable_nodes.append((node, nodes))
  224. if self.is_encrypted():
  225. self._decrypt(node, nodes)
  226. def schedule(self, minute, hour, month_day, month, week_day):
  227. """Return a list of tasks scheduled to run at the specified time.
  228. The schedule data comes from our config file's 'schedule' field, which
  229. is stored as self._data["schedule"]. Call this function as
  230. config.schedule(args).
  231. """
  232. # Tasks to run this turn, each as a list of either [task_name, kwargs],
  233. # or just the task_name:
  234. tasks = []
  235. now = {"minute": minute, "hour": hour, "month_day": month_day,
  236. "month": month, "week_day": week_day}
  237. data = self._data.get("schedule", [])
  238. for event in data:
  239. do = True
  240. for key, value in now.items():
  241. try:
  242. requirement = event[key]
  243. except KeyError:
  244. continue
  245. if requirement != value:
  246. do = False
  247. break
  248. if do:
  249. try:
  250. tasks.extend(event["tasks"])
  251. except KeyError:
  252. pass
  253. return tasks
  254. class _ConfigNode(object):
  255. def __iter__(self):
  256. for key in self.__dict__:
  257. yield key
  258. def __getitem__(self, item):
  259. return self.__dict__.__getitem__(item)
  260. def _dump(self):
  261. data = self.__dict__.copy()
  262. for key, val in data.iteritems():
  263. if isinstance(val, _ConfigNode):
  264. data[key] = val._dump()
  265. return data
  266. def _load(self, data):
  267. self.__dict__ = data.copy()
  268. def _decrypt(self, cipher, intermediates, item):
  269. base = self.__dict__
  270. for inter in intermediates:
  271. try:
  272. base = base[inter]
  273. except KeyError:
  274. return
  275. if item in base:
  276. ciphertext = base[item].decode("hex")
  277. base[item] = cipher.decrypt(ciphertext).rstrip("\x00")
  278. def get(self, *args, **kwargs):
  279. return self.__dict__.get(*args, **kwargs)
  280. def keys(self):
  281. return self.__dict__.keys()
  282. def values(self):
  283. return self.__dict__.values()
  284. def items(self):
  285. return self.__dict__.items()
  286. def iterkeys(self):
  287. return self.__dict__.iterkeys()
  288. def itervalues(self):
  289. return self.__dict__.itervalues()
  290. def iteritems(self):
  291. return self.__dict__.iteritems()
  292. class _BotFormatter(logging.Formatter):
  293. def __init__(self, color=False):
  294. self._format = super(_BotFormatter, self).format
  295. if color:
  296. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  297. self.format = lambda record: self._format(self.format_color(record))
  298. else:
  299. fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s"
  300. self.format = self._format
  301. datefmt = "%Y-%m-%d %H:%M:%S"
  302. super(_BotFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  303. def format_color(self, record):
  304. l = record.levelname.ljust(8)
  305. if record.levelno == logging.DEBUG:
  306. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  307. if record.levelno == logging.INFO:
  308. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  309. if record.levelno == logging.WARNING:
  310. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  311. if record.levelno == logging.ERROR:
  312. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  313. if record.levelno == logging.CRITICAL:
  314. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  315. return record