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.

156 lines
6.0 KiB

  1. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import re
  21. import threading
  22. from earwigbot.commands import Command
  23. class Threads(Command):
  24. """Manage wiki tasks from IRC, and check on thread status."""
  25. name = "threads"
  26. commands = ["tasks", "task", "threads", "tasklist"]
  27. def process(self, data):
  28. self.data = data
  29. if not self.config.irc["permissions"].is_owner(data):
  30. msg = "You must be a bot owner to use this command."
  31. self.reply(data, msg)
  32. return
  33. if not data.args:
  34. if data.command == "tasklist":
  35. self.do_list()
  36. else:
  37. msg = "No arguments provided. Maybe you wanted '!{0} list', '!{0} start', or '!{0} listall'?"
  38. self.reply(data, msg.format(data.command))
  39. return
  40. if data.args[0] == "list":
  41. self.do_list()
  42. elif data.args[0] == "start":
  43. self.do_start()
  44. elif data.args[0] in ["listall", "all"]:
  45. self.do_listall()
  46. else: # They asked us to do something we don't know
  47. msg = f"Unknown argument: \x0303{data.args[0]}\x0f."
  48. self.reply(data, msg)
  49. def do_list(self):
  50. """With !tasks list (or abbreviation !tasklist), list all running
  51. threads. This includes the main threads, like the irc frontend and the
  52. watcher, and task threads."""
  53. threads = threading.enumerate()
  54. normal_threads = []
  55. daemon_threads = []
  56. for thread in threads:
  57. tname = thread.name
  58. ident = thread.ident % 10000
  59. if tname == "MainThread":
  60. t = "\x0302main\x0f (id {0})"
  61. normal_threads.append(t.format(ident))
  62. elif tname in self.config.components:
  63. t = "\x0302{0}\x0f (id {1})"
  64. normal_threads.append(t.format(tname, ident))
  65. elif tname.startswith("cvworker-"):
  66. t = "\x0302copyvio worker\x0f (site {0})"
  67. daemon_threads.append(t.format(tname[len("cvworker-") :]))
  68. else:
  69. match = re.findall(r"^(.*?) \((.*?)\)$", tname)
  70. if match:
  71. t = "\x0302{0}\x0f (id {1}, since {2})"
  72. thread_info = t.format(match[0][0], ident, match[0][1])
  73. daemon_threads.append(thread_info)
  74. else:
  75. t = "\x0302{0}\x0f (id {1})"
  76. daemon_threads.append(t.format(tname, ident))
  77. if daemon_threads:
  78. if len(daemon_threads) > 1:
  79. msg = "\x02{0}\x0f threads active: {1}, and \x02{2}\x0f command/task threads: {3}."
  80. else:
  81. msg = "\x02{0}\x0f threads active: {1}, and \x02{2}\x0f command/task thread: {3}."
  82. msg = msg.format(
  83. len(threads),
  84. ", ".join(normal_threads),
  85. len(daemon_threads),
  86. ", ".join(daemon_threads),
  87. )
  88. else:
  89. msg = "\x02{0}\x0f threads active: {1}, and \x020\x0f command/task threads."
  90. msg = msg.format(len(threads), ", ".join(normal_threads))
  91. self.reply(self.data, msg)
  92. def do_listall(self):
  93. """With !tasks listall or !tasks all, list all loaded tasks, and report
  94. whether they are currently running or idle."""
  95. threads = threading.enumerate()
  96. tasklist = []
  97. for task in sorted([task.name for task in self.bot.tasks]):
  98. threadlist = [t for t in threads if t.name.startswith(task)]
  99. ids = [str(t.ident) for t in threadlist]
  100. if not ids:
  101. tasklist.append(f"\x0302{task}\x0f (idle)")
  102. elif len(ids) == 1:
  103. t = "\x0302{0}\x0f (\x02active\x0f as id {1})"
  104. tasklist.append(t.format(task, ids[0]))
  105. else:
  106. t = "\x0302{0}\x0f (\x02active\x0f as ids {1})"
  107. tasklist.append(t.format(task, ", ".join(ids)))
  108. tasks = ", ".join(tasklist)
  109. msg = f"\x02{len(tasklist)}\x0f tasks loaded: {tasks}."
  110. self.reply(self.data, msg)
  111. def do_start(self):
  112. """With !tasks start, start any loaded task by name with or without
  113. kwargs."""
  114. data = self.data
  115. try:
  116. task_name = data.args[1]
  117. except IndexError: # No task name given
  118. self.reply(data, "What task do you want me to start?")
  119. return
  120. if task_name not in [task.name for task in self.bot.tasks]:
  121. # This task does not exist or hasn't been loaded:
  122. msg = "Task could not be found; either it doesn't exist, or it wasn't loaded correctly."
  123. self.reply(data, msg.format(task_name))
  124. return
  125. data.kwargs["fromIRC"] = True
  126. data.kwargs["_IRCCallback"] = lambda: self.reply(
  127. data, f"Task \x0302{task_name}\x0f finished."
  128. )
  129. self.bot.tasks.start(task_name, **data.kwargs)
  130. msg = f"Task \x0302{task_name}\x0f started."
  131. self.reply(data, msg)