-
Notifications
You must be signed in to change notification settings - Fork 2
/
irc-command.js
51 lines (40 loc) · 1.24 KB
/
irc-command.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
window.ircCommandHandlers = {};
function registerIrcCommandHandler(cmdName, handler) {
window.ircCommandHandlers[cmdName] = handler;
}
window.addEventListener('message', function(e) {
/*
e.data looks like this:
{
sender: 'lhs-status',
type: 'highlight',
text: 'some message that was sent by mentioning us'
}
or
{
sender: 'lhs-status',
type: 'message',
text: 'some message that was sent either as a PM or as a message to the main channel (no way to tell which)'
}
*/
if (!(typeof e.data === 'object' && e.data.hasOwnProperty('sender') && e.data.sender === 'lhs-status')) {
// this message wasn't for us
return;
}
switch (e.data.type) {
case 'highlight':
handleMessageLine(e.data.text);
break;
case 'message':
if (e.data.text[0] === '^') {
handleMessageLine(e.data.text.slice(1));
}
break;
}
});
function handleMessageLine(line) {
var commandSegments = line.split(' ');
if (window.ircCommandHandlers.hasOwnProperty(commandSegments[0].toLowerCase())) {
window.ircCommandHandlers[commandSegments[0]](commandSegments.slice(1));
}
}