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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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
 * Modlog viewer
 * Pokemon Showdown - http://pokemonshowdown.com/
 *
 * Actually reading, writing, and searching modlog is handled in server/modlog/.
 *
 * @license MIT
 */

import { Dashycode, Utils } from '../../lib';
import type { ModlogID, ModlogSearch, ModlogEntry } from '../modlog';

const MAX_QUERY_LENGTH = 2500;
const DEFAULT_RESULTS_LENGTH = 100;
const MORE_BUTTON_INCREMENTS = [200, 400, 800, 1600, 3200];
const LINES_SEPARATOR = 'lines=';
const MAX_RESULTS_LENGTH = MORE_BUTTON_INCREMENTS[MORE_BUTTON_INCREMENTS.length - 1];
const IPS_REGEX = /[([]?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[)\]]?/g;

const ALIASES: { [k: string]: string } = {
	'helpticket': 'help-rooms',
	'groupchat': 'groupchat-rooms',
	'battle': 'battle-rooms',
};

/*********************************************************
 * Modlog Functions
 *********************************************************/

function getMoreButton(
	roomid: ModlogID, searchCmd: string,
	lines: number, maxLines: number, onlyPunishments: boolean, onlyNotes: boolean,
) {
	let newLines = 0;
	for (const increase of MORE_BUTTON_INCREMENTS) {
		if (increase > lines) {
			newLines = increase;
			break;
		}
	}
	if (!newLines || lines < maxLines) {
		return ''; // don't show a button if no more pre-set increments are valid or if the amount of results is already below the max
	} else {
		let cmd = `/modlog`;
		if (onlyNotes) cmd = `/modnotes`;
		if (onlyPunishments) cmd = `/punishlog`;

		return Utils.html`<br /><div style="text-align:center"><button class="button" name="send" value="${cmd} room=${roomid}, ${searchCmd}, ${LINES_SEPARATOR}${newLines}" title="View more results">Older results<br />&#x25bc;</button></div>`;
	}
}

function getRoomID(id: string) {
	if (id in ALIASES) return ALIASES[id] as ModlogID;
	return id as ModlogID;
}

