-
Notifications
You must be signed in to change notification settings - Fork 14
/
mode.js
87 lines (80 loc) · 2.25 KB
/
mode.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
"use strict";
const ModeArgumentPresence = {
NONE: 0,
PLUS_ONLY: 1,
BOTH: 2
};
function parseChannelModes(modes, args) {
const modeToPresence = new Map();
modeToPresence.set('a', ModeArgumentPresence.BOTH);
modeToPresence.set('b', ModeArgumentPresence.BOTH);
modeToPresence.set('e', ModeArgumentPresence.BOTH);
modeToPresence.set('f', ModeArgumentPresence.BOTH);
modeToPresence.set('h', ModeArgumentPresence.BOTH);
modeToPresence.set('I', ModeArgumentPresence.BOTH);
modeToPresence.set('j', ModeArgumentPresence.BOTH);
modeToPresence.set('k', ModeArgumentPresence.BOTH);
modeToPresence.set('l', ModeArgumentPresence.PLUS_ONLY);
modeToPresence.set('L', ModeArgumentPresence.BOTH);
modeToPresence.set('o', ModeArgumentPresence.BOTH);
modeToPresence.set('q', ModeArgumentPresence.BOTH);
modeToPresence.set('v', ModeArgumentPresence.BOTH);
return parseModes(modes, args, modeToPresence);
}
function parseUserModes(modes, args) {
return parseModes(modes, args, new Map());
}
function parseModes(modes, args, modeToPresence) {
let plus = null;
const parsedModes = [];
const argsIter = args[Symbol.iterator]();
for (let c of modes) {
if (c === '+') {
plus = true;
} else if (c === '-') {
plus = false;
} else {
// it's a mode
if (plus === null) {
// if we got a mode before a +/-, invalid input
return null;
}
const modeType = modeToPresence.has(c) ? modeToPresence.get(c) : ModeArgumentPresence.NONE;
// if this mode has an arg, grab it
let arg = null;
if (modeType === ModeArgumentPresence.BOTH || (modeType === ModeArgumentPresence.PLUS_ONLY && plus)) {
const nextArg = argsIter.next();
if (!nextArg.done) {
arg = nextArg.value;
} else {
return null; // not enough args
}
}
parsedModes.push({
mode: c,
plus: plus,
arg: arg
});
}
}
return parsedModes;
}
function getUserlistEntryAttributeByMode(mode) {
switch (mode) {
case 'q':
return 'owner';
case 'a':
return 'admin';
case 'o':
return 'op';
case 'h':
return 'halfop';
case 'v':
return 'voice';
default:
return null;
}
}
module.exports.parseChannelModes = parseChannelModes;
module.exports.parseUserModes = parseUserModes;
module.exports.getUserlistEntryAttributeByMode = getUserlistEntryAttributeByMode;