summaryrefslogtreecommitdiff
path: root/client/commands.js
blob: 89a7ee879a692677a906da1c8f220d8b83d8fca1 (plain)
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
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
commands = {
    "verify": (argument, _2) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/verify/server") + "<br><pre style='margin: 0 !important;'>" + require('crypto').createHash("sha256").update(window.serverVerifyKey).digest("base64").match(/.{1,35}/g).join("\n") + "</pre><br>" + l("commands/verify/user") + "<br><pre style='margin: 0 !important;'>" + require('crypto').createHash("sha256").update(window.verifyKey.publicKey).digest("base64").match(/.{1,35}/g).join("\n"), true);
        } else {
            send({
                server: true,
                type: "verify",
                user: argument
            });
        }
    },
    "lookup": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/lookup/invalid"));
        } else {
            send({
                server: true,
                type: "lookup",
                user: argument
            });
        }
    },
    "edit": (argument, _) => {
        send({
            type: "edit",
            author: userName,
            colors,
            date: new Date().getTime(),
            originalDate: argument.split(" ")[0],
            text: argument.split(" ").slice(1).join(" ")
        });

        processMessage({
            type: "edit",
            author: userName,
            colors,
            date: new Date().getTime(),
            originalDate: argument.split(" ")[0],
            text: argument.split(" ").slice(1).join(" ")
        }, {
            source: localStorage.getItem("userLogin")
        });
    },
    "delete": (argument, _) => {
        send({
            type: "delete",
            author: userName,
            colors,
            date: new Date().getTime(),
            originalDate: argument.split(" ")[0]
        });

        processMessage({
            type: "delete",
            author: userName,
            colors,
            date: new Date().getTime(),
            originalDate: argument.split(" ")[0]
        }, {
            source: localStorage.getItem("userLogin")
        });
    },
    "nick": (argument, _) => {
        if (argument.trim() === "") argument = require('os').userInfo().username;

        let oldUserName = userName;
        userName = argument;
        localStorage.setItem("username", userName);
        systemMessage(sl("commands/nick/other", [["%2", oldUserName], ["%1", argument]]), l("commands/nick/self").replace("%1", argument));
    },
    "lang": (argument, _) => {
        if (require('fs').existsSync("../shared/lang/" + argument.trim().replaceAll("/", "-") + ".json")) {
            localStorage.setItem("language", argument.trim());
        } else {
            localStorage.setItem("language", "en");
        }

        location.reload();
    },
    "whoami": (argument, _) => {
        localSystemMessage(l("commands/whoami").replace("%1", localStorage.getItem("userLogin")));
    },
    "switch": async (argument, _) => {
        await updateServer();
        location.reload();
    },
    "background": (argument, _) => {
        if (argument.trim() === "") argument = null;

        if (argument) {
            try {
                require('fs').readFileSync(argument);
            } catch (e) {
                localSystemMessage(l("commands/background/error").replace("%1", e.message));
                return;
            }

            localStorage.setItem("background", argument);
            localSystemMessage(l("commands/background/changed").replace("%1", require('path').resolve(argument)));
        } else {
            localStorage.removeItem("background");
            localSystemMessage(l("commands/background/removed"));
        }

        updateBackground();
    },
    "color": (argument, _) => {
        let parts = argument.split(" ").filter(i => i.trim() !== "");

        if (parts.length === 0) {
            colors = [ "ffffff", "ffffff" ];
            localStorage.setItem("colors", JSON.stringify(colors));
            systemMessage(sl("commands/color/reset/other", [["%1", userName]]), l("commands/color/reset/self"));
            return;
        } else if (parts.length === 1) {
            parts[1] = parts[0];
        }

        if (!parts[0].match(/(^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$)/gm)) {
            localSystemMessage(l("commands/color/invalid/first"));
            return;
        }

        if (!parts[1].match(/(^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$)/gm)) {
            localSystemMessage(l("commands/color/invalid/second"));
            return;
        }

        colors = [ parts[0], parts[1] ];
        systemMessage(sl("commands/color/changed/other", [["%1", userName]]), l("commands/color/changed/self").replace("%1", parts[0].trim()).replace("%2", parts[1].trim()));
    },
    "list": (argument, _) => {
        send({
            server: true,
            type: "list"
        });
    },
    "banlist": (argument, _) => {
        send({
            server: true,
            type: "banlist"
        });
    },
    "ban": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/ban"));
            return;
        }

        send({
            server: true,
            type: "ban",
            user: argument
        });
    },
    "unban": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/unban"));
            return;
        }

        send({
            server: true,
            type: "unban",
            user: argument
        });
    },
    "ops": (argument, _) => {
        send({
            server: true,
            type: "ops"
        });
    },
    "op": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/op"));
            return;
        }

        send({
            server: true,
            type: "op",
            user: argument
        });
    },
    "deop": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/deop"));
            return;
        }

        send({
            server: true,
            type: "deop",
            user: argument
        });
    },
    "msg": (argument, _) => {
        let parts = argument.split(" ");

        if (parts.length < 2) {
            localSystemMessage(l("commands/msg"));
            return;
        }

        send({
            server: true,
            type: "dm",
            user: parts[0],
            author: userName,
            message: parts.splice(1).join(" ")
        });
    },
    "r": (argument, _) => {
        if (argument.trim() === "") {
            localSystemMessage(l("commands/r"));
            return;
        }

        send({
            server: true,
            type: "admin",
            author: userName,
            message: argument
        });
    },
    "ping": (_1, _2) => {
        if (window.connected) {
            localSystemMessage(l("commands/ping").replace("%1", window.ping));
        } else {
            localSystemMessage(l("commands/_server2"));
        }
    },
    "url": (_1, _2) => {
        if (window.connected) {
            localSystemMessage(l("commands/url") + " " + window.serverURL);
        } else {
            localSystemMessage(l("commands/_server2"));
        }
    },
    "version": (_1, _2) => {
        if (window.connected) {
            localSystemMessage(l("commands/version/localchat") + " " + stripHTML(window.version) + "<br>" + l("commands/version/launcher") + " " + stripHTML(window.launcherVersion) + "<br>" + l("commands/version/node") + " " + process.versions.node + "<br>" + l("commands/version/chrome") + " " + process.versions.chrome + "<br>" + l("commands/version/electron") + " " + process.versions.electron + "<br>" + l("commands/version/server") + " " + stripHTML(window.serverVersion), true);
        } else {
            localSystemMessage(l("commands/version/localchat") + " " + stripHTML(window.version) + "<br>" + l("commands/version/launcher") + " " + stripHTML(window.launcherVersion) + "<br>" + l("commands/version/node") + " " + process.versions.node + "<br>" + l("commands/version/chrome") + " " + process.versions.chrome + "<br>" + l("commands/version/electron") + " " + process.versions.electron, true);
        }
    },
    "changelog": (_1, _2) => {
        localSystemMessage(window.changeLog.trim().replace("%1", window.version).replaceAll("\n", "<br>"), true);
    },
    "channel": (_1, _2) => {
        if (require('fs').existsSync(window.applicationData + "/LocalchatBetaChannel")) {
            require('fs').unlinkSync(window.applicationData + "/LocalchatBetaChannel");
            localSystemMessage(l("commands/channel/stable"));
        } else {
            require('fs').writeFileSync(window.applicationData + "/LocalchatBetaChannel", "");
            localSystemMessage(l("commands/channel/beta"));
        }
    },
    "_": (argument, command) => {
        localSystemMessage(l("commands/_").replace("%1", command));
    },
    "help": (_1, _2) => {
        localSystemMessage(l("commands/help") + "\n\xa0\xa0- " + Object.keys(commands).filter(i => i !== "_").sort((a, b) => a.localeCompare(b)).map((i) => {
            if (l("help/" + i) !== "help/" + i) {
                return i + l("help/_") + " " + l("help/" + i);
            } else {
                return i;
            }
        }).join("\n\xa0\xa0- "));
    },
    "reload": (_1, _2) => {
        location.reload();
    }
}

function runCommand(message) {
    let words = message.replaceAll(" ", " ").trim().substring(1).split(" ").filter(i => i.trim() !== "");
    let command = stripHTML(words[0]);
    words.shift();
    let argument = words.join(" ");

    console.log(command, argument);

    if (commands[command]) {
        if (window.connected) {
            commands[command](argument, command);
        } else {
            localSystemMessage(l("commands/_server"));
        }
    } else {
        commands["_"](argument, command);
    }
}