function prettifyResults(
	resultArray: ModlogEntry[], roomid: ModlogID, search: ModlogSearch, searchCmd: string,
	addModlogLinks: boolean, hideIps: boolean, maxLines: number, onlyPunishments: boolean, onlyNotes: boolean
) {
	if (resultArray === null) {
		return "|popup|The modlog query crashed.";
	}
	let roomName;
	switch (roomid) {
	case 'all':
		roomName = "all rooms";
		break;
	case 'public':
		roomName = "all public rooms";
		break;
	default:
		roomName = `room ${roomid}`;
	}
	const scope = onlyPunishments ? 'punishment-related ' : '';
	let searchString = ``;
	const excludes = search.note.filter(s => s.isExclusion).map(s => s.search) || [];
	const includes = search.note.filter(s => !s.isExclusion).map(s => s.search) || [];
	if (includes.length) searchString += `with a note including any of: ${includes.join(', ')} `;
	if (excludes.length) searchString += `with a note that does not include any of: ${excludes.join(', ')} `;
	for (const u of search.user) searchString += `${u.isExclusion ? 'not ' : ''}taken against ${u.search} `;
	for (const ip of search.ip) {
		searchString += `${ip.isExclusion ? 'not ' : ''}taken against a user on the IP ${ip.search} `;
	}
	for (const action of search.action) searchString += `${action.isExclusion ? 'not ' : ''}of the type ${action.search} `;
	for (const actionTaker of search.actionTaker) {
		searchString += `${actionTaker.isExclusion ? 'not ' : ''}taken by ${actionTaker.search} `;
	}
	if (!resultArray.length) {
		return `|popup|No ${scope}moderator actions ${searchString}found on ${roomName}.`;
	}
	const title = `[${roomid}] ${searchCmd}`;
	const lines = resultArray.length;
	let curDate = '';
	const resultString = resultArray.map(result => {
		const date = new Date(result.time || Date.now());
		const entryRoom = result.visualRoomID || result.roomID || 'global';
		let [dateString, timestamp] = Chat.toTimestamp(date, { human: true }).split(' ');
		let line = `<small>[${timestamp}] (${entryRoom})</small> ${result.action}`;
		if (result.userid) {
			line += `: [${result.userid}]`;
			if (result.autoconfirmedID) line += ` ac: [${result.autoconfirmedID}]`;
			if (result.alts.length) line += ` alts: [${result.alts.join('], [')}]`;
			if (!hideIps && result.ip) line += ` [${result.ip}]`;
		}

		if (result.loggedBy) line += `: by ${result.loggedBy}`;
		if (result.note) line += `: ${result.note}`;

		if (dateString !== curDate) {
			curDate = dateString;
			dateString = `</p><p>[${dateString}]<br />`;
		} else {
			dateString = ``;
		}
		const thisRoomID = entryRoom?.split(' ')[0];
		if (addModlogLinks) {
			if (thisRoomID.startsWith('battle-')) {
				timestamp = `<a href="/${thisRoomID}">${timestamp}</a>`;
			} else {
				const [day, time] = Chat.toTimestamp(date).split(' ');
				timestamp = `<a href="/view-chatlog-${thisRoomID}--${day}--time-${toID(time)}">${timestamp}</a>`;
			}
		}
		line = Utils.escapeHTML(line.slice(line.indexOf(')') + ` </small>`.length));
		line = line.replace(
			IPS_REGEX,
			hideIps ? '' : `[<a href="https://whatismyipaddress.com/ip/$1" target="_blank">$1</a>]`
		);
		return `${dateString}<small>[${timestamp}] (${thisRoomID})</small>${line}`;
	}).join(`<br />`);
	const [dateString, timestamp] = Chat.toTimestamp(new Date(), { human: true }).split(' ');
	let preamble;
	const modlogid = roomid + (searchString ? '-' + Dashycode.encode(searchString) : '');
	if (searchString) {
		preamble = `>view-modlog-${modlogid}\n|init|html\n|title|[Modlog]${title}\n` +
			`|pagehtml|<div class="pad"><p>The last ${scope}${Chat.count(lines, "logged actions")} ${Utils.escapeHTML(searchString)} on ${roomName}.`;
	} else {
		preamble = `>view-modlog-${modlogid}\n|init|html\n|title|[Modlog]${title}\n` +
			`|pagehtml|<div class="pad"><p>The last ${Chat.count(lines, `${scope}lines`)} of the Moderator Log of ${roomName}.`;
	}
	preamble += `</p><p>[${dateString}]<br /><small>[${timestamp}] \u2190 current server time</small>`;
	const moreButton = getMoreButton(roomid, searchCmd, lines, maxLines, onlyPunishments, onlyNotes);
	return `${preamble}${resultString}${moreButton}</div>`;
}

