-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclientcommands.js
254 lines (234 loc) · 7.68 KB
/
clientcommands.js
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
"use strict";
let utils = require('./utils.js');
let test = require('./test.js');
let serverCommandHandlers = {
'CLOSE': getHandler(0, 0, handleClose),
'HELP': getHandler(0, 0, handleHelp),
'HOP': getHandler(0, 0, handleHop),
'LOGOUT': getHandler(1, 0, handleLogout),
'ME': getHandler(1, 1, handleMe),
'MSG': getHandler(2, 2, handleMsg),
'NOTICE': getHandler(2, 2, handleNotice),
'RAW': getHandler(1, 0, handleRaw),
'QUIT': getHandler(1, 0, handleQuit),
'SERVER': getHandler(3, 0, handleServer),
'SESSIONS': getHandler(0, 0, handleSessions),
'TEST': getHandler(1, 1, handleTest),
'TOPIC': getHandler(2, 1, handleTopic),
'W': getHandler(1, 1, handleWhois),
'WHOIS': getHandler(1, 1, handleWhois),
};
function handleClose() {
this.activeEntity.removeEntity();
}
function handleHelp() {
this.user.showInfo('Common commands:');
this.user.showInfo('/server [host] [port] [password] to connect to a new server in the current server window. Prefix the port with + for SSL. If you got disconnected and want to reconnect to the same server, just type /server with no parameters.');
this.user.showInfo('/join #channel [key] to join a channel with the optional key');
this.user.showInfo('/msg <nick> <text> to start a private chat');
this.user.showInfo('/close to close the current window. This is the same as clicking the X in the window list column.');
this.user.showInfo('/sessions to list currently logged-in sessions, or /logout [all]');
this.user.showInfo('All unrecognized commands are treated as raw and sent to the server directly. For example, you can do: /privmsg #chan :text');
}
function handleHop() {
if (this.activeEntity.type === 'channel') {
this.server.requireConnected(() => {
let channel = this.activeEntity;
channel.rejoin();
});
} else {
this.user.showError('Use /hop in a channel to rejoin');
}
}
function handleLogout(all) {
if (typeof all === 'string' && all.toLowerCase() === 'all') {
let numSessions = 0;
let loggedInSessionsCopy = this.user.loggedInSessions.slice();
loggedInSessionsCopy.forEach(sessionId => {
if (this.user.removeLoggedInSession(sessionId)) {
numSessions++;
}
});
this.user.showInfo(`${numSessions} session(s) have been logged out. Feel free to close your browser.`);
} else {
if (this.user.removeLoggedInSession(this.sessionId)) {
this.user.showInfo('Your current browser session is now logged out. Feel free to close your browser.');
} else {
this.user.showInfo('Your current browser session is already logged out. Feel free to close your browser.');
}
}
}
function handleMe(text) {
if (this.activeEntity.type === 'channel' || this.activeEntity.type === 'query') {
this.server.requireConnected(() => {
let channelOrQuery = this.activeEntity;
this.user.applyStateChange('MyActionMessage', this.activeEntity.entityId, text);
this.server.send(`PRIVMSG ${channelOrQuery.name} :${utils.toCtcp('ACTION', text)}`);
});
} else {
this.user.showError('Can\'t /me in this window');
}
}
function handleMsg(targetName, text) {
utils.withParsedTarget(targetName, check(err => {
this.user.showError('Invalid target');
}, target => {
this.server.requireConnected(() => {
// send the message to the unparsed target name
const chunks = this.server.sendWrappedPrivmsg(targetName, text);
let displayed = false;
if (target instanceof ClientTarget) {
// /msg nick@server will not open the query window
if (target.server === null) {
const query = this.server.ensureQuery(target.toString());
for (const chunk of chunks) {
this.user.applyStateChange('MyChatMessage', query.entityId, chunk);
}
this.user.setActiveEntity(query.entityId);
displayed = true;
}
} else if (target instanceof ChannelTarget) {
this.server.withChannel(target.name, silentFail(channel => {
for (const chunk of chunks) {
this.user.applyStateChange('MyChatMessage', channel.entityId, chunk);
}
displayed = true;
}));
}
if (!displayed) {
for (const chunk of chunks) {
this.user.showInfo(`To ${targetName}: ${chunk}`);
}
}
});
}));
}
function handleNotice(targetName, text) {
this.server.requireConnected(() => {
for (const chunk of this.server.sendWrapped(`NOTICE ${targetName} :`, text)) {
this.user.showInfo(`Notice to ${targetName}: ${chunk}`);
}
});
}
function handleRaw(cmd) {
this.server.requireConnected(() => {
this.server.send(cmd);
});
}
function handleQuit(msg) {
this.server.requireConnected(() => {
msg = msg || ''; // empty if not provided
this.server.send(`QUIT :${msg}`);
this.server.disconnect(true);
}, {
allowUnregistered: true
});
}
function handleServer(host, port, password) {
function trySetPort(serverChanges, portStr) {
let portNum = parseInt(portStr);
if (!isNaN(portNum)) {
serverChanges.port = portNum;
}
}
// disconnect first since it's unclean to be changing host/port while connected
this.server.disconnect();
if (this.numArgs >= 1) { // if host provided
let serverChanges = {};
serverChanges.label = host;
serverChanges.host = host;
serverChanges.port = 6667;
serverChanges.ssl = false;
serverChanges.password = null;
if (this.numArgs >= 2) { // if port provided
if (port.substring(0, 1) === '+') {
trySetPort(serverChanges, port.substring(1));
serverChanges.ssl = true;
} else {
trySetPort(serverChanges, port);
}
if (this.numArgs >= 3) { // if password provided
serverChanges.password = password;
}
}
this.user.applyStateChange('EditServer', this.server.entityId, serverChanges);
}
this.server.reconnect();
}
function handleSessions() {
if (this.user.loggedInSessions.length > 0) {
this.user.showInfo('Logged-in sessions:');
this.user.loggedInSessions.forEach((sessionId, i) => {
this.user.showInfo(`${(i + 1)} - ${sessionId}${(sessionId == this.sessionId ? ' (current)' : '')}`);
});
} else {
this.user.showInfo('No logged-in sessions.');
}
}
function handleTest(testId) {
test.runTest(this, testId);
}
function handleTopic(channel, text) {
this.server.requireConnected(() => {
if (this.numArgs == 1) {
this.server.send(`TOPIC ${channel}`);
} else if (this.numArgs == 2) {
this.server.send(`TOPIC ${channel} :${text}`);
}
});
}
function handleWhois(targetName) {
this.server.requireConnected(() => {
this.server.send(`WHOIS ${targetName}`);
});
}
function handleClientCommand(activeEntity, command, args, sessionId) {
if (command in serverCommandHandlers) {
let handlerData = serverCommandHandlers[command];
let parsedArgs = parseArgs(handlerData.numPossibleArgs, args);
if (parsedArgs.length >= handlerData.numRequiredArgs) {
let handler = handlerData.handler;
let handlerThisObject = {
sessionId: sessionId,
user: activeEntity.server.user,
server: activeEntity.server,
activeEntity: activeEntity,
numArgs: parsedArgs.length
};
handler.apply(handlerThisObject, parsedArgs);
} else {
activeEntity.server.user.showError('Not enough parameters.');
}
} else {
activeEntity.server.requireConnected(function() {
activeEntity.server.send(`${command} ${args}`);
});
}
}
function getHandler(numPossibleArgs, numRequiredArgs, handler) {
let ret = {};
ret.numPossibleArgs = numPossibleArgs;
ret.numRequiredArgs = numRequiredArgs;
ret.handler = handler;
return ret;
}
function parseArgs(numPossibleArgs, str) {
let parsedArgs = [];
while (str.length > 0) {
if (parsedArgs.length < numPossibleArgs - 1) {
let spaceIdx = str.indexOf(' ');
if (spaceIdx !== -1) {
parsedArgs.push(str.substring(0, spaceIdx));
str = str.substring(spaceIdx + 1);
} else {
parsedArgs.push(str);
str = '';
}
} else {
parsedArgs.push(str);
str = '';
}
}
return parsedArgs;
}
module.exports.handleClientCommand = handleClientCommand;