-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.html
689 lines (668 loc) · 41.6 KB
/
client.html
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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<!DOCTYPE html>
<html>
<head>
<title>chat</title>
<style type="text/css">
html, body {
margin: 0;
background-color: #aaaaaa;
}
ul {
margin: 0;
padding-left: 20px;
padding-top: 0px;
padding-bottom: 5px;
}
ul.server {
padding-top: 5px;
list-style-type: "\25cf";
color: black;
}
ul.channel {
list-style-type: "\25be";
color: black;
}
ul.user {
list-style-type: "\25b8";
color: black;
}
.time {
font-weight: normal;
font-size: x-small;
color: lightslategray;
}
.error {
font-weight: bold;
color: red;
}
.info {
font-weight: normal;
color: lightslategray;
}
.wall {
font-weight: normal;
color: black;
}
.server {
font-weight: bold;
color: black;
}
.user {
font-weight: bold;
color: green;
}
.user.admin {
color: yellow;
}
.user.self {
color: orange;
}
.channel {
font-weight: bold;
color: blue;
}
.topic {
font-weight: bold;
color: black;
}
</style>
<script src="winbox.bundle.min.js"></script>
<script src="markdown-it.min.js"></script>
</head>
<body>
<script type="text/javascript">
function beep() {
var snd = new Audio('data:audio/wav;base64,UklGRsIBAABXQVZFZm10IBIAAAAHAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAJABAABkYXRhkAEAALKanZudmyoaHRwcIJydm56ZuRkeGh4ZuJmdm52dIRwcHRopmp6anpl+GR4aHhqqmp2cnKEdHRsdGTiZnpqemTgZHRsdHaGcnJ2aqhoeGh4Z/5memp6aKhodHBwhnZ2bnZm4GR4aHhm4mZ2bnZ0hHBwdGiqanpqemX8ZHhoeGqqanZycoR0dGx0ZOJmemp6ZOBkdGx0doZycnZqqGh4aHhn/mZ6anpoqGh0cHCGdnZudmbgZHhoeGbiZnZudnSEcHB0aKpqemp6Z/xkeGh4aqpqdnJyhHR0bHRk4mZ6anpk4GR0bHR2hnJydmqoaHhoeGf+ZnpqemioaHRwcIZ2dm52ZuBkeGh4ZuJmdm52dIRwcHRoqmp6anpn/GR4aHhqqmp2cnKEdHRsdGTiZnpqemTgZHRsdHaGcnJ2aqhoeGh4Zf5memp6aKhodHBwhnZ2bnZm4GR4aHhm4mZ2bnZ0hHBwdGiqanpqemf4ZHhoeGqmanZycoR0dGx0ZOJmemp6ZORkeGx0coJycnZqqGx0bHRo=');
snd.play();
}
function sanitize(string) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'=': '=',
'`': '`'
};
const reg = /[&<>"'/=`]/ig;
return string.replace(reg, (match)=>(map[match]));
};
var settings = JSON.parse(window.localStorage.getItem('workspace-settings'))||{
input:{x: 10, y: window.innerHeight-80, width: window.innerWidth-20},
selector: {x: window.innerWidth-300, y: 10, width: 290, height: window.innerHeight-100},
general:{x: 10, y: 10, width: window.innerWidth*50/100, height: window.innerHeight*60/100}
};
var input = new WinBox({
id: 'input-container',
title: 'input',
class: ['no-full','no-max','no-close'],
background: '#666',
header: 23,
border: 3,
icon: false,
x: settings.input.x+settings.input.width>window.innerWidth-10 ? Math.max(10, window.innerWidth-settings.input.width-10) : settings.input.x,
y: settings.input.y>window.innerHeight-80 ? window.innerHeight-80 : settings.input.y,
width: settings.input.width>window.innerWidth-20 ? window.innerWidth-20 : settings.input.width,
height: 47,
html: '<form action="javascript:command()" style="display:flex"><input type="text" id="message" style="flex:1" autofocus/><input type="submit" value="send" style="flex:0"/></form>',
onresize: (width,height) => { if (height != 47) input.resize(width, 47); }
});
var selector = new WinBox({
id: 'selector',
title: 'selector',
class: ['no-full','no-close'],
background: '#666',
header: 23, border: 3,
icon: false,
x: settings.selector.x+settings.selector.width>window.innerWidth-10 ? Math.max(10, window.innerWidth-settings.selector.width-10) : settings.selector.x,
y: settings.selector.y+settings.selector.height>window.innerHeight-33 ? Math.max(10, window.innerHeight-settings.selector.height-33) : settings.selector.y,
width: settings.selector.width>window.innerWidth-20 ? window.innerWidth-20 : settings.selector.width,
height: settings.selector.height>window.innerHeight-43 ? window.innerHeight-43 : settings.selector.height,
});
var general = new WinBox({
id: 'general',
title: 'general',
class: ['no-full','no-close'],
background: '#666',
header: 23, border:3,
icon: false,
x: settings.general.x+settings.general.width>window.innerWidth-10 ? Math.max(10, window.innerWidth-settings.general.width-10) : settings.general.x,
y: settings.general.y+settings.general.height>window.innerHeight-33 ? Math.max(10, window.innerHeight-settings.general.height-33) : settings.general.y,
width: settings.general.width>window.innerWidth-20 ? window.innerWidth-20 : settings.general.width,
height: settings.general.height>window.innerHeight-43 ? window.innerHeight-43 : settings.general.height,
});
var windows = {channels: new Map(), users: new Map()};
var defaultAction = (command) => { error(general, 'no channel selected. use /select to set the active channel.'); };
var myself = undefined;
var timeFormat = Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "numeric",
hour12: false
});
var ws = { send: () => { error(general, 'not connected to any server');} };
var md = window.markdownit({html: false, xhtmlOut: true, breaks: true, linkify: true, typographer: false});
function send(command, args=undefined) {
data = [command];
if (args) data.push(args);
ws.send(JSON.stringify(data));
};
function append(window, text) {
if (window) {
window.body.innerHTML += '<span class="time">'+timeFormat.format(new Date())+' </span>'+text+'<br/>';
window.body.scrollTop = window.body.scrollHeight;
}
}
function wall(window, text) {
append(window, '<span class="wall">'+text+'</span>');
}
function error(window, text) {
append(window, '<span class="error">'+text+'</span>');
}
function info(window, text) {
append(window, '<span class="info">'+text+'</span>');
}
function channelOptionsToString(options) {
let result = ''
if (options.moderated) result += 'm';
if (options.topic) result += 't';
if (options.secret) result += 's';
if (options.password) result += 'k';
if (result) return ' (+'+result+')'; else return '';
};
function userOptionsToString(options) {
let plus = '';
let minus = '';
if (options) {
if (options.admin) plus += 'a';
if (options.bot) plus += 'b';
if (options.operator) plus += 'o';
if (('voiced' in options) && !options.voiced) minus += 'v'
}
if (!plus && !minus)
return '<span class="options"></span>';
else
return '<span class="options">('+(plus?'+'+plus:'')+(minus?'-'+minus:'')+')</span>';
};
function channelToString(channel, options=undefined) {
return '<span class="channel">#'+sanitize(channel)+(options?channelOptionsToString(options):'')+'</span>';
};
function userToString(user, options=undefined) {
if (typeof user == 'string')
return '<span class="user">@'+sanitize(user)+'</span>'+' '+userOptionsToString(options);
else {
all = {...user.options, ...options}
cls = !!all.admin?'admin':'';
prefix = !!all.bot?'!':'@';
return '<span class="user '+cls+'">'+prefix+sanitize(user.nick||'(undefined)')+' '+userOptionsToString(all)+'</span>';
}
};
function messageToString(message) {
let msg = md.renderInline(message);
msg = msg.replace(/@([^@#&!\s]+)/g, (match, p1) => {
let cls = 'user';
if (p1 == myself.nick) {
beep();
cls += " self";
};
return '<span class="'+cls+'">'+match+'</span>';
});
return msg;
};
function identToString(user) {
return '<span class="user">'+sanitize(user.nick||'(undefined)')+'@'+user.url+'</span>';
};
function patternToString(pattern) {
return ('nick' in pattern ? pattern.nick : '*')+('channel' in pattern ? '#'+pattern.channel : '')+('url' in pattern ? '@'+pattern.url : '');
}
function stringToUser(value) {
result = /^(?<nick>[^@#&!\s]+)?(#(?<channel>[^@#&!\s]+))?(@(?<url>[^@#&!\s]*))?$/.exec(value);
if (!result) return undefined;
else return result.groups;
};
function addServer(url) {
selector.body.innerHTML = `<ul id="selector-servers" class="server"><li><span class="server" onclick="general.focus()">${url}</span><ul id="selector-channels" class="channel"></ul></li></ul>`;
};
function addChannel(channel) {
let clnChannel = sanitize(channel)
document.getElementById('selector-channels').innerHTML += `<li id="selector-channel-${clnChannel}"><span class="channel" onclick="getChannelWindow('${channel}').focus()">#${clnChannel}</span><ul class="user" id="selector-channel-${clnChannel}-users"></ul</li>`;
};
function removeChannel(channel) {
document.getElementById('selector-channels').removeChild(document.getElementById(`selector-channel-${sanitize(channel)}`));
};
function removeAllUsers(channel) {
let clnChannel = sanitize(channel);
let parent = document.getElementById(`selector-channel-${clnChannel}-users`);
if (parent) {
while (parent.firstChild) {
parent.removeChild(parent.lastChild);
}
}
}
function addUser(channel, user, options=undefined) {
let clnChannel = sanitize(channel);
let clnUser = sanitize(user.nick);
let parent = document.getElementById(`selector-channel-${clnChannel}-users`)
if (parent)
parent.innerHTML += `<li id="selector-channel-${clnChannel}-user-${user.url}" title="${clnUser}@${user.url}" onclick="getUserWindow('${user.nick}', '${user.url}').focus()">${userToString(clnUser, options)}</li>`;
};
function removeUser(channel, user) {
let clnChannel = sanitize(channel);
let parent = document.getElementById(`selector-channel-${clnChannel}-users`)
if (parent)
parent.removeChild(document.getElementById(`selector-channel-${clnChannel}-user-${user.url}`));
};
function updateUser(channel, user, options=undefined) {
parent = document.getElementById(`selector-channel-${sanitize(channel)}-user-${user.url}`)
if (parent) {
parent.removeChild(parent.firstChild);
parent.insertAdjacentHTML('afterbegin', userToString(user.nick));
if (options) {
parent.removeChild(parent.lastChild)
parent.insertAdjacentHTML('beforeend', userOptionsToString(options));
}
}
};
function createChannelWindow(channel) {
if (!windows.channels.has(channel)) {
let dimensions = {
x: 10+20*(windows.channels.size+windows.users.size+1),
y: 10+20*(windows.channels.size+windows.users.size+1),
width: window.innerWidth*50/100,
height: window.innerHeight*60/100
};
let subwindow = new WinBox({
id: 'window-channel-'+channel,
title: `#${sanitize(channel)}`,
class: ['no-full'],
background: '#666',
header: 23, border:3,
icon: false,
x: dimensions.x+dimensions.width>window.innerWidth-10 ? Math.max(10, window.innerWidth-dimensions.width-10) : dimensions.x,
y: dimensions.y+dimensions.height>window.innerHeight-10 ? Math.max(10, window.innerHeight-dimensions.height-10) : dimensions.y,
width: dimensions.width>window.innerWidth-20 ? window.innerWidth-20 : dimensions.width,
height: dimensions.height>window.innerHeight-20 ? window.innerHeight-20 : dimensions.height,
onfocus: () => { selectChannel(channel); setInputFocus(); },
onclose: (force) => { removeChannel(channel); windows.channels.delete(channel); send('leave',{'channel':channel, 'reason':'closed the window'}); return false; }
});
windows.channels.set(channel, subwindow);
addChannel(channel);
return subwindow;
} else {
return windows.channels.get(sanitize(channel));
}
};
function createUserWindow(from, to) {
let target = (from.url == myself.url ? to : from);
let key = target.url;
if (!windows.users.has(key)) {
let dimensions = {
x: 10+20*(windows.channels.size+windows.users.size+1),
y: 10+20*(windows.channels.size+windows.users.size+1),
width: window.innerWidth*50/100,
height: window.innerHeight*60/100
};
let subwindow = new WinBox({
id: 'window-user-'+target.url,
title: '@'+sanitize(target.nick)+' ('+sanitize(target.nick)+'@'+target.url+')',
class: ['no-full'],
background: '#666',
header: 23, border:3,
icon: false,
x: dimensions.x+dimensions.width>window.innerWidth-10 ? Math.max(10, window.innerWidth-dimensions.width-10) : dimensions.x,
y: dimensions.y+dimensions.height>window.innerHeight-10 ? Math.max(10, window.innerHeight-dimensions.height-10) : dimensions.y,
width: dimensions.width>window.innerWidth-20 ? window.innerWidth-20 : dimensions.width,
height: dimensions.height>window.innerHeight-20 ? window.innerHeight-20 : dimensions.height,
onfocus: () => { selectUser(target); setInputFocus(); },
onclose: (force) => { windows.users.delete(key); }
});
windows.users.set(key, subwindow);
if (from.url != myself.url) beep();
return subwindow;
} else {
return windows.users.get(key);
}
};
function getChannelWindow(channel) {
return windows.channels.get(sanitize(channel));
};
function getUserWindow(nick, url) {
if (windows.users.has(url))
return windows.users.get(url);
else
return createUserWindow(myself, {'nick': nick, 'url': url});
};
function selectChannel(channel) {
let clnChannel = sanitize(channel)
defaultAction = (command) => send('msg', {'channel':clnChannel, 'content':command});
};
function selectUser(user) {
defaultAction = (command) => send('msg', {'user':user, 'content':command});
};
function setInputFocus() {
document.getElementById("message").focus();
};
function connect(host) {
ws = new WebSocket(`ws://${host}`);
addServer(ws.url);
ws.onmessage = (event) => {
try {
//console.log(event.data);
let message = JSON.parse(event.data);
switch (message[0]) {
case 'version':
break;
case 'ident':
myself = message[1];
wall(general, `you are identified as ${identToString(message[1])}`);
break;
case 'motd':
wall(general, `${messageToString(message[1].content).replace('\n','<br/>')}`);
break;
case 'time':
wall(general, `the time on the server is ${sanitize(message[1].local)}`);
break;
case 'nick':
if (!('old' in message[1]) || !('channel' in message[1])) {
myself = message[1].new;
append(general, `you are now known as ${userToString(message[1].new)}`);
} else if ('channel' in message[1]) {
updateUser(message[1].channel, message[1].new);
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: ${userToString(message[1].old)} changed name to ${userToString(message[1].new)}`);
} else {
append(general, `${userToString(message[1].old)} changed name to ${userToString(message[1].new)}`);
}
break;
case 'list':
if (message[1].channels.length)
wall(general, `channel list:<br> ${message[1].channels.map(([channel,options])=>channelToString(channel, options)).join('<br> ')}`);
else
wall(general, 'channel list is empty');
break;
case 'users':
if (message[1].channel) {
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: user list:<br> ${message[1].users.map(([user,options])=>userToString(user, options)+' ('+identToString(user)+')').join('<br> ')}`);
removeAllUsers(message[1].channel);
for (let [user, options] of message[1].users) addUser(message[1].channel, user, options);
} else if (message[1].pattern) {
wall(general, `users matching ${patternToString(message[1].pattern)}:<br> ${message[1].match.map(([user,options,channels])=>userToString(user, options)+' ('+identToString(user)+') '+channels.map(([channel, options])=>channelToString(channel)+userOptionsToString(options)).join(' ')).join('<br> ')}`);
} else {
wall(general, `user list:<br> ${message[1].users.map(([user,options])=>userToString(user, options)+' ('+identToString(user)+')').join('<br> ')}`);
}
break;
case 'join':
subwindow = createChannelWindow(message[1].channel);
addUser(message[1].channel, message[1].user, {...message[1].user.options, ...message[1].options});
append(subwindow, `${channelToString(message[1].channel)}: ${userToString(message[1].user, message[1].options)} joined the channel`);
break;
case 'topic':
if (message[1].value)
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: topic was set to "<span class="topic">${sanitize(message[1].value)}</span>"`);
else
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: topic was not set`);
break;
case 'msg':
if ('channel' in message[1]) {
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: ${userToString(message[1].from)}: ${messageToString(message[1].content)}`);
} else {
subwindow = createUserWindow(message[1].from, message[1].user);
append(subwindow, `${userToString(message[1].from)}: ${userToString(message[1].user)}: ${messageToString(message[1].content)}`);
}
break;
case 'leave':
if (message[1].user.url == myself.url)
removeAllUsers(message[1].channel);
else
removeUser(message[1].channel, message[1].user);
append(getChannelWindow(message[1].channel), `${channelToString(message[1].channel)}: ${userToString(message[1].user)} left the channel (${sanitize(message[1].reason||'no reason')})`);
break;
case 'mode':
if ('channel' in message[1]) {
if ('user' in message[1]) {
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: user mode for ${userToString(message[1].user)} is ${userOptionsToString(message[1].options)||'(-)'}`);
updateUser(message[1].channel, message[1].user, {...message[1].user.options, ...message[1].options});
} else {
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: channel mode is${channelOptionsToString(message[1].options)}`);
}
} else if ('user' in message[1]) {
wall(general, `user mode for ${userToString(message[1].user)} is ${userOptionsToString(message[1].options)||'(-)'}`);
}
break;
case 'kick':
if (message[1].user.url == myself.url)
removeAllUsers(message[1].channel);
else
removeUser(message[1].channel, message[1].user);
append(getChannelWindow(message[1].channel)||general, `${channelToString(message[1].channel)}: ${userToString(message[1].user)} was kicked out of the channel (${sanitize(message[1].reason||'no reason')})`);
break;
case 'wall':
wall(general, `${userToString(message[1].from)}: ${messageToString(message[1].content)}`);
beep();
break;
case 'error':
error(general, `${sanitize(message[1].message)}`);
break;
}
} catch (exception) {
error(general, 'internal error while processing server message:<br>'+exception);
}
};
};
function help() {
append(general, '<span class="info">'
+ '<p>This is ws-chat (tentative name), an IRC like chat running in your browser.</p>'
+ '<p>command help:<table>'
+ '<tr><td style="vertical-align:top">/help</td><td>show this help message</td></tr>'
+ '<tr><td style="vertical-align:top">/connect <host:port></td><td>connect to a chat server</td></tr>'
+ '<tr><td style="vertical-align:top">/ident</td><td>ask for your own identification string</td></tr>'
+ '<tr><td style="vertical-align:top">/motd</td><td>display the server message of the day</td></tr>'
+ '<tr><td style="vertical-align:top">/motd <message></td><td>set the server message of the day (requires: admin)</td></tr>'
+ '<tr><td style="vertical-align:top">/time</td><td>show the server local time</td></tr>'
+ '<tr><td style="vertical-align:top">/nick <nickname></td><td>set the nickname you will be yelled at</td></tr>'
+ '<tr><td style="vertical-align:top">/list</td><td>list all channels on this server</td></tr>'
+ '<tr><td style="vertical-align:top">/users <pattern></td><td>list all visible users matching the specified pattern.</td></tr>'
+ '<tr><td style="vertical-align:top">/join <#channel> [<password>]</td><td>join a channel and participate ! if the channel does not exist, it is automatically created.</td></tr>'
+ '<tr><td style="vertical-align:top">/topic <#channel></td><td>show the current topic of the specified channel</td></tr>'
+ '<tr><td style="vertical-align:top">/topic <#channel> <topic...></td><td>set the topic of the specified channel</td></tr>'
+ '<tr><td style="vertical-align:top">/msg <#channel> <message...></td><td>send a message to the specified channel</td></tr>'
+ '<tr><td style="vertical-align:top">/msg <@user> <message...></td><td>send a private message to the specified user</td></tr>'
+ '<tr><td style="vertical-align:top">/leave <#channel> [<reason>]</td><td>leave the channel. the channel is automatically destroyed when the last particiapnt leaves</td></tr>'
+ '<tr><td style="vertical-align:top">/mode <#channel></td><td>query modes for this channel</td></tr>'
+ '<tr><td style="vertical-align:top">/mode <#channel> [+|- <channel modes>] [<password>]</td><td>set modes for this channel</td></tr>'
+ '<tr><td style="vertical-align:top">/mode <#channel> <@users> [+|- <channel user modes>]</td><td>set modes for all matching users on this channel</td></tr>'
+ '<tr><td style="vertical-align:top">/mode <@users> [+|- <user modes>]</td><td>set global modes for those matching users</td></tr>'
+ '<tr><td style="vertical-align:top">/kick <#channel> <@users> [<reason>]</td><td>kick some people out of a channel (requires: operator, admin)</td></tr>'
+ '<tr><td style="vertical-align:top">/wall <@users> <content></td><td>send a wall notice to all matching users (requires: admin)</td></tr>'
+ '<tr><td style="vertical-align:top">/select <#channel></td><td>makes the specified channel the active channel. everything you type in the input box will be sent to this channel</td></tr></table>'
+ '</p>'
+ '<p>Differentiate users by using an extended name syntax: <br/> @nick<#channel><@host:port>. example: @test @test#channel @[email protected]:25642 @test#[email protected]:25642.<br/>'
+ 'some commands (users, mode, kick, wall) accept a wildcard pattern: the command is applied to all users matching the pattern. Wildcards are * and ?, escape a wildcard by preceding it with \\.'
+ '<p>Channel modes are:<table>'
+ '<tr><td style="vertical-align:top">s :</td><td><i>secret</i>. the channel does not appear when listing channels with the /list command (except for server admnistrators)</td></tr>'
+ '<tr><td style="vertical-align:top">m :</td><td><i>moderated</i>. only users with the +v mode enabled can talk to the channel</td></tr>'
+ '<tr><td style="vertical-align:top">t :</td><td><i>limited topic</i>. only channel operators can change the topic of the channel</td></tr>'
+ '<tr><td style="vertical-align:top">k :</td><td><i>password (key) protected</i>. the channel is only accessible to people knowing the password</td></tr>'
+ '</table><br/>'
+ '<p>Channel specific user modes are:<table>'
+ '<tr><td style="vertical-align:top">o :</td><td><i>operator</i>. operators have super powers onto a channel</td></tr>'
+ '<tr><td style="vertical-align:top">v :</td><td><i>voiced</i>. voiced people are allowed to talk on moderated channels</td></tr>'
+ '</table><br/>'
+ '<p>User modes are:<table>'
+ '<tr><td style="vertical-align:top">a :</td><td><i>admin</i>. server administrator, better not mess with them</td></tr>'
+ '<tr><td style="vertical-align:top">b :</td><td><i>bot</i>. complacency flag to signal a user is a bot</td></tr>'
+ '</table></p>'
+ '<p>You can use <a href="https://commonmark.org/help/" target="_blank">Markdown</a> to format messages</p>'
+ '</span>')
}
function command() {
let split = (input, limit=-1) => {
separator = /\s+/g
const output = [];
let finalIndex = 0;
while (limit--) {
const lastIndex = separator.lastIndex;
const search = separator.exec(input);
if (search === null) {
break;
}
finalIndex = separator.lastIndex;
output.push(input.slice(lastIndex, search.index));
}
output.push(input.slice(finalIndex));
return output;
}
let input = document.getElementById('message');
let command = input.value.trimEnd();
if (command == '/help') {
help();
} else if (command.startsWith('/connect ')) {
args = split(command, 1);
if (args.length > 2) error(general, '/connect: too many arguments');
else connect(args[1]);
} else if (command == '/ident') {
send('ident');
} else if (command == '/motd') {
send('motd');
} else if (command.startsWith('/motd ')) {
args = split(command, 1);
send('motd',{'value': args[1]});
} else if (command == '/time') {
send('time');
} else if (command.startsWith('/nick ')) {
args = split(command, 1);
if (args.length > 2) error(general, '/nick: too many arguments');
else send('nick',{'new':args[1]});
} else if (command == '/list') {
send('list');
} else if (command == '/users') {
send('users');
} else if (command.startsWith('/users ')) {
args = split(command, 1);
if (args.length > 2) error(general, '/users: too many arguments');
else {
send('users',{'pattern':stringToUser(args[1])})
};
} else if (command.startsWith('/join ')) {
args = split(command, 2);
if (args.length > 3) error(general, '/join: too many arguments');
else {
if (!args[1].startsWith('#')) error(general, '/join: channel expected');
else send('join',{'channel':args[1].slice(1), 'password':args.length>2?args[2]:undefined})
};
} else if (command.startsWith('/topic ')) {
args = split(command, 2);
if (!args[1].startsWith('#')) error(general, '/topic: channel expected');
else send('topic',{'channel':args[1].slice(1), 'value': args.length>2?args[2]:undefined})
} else if (command.startsWith('/msg ')) {
args = split(command, 2);
if (args.length<3) error(general, '/msg: missing the message...');
else {
if (args[1].startsWith('#')) send('msg', {'channel':args[1].slice(1), 'content':args[2]});
else if (args[1].startsWith('@')) {
let user = stringToUser(args[1].slice(1));
if (!user) error(general, `/msg: malformed user specifier ${args[1].slice(1)}`);
else send('msg', {'user':user, 'content':args[2]});
} else error(general, '/msg: channel or user expected');
}
} else if (command.startsWith('/leave ')) {
args = split(command, 2);
if (!args[1].startsWith('#')) error(general, '/leave: channel expected');
else send('leave',{'channel':args[1].slice(1), 'reason': args.length>2?args[2]:undefined})
} else if (command.startsWith('/select ')) {
args = split(command, 1);
if (args.length > 2) error(general, '/select: too many arguments');
else {
if (!args[1].startsWith('#')) error(general, '/select: channel expected');
else {
let channel = sanitize(args[1].slice(1));
selectChannel(channel);
let subwindow = getChannelWindow(channel); if (subwindow) subwindow.focus();
info(general, `${channelToString(channel)} is now the active channel`);
}
};
} else if (command.startsWith('/mode ')) {
args = split(command);
if (args[1].startsWith('#')) {
if ((args.length > 2) && args[2].startsWith('@')) {
let options = {};
for (let arg of args.slice(3)) {
let value = true;
for (let chr of arg) {
if (chr == '+') value = true;
else if (chr == '-') value = false;
else if (chr == 'o') options.operator = value;
else if (chr == 'v') options.voiced = value;
else error(general, '/mode: unrecognized mode character');
}
}
let user = stringToUser(args[2].slice(1));
if (!user) error(`/mode: malformed user specifier ${args[2].slice(1)}`);
else send('mode', {'channel':args[1].slice(1), 'user':user, 'options': args.length>3 ? options : undefined});
} else {
let options = {};
let password = false;
for (let arg of args.slice(2)) {
if (password) {
options.password = arg;
password = false;
} else {
let value = true;
for (let chr of arg) {
if (chr == '+') value = true;
else if (chr == '-') value = false;
else if (chr == 'm') options.moderated = value;
else if (chr == 's') options.secret = value;
else if (chr == 't') options.topic = value;
else if (chr == 'k') { password = true; }
else error(general, '/mode: unrecognized mode character');
}
}
}
send('mode', {'channel':args[1].slice(1), 'options': args.length>2 ? options : undefined});
}
} else if (args[1].startsWith('@')) {
let options = {};
for (let arg of args.slice(2)) {
let value = true;
for (let chr of arg) {
if (chr == '+') value = true;
else if (chr == '-') value = false;
else if (chr == 'a') options.admin = value;
else if (chr == 'b') options.bot = value;
else error(general, '/mode: unrecognized mode character');
}
}
let user = stringToUser(args[1].slice(1));
if (!user) error(`/mode: malformed user specifier ${args[1].slice(1)}`);
else send('mode', {'user': user, 'options': args.length>2 ? options : undefined});
} else {
error(general, '/mode: no channel or user specified');
}
} else if (command.startsWith('/kick ')) {
args = split(command, 3);
if (!args[1].startsWith('#')) error(general, '/kick: channel expected before user');
else if (!args[2].startsWith('@')) error(general, '/kick: user expected after channel');
else send('kick',{'channel':args[1].slice(1),'user':stringToUser(args[2].slice(1)),'reason': args.length>3?args[3]:undefined});
} else if (command.startsWith('/wall ')) {
args = split(command, 2);
if (args.length<3) error(general, '/wall: missing the message...');
else if (!args[1].startsWith('@')) error(general, '/wall: user expected after channel');
else send('wall',{'user':stringToUser(args[1].slice(1)),'content':args[2]});
} else {
defaultAction(command);
}
input.value = '';
};
window.onbeforeunload = () => {
let workspace = {
'input': {'x': input.x, 'y': input.y, 'width': input.width, 'height': input.height},
'selector': {'x': selector.x, 'y': selector.y, 'width': selector.width, 'height': selector.height},
'general': {'x': general.x, 'y': general.y, 'width': general.width, 'height': general.height},
};
window.localStorage.setItem('workspace-settings', JSON.stringify(workspace));
}
help();
</script>
</body>
</html>