levix-bot 4.7.0 → 4.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -7
- package/SETUP.md +21 -2
- package/app.cjs +69 -23
- package/package.json +3 -2
- package/public/dashboard/assets/index-B1sOW2VX.js +561 -0
- package/public/dashboard/assets/index-CK8osB4o.css +2 -0
- package/public/dashboard/index.html +2 -2
- package/public/dashboard.css +50 -0
- package/public/dashboard.js +25 -2
- package/public/gateway-i18n.js +26 -0
- package/src/bootstrap/session-startup-policy.js +6 -6
- package/src/commands/group/antilink.cjs +82 -59
- package/src/commands/group/antispam.cjs +112 -0
- package/src/commands/group/clearwarns.cjs +6 -6
- package/src/commands/group/demote.cjs +11 -13
- package/src/commands/group/flagged.cjs +52 -0
- package/src/commands/group/kick.cjs +25 -15
- package/src/commands/group/media.cjs +65 -29
- package/src/commands/group/policy.cjs +215 -0
- package/src/commands/group/promote.cjs +11 -12
- package/src/commands/group/rules.cjs +9 -6
- package/src/commands/group/setwarn.cjs +17 -27
- package/src/commands/group/warn.cjs +13 -63
- package/src/commands/group.cjs +5 -2
- package/src/commands/md.cjs +44 -0
- package/src/commands/mod.cjs +101 -26
- package/src/commands/translate.cjs +585 -0
- package/src/config/ai-identity.cjs +53 -1
- package/src/config/ai-persona.md +32 -73
- package/src/config/defaults.cjs +3 -0
- package/src/config/secrets.cjs +71 -3
- package/src/config/settings.cjs +22 -1
- package/src/core/socket.js +27 -5
- package/src/db/db.cjs +66 -0
- package/src/db/store.cjs +264 -16
- package/src/diagnostics/crashReports.cjs +35 -9
- package/src/diagnostics/debugLog.cjs +405 -0
- package/src/handlers/command.handler.js +3 -1
- package/src/handlers/group.handler.js +32 -1
- package/src/handlers/message.handler.js +53 -17
- package/src/middleware/antispam.middleware.js +95 -48
- package/src/panel/feedback.cjs +32 -5
- package/src/panel/one-time-code.cjs +156 -0
- package/src/panel/password-reset.cjs +187 -91
- package/src/panel/recovery-email.cjs +333 -0
- package/src/routes/dashboard.api.esm.js +341 -29
- package/src/services/aiAgent.cjs +25 -3
- package/src/services/moderationPolicy.cjs +895 -0
- package/src/services/settingsTransfer.cjs +4 -2
- package/src/utils/linkedCreds.cjs +22 -0
- package/src/utils/logger.cjs +93 -34
- package/src/utils/markdownParser.cjs +208 -136
- package/src/utils/moderationAction.cjs +637 -0
- package/src/utils/moderationSummary.cjs +225 -0
- package/src/utils/permissions.cjs +70 -0
- package/src/utils/permissions.esm.js +48 -8
- package/src/utils/storage.cjs +8 -0
- package/src/utils/storage.esm.js +4 -0
- package/views/login.ejs +100 -28
- package/public/dashboard/assets/index-RnT7DAde.js +0 -540
- package/public/dashboard/assets/index-UcqCdCvG.css +0 -2
- package/src/commands/group/antiSpam.cjs +0 -97
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
// The technical debug log (v4.8.1 hotfix).
|
|
2
|
+
//
|
|
3
|
+
// One JSON-lines file per UTC day in `<data>/logs/debug/`, written at "debug"
|
|
4
|
+
// level by the Pino logger. Both previews in the panel show the exact tail that
|
|
5
|
+
// a send would forward, and it is never sent on its own: the only two ways it
|
|
6
|
+
// leaves the device are a feedback submission with "Attach device & technical
|
|
7
|
+
// diagnostics" ticked and a crash report the operator presses Send on.
|
|
8
|
+
//
|
|
9
|
+
// Two rules decide everything here:
|
|
10
|
+
//
|
|
11
|
+
// * Nothing secret and no message content is ever written. Every record is
|
|
12
|
+
// rebuilt key by key before it reaches the file: a secret-shaped key name
|
|
13
|
+
// becomes "[redacted]" at any depth, a message-content key keeps only its
|
|
14
|
+
// length, and every remaining string runs through the one shared sanitizer
|
|
15
|
+
// (src/diagnostics/sanitize.cjs) before being capped at 2000 characters.
|
|
16
|
+
// * A failure never reaches the caller. A full disk, a permission problem or
|
|
17
|
+
// a record that cannot be serialized must not break the other log streams
|
|
18
|
+
// or the code that logged.
|
|
19
|
+
//
|
|
20
|
+
// Files older than 14 days are deleted at boot and on the six-hour sweep in
|
|
21
|
+
// src/db/db.cjs. Clearing the logs deletes this directory too.
|
|
22
|
+
|
|
23
|
+
const crypto = require("node:crypto");
|
|
24
|
+
const fs = require("node:fs");
|
|
25
|
+
const path = require("node:path");
|
|
26
|
+
|
|
27
|
+
const { dataPath, ensureDataDir } = require("../config/paths.cjs");
|
|
28
|
+
const { sanitizeText } = require("./sanitize.cjs");
|
|
29
|
+
|
|
30
|
+
const DEBUG_DIR = dataPath("logs", "debug");
|
|
31
|
+
const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
|
32
|
+
const MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000;
|
|
33
|
+
const FIELD_MAX = 2000;
|
|
34
|
+
const DEFAULT_TAIL_BYTES = 256 * 1024;
|
|
35
|
+
const MAX_DEPTH = 8;
|
|
36
|
+
const SNAPSHOT_TTL_MS = 15 * 60 * 1000;
|
|
37
|
+
const SNAPSHOT_MAX = 32;
|
|
38
|
+
|
|
39
|
+
const FILE_PATTERN = /^debug-\d{4}-\d{2}-\d{2}\.log$/;
|
|
40
|
+
|
|
41
|
+
// Key names that name a secret, wherever they sit in the record. `code` is
|
|
42
|
+
// deliberately absent: `err.code`, `statusCode` and friends are diagnostic
|
|
43
|
+
// value, not a credential. A bare `code` is handled by the digit rule below.
|
|
44
|
+
const SECRET_KEY_PATTERN =
|
|
45
|
+
/pass(word)?|secret|token|api[_-]?key|authorization|cookie|session|creds?|private|noise|signed|identity/i;
|
|
46
|
+
|
|
47
|
+
// Exactly these names are secrets; a substring rule would swallow `otpTimeout`
|
|
48
|
+
// and the like without cause.
|
|
49
|
+
const EXACT_SECRET_KEYS = new Set([
|
|
50
|
+
"otp",
|
|
51
|
+
"pairingcode",
|
|
52
|
+
"resetcode",
|
|
53
|
+
"verificationcode",
|
|
54
|
+
"setupcode",
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
// A bare `code` is an OTP when it is a short digit string, and an error/status
|
|
58
|
+
// code otherwise (`ENOENT`, `ERR_…`, 404).
|
|
59
|
+
const DIGIT_CODE_PATTERN = /^\d{4,10}$/;
|
|
60
|
+
|
|
61
|
+
// Key names whose value is message content: only the length survives.
|
|
62
|
+
const CONTENT_KEYS = new Set([
|
|
63
|
+
"text",
|
|
64
|
+
"body",
|
|
65
|
+
"caption",
|
|
66
|
+
"conversation",
|
|
67
|
+
"message",
|
|
68
|
+
"quoted",
|
|
69
|
+
"content",
|
|
70
|
+
"prompt",
|
|
71
|
+
"reply",
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
function utcDay(time) {
|
|
75
|
+
const date = time === undefined ? new Date() : new Date(time);
|
|
76
|
+
if (!Number.isFinite(date.getTime())) return new Date().toISOString().slice(0, 10);
|
|
77
|
+
return date.toISOString().slice(0, 10);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function debugFile(day) {
|
|
81
|
+
return path.join(DEBUG_DIR, `debug-${day}.log`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function contentLength(value) {
|
|
85
|
+
if (typeof value === "string") return value.length;
|
|
86
|
+
if (value === null || value === undefined) return 0;
|
|
87
|
+
try {
|
|
88
|
+
return JSON.stringify(value).length;
|
|
89
|
+
} catch {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A marker written by an earlier pass; re-reading must not re-measure it. */
|
|
95
|
+
const CONTENT_MARKER = /^\[redacted \d+ chars\]$/;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Rebuild one value for the file. Objects are walked (bounded, cycle-safe);
|
|
99
|
+
* strings lose their identities, secrets and paths through sanitizeText.
|
|
100
|
+
*
|
|
101
|
+
* @param {unknown} value
|
|
102
|
+
* @param {number} depth
|
|
103
|
+
* @param {WeakSet<object>} seen
|
|
104
|
+
* @returns {unknown}
|
|
105
|
+
*/
|
|
106
|
+
function sanitizeValue(value, depth, seen) {
|
|
107
|
+
if (value === null) return null;
|
|
108
|
+
const type = typeof value;
|
|
109
|
+
if (type === "string") return sanitizeText(value, FIELD_MAX);
|
|
110
|
+
if (type === "number" || type === "boolean") return value;
|
|
111
|
+
if (type === "bigint") return sanitizeText(String(value), FIELD_MAX);
|
|
112
|
+
if (type !== "object") return undefined;
|
|
113
|
+
if (depth >= MAX_DEPTH) return "[truncated]";
|
|
114
|
+
if (seen.has(value)) return "[circular]";
|
|
115
|
+
seen.add(value);
|
|
116
|
+
|
|
117
|
+
if (Buffer.isBuffer(value)) {
|
|
118
|
+
return `[buffer ${value.length} bytes]`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (Array.isArray(value)) {
|
|
122
|
+
return value.map((item) => {
|
|
123
|
+
const cleaned = sanitizeValue(item, depth + 1, seen);
|
|
124
|
+
return cleaned === undefined ? null : cleaned;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const out = {};
|
|
129
|
+
for (const [key, item] of Object.entries(value)) {
|
|
130
|
+
const lower = key.toLowerCase();
|
|
131
|
+
if (SECRET_KEY_PATTERN.test(key) || EXACT_SECRET_KEYS.has(lower)) {
|
|
132
|
+
out[key] = "[redacted]";
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (lower === "code" && typeof item === "string" && DIGIT_CODE_PATTERN.test(item)) {
|
|
136
|
+
out[key] = "[redacted]";
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (CONTENT_KEYS.has(lower)) {
|
|
140
|
+
out[key] =
|
|
141
|
+
typeof item === "string" && CONTENT_MARKER.test(item)
|
|
142
|
+
? item
|
|
143
|
+
: `[redacted ${contentLength(item)} chars]`;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const cleaned = sanitizeValue(item, depth + 1, seen);
|
|
147
|
+
if (cleaned !== undefined) out[key] = cleaned;
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The exact object that reaches the file. */
|
|
153
|
+
function sanitizeRecord(record) {
|
|
154
|
+
const cleaned = sanitizeValue(record, 0, new WeakSet());
|
|
155
|
+
return cleaned && typeof cleaned === "object" && !Array.isArray(cleaned) ? cleaned : {};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// The open day and how much has been written to it. Sync appends keep the file
|
|
159
|
+
// readable the instant a record is logged, the same guarantee the other two
|
|
160
|
+
// streams give and the tests rely on.
|
|
161
|
+
let state = { day: null, bytes: 0, capped: false };
|
|
162
|
+
|
|
163
|
+
function openDay(day) {
|
|
164
|
+
ensureDataDir("logs", "debug");
|
|
165
|
+
const file = debugFile(day);
|
|
166
|
+
let bytes = 0;
|
|
167
|
+
try {
|
|
168
|
+
bytes = fs.statSync(file).size;
|
|
169
|
+
} catch {
|
|
170
|
+
bytes = 0;
|
|
171
|
+
}
|
|
172
|
+
state = { day, bytes, capped: bytes >= MAX_FILE_BYTES };
|
|
173
|
+
return file;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Test hook: forget the open-day bookkeeping (the files are untouched). */
|
|
177
|
+
function resetDebugLogState() {
|
|
178
|
+
state = { day: null, bytes: 0, capped: false };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function writeWarning(file) {
|
|
182
|
+
const line = `${JSON.stringify({
|
|
183
|
+
level: 40,
|
|
184
|
+
time: Date.now(),
|
|
185
|
+
msg: "[debug] debug log reached its 5 MB daily cap; further records for this day are dropped",
|
|
186
|
+
})}\n`;
|
|
187
|
+
try {
|
|
188
|
+
fs.appendFileSync(file, line);
|
|
189
|
+
state.bytes += Buffer.byteLength(line);
|
|
190
|
+
} catch {}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Sanitize and append one record. Called by the Pino debug stream; never
|
|
195
|
+
* throws.
|
|
196
|
+
*
|
|
197
|
+
* @param {unknown} record
|
|
198
|
+
*/
|
|
199
|
+
function writeRecord(record) {
|
|
200
|
+
let line;
|
|
201
|
+
try {
|
|
202
|
+
line = JSON.stringify(sanitizeRecord(record));
|
|
203
|
+
} catch {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!line) return;
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
const day = utcDay(record?.time);
|
|
210
|
+
if (state.day !== day) openDay(day);
|
|
211
|
+
if (state.capped) return;
|
|
212
|
+
|
|
213
|
+
const payload = `${line}\n`;
|
|
214
|
+
const size = Buffer.byteLength(payload);
|
|
215
|
+
if (state.bytes + size > MAX_FILE_BYTES) {
|
|
216
|
+
state.capped = true;
|
|
217
|
+
writeWarning(debugFile(day));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
fs.appendFileSync(debugFile(day), payload);
|
|
221
|
+
state.bytes += size;
|
|
222
|
+
} catch {
|
|
223
|
+
// A debug log that cannot be written is not a reason to fail anything.
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The stream handed to pino.multistream. It may receive several lines. */
|
|
228
|
+
const stream = {
|
|
229
|
+
write(chunk) {
|
|
230
|
+
try {
|
|
231
|
+
const text = typeof chunk === "string" ? chunk : String(chunk);
|
|
232
|
+
for (const line of text.split("\n")) {
|
|
233
|
+
if (!line.trim()) continue;
|
|
234
|
+
let record;
|
|
235
|
+
try {
|
|
236
|
+
record = JSON.parse(line);
|
|
237
|
+
} catch {
|
|
238
|
+
record = { msg: line };
|
|
239
|
+
}
|
|
240
|
+
writeRecord(record);
|
|
241
|
+
}
|
|
242
|
+
} catch {}
|
|
243
|
+
return true;
|
|
244
|
+
},
|
|
245
|
+
// pino.multistream calls end() on every destination; a no-op is enough for a
|
|
246
|
+
// stream that is already written synchronously.
|
|
247
|
+
end() {},
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
function listFiles() {
|
|
251
|
+
let names;
|
|
252
|
+
try {
|
|
253
|
+
names = fs.readdirSync(DEBUG_DIR);
|
|
254
|
+
} catch {
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
return names
|
|
258
|
+
.filter((name) => FILE_PATTERN.test(name))
|
|
259
|
+
.sort()
|
|
260
|
+
.map((name) => path.join(DEBUG_DIR, name));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The newest records across the day files, newest last, re-sanitized on read.
|
|
265
|
+
* Bounded by `maxBytes` and by the record that would cross the boundary.
|
|
266
|
+
*
|
|
267
|
+
* @param {{ maxBytes?: number }} [options]
|
|
268
|
+
* @returns {string}
|
|
269
|
+
*/
|
|
270
|
+
function readDebugTail(options = {}) {
|
|
271
|
+
const requested = Number(options?.maxBytes);
|
|
272
|
+
const maxBytes =
|
|
273
|
+
Number.isFinite(requested) && requested > 0 ? Math.trunc(requested) : DEFAULT_TAIL_BYTES;
|
|
274
|
+
|
|
275
|
+
const files = listFiles();
|
|
276
|
+
const chunks = [];
|
|
277
|
+
let remaining = maxBytes;
|
|
278
|
+
|
|
279
|
+
for (let i = files.length - 1; i >= 0 && remaining > 0; i -= 1) {
|
|
280
|
+
const file = files[i];
|
|
281
|
+
let fd;
|
|
282
|
+
try {
|
|
283
|
+
fd = fs.openSync(file, "r");
|
|
284
|
+
const size = fs.fstatSync(fd).size;
|
|
285
|
+
const take = Math.min(size, remaining);
|
|
286
|
+
const start = size - take;
|
|
287
|
+
const buffer = Buffer.alloc(take);
|
|
288
|
+
const bytesRead = fs.readSync(fd, buffer, 0, take, start);
|
|
289
|
+
let text = buffer.toString("utf8", 0, bytesRead);
|
|
290
|
+
// A byte boundary can land inside a character or a record.
|
|
291
|
+
if (start > 0) {
|
|
292
|
+
const newline = text.indexOf("\n");
|
|
293
|
+
text = newline < 0 ? "" : text.slice(newline + 1);
|
|
294
|
+
}
|
|
295
|
+
chunks.unshift(text);
|
|
296
|
+
remaining -= bytesRead;
|
|
297
|
+
} catch {
|
|
298
|
+
// A file that vanished or cannot be read is skipped.
|
|
299
|
+
} finally {
|
|
300
|
+
if (fd !== undefined) {
|
|
301
|
+
try {
|
|
302
|
+
fs.closeSync(fd);
|
|
303
|
+
} catch {}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const records = [];
|
|
309
|
+
for (const chunk of chunks) {
|
|
310
|
+
for (const line of chunk.split("\n")) {
|
|
311
|
+
if (!line.trim()) continue;
|
|
312
|
+
let record;
|
|
313
|
+
try {
|
|
314
|
+
record = JSON.parse(line);
|
|
315
|
+
} catch {
|
|
316
|
+
continue; // A truncated or hand-edited line must not hide newer ones.
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
records.push(JSON.stringify(sanitizeRecord(record)));
|
|
320
|
+
} catch {}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return records.join("\n");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Delete debug files older than 14 days. Runs at boot and every six hours. */
|
|
327
|
+
function sweepDebugLogs() {
|
|
328
|
+
const now = Date.now();
|
|
329
|
+
for (const file of listFiles()) {
|
|
330
|
+
try {
|
|
331
|
+
if (now - fs.statSync(file).mtimeMs > MAX_AGE_MS) fs.unlinkSync(file);
|
|
332
|
+
} catch {}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// The preview freezes the tail it showed. Send attaches that string and never
|
|
337
|
+
// a newer read. Missing and expired ids resolve to null, which means "attach
|
|
338
|
+
// nothing".
|
|
339
|
+
const snapshots = new Map();
|
|
340
|
+
|
|
341
|
+
function sweepSnapshots(now) {
|
|
342
|
+
for (const [id, row] of snapshots) {
|
|
343
|
+
if (now >= row.expiresAt) snapshots.delete(id);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function storeDebugSnapshot(text) {
|
|
348
|
+
const now = Date.now();
|
|
349
|
+
sweepSnapshots(now);
|
|
350
|
+
const id = crypto.randomBytes(16).toString("hex");
|
|
351
|
+
snapshots.set(id, { text: String(text ?? ""), expiresAt: now + SNAPSHOT_TTL_MS });
|
|
352
|
+
while (snapshots.size > SNAPSHOT_MAX) {
|
|
353
|
+
const oldest = snapshots.keys().next().value;
|
|
354
|
+
snapshots.delete(oldest);
|
|
355
|
+
}
|
|
356
|
+
return id;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function readDebugSnapshot(id) {
|
|
360
|
+
if (typeof id !== "string" || !id) return null;
|
|
361
|
+
const now = Date.now();
|
|
362
|
+
sweepSnapshots(now);
|
|
363
|
+
const row = snapshots.get(id);
|
|
364
|
+
if (!row) return null;
|
|
365
|
+
return row.text;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function expireDebugSnapshot(id) {
|
|
369
|
+
const row = snapshots.get(id);
|
|
370
|
+
if (row) row.expiresAt = 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Delete every file in logs/debug. Called by logger.clearLogs(). */
|
|
374
|
+
function clearDebugLogs() {
|
|
375
|
+
resetDebugLogState();
|
|
376
|
+
let names;
|
|
377
|
+
try {
|
|
378
|
+
names = fs.readdirSync(DEBUG_DIR);
|
|
379
|
+
} catch {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
for (const name of names) {
|
|
383
|
+
try {
|
|
384
|
+
fs.rmSync(path.join(DEBUG_DIR, name), { recursive: true, force: true });
|
|
385
|
+
} catch {}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
module.exports = {
|
|
390
|
+
DEBUG_DIR,
|
|
391
|
+
MAX_FILE_BYTES,
|
|
392
|
+
MAX_AGE_MS,
|
|
393
|
+
FIELD_MAX,
|
|
394
|
+
stream,
|
|
395
|
+
sanitizeRecord,
|
|
396
|
+
writeRecord,
|
|
397
|
+
readDebugTail,
|
|
398
|
+
storeDebugSnapshot,
|
|
399
|
+
readDebugSnapshot,
|
|
400
|
+
expireDebugSnapshot,
|
|
401
|
+
SNAPSHOT_TTL_MS,
|
|
402
|
+
sweepDebugLogs,
|
|
403
|
+
clearDebugLogs,
|
|
404
|
+
resetDebugLogState,
|
|
405
|
+
};
|
|
@@ -29,11 +29,13 @@ const commandList = new Map(); // canonicalName -> command module
|
|
|
29
29
|
// instead of posting a second message).
|
|
30
30
|
const EDIT_IN_PLACE_EXCLUDE = new Set([
|
|
31
31
|
"gemini",
|
|
32
|
+
"md",
|
|
32
33
|
"prayer",
|
|
33
34
|
"tts",
|
|
34
35
|
"stt",
|
|
35
36
|
"send",
|
|
36
37
|
"weather",
|
|
38
|
+
"translate",
|
|
37
39
|
"qr",
|
|
38
40
|
"status",
|
|
39
41
|
"help",
|
|
@@ -284,7 +286,7 @@ export async function handleCommand(sock, msg, body) {
|
|
|
284
286
|
const prefix = runtimeConfig.getPrefix();
|
|
285
287
|
if (!body.startsWith(prefix)) return false;
|
|
286
288
|
|
|
287
|
-
const args = body.slice(prefix.length).trim().split(
|
|
289
|
+
const args = body.slice(prefix.length).trim().split(/\s+/);
|
|
288
290
|
const invokedName = args.shift().toLowerCase();
|
|
289
291
|
const command = commands.get(invokedName);
|
|
290
292
|
if (!command) return false;
|
|
@@ -5,12 +5,41 @@ import { getGroupSettings } from "../utils/storage.esm.js";
|
|
|
5
5
|
const require = createRequire(import.meta.url);
|
|
6
6
|
const logger = require("../utils/logger.cjs");
|
|
7
7
|
const { tr } = require("../utils/i18n.cjs");
|
|
8
|
+
const { buildModerationSummary } = require("../utils/moderationSummary.cjs");
|
|
9
|
+
const { resolveModerationPolicy } = require("../services/moderationPolicy.cjs");
|
|
10
|
+
const { clearMemberModeration } = require("../utils/moderationAction.cjs");
|
|
11
|
+
|
|
12
|
+
function participantIdentities(participant) {
|
|
13
|
+
if (typeof participant === "string") return [participant];
|
|
14
|
+
if (!participant || typeof participant !== "object") return [];
|
|
15
|
+
const ids = [];
|
|
16
|
+
for (const field of ["id", "lid", "phoneNumber", "jid", "pn"]) {
|
|
17
|
+
const value = participant[field];
|
|
18
|
+
if (typeof value === "string" && value && !ids.includes(value)) ids.push(value);
|
|
19
|
+
}
|
|
20
|
+
return ids;
|
|
21
|
+
}
|
|
8
22
|
|
|
9
23
|
// Handle group participant updates (join/leave)
|
|
10
24
|
export async function handleGroupParticipantsUpdate(sock, update) {
|
|
11
25
|
const { id, participants, action } = update;
|
|
12
26
|
logger.info({ update }, "Received group participants update");
|
|
13
27
|
|
|
28
|
+
if (action === "remove" || action === "leave") {
|
|
29
|
+
try {
|
|
30
|
+
const settings = getGroupSettings(id) || {};
|
|
31
|
+
if (resolveModerationPolicy(settings).membership === "clear") {
|
|
32
|
+
for (const participant of participants || []) {
|
|
33
|
+
for (const identity of participantIdentities(participant)) {
|
|
34
|
+
clearMemberModeration(id, identity);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
logger.error({ err: error, update }, "Error clearing moderation on leave");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
14
43
|
if (action !== "add") {
|
|
15
44
|
logger.info(`[Group] ${action} action for ${id}`);
|
|
16
45
|
return;
|
|
@@ -30,7 +59,9 @@ export async function handleGroupParticipantsUpdate(sock, update) {
|
|
|
30
59
|
const randomIndex = Math.floor(Math.random() * welcomeConfig.messages.length);
|
|
31
60
|
const welcomeMessage = welcomeConfig.messages[randomIndex];
|
|
32
61
|
|
|
33
|
-
const
|
|
62
|
+
const welcomeText = welcomeMessage.replace(/\${user}/g, `@${participant.split("@")[0]}`);
|
|
63
|
+
const summary = buildModerationSummary(settings);
|
|
64
|
+
const finalMessage = summary ? `${welcomeText}\n\n${summary}` : welcomeText;
|
|
34
65
|
|
|
35
66
|
await delay(1000);
|
|
36
67
|
await sock.sendMessage(id, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
-
import {
|
|
2
|
+
import { observeAntiSpam } from "../middleware/antispam.middleware.js";
|
|
3
3
|
import { checkBlacklist } from "../middleware/blacklist.middleware.js";
|
|
4
4
|
import { trackForwardedMessage } from "../middleware/forward-tracking.middleware.js";
|
|
5
5
|
import normalizeJid from "../utils/normalizeJid.esm.js";
|
|
@@ -127,7 +127,12 @@ async function processMessage(sock, msg, body) {
|
|
|
127
127
|
try {
|
|
128
128
|
const wasCommand = await handleCommand(sock, msg, body);
|
|
129
129
|
if (wasCommand) {
|
|
130
|
-
|
|
130
|
+
const text = typeof body === "string" ? body : "";
|
|
131
|
+
const command = text.trim().split(/\s+/, 1)[0] || "";
|
|
132
|
+
logger.info(
|
|
133
|
+
{ command, chat: isGroup ? "group" : "private", bodyLength: text.length },
|
|
134
|
+
"[Message] Handled as command",
|
|
135
|
+
);
|
|
131
136
|
return;
|
|
132
137
|
}
|
|
133
138
|
} catch (error) {
|
|
@@ -140,23 +145,54 @@ async function processMessage(sock, msg, body) {
|
|
|
140
145
|
}
|
|
141
146
|
}
|
|
142
147
|
|
|
143
|
-
//
|
|
144
|
-
|
|
148
|
+
// One message, one decision. Detectors only report hits; enforceHits applies
|
|
149
|
+
// them. A missing id is refused here so a live redelivery cannot be guessed.
|
|
150
|
+
// Direct detector calls used by older tests still enforce without an id.
|
|
151
|
+
export async function handleGroupModeration(sock, msg) {
|
|
145
152
|
try {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const linkActionTaken = await handleAntiLink(sock, msg, {}, normalizeJid);
|
|
154
|
-
if (linkActionTaken) return;
|
|
155
|
-
|
|
156
|
-
const mediaActionTaken = await handleMediaControl(sock, msg, {}, normalizeJid);
|
|
157
|
-
if (mediaActionTaken) return;
|
|
153
|
+
if (!msg?.key?.id) {
|
|
154
|
+
logger.warn(
|
|
155
|
+
{ group: msg?.key?.remoteJid },
|
|
156
|
+
"Skipping group moderation: message has no id",
|
|
157
|
+
);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
158
160
|
|
|
159
|
-
|
|
161
|
+
const { detectAntiLink } = require("../commands/group/antilink.cjs");
|
|
162
|
+
const { detectMedia } = require("../commands/group/media.cjs");
|
|
163
|
+
const { detectForbiddenWords } = require("../commands/mod.cjs");
|
|
164
|
+
const { enforceHits, isProtectedMember, senderCandidates } = require("../utils/moderationAction.cjs");
|
|
165
|
+
const { getGroupSettings } = require("../utils/storage.cjs");
|
|
166
|
+
const { resolveGroupMetadata } = require("../utils/groupMetadataCache.cjs");
|
|
167
|
+
const { resolveModerationPolicy } = require("../services/moderationPolicy.cjs");
|
|
168
|
+
|
|
169
|
+
const groupId = msg.key.remoteJid;
|
|
170
|
+
const settings = getGroupSettings(groupId) || {};
|
|
171
|
+
resolveModerationPolicy(settings);
|
|
172
|
+
const groupMetadata = await resolveGroupMetadata(sock, groupId);
|
|
173
|
+
const candidates = senderCandidates(msg, sock);
|
|
174
|
+
if (msg.key.fromMe || isProtectedMember(sock, groupMetadata, candidates)) return;
|
|
175
|
+
|
|
176
|
+
const hits = [];
|
|
177
|
+
const collect = (name, fn) => {
|
|
178
|
+
try {
|
|
179
|
+
const hit = fn();
|
|
180
|
+
if (hit) hits.push(hit);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logger.error({ err: error, detector: name }, "moderation detector failed");
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
collect("forbidden", () => detectForbiddenWords(msg, settings));
|
|
186
|
+
collect("link", () => detectAntiLink(msg, settings));
|
|
187
|
+
collect("media", () => detectMedia(msg, settings));
|
|
188
|
+
try {
|
|
189
|
+
const spam = await observeAntiSpam(sock, msg, settings);
|
|
190
|
+
if (spam) hits.push(spam);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
logger.error({ err: error, detector: "spam" }, "moderation detector failed");
|
|
193
|
+
}
|
|
194
|
+
if (!hits.length) return;
|
|
195
|
+
return await enforceHits(sock, { msg, groupMetadata, hits });
|
|
160
196
|
} catch (error) {
|
|
161
197
|
logger.error({ err: error }, "Error in group moderation");
|
|
162
198
|
}
|