Skip to content

Commit 47c5aa5

Browse files
committed
Merge branch 'master' into cap-notify
2 parents 73e4e27 + a96c42a commit 47c5aa5

File tree

11 files changed

+377
-301
lines changed

11 files changed

+377
-301
lines changed

src/client.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ module.exports = class IrcClient extends EventEmitter {
197197
var client = this;
198198

199199
this.command_handler.on('all', function(event_name, event_arg) {
200-
client.resetPingTimer();
200+
client.resetPingTimeoutTimer();
201201

202202
// Add a reply() function to selected message events
203203
if (['privmsg', 'notice', 'action'].indexOf(event_name) > -1) {
@@ -294,33 +294,24 @@ module.exports = class IrcClient extends EventEmitter {
294294

295295

296296
startPeriodicPing() {
297-
var that = this;
298-
var ping_timer = null;
299-
var timeout_timer = null;
297+
let that = this;
298+
let ping_timer = null;
299+
let timeout_timer = null;
300300

301301
if(that.options.ping_interval <= 0 || that.options.ping_timeout <= 0) {
302302
return;
303303
}
304304

305-
function scheduleNextPing() {
305+
// Constantly ping the server for lag and time syncing functions
306+
function pingServer() {
307+
that.ping();
306308
ping_timer = that.connection.setTimeout(pingServer, that.options.ping_interval*1000);
307309
}
308310

309-
function resetPingTimer() {
310-
if(ping_timer) {
311-
that.connection.clearTimeout(ping_timer);
312-
}
313-
314-
if(timeout_timer) {
315-
that.connection.clearTimeout(timeout_timer);
316-
}
317-
318-
scheduleNextPing();
319-
}
320-
321-
function pingServer() {
311+
// Data from the server was detected so restart the timeout
312+
function resetPingTimeoutTimer() {
313+
that.connection.clearTimeout(timeout_timer);
322314
timeout_timer = that.connection.setTimeout(pingTimeout, that.options.ping_timeout*1000);
323-
that.ping();
324315
}
325316

326317
function pingTimeout() {
@@ -329,13 +320,13 @@ module.exports = class IrcClient extends EventEmitter {
329320
that.connection.end(end_msg, true);
330321
}
331322

332-
this.resetPingTimer = resetPingTimer;
333-
scheduleNextPing();
323+
this.resetPingTimeoutTimer = resetPingTimeoutTimer;
324+
ping_timer = that.connection.setTimeout(pingServer, that.options.ping_interval*1000);
334325
}
335326

336327

337328
// Gets overridden with a function in startPeriodicPing(). Only set here for completeness.
338-
resetPingTimer() {}
329+
resetPingTimeoutTimer() {}
339330

340331

341332

@@ -379,7 +370,7 @@ module.exports = class IrcClient extends EventEmitter {
379370

380371

381372
ping(message) {
382-
this.raw('PING', message || '*');
373+
this.raw('PING', message || 'kiwitime-' + Date.now());
383374
}
384375

385376

@@ -440,7 +431,12 @@ module.exports = class IrcClient extends EventEmitter {
440431
var raw = ['MODE', channel, mode];
441432

442433
if (extra_args) {
443-
raw.push(extra_args);
434+
if (Array.isArray(extra_args)) {
435+
raw = raw.concat(extra_args);
436+
}
437+
else {
438+
raw.push(extra_args);
439+
}
444440
}
445441

446442
this.raw(raw);
@@ -747,7 +743,6 @@ module.exports = class IrcClient extends EventEmitter {
747743
};
748744
}
749745

750-
751746
matchNotice(match_regex, cb) {
752747
return this.match(match_regex, cb, 'notice');
753748
}

src/commands/handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = class IrcCommandHandler extends EventEmitter {
6060
}
6161

6262
if (this.handlers[command_name]) {
63-
this.handlers[command_name].call(this, irc_command);
63+
this.handlers[command_name](irc_command, this);
6464
} else {
6565
this.emitUnknownCommand(irc_command);
6666
}

src/commands/handlers/channel.js

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ var _ = {
66
var Helpers = require('../../helpers');
77

88
var handlers = {
9-
RPL_CHANNELMODEIS: function(command) {
9+
RPL_CHANNELMODEIS: function(command, handler) {
1010
var channel = command.params[1];
1111
var raw_modes = command.params[2];
1212
var raw_params = command.params.slice(3);
13-
var modes = this.parseModeList.call(this, raw_modes, raw_params);
13+
var modes = handler.parseModeList(raw_modes, raw_params);
1414

15-
this.emit('channel info', {
15+
handler.emit('channel info', {
1616
channel: channel,
1717
modes: modes,
1818
raw_modes: raw_modes,
@@ -21,30 +21,29 @@ var handlers = {
2121
},
2222

2323

24-
RPL_CREATIONTIME: function(command) {
24+
RPL_CREATIONTIME: function(command, handler) {
2525
var channel = command.params[1];
2626

27-
this.emit('channel info', {
27+
handler.emit('channel info', {
2828
channel: channel,
2929
created_at: parseInt(command.params[2], 10)
3030
});
3131
},
3232

3333

34-
RPL_CHANNEL_URL: function(command) {
34+
RPL_CHANNEL_URL: function(command, handler) {
3535
var channel = command.params[1];
3636

37-
this.emit('channel info', {
37+
handler.emit('channel info', {
3838
channel: channel,
3939
url: command.params[command.params.length - 1]
4040
});
4141
},
4242

4343

44-
RPL_NAMEREPLY: function(command) {
45-
var that = this;
44+
RPL_NAMEREPLY: function(command, handler) {
4645
var members = command.params[command.params.length - 1].split(' ');
47-
var cache = this.cache('names.' + command.params[2]);
46+
var cache = handler.cache('names.' + command.params[2]);
4847

4948
if (!cache.members) {
5049
cache.members = [];
@@ -59,10 +58,10 @@ var handlers = {
5958
var user = null;
6059

6160
// If we have prefixes, strip them from the nick and keep them separate
62-
if (that.network.options.PREFIX) {
63-
for (j = 0; j < that.network.options.PREFIX.length; j++) {
64-
if (member[0] === that.network.options.PREFIX[j].symbol) {
65-
modes.push(that.network.options.PREFIX[j].mode);
61+
if (handler.network.options.PREFIX) {
62+
for (j = 0; j < handler.network.options.PREFIX.length; j++) {
63+
if (member[0] === handler.network.options.PREFIX[j].symbol) {
64+
modes.push(handler.network.options.PREFIX[j].mode);
6665
member = member.substring(1);
6766
}
6867
}
@@ -81,18 +80,18 @@ var handlers = {
8180
},
8281

8382

84-
RPL_ENDOFNAMES: function(command) {
85-
var cache = this.cache('names.' + command.params[1]);
86-
this.emit('userlist', {
83+
RPL_ENDOFNAMES: function(command, handler) {
84+
var cache = handler.cache('names.' + command.params[1]);
85+
handler.emit('userlist', {
8786
channel: command.params[1],
8887
users: cache.members || []
8988
});
9089
cache.destroy();
9190
},
9291

9392

94-
RPL_INVITELIST: function(command) {
95-
var cache = this.cache('inviteList.' + command.params[1]);
93+
RPL_INVITELIST: function(command, handler) {
94+
var cache = handler.cache('inviteList.' + command.params[1]);
9695
if (!cache.invites) {
9796
cache.invites = [];
9897
}
@@ -106,9 +105,9 @@ var handlers = {
106105
},
107106

108107

109-
RPL_ENDOFINVITELIST: function(command) {
110-
var cache = this.cache('inviteList.' + command.params[1]);
111-
this.emit('inviteList', {
108+
RPL_ENDOFINVITELIST: function(command, handler) {
109+
var cache = handler.cache('inviteList.' + command.params[1]);
110+
handler.emit('inviteList', {
112111
channel: command.params[1],
113112
invites: cache.invites || []
114113
});
@@ -117,8 +116,8 @@ var handlers = {
117116
},
118117

119118

120-
RPL_BANLIST: function(command) {
121-
var cache = this.cache('banlist.' + command.params[1]);
119+
RPL_BANLIST: function(command, handler) {
120+
var cache = handler.cache('banlist.' + command.params[1]);
122121
if (!cache.bans) {
123122
cache.bans = [];
124123
}
@@ -132,9 +131,9 @@ var handlers = {
132131
},
133132

134133

135-
RPL_ENDOFBANLIST: function(command) {
136-
var cache = this.cache('banlist.' + command.params[1]);
137-
this.emit('banlist', {
134+
RPL_ENDOFBANLIST: function(command, handler) {
135+
var cache = handler.cache('banlist.' + command.params[1]);
136+
handler.emit('banlist', {
138137
channel: command.params[1],
139138
bans: cache.bans || []
140139
});
@@ -143,25 +142,25 @@ var handlers = {
143142
},
144143

145144

146-
RPL_TOPIC: function(command) {
147-
this.emit('topic', {
145+
RPL_TOPIC: function(command, handler) {
146+
handler.emit('topic', {
148147
channel: command.params[1],
149148
topic: command.params[command.params.length - 1]
150149
});
151150
},
152151

153152

154-
RPL_NOTOPIC: function(command) {
155-
this.emit('topic', {
153+
RPL_NOTOPIC: function(command, handler) {
154+
handler.emit('topic', {
156155
channel: command.params[1],
157156
topic: ''
158157
});
159158
},
160159

161160

162-
RPL_TOPICWHOTIME: function(command) {
161+
RPL_TOPICWHOTIME: function(command, handler) {
163162
var parsed = Helpers.parseMask(command.params[2]);
164-
this.emit('topicsetby', {
163+
handler.emit('topicsetby', {
165164
nick: parsed.nick,
166165
ident: parsed.user,
167166
hostname: parsed.host,
@@ -171,7 +170,7 @@ var handlers = {
171170
},
172171

173172

174-
JOIN: function(command) {
173+
JOIN: function(command, handler) {
175174
var channel;
176175
var gecos_idx = 1;
177176
var data = {};
@@ -180,7 +179,7 @@ var handlers = {
180179
channel = command.params[0];
181180
}
182181

183-
if (this.network.cap.isEnabled('extended-join')) {
182+
if (handler.network.cap.isEnabled('extended-join')) {
184183
data.account = command.params[1] === '*' ? false : command.params[1];
185184
gecos_idx = 2;
186185
}
@@ -192,14 +191,14 @@ var handlers = {
192191
data.channel = channel;
193192
data.time = command.getServerTime();
194193

195-
this.emit('join', data);
194+
handler.emit('join', data);
196195
},
197196

198197

199-
PART: function(command) {
198+
PART: function(command, handler) {
200199
var time = command.getServerTime();
201200

202-
this.emit('part', {
201+
handler.emit('part', {
203202
nick: command.nick,
204203
ident: command.ident,
205204
hostname: command.hostname,
@@ -210,10 +209,10 @@ var handlers = {
210209
},
211210

212211

213-
KICK: function(command) {
212+
KICK: function(command, handler) {
214213
var time = command.getServerTime();
215214

216-
this.emit('kick', {
215+
handler.emit('kick', {
217216
kicked: command.params[1],
218217
nick: command.nick,
219218
ident: command.ident,
@@ -225,10 +224,10 @@ var handlers = {
225224
},
226225

227226

228-
QUIT: function(command) {
227+
QUIT: function(command, handler) {
229228
var time = command.getServerTime();
230229

231-
this.emit('quit', {
230+
handler.emit('quit', {
232231
nick: command.nick,
233232
ident: command.ident,
234233
hostname: command.hostname,
@@ -238,7 +237,7 @@ var handlers = {
238237
},
239238

240239

241-
TOPIC: function(command) {
240+
TOPIC: function(command, handler) {
242241
// If we don't have an associated channel, no need to continue
243242
if (!command.params[0]) {
244243
return;
@@ -250,7 +249,7 @@ var handlers = {
250249
var channel = command.params[0];
251250
var topic = command.params[command.params.length - 1] || '';
252251

253-
this.emit('topic', {
252+
handler.emit('topic', {
254253
nick: command.nick,
255254
channel: channel,
256255
topic: topic,
@@ -259,10 +258,10 @@ var handlers = {
259258
},
260259

261260

262-
INVITE: function(command) {
261+
INVITE: function(command, handler) {
263262
var time = command.getServerTime();
264263

265-
this.emit('invite', {
264+
handler.emit('invite', {
266265
nick: command.nick,
267266
ident: command.ident,
268267
hostname: command.hostname,
@@ -273,8 +272,8 @@ var handlers = {
273272
},
274273

275274

276-
RPL_INVITING: function(command) {
277-
this.emit('invited', {
275+
RPL_INVITING: function(command, handler) {
276+
handler.emit('invited', {
278277
nick: command.params[0],
279278
channel: command.params[1]
280279
});

0 commit comments

Comments
 (0)