2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on check_mode
|
|
|
|
#--depends-on commands
|
2019-11-22 11:48:29 +00:00
|
|
|
#--depends-on permissions
|
2019-05-25 20:40:06 +00:00
|
|
|
|
2019-01-28 23:50:43 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
_name = "ChanAccess"
|
|
|
|
|
2019-01-29 15:33:55 +00:00
|
|
|
def _has_channel_access(self, target, user, require_access):
|
2019-01-29 15:35:38 +00:00
|
|
|
access = target.get_user_setting(user.get_id(), "access", [])
|
2019-11-22 11:48:29 +00:00
|
|
|
identified = self.exports.get_one("is-identified")(user)
|
2019-01-29 15:33:55 +00:00
|
|
|
|
2019-11-27 21:53:33 +00:00
|
|
|
return (require_access in access or "*" in access) and identified
|
2019-01-29 15:33:55 +00:00
|
|
|
|
2019-06-14 16:23:22 +00:00
|
|
|
def _command_check(self, event, target, require_access):
|
2019-01-30 11:16:09 +00:00
|
|
|
if event["is_channel"]:
|
|
|
|
if require_access:
|
2019-06-14 16:23:22 +00:00
|
|
|
if self._has_channel_access(target, event["user"],
|
2019-01-30 11:16:09 +00:00
|
|
|
require_access):
|
2019-09-26 11:14:55 +00:00
|
|
|
return utils.consts.PERMISSION_FORCE_SUCCESS, None
|
2019-01-30 11:16:09 +00:00
|
|
|
else:
|
2019-09-26 11:14:55 +00:00
|
|
|
return (utils.consts.PERMISSION_ERROR,
|
|
|
|
"You do not have permission to do this")
|
2019-01-28 23:50:43 +00:00
|
|
|
|
2019-06-14 16:23:22 +00:00
|
|
|
@utils.hook("preprocess.command")
|
|
|
|
def preprocess_command(self, event):
|
|
|
|
require_access = event["hook"].get_kwarg("require_access")
|
|
|
|
if require_access:
|
|
|
|
return self._command_check(event, event["target"], require_access)
|
|
|
|
|
|
|
|
@utils.hook("check.command.channel-access")
|
|
|
|
def check_command(self, event):
|
2019-06-16 19:45:14 +00:00
|
|
|
target = event["target"]
|
|
|
|
access = event["request_args"][0]
|
|
|
|
if len(event["request_args"]) > 1:
|
|
|
|
target = event["request_args"][0]
|
|
|
|
access = event["request_args"][1]
|
|
|
|
|
|
|
|
return self._command_check(event, target, access)
|
2019-01-29 15:33:55 +00:00
|
|
|
|
2019-01-28 23:50:43 +00:00
|
|
|
@utils.hook("received.command.access", min_args=1, channel_only=True)
|
|
|
|
def access(self, event):
|
|
|
|
"""
|
|
|
|
:help: Show/modify channel access for a user
|
|
|
|
:usage: list <nickname>
|
|
|
|
:usage: add <nickname> <permission1 permission2 ...>
|
|
|
|
:usage: remove <nickname> <permission1 permission2 ...>
|
|
|
|
:usage: set <nickname> <permission1 permission2 ...>
|
|
|
|
:require_mode: high
|
|
|
|
"""
|
|
|
|
subcommand = event["args_split"][0].lower()
|
|
|
|
target = event["server"].get_user(event["args_split"][1])
|
2019-01-29 00:00:15 +00:00
|
|
|
access = event["target"].get_user_setting(target.get_id(), "access", [])
|
2019-01-28 23:50:43 +00:00
|
|
|
|
|
|
|
if subcommand == "list":
|
|
|
|
event["stdout"].write("Access for %s: %s" % (target.nickname,
|
|
|
|
" ".join(access)))
|
|
|
|
elif subcommand == "set":
|
|
|
|
if not len(event["args_split"]) > 2:
|
|
|
|
raise utils.EventError("Please provide a list of permissions")
|
2019-01-29 00:00:15 +00:00
|
|
|
event["target"].set_user_setting(target.get_id(), "access",
|
2019-01-28 23:50:43 +00:00
|
|
|
event["args_split"][2:])
|
|
|
|
elif subcommand == "add":
|
|
|
|
if not len(event["args_split"]) > 2:
|
|
|
|
raise utils.EventError("Please provide a list of permissions")
|
|
|
|
for acc in event["args_split"][2:]:
|
|
|
|
if acc in access:
|
|
|
|
raise utils.EventError("%s already has '%s' permission" % (
|
|
|
|
target.nickname, acc))
|
|
|
|
access.append(acc)
|
2019-01-29 00:00:15 +00:00
|
|
|
event["target"].set_user_setting(target.get_id(), "access", access)
|
2019-01-28 23:50:43 +00:00
|
|
|
event["stdout"].write("Added permission to %s: %s" % (
|
|
|
|
target.nickname, " ".join(event["args_split"][2:])))
|
|
|
|
elif subcommand == "remove":
|
|
|
|
if not len(event["args_split"]) > 2:
|
|
|
|
raise utils.EventError("Please provide a list of permissions")
|
|
|
|
for acc in event["args_split"][2:]:
|
|
|
|
if not acc in access:
|
|
|
|
raise utils.EventError("%s does not have '%s' permission" %
|
|
|
|
(target.nickname, acc))
|
|
|
|
access.remove(acc)
|
2019-01-29 00:02:56 +00:00
|
|
|
if access:
|
|
|
|
event["target"].set_user_setting(target.get_id(), "access",
|
|
|
|
access)
|
|
|
|
else:
|
|
|
|
event["target"].del_user_setting(target.get_id(), "access")
|
2019-01-28 23:50:43 +00:00
|
|
|
event["stdout"].write("Removed permission from %s: %s" % (
|
|
|
|
target.nickname, " ".join(event["args_split"][2:])))
|
2019-01-29 00:14:33 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown command '%s'" % subcommand)
|