2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on channel_access
|
2019-06-19 21:33:03 +00:00
|
|
|
#--depends-on check_mode
|
2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2019-04-07 13:00:27 +00:00
|
|
|
import enum
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-08-28 13:55:57 +00:00
|
|
|
|
2019-04-07 13:00:27 +00:00
|
|
|
class ConfigInvalidValue(Exception):
|
|
|
|
pass
|
2019-04-07 16:36:29 +00:00
|
|
|
class ConfigSettingInexistent(Exception):
|
|
|
|
pass
|
2019-04-07 13:00:27 +00:00
|
|
|
|
|
|
|
class ConfigResults(enum.Enum):
|
|
|
|
Changed = 1
|
|
|
|
Retrieved = 2
|
2019-04-07 16:45:35 +00:00
|
|
|
Removed = 3
|
2019-04-07 13:00:27 +00:00
|
|
|
class ConfigResult(object):
|
|
|
|
def __init__(self, result, data=None):
|
|
|
|
self.result = result
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
class ConfigChannelTarget(object):
|
|
|
|
def __init__(self, bot, server, channel_name):
|
|
|
|
self._bot = bot
|
|
|
|
self._server = server
|
|
|
|
self._channel_name = channel_name
|
|
|
|
def _get_id(self):
|
|
|
|
return self._server.channels.get_id(self._channel_name)
|
|
|
|
def set_setting(self, setting, value):
|
|
|
|
channel_id = self._get_id()
|
|
|
|
self._bot.database.channel_settings.set(channel_id, setting, value)
|
|
|
|
def get_setting(self, setting, default=None):
|
|
|
|
channel_id = self._get_id()
|
|
|
|
return self._bot.database.channel_settings.get(channel_id, setting,
|
|
|
|
default)
|
2019-04-07 16:36:29 +00:00
|
|
|
def del_setting(self, setting):
|
|
|
|
channel_id = self._get_id()
|
|
|
|
self._bot.database.channel_settings.delete(channel_id, setting)
|
2019-04-07 13:00:27 +00:00
|
|
|
|
2019-06-16 19:48:31 +00:00
|
|
|
def get_user_setting(self, user_id, setting, default=None):
|
2019-09-05 13:13:40 +00:00
|
|
|
return self._bot.database.user_channel_settings.get(user_id,
|
2019-06-16 19:48:31 +00:00
|
|
|
self._get_id(), setting, default)
|
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-05-23 10:45:35 +00:00
|
|
|
def _to_context(self, server, channel, user, context_desc):
|
|
|
|
context_desc_lower = context_desc.lower()
|
2019-09-17 09:21:48 +00:00
|
|
|
|
2019-09-17 09:25:42 +00:00
|
|
|
if context_desc == "*":
|
|
|
|
if channel == user:
|
|
|
|
# we're in PM
|
2019-09-17 16:56:56 +00:00
|
|
|
return user, "set", None
|
2019-09-17 09:25:42 +00:00
|
|
|
else:
|
|
|
|
#we're in a channel
|
2019-09-17 16:56:56 +00:00
|
|
|
return channel, "channelset", None
|
2019-09-20 13:52:41 +00:00
|
|
|
elif server.is_channel(context_desc):
|
2019-09-17 16:56:56 +00:00
|
|
|
return context_desc, "channelset", context_desc
|
2019-09-17 09:21:48 +00:00
|
|
|
elif server.irc_lower(context_desc) == user.nickname_lower:
|
2019-09-17 16:56:56 +00:00
|
|
|
return user, "set", None
|
2019-09-17 09:21:48 +00:00
|
|
|
elif "user".startswith(context_desc_lower):
|
2019-09-17 16:56:56 +00:00
|
|
|
return user, "set", None
|
2019-06-19 09:28:53 +00:00
|
|
|
elif "channel".startswith(context_desc_lower):
|
2019-09-17 16:56:56 +00:00
|
|
|
return channel, "channelset", None
|
2019-06-19 09:28:53 +00:00
|
|
|
elif "server".startswith(context_desc_lower):
|
2019-09-17 16:56:56 +00:00
|
|
|
return server, "serverset", None
|
2019-06-19 09:28:53 +00:00
|
|
|
elif "bot".startswith(context_desc_lower):
|
2019-09-17 16:56:56 +00:00
|
|
|
return self.bot, "botset", None
|
2019-05-23 10:45:35 +00:00
|
|
|
else:
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
@utils.hook("preprocess.command")
|
|
|
|
def preprocess_command(self, event):
|
|
|
|
require_setting = event["hook"].get_kwarg("require_setting", None)
|
|
|
|
if not require_setting == None:
|
|
|
|
require_setting_unless = event["hook"].get_kwarg(
|
|
|
|
"require_setting_unless", None)
|
|
|
|
if not require_setting_unless == None:
|
|
|
|
require_setting_unless = int(require_setting_unless)
|
|
|
|
if len(event["args_split"]) >= require_setting_unless:
|
|
|
|
return
|
|
|
|
|
|
|
|
context, _, require_setting = require_setting.rpartition(":")
|
|
|
|
require_setting = require_setting.lower()
|
|
|
|
channel = None
|
|
|
|
if event["is_channel"]:
|
|
|
|
channel = event["target"]
|
|
|
|
|
2019-06-14 11:27:45 +00:00
|
|
|
context = context or "user"
|
2019-09-17 16:56:56 +00:00
|
|
|
target, setting_context, _ = self._to_context(event["server"],
|
|
|
|
channel, event["user"], context)
|
2019-05-23 10:45:35 +00:00
|
|
|
|
2019-06-14 11:27:45 +00:00
|
|
|
export_settings = self._get_export_setting(setting_context)
|
2019-05-23 10:45:35 +00:00
|
|
|
setting_info = export_settings.get(require_setting, None)
|
|
|
|
if setting_info:
|
|
|
|
value = target.get_setting(require_setting, None)
|
|
|
|
if value == None:
|
2019-07-02 19:59:11 +00:00
|
|
|
example = setting_info.example or "<value>"
|
2019-09-17 09:31:28 +00:00
|
|
|
if context == "user":
|
|
|
|
context = event["user"].nickname
|
|
|
|
elif context == "channel" and not channel == None:
|
|
|
|
context = channel.name
|
|
|
|
else:
|
|
|
|
context = context[0]
|
|
|
|
|
2019-06-14 11:27:45 +00:00
|
|
|
return "Please set %s, e.g.: %sconfig %s %s %s" % (
|
2019-09-17 09:31:28 +00:00
|
|
|
require_setting, event["command_prefix"], context,
|
2019-05-23 10:45:35 +00:00
|
|
|
require_setting, example)
|
|
|
|
|
2019-04-07 13:00:27 +00:00
|
|
|
def _get_export_setting(self, context):
|
|
|
|
settings = self.exports.get_all(context)
|
2019-06-28 22:16:05 +00:00
|
|
|
return {setting.name.lower(): setting for setting in settings}
|
2019-04-07 13:00:27 +00:00
|
|
|
|
|
|
|
def _config(self, export_settings, target, setting, value=None):
|
|
|
|
if not value == None:
|
2019-06-28 22:16:05 +00:00
|
|
|
validated_value = export_settings[setting].parse(value)
|
2019-04-07 13:00:27 +00:00
|
|
|
if not validated_value == None:
|
|
|
|
target.set_setting(setting, validated_value)
|
2019-04-28 09:50:19 +00:00
|
|
|
return ConfigResult(ConfigResults.Changed, validated_value)
|
2019-04-07 13:00:27 +00:00
|
|
|
else:
|
|
|
|
raise ConfigInvalidValue()
|
|
|
|
else:
|
2019-04-07 16:38:52 +00:00
|
|
|
unset = False
|
|
|
|
if setting.startswith("-"):
|
|
|
|
setting = setting[1:]
|
|
|
|
unset = True
|
|
|
|
|
2019-04-07 13:00:27 +00:00
|
|
|
existing_value = target.get_setting(setting, None)
|
2019-04-07 16:36:29 +00:00
|
|
|
if not existing_value == None:
|
2019-04-07 16:38:52 +00:00
|
|
|
if unset:
|
2019-04-07 16:36:29 +00:00
|
|
|
target.del_setting(setting)
|
2019-04-07 16:45:35 +00:00
|
|
|
return ConfigResult(ConfigResults.Removed)
|
2019-04-07 16:36:29 +00:00
|
|
|
else:
|
2019-09-12 11:17:09 +00:00
|
|
|
formatted = export_settings[setting].format(existing_value)
|
|
|
|
return ConfigResult(ConfigResults.Retrieved, formatted)
|
2019-04-07 16:36:29 +00:00
|
|
|
else:
|
|
|
|
raise ConfigSettingInexistent()
|
2019-04-07 13:00:27 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.config", min_args=1)
|
|
|
|
def config(self, event):
|
|
|
|
"""
|
|
|
|
:help: Change config options
|
2019-04-07 16:36:29 +00:00
|
|
|
:usage: <context>[:name] [-][setting [value]]
|
2019-04-07 13:00:27 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
arg_count = len(event["args_split"])
|
2019-05-23 10:45:35 +00:00
|
|
|
context_desc, _, name = event["args_split"][0].partition(":")
|
2019-04-07 13:00:27 +00:00
|
|
|
|
|
|
|
setting = None
|
|
|
|
value = None
|
|
|
|
if arg_count > 1:
|
|
|
|
setting = event["args_split"][1].lower()
|
|
|
|
if arg_count > 2:
|
|
|
|
value = " ".join(event["args_split"][2:])
|
|
|
|
|
2019-06-17 10:27:48 +00:00
|
|
|
try:
|
2019-09-17 16:56:56 +00:00
|
|
|
target, context, name_override = self._to_context(event["server"],
|
2019-06-17 10:27:48 +00:00
|
|
|
event["target"], event["user"], context_desc)
|
|
|
|
except ValueError:
|
|
|
|
raise utils.EventError(
|
2019-06-17 10:29:36 +00:00
|
|
|
"Unknown context '%s'. Please provide "
|
|
|
|
"'user', 'channel', 'server' or 'bot'" % context_desc)
|
2019-04-07 13:00:27 +00:00
|
|
|
|
2019-09-17 16:56:56 +00:00
|
|
|
name = name_override or name
|
|
|
|
|
2019-06-14 11:09:42 +00:00
|
|
|
permission_check = utils.Check("permission", "config")
|
|
|
|
|
2019-05-23 10:45:35 +00:00
|
|
|
if context == "set":
|
2019-04-07 13:00:27 +00:00
|
|
|
if name:
|
2019-06-15 17:42:14 +00:00
|
|
|
event["check_assert"](
|
|
|
|
utils.Check("self", name)|permission_check)
|
2019-04-07 13:00:27 +00:00
|
|
|
target = event["server"].get_user(name)
|
|
|
|
else:
|
|
|
|
target = event["user"]
|
2019-05-23 10:45:35 +00:00
|
|
|
elif context == "channelset":
|
2019-04-07 13:00:27 +00:00
|
|
|
if name:
|
|
|
|
if name in event["server"].channels:
|
|
|
|
target = event["server"].channels.get(name)
|
|
|
|
else:
|
|
|
|
target = ConfigChannelTarget(self.bot, event["server"],
|
|
|
|
name)
|
|
|
|
else:
|
2019-04-15 15:48:04 +00:00
|
|
|
if event["is_channel"]:
|
|
|
|
target = event["target"]
|
|
|
|
else:
|
|
|
|
raise utils.EventError(
|
|
|
|
"Cannot change config for current channel when in "
|
|
|
|
"private message")
|
2019-06-16 19:48:31 +00:00
|
|
|
event["check_assert"](permission_check|
|
2019-08-16 09:25:46 +00:00
|
|
|
utils.Check("channel-access", target, "config")|
|
2019-06-18 06:59:53 +00:00
|
|
|
utils.Check("channel-mode", target, "o"))
|
2019-06-14 11:09:42 +00:00
|
|
|
elif context == "serverset" or context == "botset":
|
2019-06-15 17:42:14 +00:00
|
|
|
event["check_assert"](permission_check)
|
2019-05-23 10:45:35 +00:00
|
|
|
|
|
|
|
export_settings = self._get_export_setting(context)
|
2019-04-07 13:00:27 +00:00
|
|
|
if not setting == None:
|
2019-04-07 16:43:00 +00:00
|
|
|
if not setting.lstrip("-") in export_settings:
|
2019-04-07 13:00:27 +00:00
|
|
|
raise utils.EventError("Setting not found")
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = self._config(export_settings, target, setting, value)
|
|
|
|
except ConfigInvalidValue:
|
2019-06-29 20:33:26 +00:00
|
|
|
example = export_settings[setting].get_example()
|
2019-06-14 16:35:12 +00:00
|
|
|
if not example == None:
|
2019-06-29 20:33:26 +00:00
|
|
|
raise utils.EventError("Invalid value. %s" %
|
2019-06-14 16:35:12 +00:00
|
|
|
example)
|
|
|
|
else:
|
|
|
|
raise utils.EventError("Invalid value")
|
2019-04-07 16:36:29 +00:00
|
|
|
except ConfigSettingInexistent:
|
|
|
|
raise utils.EventError("Setting not set")
|
|
|
|
|
2019-09-17 16:56:56 +00:00
|
|
|
for_str = ""
|
|
|
|
if name_override:
|
|
|
|
for_str = " for %s" % name_override
|
2019-04-07 13:00:27 +00:00
|
|
|
if result.result == ConfigResults.Changed:
|
2019-09-20 12:39:33 +00:00
|
|
|
event["stdout"].write("Config '%s'%s set to %s" %
|
2019-09-17 16:56:56 +00:00
|
|
|
(setting, for_str, result.data))
|
2019-04-07 13:00:27 +00:00
|
|
|
elif result.result == ConfigResults.Retrieved:
|
2019-09-17 16:56:56 +00:00
|
|
|
event["stdout"].write("%s%s: %s" % (setting, for_str,
|
|
|
|
result.data))
|
2019-04-07 16:45:35 +00:00
|
|
|
elif result.result == ConfigResults.Removed:
|
2019-09-17 16:56:56 +00:00
|
|
|
event["stdout"].write("Unset setting '%s'%s" %
|
|
|
|
(setting, for_str))
|
2019-04-07 13:00:27 +00:00
|
|
|
else:
|
|
|
|
event["stdout"].write("Available config: %s" %
|
|
|
|
", ".join(export_settings.keys()))
|