-
Notifications
You must be signed in to change notification settings - Fork 3
/
irc-caps.js
99 lines (85 loc) · 3.22 KB
/
irc-caps.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
/**
* @class
*/
class Caps {
/**
* @func
* @param {Bot} bot
*/
constructor(bot) {
this.bot = bot;
this.caps = this.bot.config.caps || [];
this.availablecaps = [];
this.stringcaps = [];
this.args = {};
// Iterate over list provided of caps and check if it is a string or a function
for (const cap of this.caps) {
this.stringcaps.push(cap.name || cap);
}
}
/**
* @func
* @param {Parser} event
*/
handler(event) {
// Main handling code for CAP
const servcaps = event.arguments[1] !== '*' ? event.arguments[1].split(' ') : event.arguments[2].split(' ');
const capsfunctions = this.caps.filter(cap => typeof cap !== 'string' && this.availablecaps.includes(cap.name));
if (event.arguments[0] === 'LS') {
// Don't blindly assume server supports our requested caps, even though server sends a CAP NACK response
for (const c of servcaps) {
const [cap, args] = c.trim().split('=');
if (this.stringcaps.includes(cap)) {
this.availablecaps.push(cap);
if (typeof args !== 'undefined') {
this.args[cap] = args.split(',');
} else {
this.args[cap] = null;
}
}
}
if (event.arguments[1] !== '*') {
if (!this.availablecaps.length) {
this.bot.send('CAP END');
} else {
this.bot.send(`CAP REQ :${this.availablecaps.join(' ')}`);
}
}
} else if (event.arguments[0] === 'ACK') {
if (capsfunctions.length) {
for (const cap of capsfunctions) {
if (typeof cap.run === 'function') { // Check if the cap has the `run` property
cap.run(this.bot, this.args[cap.name]); // Run the cap with the arguments collected during CAP LS
} else {
continue;
}
}
} else {
this.bot.send('CAP END');
}
} else if (event.arguments[0] === 'NEW') {
const newcaps = [];
for (const c of this.stringcaps) {
if (servcaps.includes(c)) { // Check if the server supports the CAPs we want
this.availablecaps.push(c); // Add the new CAP to the list of available CAPs
newcaps.push(c);
}
}
if (newcaps.length) {
this.bot.send(`CAP REQ :${newcaps.join(' ')}`); // Request the new CAP
}
} else if (event.arguments[0] === 'DEL') {
for (const c of servcaps) {
if (this.availablecaps.includes(c)) {
this.availablecaps.splice(this.availablecaps.indexOf(c), 1);
}
if (this.stringcaps.includes(c)) {
const index = this.stringcaps.indexOf(c);
this.stringcaps.splice(index, 1);
this.caps.splice(index, 1);
}
}
}
}
}
module.exports = Caps;