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.

261 lines
11 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. import re
  25. from textwrap import fill, wrap
  26. try:
  27. import bcrypt
  28. except ImportError:
  29. bcrypt = None
  30. try:
  31. import yaml
  32. except ImportError:
  33. yaml = None
  34. from earwigbot import exceptions
  35. __all__ = ["ConfigScript"]
  36. class ConfigScript(object):
  37. """A script to guide a user through the creation of a new config file."""
  38. WIDTH = 79
  39. BCRYPT_ROUNDS = 12
  40. def __init__(self, config):
  41. self.config = config
  42. self.data = OrderedDict(
  43. ("metadata", OrderedDict()),
  44. ("components", OrderedDict()),
  45. ("wiki", OrderedDict()),
  46. ("irc", OrderedDict()),
  47. ("commands", OrderedDict()),
  48. ("tasks", OrderedDict()),
  49. ("schedule", [])
  50. )
  51. def _print(self, msg):
  52. print fill(re.sub("\s\s+", " ", msg), self.WIDTH)
  53. def _ask(self, text, default=None):
  54. text = "> " + text
  55. if default:
  56. text += " [{0}]".format(default)
  57. lines = wrap(re.sub("\s\s+", " ", msg), self.WIDTH)
  58. if len(lines) > 1:
  59. print "\n".join(lines[:-1])
  60. return raw_input(lines[-1] + " ") or default
  61. def _ask_bool(self, text, default=True):
  62. text = "> " + text
  63. if default:
  64. text += " [Y/n]"
  65. else:
  66. text += " [y/N]"
  67. lines = wrap(re.sub("\s\s+", " ", msg), self.WIDTH)
  68. if len(lines) > 1:
  69. print "\n".join(lines[:-1])
  70. while True:
  71. answer = raw_input(lines[-1] + " ").lower()
  72. if not answer:
  73. return default
  74. if answer.startswith("y"):
  75. return True
  76. if answer.startswith("n"):
  77. return False
  78. def _set_metadata(self):
  79. print
  80. self.data["metadata"] = OrderedDict(("version", 1))
  81. self._print("""I can encrypt passwords stored in your config file in
  82. addition to preventing other users on your system from
  83. reading the file. Encryption is recommended is the bot
  84. is to run on a public computer like the Toolserver, but
  85. otherwise the need to enter a key everytime you start
  86. the bot may be annoying.""")
  87. if self._ask_bool("Encrypt stored passwords?"):
  88. self.data["metadata"]["encryptPasswords"] = True
  89. key = getpass("> Enter an encryption key: ")
  90. print "Running {0} rounds of bcrypt...".format(self.BCRYPT_ROUNDS),
  91. signature = bcrypt.hashpw(key, bcrypt.gensalt(self.BCRYPT_ROUNDS))
  92. self.data["metadata"]["signature"] = signature
  93. print " done."
  94. else:
  95. self.data["metadata"]["encryptPasswords"] = False
  96. self._print("""The bot can temporarily store its logs in the logs/
  97. subdirectory. Error logs are kept for a month whereas
  98. normal logs are kept for a week. If you disable this,
  99. the bot will still print logs to stdout.""")
  100. question = "Enable logging?"
  101. self.data["metadata"]["enableLogging"] = self._ask_bool(question)
  102. def _set_components(self):
  103. print
  104. self._print("""The bot contains three separate components that can run
  105. independently of each other.""")
  106. self._print("""- The IRC front-end runs on a normal IRC server, like
  107. freenode, and expects users to interact with it through
  108. commands.""")
  109. self._print("""- The IRC watcher runs on a wiki recent-changes server,
  110. like irc.wikimedia.org, and listens for edits. Users
  111. cannot interact with this component. It can detect
  112. specific events and report them to "feed" channels on
  113. the front-end, or start bot tasks.""")
  114. self._print("""- The wiki task scheduler runs wiki-editing bot tasks in
  115. separate threads at user-defined times through a
  116. cron-like interface. Tasks which are not scheduled can
  117. be started by the IRC watcher manually through the IRC
  118. front-end.""")
  119. frontend = self._ask_bool("Enable the IRC front-end?")
  120. watcher = self._ask_bool("Enable the IRC watcher?")
  121. scheduler = self._ask_bool("Enable the wiki task scheduler?")
  122. self.data["components"]["irc_frontend"] = frontend
  123. self.data["components"]["irc_watcher"] = watcher
  124. self.data["components"]["wiki_scheduler"] = scheduler
  125. def _login(self, kwargs):
  126. self.config.wiki._load(self.data["wiki"])
  127. print "Trying to login to the site...",
  128. try:
  129. site = self.config.bot.wiki.add_site(**kargs)
  130. except exceptions.APIError:
  131. print " API error!"
  132. question = "Would you like to re-enter the site information?"
  133. if self._ask_bool(question):
  134. return self._set_wiki()
  135. question = "This will cancel the setup process. Are you sure?"
  136. if self._ask_bool(question, default=False):
  137. raise exceptions.NoConfigError()
  138. return self._set_wiki()
  139. except exceptions.LoginError:
  140. print " login error!"
  141. question = "Would you like to re-enter your login information?"
  142. if self._ask_bool(question):
  143. self.data["wiki"]["username"] = self._ask("Bot username:")
  144. self.data["wiki"]["password"] = getpass("> Bot password: ")
  145. return self._login(kwargs)
  146. question = "Would you like to re-enter the site information?"
  147. if self._ask_bool(question):
  148. return self._set_wiki()
  149. self._print("""Moving on. You can modify the login information
  150. stored in the bot's config in the future.""")
  151. else:
  152. print " success."
  153. return site
  154. def _set_wiki(self):
  155. print
  156. wmf = self._ask_bool("""Will this bot run on Wikimedia Foundation
  157. wikis, like Wikipedia?""")
  158. if wmf:
  159. msg = "Site project (e.g. 'wikipedia', 'wiktionary', 'wikimedia'):"
  160. project = self._ask(msg, default="wikipedia").lower()
  161. msg = "Site language code (e.g. 'en', 'fr', 'commons'):"
  162. lang = self._ask(msg, default="en").lower()
  163. kwargs = {"project": project, "lang": lang}
  164. else:
  165. msg = "Site base URL, without the script path and trailing slash;"
  166. msg += " can be protocol-insensitive (e.g. '//en.wikipedia.org'):"
  167. url = self._ask(msg)
  168. script = self._ask("Site script path:", default="/w")
  169. kwargs = {"base_url": url, "script_path": script, "sql": sql}
  170. self.data["wiki"]["username"] = self._ask("Bot username:")
  171. self.data["wiki"]["password"] = getpass("> Bot password: ")
  172. self.data["wiki"]["userAgent"] = "EarwigBot/$1 (Python/$2; https://github.com/earwig/earwigbot)"
  173. self.data["wiki"]["summary"] = "([[WP:BOT|Bot]]): $2"
  174. self.data["wiki"]["useHTTPS"] = True
  175. self.data["wiki"]["assert"] = "user"
  176. self.data["wiki"]["maxlag"] = 10
  177. self.data["wiki"]["waitTime"] = 3
  178. self.data["wiki"]["defaultSite"] = self._login(kwargs).name
  179. self.data["wiki"]["sql"] = {}
  180. if wmf:
  181. msg = "Will this bot run from the Wikimedia Toolserver?"
  182. toolserver = self._ask_bool(msg, default=False)
  183. if toolserver:
  184. args = (("host", "$1-p.rrdb.toolserver.org"), ("db": "$1_p"))
  185. self.data["wiki"]["sql"] = OrderedDict(args)
  186. self.data["wiki"]["shutoff"] = {}
  187. msg = "Would you like to enable an automatic shutoff page for the bot?"
  188. if self._ask_bool(msg):
  189. self._print("""The page title can contain two wildcards: $1 will be
  190. substituted with the bot's username, and $2 with the
  191. current task number. This can be used to implement a
  192. separate shutoff page for each task.""")
  193. page = self._ask("Page title:", default="User:$1/Shutoff")
  194. disabled = self._ask("Page content when *not* shut off:", "run")
  195. args = (("page", page), ("disabled", disabled))
  196. self.data["wiki"]["shutoff"] = OrderedDict(args)
  197. self.data["wiki"]["search"] = {}
  198. def _set_irc(self):
  199. # create permissions.db with us if frontend
  200. # create rules.py if watcher
  201. pass
  202. def _set_commands(self):
  203. # disable: True if no IRC frontend or prompted
  204. # create commands/
  205. pass
  206. def _set_tasks(self):
  207. # disable: True if prompted
  208. # create tasks/
  209. pass
  210. def _set_schedule(self):
  211. pass
  212. def _save(self):
  213. with open(self.config.path, "w") as fp:
  214. yaml.dump(self.data, stream=fp, default_flow_style=False)
  215. def make_new(self):
  216. """Make a new config file based on the user's input."""
  217. self._set_metadata()
  218. self._set_components()
  219. self._set_wiki()
  220. components = self.data["components"]
  221. if components["irc_frontend"] or components["irc_watcher"]:
  222. self._set_irc()
  223. self._set_commands()
  224. self._set_tasks()
  225. if components["wiki_scheduler"]:
  226. self._set_schedule()
  227. self._print("""I am now saving config.yml with your settings. YAML is a
  228. relatively straightforward format and you should be able
  229. to update these settings in the future when necessary.
  230. I will start the bot at your signal. Feel free to
  231. contact me at wikipedia.earwig at gmail.com if you have
  232. any questions.""")
  233. self._save()
  234. if not self._ask_bool("Start the bot now?"):
  235. exit()