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.

123 lines
5.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. __all__ = ["Command"]
  23. class Command(object):
  24. """
  25. **EarwigBot: Base IRC Command**
  26. This package provides built-in IRC "commands" used by the bot's front-end
  27. component. Additional commands can be installed as plugins in the bot's
  28. working directory.
  29. This class (import with ``from earwigbot.commands import Command``), can be
  30. subclassed to create custom IRC commands.
  31. This docstring is reported to the user when they type ``"!help
  32. <command>"``.
  33. """
  34. # The command's name, as reported to the user when they use !help:
  35. name = None
  36. # A list of names that will trigger this command. If left empty, it will
  37. # be triggered by the command's name and its name only:
  38. commands = []
  39. # Hooks are "msg", "msg_private", "msg_public", and "join". "msg" is the
  40. # default behavior; if you wish to override that, change the value in your
  41. # command subclass:
  42. hooks = ["msg"]
  43. def __init__(self, bot):
  44. """Constructor for new commands.
  45. This is called once when the command is loaded (from
  46. :py:meth:`commands.load() <earwigbot.managers._ResourceManager.load>`).
  47. *bot* is out base :py:class:`~earwigbot.bot.Bot` object. Don't override
  48. this directly; if you do, remember to place
  49. ``super(Command, self).__init()`` first. Use :py:meth:`setup` for
  50. typical command-init/setup needs.
  51. """
  52. self.bot = bot
  53. self.config = bot.config
  54. self.logger = bot.commands.logger.getChild(self.name)
  55. # Convenience functions:
  56. self.say = lambda target, msg, hidelog=False: self.bot.frontend.say(target, msg, hidelog)
  57. self.reply = lambda data, msg, hidelog=False: self.bot.frontend.reply(data, msg, hidelog)
  58. self.action = lambda target, msg, hidelog=False: self.bot.frontend.action(target, msg, hidelog)
  59. self.notice = lambda target, msg, hidelog=False: self.bot.frontend.notice(target, msg, hidelog)
  60. self.join = lambda chan, hidelog=False: self.bot.frontend.join(chan, hidelog)
  61. self.part = lambda chan, msg=None, hidelog=False: self.bot.frontend.part(chan, msg, hidelog)
  62. self.mode = lambda t, level, msg, hidelog=False: self.bot.frontend.mode(t, level, msg, hidelog)
  63. self.ping = lambda target, hidelog=False: self.bot.frontend.ping(target, hidelog)
  64. self.pong = lambda target, hidelog=False: self.bot.frontend.pong(target, hidelog)
  65. self.setup()
  66. def __repr__(self):
  67. """Return the canonical string representation of the Command."""
  68. res = "Command(name={0!r}, commands={1!r}, hooks={2!r}, bot={3!r})"
  69. return res.format(self.name, self.commands, self.hooks, self.bot)
  70. def __str__(self):
  71. """Return a nice string representation of the Command."""
  72. return "<Command {0} of {1}>".format(self.name, self.bot)
  73. def setup(self):
  74. """Hook called immediately after the command is loaded.
  75. Does nothing by default; feel free to override.
  76. """
  77. pass
  78. def check(self, data):
  79. """Return whether this command should be called in response to *data*.
  80. Given a :py:class:`~earwigbot.irc.data.Data` instance, return ``True``
  81. if we should respond to this activity, or ``False`` if we should ignore
  82. it and move on. Be aware that since this is called for each message
  83. sent on IRC, it should be cheap to execute and unlikely to throw
  84. exceptions.
  85. Most commands return ``True`` only if :py:attr:`data.command
  86. <earwigbot.irc.data.Data.command>` ``==`` :py:attr:`self.name <name>`,
  87. or :py:attr:`data.command <earwigbot.irc.data.Data.command>` is in
  88. :py:attr:`self.commands <commands>` if that list is overriden. This is
  89. the default behavior; you should only override it if you wish to change
  90. that.
  91. """
  92. if self.commands:
  93. return data.is_command and data.command in self.commands
  94. return data.is_command and data.command == self.name
  95. def process(self, data):
  96. """Main entry point for doing a command.
  97. Handle an activity (usually a message) on IRC. At this point, thanks
  98. to :py:meth:`check` which is called automatically by the command
  99. handler, we know this is something we should respond to. Place your
  100. command's body here.
  101. """
  102. pass