move "+5m" syntax parsing out to utils.parse

This commit is contained in:
jesopo 2019-11-18 15:56:44 +00:00
parent fe25c6bc26
commit 65023dc84b
2 changed files with 15 additions and 14 deletions

View file

@ -27,16 +27,6 @@ KICK_REASON_SETTING = utils.Setting("default-kick-reason",
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
_name = "ChanOp" _name = "ChanOp"
def _parse_time(self, args, min_args):
if args and args[0][0] == "+":
if len(args[1:]) < min_args:
raise utils.EventError("Not enough arguments")
time = utils.datetime.from_pretty_time(args[0][1:])
if time == None:
raise utils.EventError("Invalid timeframe")
return time, args[1:]
return None, args
def _kick_reason(self, server, channel): def _kick_reason(self, server, channel):
return channel.get_setting("default-kick-reason", return channel.get_setting("default-kick-reason",
server.get_setting("default-kick-reason", server.get_setting("default-kick-reason",
@ -109,7 +99,7 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("require_access", "ban") @utils.kwarg("require_access", "ban")
@utils.kwarg("usage", "[+time] <target>") @utils.kwarg("usage", "[+time] <target>")
def ban(self, event): def ban(self, event):
time, args = self._parse_time(event["args_split"], 1) time, args = utils.parse.timed_args(event["args_split"], 1)
self._ban(event["server"], event["target"], args[0], True, time, True) self._ban(event["server"], event["target"], args[0], True, time, True)
@utils.hook("received.command.unban") @utils.hook("received.command.unban")
@ -128,7 +118,7 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("require_access", "kickban") @utils.kwarg("require_access", "kickban")
@utils.kwarg("usage", "[+time] <nickname> [reason]") @utils.kwarg("usage", "[+time] <nickname> [reason]")
def kickban(self, event): def kickban(self, event):
time, args = self._parse_time(event["args_split"], 1) time, args = utils.parse.timed_args(event["args_split"], 1)
self._ban(event["server"], event["target"], args[0], False, time, True) self._ban(event["server"], event["target"], args[0], False, time, True)
self._kick(event["server"], event["target"], args[0], args[1:]) self._kick(event["server"], event["target"], args[0], args[1:])
@ -199,7 +189,7 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("help", "Mute a given user") @utils.kwarg("help", "Mute a given user")
def _mute(self, event): def _mute(self, event):
add = event["command"] == "mute" add = event["command"] == "mute"
time, args = self._parse_time(event["args_split"], 1) time, args = utils.parse.timed_args(event["args_split"], 1)
target_name = args[0] target_name = args[0]
if not event["server"].has_user(target_name): if not event["server"].has_user(target_name):
@ -349,7 +339,7 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("usage", "[+time]") @utils.kwarg("usage", "[+time]")
@utils.kwarg("help", "Mute the current channel") @utils.kwarg("help", "Mute the current channel")
def cmute(self, event): def cmute(self, event):
time, args = self._parse_time(event["args_split"], 0) time, args = utils.parse.timed_args(event["args_split"], 0)
event["target"].send_mode("+m") event["target"].send_mode("+m")
if time: if time:

View file

@ -1,4 +1,5 @@
import decimal, io, typing import decimal, io, typing
from . import datetime, errors
COMMENT_TYPES = ["#", "//"] COMMENT_TYPES = ["#", "//"]
def hashflags(filename: str def hashflags(filename: str
@ -109,3 +110,13 @@ def parse_number(s: str) -> str:
raise ValueError("Unknown unit '%s' given to parse_number" % unit) raise ValueError("Unknown unit '%s' given to parse_number" % unit)
return str(number) return str(number)
def timed_args(args, min_args):
if args and args[0][0] == "+":
if len(args[1:]) < min_args:
raise errors.EventError("Not enough arguments")
time = datetime.from_pretty_time(args[0][1:])
if time == None:
raise errors.EventError("Invalid timeframe")
return time, args[1:]
return None, args