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"
|
|
|
|
CHANNELSETADD_HELP = ("Add to a specified channel setting for the "
|
|
|
|
"current channel")
|
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-11-06 13:01:30 +00:00
|
|
|
def _set(self, category, event, target, array):
|
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
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
setting = event["args_split"][0].lower()
|
2018-09-02 18:54:45 +00:00
|
|
|
if setting in settings_dict:
|
2018-12-05 12:13:29 +00:00
|
|
|
setting_options = settings_dict[setting]
|
|
|
|
if not setting_options.get("array", False) == array:
|
|
|
|
if array:
|
|
|
|
raise utils.EventError(
|
|
|
|
"Can't add to a non-array setting")
|
|
|
|
else:
|
|
|
|
raise utils.EventError(
|
|
|
|
"You can only 'add' to an array setting")
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
value = " ".join(event["args_split"][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:
|
2018-11-06 13:01:30 +00:00
|
|
|
if array:
|
|
|
|
current_value = target.get_setting(setting, [])
|
|
|
|
current_value.append(value)
|
|
|
|
target.set_setting(setting, current_value)
|
|
|
|
else:
|
|
|
|
target.set_setting(setting, value)
|
|
|
|
|
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")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown setting")
|
|
|
|
elif len(event["args_split"]) == 1:
|
|
|
|
event["stderr"].write("Please provide a value")
|
|
|
|
else:
|
2018-10-06 16:16:17 +00:00
|
|
|
shown_settings = [key for key, value in settings_dict.items()
|
|
|
|
if not value.get("hidden", False)]
|
2019-01-03 22:02:51 +00:00
|
|
|
shown_settings = sorted(shown_settings)
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write("Available settings: %s" % (
|
2018-10-06 16:16:17 +00:00
|
|
|
", ".join(shown_settings)))
|
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")
|
|
|
|
@utils.hook("received.command.setadd",
|
|
|
|
help="Add to 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
|
|
|
"""
|
2018-11-06 13:01:30 +00:00
|
|
|
self._set("set", event, event["user"], event["command"]=="setadd")
|
2016-03-29 11:56:58 +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)
|
2018-11-06 13:01:30 +00:00
|
|
|
@utils.hook("received.command.channelsetadd", channel_only=True,
|
2019-01-29 07:17:29 +00:00
|
|
|
help=CHANNELSETADD_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
|
|
|
"""
|
2018-11-06 13:48:32 +00:00
|
|
|
self._set("channelset", event, event["target"],
|
2018-11-06 13:01:30 +00:00
|
|
|
event["command"].startswith("channelsetadd"))
|
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")
|
|
|
|
@utils.hook("received.command.serversetadd",
|
|
|
|
help="Add to 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
|
|
|
"""
|
2018-11-06 13:01:30 +00:00
|
|
|
self._set("serverset", event, event["server"],
|
2018-11-06 13:44:33 +00:00
|
|
|
event["command"]=="serversetadd")
|
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")
|
|
|
|
@utils.hook("received.command.botsetadd",
|
|
|
|
help="Add to 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
|
|
|
|
"""
|
2018-11-12 20:33:56 +00:00
|
|
|
self._set("botset", event, self.bot, event["command"]=="botsetadd")
|
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
|
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
|
|
|
|
:permission: channelunsetoverride
|
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"])
|