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.

147 lines
5.5 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009, 2010, 2011 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. """
  23. EarwigBot's Main Module
  24. The core is essentially responsible for starting the various bot components
  25. (irc, scheduler, etc) and making sure they are all happy. An explanation of the
  26. different components follows:
  27. EarwigBot has three components that can run independently of each other: an IRC
  28. front-end, an IRC watcher, and a wiki scheduler.
  29. * The IRC front-end runs on a normal IRC server and expects users to interact
  30. with it/give it commands.
  31. * The IRC watcher runs on a wiki recent-changes server and listens for edits.
  32. Users cannot interact with this part of the bot.
  33. * The wiki scheduler runs wiki-editing bot tasks in separate threads at
  34. user-defined times through a cron-like interface.
  35. There is a "priority" system here:
  36. 1. If the IRC frontend is enabled, it will run on the main thread, and the IRC
  37. watcher and wiki scheduler (if enabled) will run on separate threads.
  38. 2. If the wiki scheduler is enabled, it will run on the main thread, and the
  39. IRC watcher (if enabled) will run on a separate thread.
  40. 3. If the IRC watcher is enabled, it will run on the main (and only) thread.
  41. Else, the bot will stop, as no components are enabled.
  42. """
  43. import logging
  44. import threading
  45. import time
  46. from earwigbot import frontend
  47. from earwigbot import tasks
  48. from earwigbot import watcher
  49. from earwigbot.config import config
  50. logger = logging.getLogger("earwigbot")
  51. f_conn = None
  52. w_conn = None
  53. def irc_watcher(f_conn=None):
  54. """Function to handle the IRC watcher as another thread (if frontend and/or
  55. scheduler is enabled), otherwise run as the main thread."""
  56. global w_conn
  57. while 1: # restart the watcher component if it breaks (and nothing else)
  58. w_conn = watcher.get_connection()
  59. w_conn.connect()
  60. try:
  61. watcher.main(w_conn, f_conn)
  62. except:
  63. logger.exception("Watcher had an error")
  64. time.sleep(5) # sleep a bit before restarting watcher
  65. logger.warn("Watcher has stopped; restarting component")
  66. def wiki_scheduler():
  67. """Function to handle the wiki scheduler as another thread, or as the
  68. primary thread if the IRC frontend is not enabled."""
  69. while 1:
  70. time_start = time.time()
  71. now = time.gmtime(time_start)
  72. tasks.schedule(now)
  73. time_end = time.time()
  74. time_diff = time_start - time_end
  75. if time_diff < 60: # sleep until the next minute
  76. time.sleep(60 - time_diff)
  77. def irc_frontend():
  78. """If the IRC frontend is enabled, make it run on our primary thread, and
  79. enable the wiki scheduler and IRC watcher on new threads if they are
  80. enabled."""
  81. global f_conn
  82. logger.info("Starting IRC frontend")
  83. f_conn = frontend.get_connection()
  84. frontend.startup(f_conn)
  85. if "wiki_schedule" in config.components:
  86. logger.info("Starting wiki scheduler")
  87. tasks.load()
  88. t_scheduler = threading.Thread(target=wiki_scheduler)
  89. t_scheduler.name = "wiki-scheduler"
  90. t_scheduler.daemon = True
  91. t_scheduler.start()
  92. if "irc_watcher" in config.components:
  93. logger.info("Starting IRC watcher")
  94. t_watcher = threading.Thread(target=irc_watcher, args=(f_conn,))
  95. t_watcher.name = "irc-watcher"
  96. t_watcher.daemon = True
  97. t_watcher.start()
  98. frontend.main()
  99. if "irc_watcher" in config.components:
  100. w_conn.close()
  101. f_conn.close()
  102. def main():
  103. if "irc_frontend" in config.components:
  104. # Make the frontend run on our primary thread if enabled, and enable
  105. # additional components through that function
  106. irc_frontend()
  107. elif "wiki_schedule" in config.components:
  108. # Run the scheduler on the main thread, but also run the IRC watcher on
  109. # another thread iff it is enabled
  110. logger.info("Starting wiki scheduler")
  111. tasks.load()
  112. if "irc_watcher" in enabled:
  113. logger.info("Starting IRC watcher")
  114. t_watcher = threading.Thread(target=irc_watcher)
  115. t_watcher.name = "irc-watcher"
  116. t_watcher.daemon = True
  117. t_watcher.start()
  118. wiki_scheduler()
  119. elif "irc_watcher" in config.components:
  120. # The IRC watcher is our only enabled component, so run its function
  121. # only and don't worry about anything else:
  122. logger.info("Starting IRC watcher")
  123. irc_watcher()
  124. else: # Nothing is enabled!
  125. logger.critical("No bot parts are enabled; stopping")
  126. exit(1)