diff options
| -rw-r--r-- | modules/birthday.py | 17 | ||||
| -rw-r--r-- | modules/commands/__init__.py | 24 | ||||
| -rw-r--r-- | modules/ip_addresses.py | 13 | ||||
| -rw-r--r-- | modules/ircv3_sasl/__init__.py | 9 | ||||
| -rw-r--r-- | modules/location.py | 10 | ||||
| -rw-r--r-- | modules/proxy.py | 11 |
6 files changed, 31 insertions, 53 deletions
diff --git a/modules/birthday.py b/modules/birthday.py index 563c7a73..f1814ec6 100644 --- a/modules/birthday.py +++ b/modules/birthday.py @@ -31,21 +31,20 @@ def _format(years, dt): else: return _format_noyear(dt) -class BirthdaySetting(utils.Setting): - def parse(self, value: str) -> typing.Any: - parsed = _parse(value) - if parsed: - years, parsed = parsed - return _format(years, parsed) - return None +def _parse_setting(value): + parsed = _parse(value) + if parsed: + years, parsed = parsed + return _format(years, parsed) + return None def _apostrophe(nickname): if nickname[-1].lower() == "s": return "%s'" % nickname return "%s's" % nickname -@utils.export("set", BirthdaySetting("birthday", "Set your birthday", - example="1995-09-15")) +@utils.export("set", utils.FunctionSetting(_parse_setting, "birthday", + "Set your birthday", example="1995-09-15")) class Module(ModuleManager.BaseModule): @utils.hook("received.command.birthday") def birthday(self, event): diff --git a/modules/commands/__init__.py b/modules/commands/__init__.py index d78ffbbc..88bea18e 100644 --- a/modules/commands/__init__.py +++ b/modules/commands/__init__.py @@ -16,33 +16,21 @@ MSGID_TAG = utils.irc.MessageTag("msgid", "draft/msgid") NON_ALPHANUMERIC = [char for char in string.printable if not char.isalnum()] -def _command_method_validate(s): - if s.upper() in COMMAND_METHODS: - return s.upper() - class BadContextException(Exception): def __init__(self, required_context): self.required_context = required_context Exception.__init__(self) -class CommandMethodSetting(utils.Setting): - example = "NOTICE" - def parse(self, value: str) -> typing.Any: - upper = value.upper() - if upper in COMMAND_METHODS: - return upper - return None +SETTING_COMMANDMETHOD = utils.OptionsSetting(COMMAND_METHODS, COMMAND_METHOD, + "Set the method used to respond to commands") @utils.export("channelset", utils.Setting("command-prefix", "Set the command prefix used in this channel", example="!")) @utils.export("serverset", utils.Setting("command-prefix", "Set the command prefix used on this server", example="!")) -@utils.export("serverset", CommandMethodSetting("command-method", - "Set the method used to respond to commands")) -@utils.export("channelset", CommandMethodSetting("command-method", - "Set the method used to respond to commands")) -@utils.export("botset", CommandMethodSetting("command-method", - "Set the method used to respond to commands")) +@utils.export("serverset", SETTING_COMMANDMETHOD) +@utils.export("channelset", SETTING_COMMANDMETHOD) +@utils.export("botset", SETTING_COMMANDMETHOD) @utils.export("channelset", utils.BoolSetting("hide-prefix", "Disable/enable hiding prefix in command reponses")) @utils.export("channelset", utils.BoolSetting("commands", @@ -94,7 +82,7 @@ class Module(ModuleManager.BaseModule): def _command_method(self, target, server): return target.get_setting(COMMAND_METHOD, server.get_setting(COMMAND_METHOD, - self.bot.get_setting(COMMAND_METHOD, "PRIVMSG"))).upper() + self.bot.get_setting(COMMAND_METHOD, "PRIVMSG"))) def _find_command_hook(self, server, command, is_channel, args_split): if not self.has_command(command): diff --git a/modules/ip_addresses.py b/modules/ip_addresses.py index 66c59826..7cb540cf 100644 --- a/modules/ip_addresses.py +++ b/modules/ip_addresses.py @@ -9,17 +9,16 @@ REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*" REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}" REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I) -class DnsSetting(utils.Setting): - def parse(self, value: str) -> typing.Any: - if utils.is_ip(value): - return value - return None +def _parse(value): + if utils.is_ip(value): + return value + return None @utils.export("botset", utils.BoolSetting("configurable-nameservers", "Whether or not users can configure their own nameservers")) -@utils.export("serverset", DnsSetting("dns-nameserver", +@utils.export("serverset", utils.FunctionSetting(_parse, "dns-nameserver", "Set DNS nameserver", example="8.8.8.8")) -@utils.export("channelset", DnsSetting("dns-nameserver", +@utils.export("channelset", utils.FunctionSetting(_parse, "dns-nameserver", "Set DNS nameserver", example="8.8.8.8")) class Module(ModuleManager.BaseModule): @utils.hook("received.command.dns", min_args=1) diff --git a/modules/ircv3_sasl/__init__.py b/modules/ircv3_sasl/__init__.py index d66661b1..805ccb71 100644 --- a/modules/ircv3_sasl/__init__.py +++ b/modules/ircv3_sasl/__init__.py @@ -13,12 +13,11 @@ USERPASS_MECHANISMS = [ "PLAIN" ] -class SaslSetting(utils.Setting): - def parse(self, value: str) -> typing.Any: - mechanism, _, arguments = value.partition(" ") - return {"mechanism": mechanism.upper(), "args": arguments} +def _parse(value): + mechanism, _, arguments = value.partition(" ") + return {"mechanism": mechanism.upper(), "args": arguments} -@utils.export("serverset", SaslSetting("sasl", +@utils.export("serverset", utils.FunctionSetting(_parse, "sasl", "Set the sasl username/password for this server", example="PLAIN BitBot:hunter2")) @utils.export("serverset", utils.BoolSetting("sasl-hard-fail", diff --git a/modules/location.py b/modules/location.py index 9f405383..1858a915 100644 --- a/modules/location.py +++ b/modules/location.py @@ -6,16 +6,10 @@ from src import ModuleManager, utils URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json" -class LocationSetting(utils.Setting): - _func = None - def parse(self, value: str) -> typing.Any: - return self._func(value) - class Module(ModuleManager.BaseModule): def on_load(self): - setting = LocationSetting("location", "Set your location", - example="London, GB") - setting._func = self._get_location + setting = utils.FunctionSetting(self._get_location, "location", + "Set your location", example="London, GB") self.exports.add("set", setting) self.exports.add("get-location", self._get_location) diff --git a/modules/proxy.py b/modules/proxy.py index 744afae1..c66710c8 100644 --- a/modules/proxy.py +++ b/modules/proxy.py @@ -8,13 +8,12 @@ TYPES = { "http": socks.HTTP } -class ProxySetting(utils.Setting): - def parse(self, value: str) -> typing.Any: - parsed = urllib.parse.urlparse(value) - if parsed.scheme in TYPES and parsed.hostname: - return value +def _parse(value): + parsed = urllib.parse.urlparse(value) + if parsed.scheme in TYPES and parsed.hostname: + return value -@utils.export("serverset", ProxySetting("proxy", +@utils.export("serverset", utils.FunctionSetting(_parse, "proxy", "Proxy configuration for the current server", example="socks5://localhost:9050")) class Module(ModuleManager.BaseModule): |
