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.

256 lines
9.2 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. EarwigBot's XML Config File Parser
  4. This handles all tasks involving reading and writing to our config file,
  5. including encrypting and decrypting passwords and making a new config file from
  6. scratch at the inital bot run.
  7. Usually you'll just want to do "from core.config import config" and access
  8. config data from within that object.
  9. """
  10. from collections import defaultdict
  11. from os import makedirs, path
  12. from xml.dom import minidom
  13. from xml.parsers.expat import ExpatError
  14. from lib import blowfish
  15. script_dir = path.dirname(path.abspath(__file__))
  16. root_dir = path.split(script_dir)[0]
  17. config_path = path.join(root_dir, "config.xml")
  18. _config = None # holds the parsed DOM object for our config file
  19. config = None # holds an instance of Container() with our config data
  20. class ConfigParseError(Exception):
  21. """Base exception for when we could not parse the config file."""
  22. class TypeMismatchError(ConfigParseError):
  23. """A field does not fit to its expected type; e.g., an arbitrary string
  24. where we expected a boolean or integer."""
  25. class MissingElementError(ConfigParseError):
  26. """An element in the config file is missing a required sub-element."""
  27. class MissingAttributeError(ConfigParseError):
  28. """An element is missing a required attribute to be parsed correctly."""
  29. class Container(object):
  30. """A class to hold information in a nice, accessable manner."""
  31. def _load_config():
  32. """Load data from our XML config file (config.xml) into a DOM object."""
  33. global _config
  34. _config = minidom.parse(config_path)
  35. def verify_config():
  36. """Check to see if we have a valid config file, and if not, notify the
  37. user. If there is no config file at all, offer to make one; otherwise,
  38. exit."""
  39. if path.exists(config_path):
  40. try:
  41. _load_config()
  42. except ExpatError as error:
  43. print "Could not parse config file {0}:\n{1}".format(config_path,
  44. error)
  45. exit()
  46. else:
  47. if not _config.getElementsByTagName("config"):
  48. e = "Config file is missing a <config> tag."
  49. raise MissingElementError(e)
  50. return are_passwords_encrypted()
  51. else:
  52. print "You haven't configured the bot yet!"
  53. choice = raw_input("Would you like to do this now? [y/n] ")
  54. if choice.lower().startswith("y"):
  55. return make_new_config()
  56. else:
  57. exit()
  58. def make_new_config():
  59. """Make a new XML config file based on the user's input."""
  60. makedirs(config_dir)
  61. encrypt = raw_input("Would you like to encrypt passwords stored in " +
  62. "config.xml? [y/n] ")
  63. if encrypt.lower().startswith("y"):
  64. is_encrypted = True
  65. else:
  66. is_encrypted = False
  67. return is_encrypted
  68. def are_passwords_encrypted():
  69. """Determine if the passwords in our config file are encrypted, returning
  70. either True or False."""
  71. element = _config.getElementsByTagName("config")[0]
  72. return attribute_to_bool(element, "encrypt-passwords", default=False)
  73. def attribute_to_bool(element, attribute, default=None):
  74. """Return True if the value of element's attribute is 'true', '1', or 'on';
  75. return False if it is 'false', '0', or 'off' (regardless of
  76. capitalization); return default if it is empty; raise TypeMismatchError if
  77. it does match any of those."""
  78. value = element.getAttribute(attribute).lower()
  79. if value in ["true", "1", "on"]:
  80. return True
  81. elif value in ["false", "0", "off"]:
  82. return False
  83. elif value == '':
  84. return default
  85. else:
  86. e = ("Expected a bool in attribute '{0}' of element '{1}', but " +
  87. "got '{2}'.").format(attribute, element.tagName, value)
  88. raise TypeMismatchError(e)
  89. def get_first_element(parent, tag_name):
  90. """Return the first child of the parent element with the given tag name, or
  91. return None if no child of that name exists."""
  92. try:
  93. return parent.getElementsByTagName(tag_name)[0]
  94. except IndexError:
  95. return None
  96. def get_required_element(parent, tag_name):
  97. """Return the first child of the parent element with the given tag name, or
  98. raise MissingElementError() if no child of that name exists."""
  99. element = get_first_element(parent, tag_name)
  100. if not element:
  101. e = "A <{0}> tag is missing a required <{1}> child tag.".format(
  102. parent.tagName, tag_name)
  103. raise MissingElementError(e)
  104. return element
  105. def get_required_attribute(element, attr_name):
  106. """Return the value of the attribute 'attr_name' in 'element'. If
  107. undefined, raise MissingAttributeError()."""
  108. attribute = element.getAttribute(attr_name)
  109. if not attribute:
  110. e = "A <{0}> tag is missing the required attribute '{1}'.".format(
  111. element.tagName, attr_name)
  112. raise MissingAttributeError(e)
  113. return attribute
  114. def parse_config(key):
  115. """A thin wrapper for the actual config parser in _parse_config(): catch
  116. parsing exceptions and report them to the user cleanly."""
  117. try:
  118. _parse_config(key)
  119. except ConfigParseError as error:
  120. print "\nError parsing config file:"
  121. print error
  122. exit(1)
  123. except blowfish.BlowfishError as error:
  124. print "\nError decrypting passwords:"
  125. print error
  126. exit(1)
  127. def _parse_config(key):
  128. """Parse config data from a DOM object into the 'config' global variable.
  129. The key is used to unencrypt passwords stored in the XML config file."""
  130. _load_config() # we might be re-loading unnecessarily here, but no harm in
  131. # that!
  132. data = _config.getElementsByTagName("config")[0]
  133. cfg = Container()
  134. cfg.components = parse_components(data)
  135. cfg.wiki = parse_wiki(data, key)
  136. cfg.irc = parse_irc(data, key)
  137. cfg.schedule = parse_schedule(data)
  138. cfg.watcher = parse_watcher(data)
  139. global config
  140. config = cfg
  141. def parse_components(data):
  142. """Parse everything within the <components> XML tag of our config file.
  143. The components object here will exist as config.components, and is a dict
  144. of our enabled components: components[name] = True if it is enabled, False
  145. if it is disabled."""
  146. components = defaultdict(lambda: False) # all components are disabled by
  147. # default
  148. element = get_required_element(data, "components")
  149. for component in element.getElementsByTagName("component"):
  150. name = get_required_attribute(component, "name")
  151. components[name] = True
  152. return components
  153. def parse_wiki(data, key):
  154. """Parse everything within the <wiki> tag of our XML config file."""
  155. pass
  156. def parse_irc_server(data, key):
  157. """Parse everything within a <server> tag."""
  158. server = Container()
  159. connection = get_required_element(data, "connection")
  160. server.host = get_required_attribute(connection, "host")
  161. server.port = get_required_attribute(connection, "port")
  162. server.nick = get_required_attribute(connection, "nick")
  163. server.ident = get_required_attribute(connection, "ident")
  164. server.realname = get_required_attribute(connection, "realname")
  165. nickserv = get_first_element(data, "nickserv")
  166. if nickserv:
  167. server.nickserv = Container()
  168. server.nickserv.username = get_required_attribute(nickserv, "username")
  169. password = get_required_attribute(nickserv, "password")
  170. if are_passwords_encrypted():
  171. server.nickserv.password = blowfish.decrypt(key, password)
  172. else:
  173. server.nickserv.password = password
  174. channels = get_first_element(data, "channels")
  175. if channels:
  176. server.channels = list()
  177. for channel in channels.getElementsByTagName("channel"):
  178. name = get_required_attribute(channel, "name")
  179. server.channels.append(name)
  180. return server
  181. def parse_irc(data, key):
  182. """Parse everything within the <irc> tag of our XML config file."""
  183. irc = Container()
  184. element = get_first_element(data, "irc")
  185. if not element:
  186. return irc
  187. servers = get_first_element(element, "servers")
  188. if servers:
  189. for server in servers.getElementsByTagName("server"):
  190. server_name = get_required_attribute(server, "name")
  191. if server_name == "frontend":
  192. irc.frontend = parse_irc_server(server, key)
  193. elif server_name == "watcher":
  194. irc.watcher = parse_irc_server(server, key)
  195. else:
  196. print ("Warning: config file specifies a <server> with " +
  197. "unknown name '{0}'. Ignoring.").format(server_name)
  198. permissions = get_first_element(element, "permissions")
  199. if permissions:
  200. irc.permissions = dict()
  201. for group in permissions.getElementsByTagName("group"):
  202. group_name = get_required_attribute(group, "name")
  203. irc.permissions[group_name] = list()
  204. for user in group.getElementsByTagName("user"):
  205. hostname = get_required_attribute(user, "host")
  206. irc.permissions[group_name].append(hostname)
  207. return irc
  208. def parse_schedule(data):
  209. """Parse everything within the <schedule> tag of our XML config file."""
  210. pass
  211. def parse_watcher(data):
  212. """Parse everything within the <watcher> tag of our XML config file."""
  213. pass