File size: 4,931 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Code to manage username prefixes that force battles to be public or disable modchat.
 * @author Annika
 */

import { FS } from '../../lib';

const PREFIXES_FILE = 'config/chat-plugins/username-prefixes.json';
const PREFIX_DURATION = 10 * 24 * 60 * 60 * 1000;

export class PrefixManager {
	constructor() {
		// after a restart/newly using the plugin, load prefixes from config.js
		if (!Chat.oldPlugins['username-prefixes']) this.refreshConfig(true);
	}

	save() {
		FS(PREFIXES_FILE).writeUpdate(() => JSON.stringify(Config.forcedprefixes || []));
	}

	refreshConfig(configJustLoaded = false) {
		if (!Config.forcedprefixes) Config.forcedprefixes = [];
		// ensure everything is in the right format
		if (!Array.isArray(Config.forcedprefixes)) {
			const convertedPrefixes = [];
			for (const type in Config.forcedprefixes) {
				for (const prefix of Config.forcedprefixes[type].map(toID)) {
					convertedPrefixes.push({ type, prefix, expireAt: Date.now() + PREFIX_DURATION });
				}
			}
			Config.forcedprefixes = convertedPrefixes;
		}
		if (configJustLoaded) {
			for (const entry of Config.forcedprefixes) {
				entry.prefix = toID(entry.prefix);
			}
		}

		let data: AnyObject[];
		try {
			data = JSON.parse(FS(PREFIXES_FILE).readSync());
		} catch (e: any) {
			if (e.code !== 'ENOENT') throw e;
			return;
		}
		if (data.length) {
			for (const entry of data) {
				if (Config.forcedprefixes.includes(entry)) continue;
				Config.forcedprefixes.push(entry);
			}
		}
	}

	addPrefix(prefix: ID, type: 'privacy' | 'modchat') {
		if (!Config.forcedprefixes) Config.forcedprefixes = [];
		const entry = Config.forcedprefixes.find((x: AnyObject) => x.prefix === prefix && x.type === type);
		if (entry) {
			throw new Chat.ErrorMessage(`Username prefix '${prefix}' is already configured to force ${type}.`);
		}

		Config.forcedprefixes.push({ type, prefix, expireAt: Date.now() + PREFIX_DURATION });
		this.save();
	}

	removePrefix(prefix: ID, type: 'privacy' | 'modchat') {
		const entry = Config.forcedprefixes.findIndex((x: AnyObject) => x.prefix === prefix && x.type === type);
		if (entry < 0) {
			throw new Chat.ErrorMessage(`Username prefix '${prefix}' is not configured to force ${type}!`);
		}

		Config.forcedprefixes.splice(entry, 1);
		this.save();
	}

	validateType(type: string) {
		if (type !== 'privacy' && type !== 'modchat') {
			throw new Chat.ErrorMessage(`'${type}' is not a valid type of forced prefix. Valid types are 'privacy' and 'modchat'.`);
		}
		return type;
	}
}

export const prefixManager = new PrefixManager();

export const commands: Chat.ChatCommands = {
	forceprefix: 'usernameprefix',
	forcedprefix: 'usernameprefix',
	forcedprefixes: 'usernameprefix',
	usernameprefixes: 'usernameprefix',
	usernameprefix: {
		help: '',
		''() {
			this.parse(`/help forcedprefix`);
		},

		delete: 'add',
		remove: 'add',
		add(target, room, user, connection, cmd) {
			this.checkCan('addhtml');

			const isAdding = cmd.includes('add');

			const [prefix, type] = target.split(',').map(toID);
			if (!prefix || !type) return this.parse(`/help usernameprefix`);
			if (prefix.length > 18) {
				throw new Chat.ErrorMessage(`Specified prefix '${prefix}' is longer than the maximum user ID length.`);
			}

			if (isAdding) {
				prefixManager.addPrefix(prefix, prefixManager.validateType(type));
			} else {
				prefixManager.removePrefix(prefix, prefixManager.validateType(type));
			}

			this.globalModlog(`FORCEDPREFIX ${isAdding ? 'ADD' : 'REMOVE'}`, null, `'${prefix}' ${isAdding ? 'to' : 'from'} ${type}`);
			this.addGlobalModAction(`${user.name} set the username prefix ${prefix} to${isAdding ? '' : ' no longer'} disable ${type}.`);
		},

		view(target) {
			this.checkCan('addhtml');

			const types = target ? [prefixManager.validateType(toID(target))] : ['privacy', 'modchat'];

			const entries = Config.forcedprefixes.filter((x: any) => types.includes(x.type));

			return this.sendReplyBox(types.map(type => {
				const prefixes = entries.filter((x: any) => x.type === type).map((x: any) => x.prefix);
				const info = prefixes.length ?
					`<code>${prefixes.join('</code>, <code>')}</code>` : `none`;
				return `Username prefixes that disable <strong>${type}</strong>: ${info}.`;
			}).join(`<br />`));
		},
	},
	usernameprefixhelp() {
		return this.sendReplyBox(
			`<code>/usernameprefix add [prefix], [type]</code>: Sets the username prefix [prefix] to disable privacy or modchat on battles where at least one player has the prefix.<br />` +
			`<code>/usernameprefix remove [prefix], [type]</code>: Removes a prefix configuration.<br />` +
			`<code>/usernameprefix view [optional type]</code>: Displays the currently configured username prefixes.<br />` +
			`Valid types are <code>privacy</code> (which forces battles to take place in public rooms) and <code>modchat</code> (which prevents players from setting moderated chat).<br />` +
			`Requires: * ~`
		);
	},
};