Repository navigation
Expand file tree
/
Copy pathcommand_handler.py
More file actions
240 lines (203 loc) · 9.01 KB
/
Copy pathcommand_handler.py
File metadata and controls
240 lines (203 loc) · 9.01 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Parsing helpers for slash commands handled by the bot."""
from typing import Optional, Tuple, Dict
Command = Tuple[str, Dict[str, str]]
def parse_private_command(text: str) -> Optional[Command]:
"""Return a parsed ``(action, args)`` tuple for the incoming private command."""
if not text:
return None
text = text.strip()
if not text.startswith("/"):
return None
# Normalize spacing after the command keyword
parts = text.split(maxsplit=1)
cmd = parts[0].lower()
rest = parts[1].strip() if len(parts) > 1 else ""
# Helper for splitting <a>|<b>
def split_pipe(s: str, require_right=True) -> Tuple[str, str]:
if "|" in s:
left, right = s.split("|", 1)
else:
left, right = s, ""
left = left.strip()
right = right.strip()
if require_right and not right:
raise ValueError("Password must not be empty")
return left, right
def split_badwords(s: str):
if not s:
return []
return [part.strip() for part in s.replace(",", " ").split() if part.strip()]
if cmd == "/ru": # register user (interactive)
username = (rest or "").strip()
if "|" in username:
raise ValueError(
"Use the new interactive format: /ru <optional username> (no '|')."
)
return ("register_user", {"username": username})
if cmd == "/rc": # register/create channel
channel_name = (rest or "").strip()
if not channel_name:
raise ValueError("Format: /rc <channel_name>")
if "|" in channel_name:
raise ValueError(
"Channel name must not contain '|'. The password will be requested interactively."
)
if any(sep in channel_name for sep in ("/", "\\")):
raise ValueError(
"Channel name must not contain '/' or '\\'. Channel path is fixed by configuration."
)
if not channel_name[0].isalnum():
raise ValueError(
"Channel name must start with a letter or number (no leading symbols)."
)
return ("create_channel", {"name": channel_name})
if cmd == "/dc": # delete channel
if not rest:
raise ValueError("Format: /dc <channel_name OR full_path> [--force]")
force = False
tokens = rest.split()
filtered = []
for t in tokens:
if t == "--force":
force = True
else:
filtered.append(t)
target = " ".join(filtered).strip()
if not target:
raise ValueError("Channel target must not be empty")
return ("delete_channel", {"target": target, "force": force})
if cmd == "/du": # delete user account
if not rest:
raise ValueError("Format: /du <username> [--force]")
force = False
tokens = rest.split()
filtered = []
for t in tokens:
if t == "--force":
force = True
else:
filtered.append(t)
username = " ".join(filtered).strip()
if not username:
raise ValueError("Username must not be empty")
return ("delete_user", {"username": username, "force": force})
if cmd == "/lc": # list channels
path = rest.strip() if rest else ""
return ("list_channels", {"path": path})
if cmd == "/bwl": # optional search text narrows the numbered list
return ("badword_list", {"query": rest.strip().lower()})
if cmd == "/bw": # interactive badword menu
return ("badword_menu", {})
if cmd == "/bwt": # test which entries would flag a text
if not rest:
raise ValueError("Format: /bwt <text to test>")
return ("badword_test", {"text": rest})
if cmd == "/bwa":
if not rest:
raise ValueError("Format: /bwa <word[,word2,...]>")
words = split_badwords(rest)
if not words:
raise ValueError("You must provide at least one word to add.")
return ("badword_add", {"words": ",".join(words)})
if cmd == "/bwd": # words and/or numbers from the last /bwl, e.g. 3,5 or 3-5
if not rest:
raise ValueError("Format: /bwd <number|word[,number2|word2,...]>")
words = split_badwords(rest)
if not words:
raise ValueError("You must provide at least one word to remove.")
return ("badword_delete", {"words": ",".join(words)})
if cmd == "/v":
return ("version_info", {})
if cmd == "/oc": # owner check
if not rest:
raise ValueError("Format: /oc <name OR full_path>")
target = rest.strip()
return ("owner_check", {"target": target})
if cmd == "/so": # set owner (admin only)
if not rest:
raise ValueError("Format: /so <name OR full_path>|<username>")
if "|" not in rest:
raise ValueError("You must provide the new username after '|'")
target, newowner = rest.split("|", 1)
target = target.strip()
newowner = newowner.strip()
if not target or not newowner:
raise ValueError("Channel target and username must not be empty")
return ("set_owner", {"target": target, "username": newowner})
if cmd == "/to": # transfer owner (admin or current owner)
if not rest:
raise ValueError("Format: /to <name OR full_path>|<username>")
if "|" not in rest:
raise ValueError("You must provide the new username after '|'")
target, newowner = rest.split("|", 1)
target = target.strip()
newowner = newowner.strip()
if not target or not newowner:
raise ValueError("Channel target and username must not be empty")
return ("transfer_owner", {"target": target, "username": newowner})
if cmd in ("/help", "/h"):
topic = rest.strip() if rest else ""
return ("help", {"topic": topic})
if cmd == "/cs": # change bot status (admin only)
if not rest:
raise ValueError("Format: /cs <status message>")
status = rest.strip()
return ("change_status", {"status": status})
if cmd == "/kc": # kick users by nickname(s)
if not rest:
raise ValueError("Format: /kc <nickname[,nickname2,...]>[|reason]")
names_part, reason = (rest.split("|", 1) + [""])[:2]
names = [n.strip() for n in names_part.split(",") if n.strip()]
if not names:
raise ValueError("At least one nickname is required")
return ("kick_users", {"names": ",".join(names), "reason": reason.strip()})
if cmd == "/bn": # ban users by nickname(s)
if not rest:
raise ValueError("Format: /bn <nickname[,nickname2,...]>[|reason]")
names_part, reason = (rest.split("|", 1) + [""])[:2]
names = [n.strip() for n in names_part.split(",") if n.strip()]
if not names:
raise ValueError("At least one nickname is required")
return ("ban_users", {"names": ",".join(names), "reason": reason.strip()})
if cmd == "/ubn": # unban by username/IP depending on config
if not rest:
raise ValueError("Format: /ubn <value[,value2,...]>")
vals = [n.strip() for n in rest.split(",") if n.strip()]
if not vals:
raise ValueError("At least one value is required")
return ("unban", {"values": ",".join(vals)})
if cmd == "/lb": # list bans
return ("list_bans", {})
if cmd == "/ab": # interactive auto-moderation menu
return ("abuse_menu", {})
if cmd == "/abs": # auto-moderation status (warnings and temp bans)
return ("abuse_status", {})
if cmd == "/abf": # forgive: clear strikes / lift temp bans
if not rest:
raise ValueError("Format: /abf <number|username|IP[,...]>")
return ("abuse_forgive", {"targets": rest})
if cmd == "/abt": # feature switches: /abt [list] | /abt <number|name> [on|off]
return ("feature_toggle", {"args": rest})
if cmd == "/abw": # whitelist: /abw [list] | add <entries> | del <numbers|entries>
return ("abuse_whitelist", {"args": rest})
if cmd == "/tb": # temporary ban: /tb <nick[,nick2]> <minutes>[|reason]
usage = "Format: /tb <nickname[,nickname2,...]> <minutes>[|reason]"
if not rest:
raise ValueError(usage)
target_part, reason = (rest.split("|", 1) + [""])[:2]
pieces = target_part.strip().rsplit(maxsplit=1)
if len(pieces) != 2 or not pieces[1].isdigit():
raise ValueError(usage)
minutes = int(pieces[1])
if not 1 <= minutes <= 10080:
raise ValueError("Minutes must be between 1 and 10080 (7 days).")
names = [n.strip() for n in pieces[0].split(",") if n.strip()]
if not names:
raise ValueError(usage)
return (
"temp_ban",
{"names": ",".join(names), "minutes": minutes, "reason": reason.strip()},
)
if cmd == "/lu": # list users
return ("list_users", {})
return None