async function getModlog(
	connection: Connection, roomid: ModlogID = 'global', search: ModlogSearch,
	searchCmd: string, maxLines = 20, onlyPunishments = false, timed = false, onlyNotes = false,
) {
	const targetRoom = Rooms.search(roomid);
	const user = connection.user;
	roomid = getRoomID(roomid);

	// permission checking
	if (roomid === 'all' || roomid === 'public') {
		if (!user.can('modlog')) {
			return connection.popup("Access denied");
		}
	} else {
		if (!user.can('modlog', null, targetRoom) && !user.can('modlog')) {
			return connection.popup("Access denied");
		}
	}

	const hideIps = !user.can('lock');
	const addModlogLinks = !!(
		(user.tempGroup !== ' ' || (targetRoom && targetRoom.settings.isPrivate !== true))
	);
	if (hideIps && search.ip.length) {
		connection.popup(`You cannot search for IPs.`);
		return;
	}
	if (Object.values(search).join('').length > MAX_QUERY_LENGTH) {
		connection.popup(`Your search query is too long.`);
		return;
	}

	if (search.note?.length) {
		for (const [i, noteSearch] of search.note.entries()) {
			if (/^["'].+["']$/.test(noteSearch.search)) {
				search.note[i] = { ...noteSearch, search: noteSearch.search.substring(1, noteSearch.search.length - 1) };
				search.note[i].isExact = true;
			}
		}
	}

	for (const [i, userSearch] of search.user.entries()) {
		if (/^["'].+["']$/.test(userSearch.search)) {
			userSearch.search = userSearch.search.substring(1, userSearch.search.length - 1);
			userSearch.isExact = true;
		}
		userSearch.search = toID(userSearch.search);

		search.user[i] = userSearch;
	}
	if (onlyNotes) search.action.push({ search: 'NOTE' });

	const response = await Rooms.Modlog.search(roomid, search, maxLines, onlyPunishments);
	if (!response) return connection.popup(`The moderator log is currently disabled.`);

	connection.send(
		prettifyResults(
			response.results,
			roomid,
			search,
			searchCmd,
			addModlogLinks,
			hideIps,
			maxLines,
			onlyPunishments,
			onlyNotes,
		)
	);
	if (timed) connection.popup(`The modlog query took ${response.duration} ms to complete.`);
}

const shouldSearchGlobal = ['staff', 'adminlog'];

export const commands: Chat.ChatCommands = {
	ml: 'modlog',
	punishlog: 'modlog',
	pl: 'modlog',
	timedmodlog: 'modlog',
	mlid: 'modlog',
	mlip: 'modlog',
	plid: 'modlog',
	plip: 'modlog',
	modnotes: 'modlog',
	modlog(target, room, user, connection, cmd) {
		let roomid: ModlogID = (!room || shouldSearchGlobal.includes(room.roomid) ? 'global' : room.roomid);
		const onlyPunishments = cmd.startsWith('pl') || cmd.startsWith('punishlog');
		let lines;
		const possibleParam = cmd.slice(2);
		const targets = target.split(',').map(f => f.trim()).filter(Boolean);
		const search: ModlogSearch = { note: [], user: [], ip: [], action: [], actionTaker: [] };

		switch (possibleParam) {
		case 'id':
			targets.unshift(`user='${targets.shift()}'`);
			break;
		case 'ip':
			targets.unshift(`ip=${targets.shift()}`);
			break;
		}

		for (const [i, option] of targets.entries()) {
			let [param, value] = option.split('=').map(part => part.trim());
			if (!value) {
				// If no specific parameter is specified, we should search all fields
				value = param.trim();
				if (i === 0 && value) {
					// they might mean a roomid, as per the old format of /modlog
					param = 'room';
					// if the room exists, they probably mean a roomid. otherwise, assume they're misusing it.
					// we except gdrivers+ from this because drivers can access deleted room modlogs
					if (!Rooms.search(toID(value)) && !user.can('lock')) {
						return this.parse(`/help modlog`);
					}
				} else {
					this.errorReply(`You must specify a search type and search value.`);
					return this.parse(`/help modlog`);
				}
			}
			const isExclusion = param.endsWith('!');
			param = toID(param);
			switch (param) {
			case 'note': case 'text':
				if (!search.note) search.note = [];
				search.note.push({ search: value, isExclusion });
				break;
			case 'user': case 'name': case 'username': case 'userid':
				search.user.push({ search: value });
				break;
			case 'ip': case 'ipaddress': case 'ipaddr':
				search.ip.push({ search: value, isExclusion });
				break;
			case 'action': case 'punishment':
				search.action.push({ search: value.toUpperCase(), isExclusion });
				break;
			case 'actiontaker': case 'moderator': case 'staff': case 'mod':
				search.actionTaker.push({ search: toID(value), isExclusion });
				break;
			case 'room': case 'roomid':
				roomid = value.toLowerCase().replace(/[^a-z0-9-]+/g, '') as ModlogID;
				break;
			case 'lines': case 'maxlines':
				lines = parseInt(value);
				if (isNaN(lines) || lines < 1) return this.errorReply(`Invalid linecount: '${value}'.`);
				break;
			default:
				this.errorReply(`Invalid modlog parameter: '${param}'.`);
				return this.errorReply(`Please specify 'room', 'note', 'user', 'ip', 'action', 'staff', or 'lines'.`);
			}
		}

		const targetRoom = Rooms.search(roomid);
		// if a room alias was used, replace alias with actual id
		if (targetRoom) roomid = targetRoom.roomid;

		if (roomid.includes('-')) {
			if (user.can('modlog')) {
				// default to global modlog for staff convenience
				roomid = 'global';
			} else {
				return this.errorReply(`Only global staff may view battle and groupchat modlogs.`);
			}
		}

		if (!target && !lines) {
			lines = 20;
		}
		if (!lines) lines = DEFAULT_RESULTS_LENGTH;
		if (lines > MAX_RESULTS_LENGTH) lines = MAX_RESULTS_LENGTH;

		void getModlog(
			connection,
			roomid,
			search,
			target.replace(/^\s?([^,=]*),\s?/, '').replace(/,?\s*(room|lines)\s*=[^,]*,?/g, ''),
			lines,
			onlyPunishments,
			cmd === 'timedmodlog',
			cmd === 'modnotes',
		);
	},
	modloghelp() {
		this.sendReplyBox(
			`<code>/modlog [comma-separated list of parameters]</code>: searches the moderator log, defaulting to the current room unless specified otherwise.<br />` +
			`You can replace the <code>=</code> in a parameter with a <code>!=</code> to exclude entries that match that parameter.<br />` +
			`<details><summary><strong>Parameters</strong></summary>` +
			`<ul>` +
			`<li><code>room=[room]</code> - searches a room's modlog</li>` +
			`<li><code>userid=[user]</code> - searches for a username (or fragment of one)</li>` +
			`<li><code>note=[text]</code> - searches the contents of notes/reasons</li>` +
			`<li><code>ip=[IP address]</code> - searches for an IP address (or fragment of one)</li>` +
			`<li><code>staff=[user]</code> - searches for actions taken by a particular staff member</li>` +
			`<li><code>action=[type]</code> - searches for a particular type of action</li>` +
			`<li><code>lines=[number]</code> - displays the given number of lines</li>` +
			`</ul>` +
			`</details>` +
			`<details><summary><strong>Additional commands</strong></summary>` +
			`<ul>` +
			`<li><code>/mlid [user]</code> - searches for actions taken against a specific user</li>` +
			`<li><code>/mlip [IP address]</code> - searches for actions taken against a specific IP address</li>` +
			`<li><code>/punishlog</code>, <code>/pl</code>, <code>/plid</code>, <code>/plip</code> - like <code>/modlog</code>, but only displays punishments</li>` +
			`<li><code>/modnotes</code> - searches only modnotes</li>` +
			`</ul>` +
			`</details>`
		);
	},
	mls: 'modlogstats',
	modlogstats(target, room, user) {
		this.checkCan('lock');
		target = toID(target);
		if (!target) return this.parse(`/help modlogstats`);
		return this.parse(`/join view-modlogstats-${target}`);
	},
	modlogstatshelp: [`/modlogstats [userid] - Fetch all information on that [userid] from the modlog (IPs, alts, etc). Requires: @ ~`],
};

export const pages: Chat.PageTable = {
	async modlogstats(query, user) {
		this.checkCan('lock');
		const target = toID(query.shift());
		if (!target || target.length > 18) {
			return this.errorReply(`Invalid userid - must be between 1 and 18 characters long.`);
		}
		this.title = `[Modlog Stats] ${target}`;
		this.setHTML(`<div class="pad"><strong>Running modlog search...</strong></div>`);
		const entries = await Rooms.Modlog.search('global', {
			user: [{
				search: target,
				isExact: true,
			}], note: [], ip: [], action: [], actionTaker: [],
		}, 1000);
		if (!entries?.results.length) {
			return this.errorReply(`No data found.`);
		}
		const punishmentTable = new Utils.Multiset<string>();
		const punishmentsByIp = new Map<string, Utils.Multiset<string>>();
		const actionsWithIp = new Set<string>();
		const alts = new Set<string>();
		const autoconfirmed = new Set<string>();
		const ips = new Set<string>();
		for (const entry of entries.results) {
			if (entry.action !== 'NOTE') {
				punishmentTable.add(entry.action);
				if (entry.ip) {
					let ipTable = punishmentsByIp.get(entry.ip);
					if (!ipTable) {
						ipTable = new Utils.Multiset<string>();
						punishmentsByIp.set(entry.ip, ipTable);
					}
					ipTable.add(entry.action);
					actionsWithIp.add(entry.action);
				}
			}
			if (entry.alts) {
				for (const alt of entry.alts) alts.add(alt);
			}
			if (entry.autoconfirmedID) autoconfirmed.add(entry.autoconfirmedID);
			if (entry.ip) ips.add(entry.ip);
		}
		let buf = `<div class="pad"><h2>Modlog information for ${target}</h2><hr />`;
		if (alts.size) {
			buf += `<strong>Listed alts:</strong> `;
			buf += `<small>(These are userids sharing the same IP at the time of the punishment, they may not be direct alts)</small><br />`;
			buf += [...alts]
				.map(id => `<a href="https://${Config.routes.root}/users/${toID(id)}">${toID(id)}</a>`)
				.join(', ');
			buf += `<br /><br />`;
		}

		if (autoconfirmed.size) {
			buf += `<strong>Autoconfirmed alts:</strong>`;
			buf += ` (these are autoconfirmed accounts linked to their name, and are very likely them)<br />`;
			buf += [...autoconfirmed]
				.map(id => `<a href="https://${Config.routes.root}/users/${toID(id)}">${toID(id)}</a>`)
				.join(', ');
			buf += `<br /><br />`;
		}

		if (ips.size) {
			buf += `<strong>Known IPs:</strong><br />`;
			const mapped = await Promise.all([...ips].map(async ip => {
				const info = await IPTools.lookup(ip);
				return `<a href="https://whatismyipaddress.com/ip/${ip}">${ip}</a> [${info.hostType}]`;
			}));
			buf += mapped.join(', ');
			buf += `<br /><br />`;
		}

		if (punishmentTable.size) {
			buf += `<strong>Punishments:</strong><br />`;
			buf += `<div class="ladder pad"><table>`;
			buf += `<tr><th>Punishment type</th><th>Count</th></tr>`;
			for (const [punishment, number] of punishmentTable) {
				buf += `<tr><td>${punishment}</td><td>${number}</td></tr>`;
			}
			buf += `</table></div><br />`;
		}
		if (punishmentsByIp.size) {
			buf += `<strong>Punishments by IP:</strong><br />`;
			const keys = [...actionsWithIp];
			buf += `<div class="ladder pad"><table>`;
			buf += `<tr><th></th>${keys.map(k => `<th>${k}</th>`).join("")}</tr>`;
			for (const [ip, table] of punishmentsByIp) {
				buf += `<tr><td><a href="https://whatismyipaddress.com/ip/${ip}">${ip}</a></td>`;
				for (const key of keys) {
					buf += `<td>${table.get(key)}</td>`;
				}
				buf += `</tr>`;
			}
			buf += `</table></div>`;
		}

		buf += `<br /><br />`;
		return buf;
	},
};