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
|
|
|
|
2018-11-06 13:01:30 +00:00
|
|
|
CHANNELSET_HELP = "Get a specified channel setting for the current channel"
|
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
if context_desc_lower == "user":
|
|
|
|
return user, "set"
|
|
|
|
elif context_desc_lower == "channel":
|
|
|
|
return channel, "channelset"
|
|
|
|
elif context_desc_lower == "server":
|
|
|
|
return server, "serverset"
|
|
|
|
elif context_desc_lower == "bot":
|
|
|
|
return self.bot, "botset"
|
|
|
|
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"]
|
|
|
|
|
|
|
|
target, context = self._to_context(event["server"], channel,
|
|
|
|
event["user"], context or "user")
|
|
|
|
|
|
|
|
export_settings = self._get_export_setting(context)
|
|
|
|
setting_info = export_settings.get(require_setting, None)
|
|
|
|
if setting_info:
|
|
|
|
value = target.get_setting(require_setting, None)
|
|
|
|
if value == None:
|
|
|
|
example = setting_info.get("example", "<value>")
|
|
|
|
return "Please set %s, e.g.: %s%s %s %s" % (
|
|
|
|
require_setting, event["command_prefix"], context,
|
|
|
|
require_setting, example)
|
|
|
|
|
2019-05-23 09:34:10 +00:00
|
|
|
def _set(self, category, event, target, arg_index=0):
|
2019-01-30 19:07:34 +00:00
|
|
|
args = event["args_split"][arg_index:]
|
2018-10-12 09:57:11 +00:00
|
|
|
settings = self.exports.get_all(category)
|
2018-12-05 12:13:29 +00:00
|
|
|
settings_dict = {setting["setting"]: setting for setting in settings}
|
2018-10-12 09:57:11 +00:00
|
|
|
|
2019-05-23 09:36:59 +00:00
|
|
|
setting = args[0].lower()
|
|
|
|
if setting in settings_dict:
|
|
|
|
setting_options = settings_dict[setting]
|
|
|
|
if len(args) > 1:
|
2019-01-30 19:07:34 +00:00
|
|
|
value = " ".join(args[1:])
|
2018-12-05 12:13:29 +00:00
|
|
|
value = setting_options.get("validate", lambda x: x)(value)
|
2018-10-12 09:57:11 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if not value == None:
|
2019-05-23 09:34:10 +00:00
|
|
|
target.set_setting(setting, value)
|
2018-11-06 13:01:30 +00:00
|
|
|
|
2018-10-16 22:13:19 +00:00
|
|
|
self.events.on("set").on(category).on(setting).call(
|
2018-10-12 09:57:11 +00:00
|
|
|
value=value, target=target)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write("Saved setting")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Invalid value")
|
2019-05-23 09:36:59 +00:00
|
|
|
elif len(args) == 1:
|
|
|
|
example = setting_options.get("example", None)
|
|
|
|
if example:
|
|
|
|
event["stderr"].write("Please provide a value, e.g. %s"
|
|
|
|
% example)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Please provide a value")
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2019-05-23 09:36:59 +00:00
|
|
|
shown_settings = [key for key, value in settings_dict.items()
|
2018-10-06 16:16:17 +00:00
|
|
|
if not value.get("hidden", False)]
|
2019-05-23 09:36:59 +00:00
|
|
|
shown_settings = sorted(shown_settings)
|
|
|
|
event["stdout"].write("Available settings: %s" % (
|
|
|
|
", ".join(shown_settings)))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown setting")
|
|
|
|
|
2018-10-12 09:57:11 +00:00
|
|
|
|
2018-11-06 13:01:30 +00:00
|
|
|
@utils.hook("received.command.set", help="Set a specified user setting")
|
2016-03-29 11:56:58 +00:00
|
|
|
def set(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:usage: <setting> <value>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-05-23 09:34:10 +00:00
|
|
|
self._set("set", event, event["user"])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-01-30 11:16:42 +00:00
|
|
|
@utils.hook("received.command.channelset", min_args=1, private_only=True,
|
|
|
|
help=CHANNELSET_HELP)
|
|
|
|
def private_channel_set(self, event):
|
|
|
|
"""
|
|
|
|
:usage: <channel> <setting> <value>
|
|
|
|
:channel_arg: 0
|
|
|
|
:require_access: channelset
|
|
|
|
:permission: channelsetoverride
|
|
|
|
"""
|
2019-01-30 19:40:20 +00:00
|
|
|
channel = event["server"].channels.get(event["args_split"][0])
|
2019-01-30 19:07:34 +00:00
|
|
|
self._set("channelset", event, channel, False, 1)
|
2019-01-30 11:16:42 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.channelset", channel_only=True,
|
2019-01-29 07:17:29 +00:00
|
|
|
help=CHANNELSET_HELP)
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_set(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:usage: <setting> <value>
|
2019-01-29 07:17:29 +00:00
|
|
|
:require_mode: high
|
|
|
|
:permission: channelsetoverride
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-05-23 09:34:10 +00:00
|
|
|
self._set("channelset", event, event["target"])
|
2018-09-09 08:34:55 +00:00
|
|
|
|
2018-11-06 13:01:30 +00:00
|
|
|
@utils.hook("received.command.serverset",
|
|
|
|
help="Set a specified server setting for the current server")
|
2018-09-09 08:34:55 +00:00
|
|
|
def server_set(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:usage: <setting> <value>
|
|
|
|
:permission: serverset
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-05-23 09:34:10 +00:00
|
|
|
self._set("serverset", event, event["server"])
|
2018-08-08 19:45:57 +00:00
|
|
|
|
2018-11-06 13:01:30 +00:00
|
|
|
@utils.hook("received.command.botset", help="Set a specified bot setting")
|
2018-10-04 16:09:52 +00:00
|
|
|
def bot_set(self, event):
|
|
|
|
"""
|
|
|
|
:help: Set a specified bot setting
|
|
|
|
:usage: <setting> <value>
|
|
|
|
:permission: botset
|
|
|
|
"""
|
2019-05-23 09:34:10 +00:00
|
|
|
self._set("botset", event, self.bot)
|
2018-10-04 16:09:52 +00:00
|
|
|
|
2018-08-08 19:45:57 +00:00
|
|
|
def _get(self, event, setting, qualifier, value):
|
|
|
|
if not value == None:
|
|
|
|
event["stdout"].write("'%s'%s: %s" % (setting,
|
2018-08-08 19:49:34 +00:00
|
|
|
qualifier, str(value)))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-08-08 19:45:57 +00:00
|
|
|
event["stdout"].write("'%s' has no value set" % setting)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.get", min_args=1)
|
2018-09-09 08:34:55 +00:00
|
|
|
def get(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get a specified user setting
|
|
|
|
:usage: <setting>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-09 08:34:55 +00:00
|
|
|
setting = event["args_split"][0]
|
|
|
|
self._get(event, setting, "", event["user"].get_setting(
|
|
|
|
setting, None))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.channelget", channel_only=True, min_args=1)
|
2018-08-08 19:45:57 +00:00
|
|
|
def channel_get(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get a specified channel setting for the current channel
|
|
|
|
:usage: <setting>
|
|
|
|
:require_mode: o
|
2019-01-30 08:10:41 +00:00
|
|
|
:permission: channelsetoverride
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-08 19:45:57 +00:00
|
|
|
setting = event["args_split"][0]
|
2018-08-08 19:49:34 +00:00
|
|
|
self._get(event, setting, " for %s" % event["target"].name,
|
|
|
|
event["target"].get_setting(setting, None))
|
2018-08-08 19:45:57 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.serverget", min_args=1)
|
2018-09-09 08:34:55 +00:00
|
|
|
def server_get(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get a specified server setting for the current server
|
|
|
|
:usage: <setting>
|
|
|
|
:permission: serverget
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-08 19:45:57 +00:00
|
|
|
setting = event["args_split"][0]
|
2018-09-09 08:38:35 +00:00
|
|
|
self._get(event, setting, "", event["server"].get_setting(
|
|
|
|
setting, None))
|
2018-10-04 16:09:52 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.botget", min_args=1)
|
|
|
|
def bot_get(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get a specified bot setting
|
|
|
|
:usage: <setting>
|
|
|
|
:permission: botget
|
|
|
|
"""
|
|
|
|
setting = event["args_split"][0]
|
|
|
|
self._get(event, setting, "", self.bot.get_setting(setting, None))
|
2019-01-10 22:40:57 +00:00
|
|
|
|
|
|
|
def _unset(self, event, setting, category, target):
|
|
|
|
settings = self.exports.get_all(category)
|
|
|
|
settings_dict = {setting["setting"]: setting for setting in settings}
|
|
|
|
setting = setting.lower()
|
|
|
|
|
|
|
|
if setting in settings_dict:
|
|
|
|
target.del_setting(setting)
|
|
|
|
event["stdout"].write("Unset %s" % setting)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown setting")
|
|
|
|
|
|
|
|
@utils.hook("received.command.unset", min_args=1)
|
|
|
|
def unset(self, event):
|
|
|
|
"""
|
|
|
|
:help: Unset a specified user setting
|
|
|
|
:usage: <setting>
|
|
|
|
"""
|
|
|
|
self._unset(event, event["args_split"][0], "set", event["user"])
|
|
|
|
|
2019-01-29 07:17:29 +00:00
|
|
|
@utils.hook("received.command.channelunset", min_args=1)
|
2019-01-10 22:40:57 +00:00
|
|
|
def channel_unset(self, event):
|
|
|
|
"""
|
|
|
|
:help: Unset a specified user setting
|
|
|
|
:usage: <setting>
|
2019-01-29 07:17:29 +00:00
|
|
|
:require_mode: high
|
2019-01-30 08:10:41 +00:00
|
|
|
:permission: channelsetoverride
|
2019-01-10 22:40:57 +00:00
|
|
|
"""
|
2019-01-10 22:43:12 +00:00
|
|
|
self._unset(event, event["args_split"][0], "channelset", event["user"])
|
2019-04-07 13:00:27 +00:00
|
|
|
|
|
|
|
def _get_export_setting(self, context):
|
|
|
|
settings = self.exports.get_all(context)
|
|
|
|
return {setting["setting"].lower(): setting for setting in settings}
|
|
|
|
|
|
|
|
def _config(self, export_settings, target, setting, value=None):
|
|
|
|
if not value == None:
|
2019-04-24 10:02:41 +00:00
|
|
|
validation = export_settings[setting].get("validate", lambda x: x)
|
2019-04-07 13:00:27 +00:00
|
|
|
validated_value = validation(value)
|
|
|
|
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:
|
|
|
|
return ConfigResult(ConfigResults.Retrieved, existing_value)
|
|
|
|
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
|
|
|
:permission: config
|
|
|
|
"""
|
|
|
|
|
|
|
|
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-05-23 10:45:35 +00:00
|
|
|
target, context = self._to_context(event["server"],
|
|
|
|
event["target"], event["user"], context_desc)
|
2019-04-07 13:00:27 +00:00
|
|
|
|
2019-05-23 10:45:35 +00:00
|
|
|
if context == "set":
|
2019-04-07 13:00:27 +00:00
|
|
|
if name:
|
|
|
|
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-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:
|
|
|
|
raise utils.EventError("Invalid value")
|
2019-04-07 16:36:29 +00:00
|
|
|
except ConfigSettingInexistent:
|
|
|
|
raise utils.EventError("Setting not set")
|
|
|
|
|
2019-04-07 13:00:27 +00:00
|
|
|
if result.result == ConfigResults.Changed:
|
2019-04-28 09:50:19 +00:00
|
|
|
event["stdout"].write("Config '%s' set to %s" %
|
|
|
|
(setting, result.data))
|
2019-04-07 13:00:27 +00:00
|
|
|
elif result.result == ConfigResults.Retrieved:
|
|
|
|
event["stdout"].write("%s: %s" % (setting, result.data))
|
2019-04-07 16:45:35 +00:00
|
|
|
elif result.result == ConfigResults.Removed:
|
|
|
|
event["stdout"].write("Unset setting")
|
2019-04-07 13:00:27 +00:00
|
|
|
else:
|
|
|
|
event["stdout"].write("Available config: %s" %
|
|
|
|
", ".join(export_settings.keys()))
|