A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

142 linhas
6.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 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. import re
  23. from earwigbot.commands import Command
  24. class Access(Command):
  25. """Control and get info on who can access the bot."""
  26. name = "access"
  27. commands = ["access", "permission", "permissions", "perm", "perms"]
  28. def process(self, data):
  29. permdb = self.config.irc["permissions"]
  30. if not data.args or data.args[0] == "self":
  31. self.do_self(data, permdb)
  32. elif data.args[0] == "list":
  33. self.do_list(data, permdb)
  34. elif data.args[0] == "add":
  35. self.do_add(data, permdb)
  36. elif data.args[0] == "remove":
  37. self.do_remove(data, permdb)
  38. elif data.args[0] == "help":
  39. self.reply(data, "Subcommands are self, list, add, and remove.")
  40. else:
  41. msg = "Unknown subcommand \x0303{0}\x0F. Subcommands are self, list, add, remove."
  42. self.reply(data, msg.format(data.args[0]))
  43. def do_self(self, data, permdb):
  44. if permdb.is_owner(data):
  45. msg = "You are a bot owner (matching rule \x0302{0}\x0F)."
  46. self.reply(data, msg.format(permdb.is_owner(data)))
  47. elif permdb.is_admin(data):
  48. msg = "You are a bot admin (matching rule \x0302{0}\x0F)."
  49. self.reply(data, msg.format(permdb.is_admin(data)))
  50. else:
  51. self.reply(data, "You do not match any bot access rules.")
  52. def do_list(self, data, permdb):
  53. if len(data.args) > 1:
  54. if data.args[1] in ["owner", "owners"]:
  55. name, rules = "owners", permdb.users.get(permdb.OWNER)
  56. elif data.args[1] in ["admin", "admins"]:
  57. name, rules = "admins", permdb.users.get(permdb.ADMIN)
  58. else:
  59. msg = "Unknown access level \x0302{0}\x0F."
  60. self.reply(data, msg.format(data.args[1]))
  61. return
  62. if rules:
  63. msg = "Bot {0}: {1}.".format(name, ", ".join(map(str, rules)))
  64. else:
  65. msg = "No bot {0}.".format(name)
  66. self.reply(data, msg)
  67. else:
  68. owners = len(permdb.users.get(permdb.OWNER, []))
  69. admins = len(permdb.users.get(permdb.ADMIN, []))
  70. msg = "There are \x02{0}\x0F bot owners and \x02{1}\x0F bot admins. Use '!{2} list owners' or '!{2} list admins' for details."
  71. self.reply(data, msg.format(owners, admins, data.command))
  72. def do_add(self, data, permdb):
  73. user = self.get_user_from_args(data, permdb)
  74. if user:
  75. nick, ident, host = user
  76. if data.args[1] in ["owner", "owners"]:
  77. name, level, adder = "owner", permdb.OWNER, permdb.add_owner
  78. else:
  79. name, level, adder = "admin", permdb.ADMIN, permdb.add_admin
  80. if permdb.has_exact(level, nick, ident, host):
  81. rule = "{0}!{1}@{2}".format(nick, ident, host)
  82. msg = "\x0302{0}\x0F is already a bot {1}.".format(rule, name)
  83. self.reply(data, msg)
  84. else:
  85. rule = adder(nick, ident, host)
  86. msg = "Added bot {0} \x0302{1}\x0F.".format(name, rule)
  87. self.reply(data, msg)
  88. def do_remove(self, data, permdb):
  89. user = self.get_user_from_args(data, permdb)
  90. if user:
  91. nick, ident, host = user
  92. if data.args[1] in ["owner", "owners"]:
  93. name, rmver = "owner", permdb.remove_owner
  94. else:
  95. name, rmver = "admin", permdb.remove_admin
  96. rule = rmver(nick, ident, host)
  97. if rule:
  98. msg = "Removed bot {0} \x0302{1}\x0F.".format(name, rule)
  99. self.reply(data, msg)
  100. else:
  101. rule = "{0}!{1}@{2}".format(nick, ident, host)
  102. msg = "No bot {0} matching \x0302{1}\x0F.".format(name, rule)
  103. self.reply(data, msg)
  104. def get_user_from_args(self, data, permdb):
  105. if not permdb.is_owner(data):
  106. msg = "You must be a bot owner to add or remove users to the access list."
  107. self.reply(data, msg)
  108. return
  109. levels = ["owner", "owners", "admin", "admins"]
  110. if len(data.args) == 1 or data.args[1] not in levels:
  111. msg = "Please specify an access level ('owners' or 'admins')."
  112. self.reply(data, msg)
  113. return
  114. if len(data.args) == 2:
  115. self.no_arg_error(data)
  116. return
  117. kwargs = data.kwargs
  118. if "nick" in kwargs or "ident" in kwargs or "host" in kwargs:
  119. nick = kwargs.get("nick", "*")
  120. ident = kwargs.get("ident", "*")
  121. host = kwargs.get("host", "*")
  122. return nick, ident, host
  123. user = re.match(r"(.*?)!(.*?)@(.*?)$", data.args[2])
  124. if not user:
  125. self.no_arg_error(data)
  126. return
  127. return user.group(1), user.group(2), user.group(3)
  128. def no_arg_error(self, data):
  129. msg = 'Please specify a user, either as "\x0302nick\x0F!\x0302ident\x0F@\x0302host\x0F"'
  130. msg += ' or "nick=\x0302nick\x0F, ident=\x0302ident\x0F, host=\x0302host\x0F".'
  131. self.reply(data, msg)