A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

151 líneas
5.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 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. import threading
  23. import re
  24. from earwigbot.commands import BaseCommand
  25. from earwigbot.exceptions import KwargParseError
  26. class Command(BaseCommand):
  27. """Manage wiki tasks from IRC, and check on thread status."""
  28. name = "threads"
  29. def check(self, data):
  30. commands = ["tasks", "task", "threads", "tasklist"]
  31. return data.is_command and data.command in commands
  32. def process(self, data):
  33. self.data = data
  34. if data.host not in self.config.irc["permissions"]["owners"]:
  35. msg = "you must be a bot owner to use this command."
  36. self.reply(data, msg)
  37. return
  38. if not data.args:
  39. if data.command == "tasklist":
  40. self.do_list()
  41. else:
  42. msg = "no arguments provided. Maybe you wanted '!{0} list', '!{0} start', or '!{0} listall'?"
  43. self.reply(data, msg.format(data.command))
  44. return
  45. if data.args[0] == "list":
  46. self.do_list()
  47. elif data.args[0] == "start":
  48. self.do_start()
  49. elif data.args[0] in ["listall", "all"]:
  50. self.do_listall()
  51. else: # They asked us to do something we don't know
  52. msg = "unknown argument: \x0303{0}\x0301.".format(data.args[0])
  53. self.reply(data, msg)
  54. def do_list(self):
  55. """With !tasks list (or abbreviation !tasklist), list all running
  56. threads. This includes the main threads, like the irc frontend and the
  57. watcher, and task threads."""
  58. threads = threading.enumerate()
  59. normal_threads = []
  60. task_threads = []
  61. for thread in threads:
  62. tname = thread.name
  63. if tname == "MainThread":
  64. t = "\x0302MainThread\x0301 (id {0})"
  65. normal_threads.append(t.format(thread.ident))
  66. elif tname in self.config.components:
  67. t = "\x0302{0}\x0301 (id {1})"
  68. normal_threads.append(t.format(tname, thread.ident))
  69. elif tname.startswith("reminder"):
  70. tname = tname.replace("reminder ", "")
  71. t = "\x0302reminder\x0301 (until {0})"
  72. normal_threads.append(t.format(tname))
  73. else:
  74. tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0]
  75. t = "\x0302{0}\x0301 (id {1}, since {2})"
  76. task_threads.append(t.format(tname, thread.ident, start_time))
  77. if task_threads:
  78. msg = "\x02{0}\x0F threads active: {1}, and \x02{2}\x0F task threads: {3}."
  79. msg = msg.format(len(threads), ', '.join(normal_threads),
  80. len(task_threads), ', '.join(task_threads))
  81. else:
  82. msg = "\x02{0}\x0F threads active: {1}, and \x020\x0F task threads."
  83. msg = msg.format(len(threads), ', '.join(normal_threads))
  84. self.reply(self.data, msg)
  85. def do_listall(self):
  86. """With !tasks listall or !tasks all, list all loaded tasks, and report
  87. whether they are currently running or idle."""
  88. threads = threading.enumerate()
  89. tasklist = []
  90. for task in sorted(self.bot.tasks):
  91. threadlist = [t for t in threads if t.name.startswith(task)]
  92. ids = [str(t.ident) for t in threadlist]
  93. if not ids:
  94. tasklist.append("\x0302{0}\x0301 (idle)".format(task))
  95. elif len(ids) == 1:
  96. t = "\x0302{0}\x0301 (\x02active\x0F as id {1})"
  97. tasklist.append(t.format(task, ids[0]))
  98. else:
  99. t = "\x0302{0}\x0301 (\x02active\x0F as ids {1})"
  100. tasklist.append(t.format(task, ', '.join(ids)))
  101. tasks = ", ".join(tasklist)
  102. msg = "\x02{0}\x0F tasks loaded: {1}.".format(len(tasklist), tasks)
  103. self.reply(self.data, msg)
  104. def do_start(self):
  105. """With !tasks start, start any loaded task by name with or without
  106. kwargs."""
  107. data = self.data
  108. try:
  109. task_name = data.args[1]
  110. except IndexError: # No task name given
  111. self.reply(data, "what task do you want me to start?")
  112. return
  113. try:
  114. data.parse_kwargs()
  115. except KwargParseError, arg:
  116. msg = "error parsing argument: \x0303{0}\x0301.".format(arg)
  117. self.reply(data, msg)
  118. return
  119. if task_name not in self.bot.tasks:
  120. # This task does not exist or hasn't been loaded:
  121. msg = "task could not be found; either it doesn't exist, or it wasn't loaded correctly."
  122. self.reply(data, msg.format(task_name))
  123. return
  124. data.kwargs["fromIRC"] = True
  125. self.bot.tasks.start(task_name, **data.kwargs)
  126. msg = "task \x0302{0}\x0301 started.".format(task_name)
  127. self.reply(data, msg)