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.

132 lines
5.4 KiB

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