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.

197 lines
8.0 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. __all__ = ["ConfigScript"]
  35. class ConfigScript(object):
  36. """A script to guide a user through the creation of a new config file."""
  37. WIDTH = 79
  38. BCRYPT_ROUNDS = 12
  39. def __init__(self, config):
  40. self.config = config
  41. self.data = OrderedDict(
  42. ("metadata", OrderedDict()),
  43. ("components", OrderedDict()),
  44. ("wiki", OrderedDict()),
  45. ("irc", OrderedDict()),
  46. ("commands", OrderedDict()),
  47. ("tasks", OrderedDict()),
  48. ("schedule", [])
  49. )
  50. def _print(self, msg):
  51. print fill(re.sub("\s\s+", " ", msg), self.WIDTH)
  52. def _ask_bool(self, text, default=True):
  53. text = "> " + text
  54. if default:
  55. text += " [Y/n]"
  56. else:
  57. text += " [y/N]"
  58. lines = wrap(re.sub("\s\s+", " ", msg), self.WIDTH)
  59. if len(lines) > 1:
  60. print "\n".join(lines[:-1])
  61. while True:
  62. answer = raw_input(lines[-1] + " ").lower()
  63. if not answer:
  64. return default
  65. if answer.startswith("y"):
  66. return True
  67. if answer.startswith("n"):
  68. return False
  69. def _set_metadata(self):
  70. print
  71. self.data["metadata"] = OrderedDict(("version", 1))
  72. self._print("""I can encrypt passwords stored in your config file in
  73. addition to preventing other users on your system from
  74. reading the file. Encryption is recommended is the bot
  75. is to run on a public computer like the Toolserver, but
  76. otherwise the need to enter a key everytime you start
  77. the bot may be annoying.""")
  78. if self._ask_bool("Encrypt stored passwords?"):
  79. self.data["metadata"]["encryptPasswords"] = True
  80. key = getpass("> Enter an encryption key: ")
  81. print "Running {0} rounds of bcrypt...".format(self.BCRYPT_ROUNDS),
  82. signature = bcrypt.hashpw(key, bcrypt.gensalt(self.BCRYPT_ROUNDS))
  83. self.data["metadata"]["signature"] = signature
  84. print " done."
  85. else:
  86. self.data["metadata"]["encryptPasswords"] = False
  87. self._print("""The bot can temporarily store its logs in the logs/
  88. subdirectory. Error logs are kept for a month whereas
  89. normal logs are kept for a week. If you disable this,
  90. the bot will still print logs to stdout.""")
  91. question = "Enable logging?"
  92. self.data["metadata"]["enableLogging"] = self._ask_bool(question)
  93. def _set_components(self):
  94. print
  95. self._print("""The bot contains three separate components that can run
  96. independently of each other.""")
  97. self._print("""- The IRC front-end runs on a normal IRC server, like
  98. freenode, and expects users to interact with it through
  99. commands.""")
  100. self._print("""- The IRC watcher runs on a wiki recent-changes server,
  101. like irc.wikimedia.org, and listens for edits. Users
  102. cannot interact with this component. It can detect
  103. specific events and report them to "feed" channels on
  104. the front-end, or start bot tasks.""")
  105. self._print("""- The wiki task scheduler runs wiki-editing bot tasks in
  106. separate threads at user-defined times through a
  107. cron-like interface. Tasks which are not scheduled can
  108. be started by the IRC watcher manually through the IRC
  109. front-end.""")
  110. frontend = self._ask_bool("Enable the IRC front-end?")
  111. watcher = self._ask_bool("Enable the IRC watcher?")
  112. scheduler = self._ask_bool("Enable the wiki task scheduler?")
  113. self.data["components"]["irc_frontend"] = frontend
  114. self.data["components"]["irc_watcher"] = watcher
  115. self.data["components"]["wiki_scheduler"] = scheduler
  116. def _set_wiki(self):
  117. print
  118. wmf = self._ask_bool("""Will this bot run on Wikimedia Foundation
  119. wikis, like Wikipedia?""")
  120. if wmf:
  121. sitename = ? # setup sites.db
  122. else:
  123. sitename = ? # setup sites.db
  124. self.data["wiki"]["username"] = raw_input("> Bot username: ")
  125. self.data["wiki"]["password"] = getpass("> Bot password: ")
  126. self.data["wiki"]["userAgent"] = "EarwigBot/$1 (Python/$2; https://github.com/earwig/earwigbot)"
  127. self.data["wiki"]["summary"] = "([[WP:BOT|Bot]]): $2"
  128. shutoff
  129. self.data["wiki"]["useHTTPS"] = True
  130. self.data["wiki"]["assert"] = "user"
  131. self.data["wiki"]["maxlag"] = 10
  132. self.data["wiki"]["waitTime"] = 2
  133. self.data["wiki"]["defaultSite"] = sitename
  134. ts = self._ask_bool("Will this bot run from the Wikimedia Toolserver?")
  135. if ts:
  136. args = (("host", "$1-p.rrdb.toolserver.org"), ("db": "$1_p"))
  137. self.data["wiki"]["sql"] = OrderedDict(args)
  138. else:
  139. self.data["wiki"]["sql"] = {}
  140. self.data["wiki"]["search"] = {}
  141. def _set_irc(self):
  142. # create permissions.db with us if frontend
  143. # create rules.py if watcher
  144. pass
  145. def _set_commands(self):
  146. # disable: True if no IRC frontend or prompted
  147. # create commands/
  148. pass
  149. def _set_tasks(self):
  150. # disable: True if prompted
  151. # create tasks/
  152. pass
  153. def _set_schedule(self):
  154. pass
  155. def _save(self):
  156. with open(self.config.path, "w") as fp:
  157. yaml.dump(self.data, stream=fp, default_flow_style=False)
  158. def make_new(self):
  159. """Make a new config file based on the user's input."""
  160. self._set_metadata()
  161. self._set_components()
  162. self._set_wiki()
  163. components = self.data["components"]
  164. if components["irc_frontend"] or components["irc_watcher"]:
  165. self._set_irc()
  166. self._set_commands()
  167. self._set_tasks()
  168. if components["wiki_scheduler"]:
  169. self._set_schedule()
  170. self._print("""I am now saving config.yml with your settings. YAML is a
  171. relatively straightforward format and you should be able
  172. to update these settings in the future when necessary.
  173. I will start the bot at your signal. Feel free to
  174. contact me at wikipedia.earwig at gmail.com if you have
  175. any questions.""")
  176. self._save()
  177. if not self._ask_bool("Start the bot now?"):
  178. exit()