1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
from src import ModuleManager, utils
class Module(ModuleManager.BaseModule):
def _spec_chunk(self, server, channel, user, types, args):
options = []
first_error = None
for type in types:
chunk = None
n = 0
error = None
if type == "time" and args:
time, _ = utils.parse.timed_args(args)
chunk = time
n = 1
error = "Invalid timeframe"
elif type == "rchannel":
if channel:
chunk = channel
elif args:
n = 1
if args[0] in server.channels:
chunk = server.channels.get(args[0])
error = "No such channel"
else:
error = "No channel provided"
elif type == "channel" and args:
if args[0] in server.channels:
chunk = server.channels.get(args[0])
n = 1
error = "No such channel"
elif type == "cuser" and args:
tuser = server.get_user(args[0], create=False)
if tuser and channel.has_user(tuser):
chunk = tuser
n = 1
error = "That user is not in this channel"
elif type == "ruser":
if args:
chunk = server.get_user(args[0], create=False)
n = 1
else:
chunk = user
error = "No such user"
elif type == "user" and args:
chunk = server.get_user(args[0], create=False)
n = 1
error = "No such user"
elif type == "word" and args:
chunk = args[0]
n = 1
elif type == "...":
chunk = " ".join(args)
n = len(args)
options.append([type, chunk, n, error])
return options
@utils.hook("preprocess.command")
def preprocess(self, event):
spec = event["hook"].get_kwarg("spec", None)
if not spec == None:
server = event["server"]
channel = event["target"] if event["is_channel"] else None
user = event["user"]
args = event["args_split"].copy()
out = []
for word in spec.split():
types = word[1:].split("|")
optional = word[0] == "?"
options = self._spec_chunk(server, channel, user, types, args)
found = None
first_error = None
for type, chunk, n, error in options:
if error and not first_error:
first_error = error
if chunk:
found = True
args = args[n:]
if len(types) > 1:
chunk = [type, chunk]
found = chunk
break
out.append(found)
if not optional and not found:
error = first_error or "Invalid arguments"
return utils.consts.PERMISSION_HARD_FAIL, error
event["kwargs"]["spec"] = out
|