-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathhandler.js
More file actions
207 lines (175 loc) · 6.14 KB
/
Copy pathhandler.js
File metadata and controls
207 lines (175 loc) · 6.14 KB
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
'use strict';
const _ = {
reduce: require('lodash/reduce'),
find: require('lodash/find'),
uniq: require('lodash/uniq'),
};
const EventEmitter = require('eventemitter3');
const irc_numerics = require('./numerics');
const IrcCommand = require('./command');
module.exports = class IrcCommandHandler extends EventEmitter {
constructor(client) {
super();
// Adds an 'all' event to .emit()
this.addAllEventName();
this.client = client;
this.connection = client.connection;
this.network = client.network;
this.handlers = [];
this.request_extra_caps = [];
this.resetCache();
require('./handlers/registration')(this);
require('./handlers/channel')(this);
require('./handlers/user')(this);
require('./handlers/messaging')(this);
require('./handlers/misc')(this);
require('./handlers/generics')(this);
}
dispatch(message) {
const irc_command = new IrcCommand(message.command.toUpperCase(), message);
// Batched commands will be collected and executed as a transaction
const batch_id = irc_command.getTag('batch');
if (batch_id) {
const cache_key = 'batch.' + batch_id;
if (this.hasCache(cache_key)) {
const cache = this.cache(cache_key);
cache.commands.push(irc_command);
} else {
// If we don't have this batch ID in cache, it either means that the
// server hasn't sent the starting batch command or that the server
// has already sent the end batch command.
}
} else {
// Check for labeled-response on non-batched single-message responses.
// Batched labeled responses are handled in the BATCH end handler.
// ACK is handled in its own handler.
const label = irc_command.getTag('label');
if (label && irc_command.command !== 'BATCH' && irc_command.command !== 'ACK') {
irc_command.label = label;
this.client._resolvePendingLabel(label, {
type: 'single',
command: irc_command,
});
}
this.executeCommand(irc_command);
}
}
executeCommand(irc_command) {
let command_name = irc_command.command;
// Check if we have a numeric->command name- mapping for this command
if (irc_numerics[irc_command.command.toUpperCase()]) {
command_name = irc_numerics[irc_command.command.toUpperCase()];
}
if (this.handlers[command_name]) {
this.handlers[command_name](irc_command, this);
} else {
this.emitUnknownCommand(irc_command);
}
}
requestExtraCaps(cap) {
this.request_extra_caps = _.uniq(this.request_extra_caps.concat(cap));
}
addHandler(command, handler) {
if (typeof handler !== 'function') {
return false;
}
this.handlers[command] = handler;
}
emitUnknownCommand(command) {
this.emit('unknown command', command);
}
// Adds an 'all' event to .emit()
addAllEventName() {
const original_emit = this.emit;
this.emit = function() {
const args = Array.prototype.slice.call(arguments, 0);
original_emit.apply(this, ['all'].concat(args));
original_emit.apply(this, args);
};
}
/**
* Convert a mode string such as '+k pass', or '-i' to a readable
* format.
* [ { mode: '+k', param: 'pass' } ]
* [ { mode: '-i', param: null } ]
*/
parseModeList(mode_string, mode_params) {
const chanmodes = this.network.options.CHANMODES || [];
let prefixes = this.network.options.PREFIX || [];
let always_param = (chanmodes[0] || '').concat((chanmodes[1] || ''));
const modes = [];
let i;
let j;
let add;
if (!mode_string) {
return modes;
}
prefixes = _.reduce(prefixes, function(list, prefix) {
list.push(prefix.mode);
return list;
}, []);
always_param = always_param.split('').concat(prefixes);
const hasParam = function(mode, isAdd) {
const matchMode = function(m) {
return m === mode;
};
if (_.find(always_param, matchMode)) {
return true;
}
if (isAdd && _.find((chanmodes[2] || '').split(''), matchMode)) {
return true;
}
return false;
};
j = 0;
for (i = 0; i < mode_string.length; i++) {
switch (mode_string[i]) {
case '+':
add = true;
break;
case '-':
add = false;
break;
default:
if (hasParam(mode_string[i], add)) {
modes.push({ mode: (add ? '+' : '-') + mode_string[i], param: mode_params[j] });
j++;
} else {
modes.push({ mode: (add ? '+' : '-') + mode_string[i], param: null });
}
}
}
return modes;
}
/**
* Cache object for commands buffering data before emitting them
* eg.
* var cache = this.cache('userlist');
* cache.nicks = [];
* cache.destroy();
*/
cache(id) {
let cache = this._caches[id];
if (!cache) {
const destroyCacheFn = (cacheToDestroy, idInCache) => {
return function() {
delete cacheToDestroy[idInCache];
};
};
// We don't want the destoryCache to be iterable
cache = Object.defineProperty({}, 'destroy', {
enumerable: false,
configurable: false,
value: destroyCacheFn(this._caches, id)
});
this._caches[id] = cache;
}
return cache;
}
hasCache(id) {
return this._caches && Object.prototype.hasOwnProperty.call(this._caches, id);
}
resetCache() {
this._caches = Object.create(null);
}
};