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.

94 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Manage wiki tasks from IRC, and check on thread status.
  3. import threading
  4. from irc.base_command import BaseCommand
  5. from wiki import task_manager
  6. from config.main import *
  7. from config.irc import *
  8. class Tasks(BaseCommand):
  9. def get_hooks(self):
  10. return ["msg"]
  11. def get_help(self, command):
  12. return "Manage wiki tasks from IRC, and check on thread status."
  13. def check(self, data):
  14. if data.is_command and data.command in ["tasks", "threads", "tasklist"]:
  15. return True
  16. return False
  17. def process(self, data):
  18. self.data = data
  19. if data.host not in OWNERS:
  20. self.connection.reply(data, "at this time, you must be a bot owner to use this command.")
  21. return
  22. if not data.args:
  23. if data.command == "tasklist":
  24. self.do_list()
  25. else:
  26. self.connection.reply(data, "no arguments provided. Maybe you wanted '!{cmnd} list', '!{cmnd} start', or '!{cmnd} listall'?".format(cmnd=data.command))
  27. return
  28. if data.args[0] == "list":
  29. self.do_list()
  30. elif data.args[0] == "start":
  31. self.do_start()
  32. elif data.args[0] == "listall":
  33. self.do_listall()
  34. else: # they asked us to do something we don't know
  35. self.connection.reply(data, "unknown argument: \x0303{}\x0301.".format(data.args[0]))
  36. def do_list(self):
  37. threads = threading.enumerate()
  38. normal_threads = []
  39. task_threads = []
  40. task_thread_num = 0
  41. for thread in threads:
  42. tname = thread.name
  43. if tname == "MainThread":
  44. tname = self.get_main_thread_name()
  45. normal_threads.append("\x0302{}\x0301 (as main thread, id {})".format(tname, thread.ident))
  46. elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]:
  47. normal_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident))
  48. else:
  49. task_thread_num += 1
  50. task_threads.append("\x0302{}\x0301 (id {})".format(tname, thread.ident))
  51. if task_threads:
  52. msg = "\x02{}\x0F threads active: {}, and \x02{}\x0F task threads: {}.".format(len(threads), ', '.join(normal_threads), task_thread_num, ', '.join(task_threads))
  53. else:
  54. msg = "\x02{}\x0F threads active: {}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads))
  55. self.connection.reply(self.data, msg)
  56. def do_listall(self):
  57. tasks = task_manager.task_list.keys()
  58. self.connection.reply(self.data, ', '.join(tasks))
  59. def do_start(self):
  60. kwargs = {}
  61. try:
  62. task_manager.start_task(self.data.args[1], **kwargs)
  63. except IndexError: # no task name given
  64. self.connection.reply(self.data, "what task do you want me to start?")
  65. else:
  66. self.connection.reply(self.data, "task '{}' started.".format(self.data.args[1]))
  67. def get_main_thread_name(self):
  68. """Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher"."""
  69. if enable_irc_frontend:
  70. return "irc-frontend"
  71. elif enable_wiki_schedule:
  72. return "wiki-scheduler"
  73. else:
  74. return "irc-watcher"