Skip to content

Commit b2df6af

Browse files
committed
Command handler passed as an arg; Tests passing;
1 parent 02c5880 commit b2df6af

File tree

10 files changed

+278
-278
lines changed

10 files changed

+278
-278
lines changed

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
});

src/commands/handlers/generics.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module.exports = function AddCommandHandlers(command_controller) {
114114
generic_keys.forEach(function(generic_command) {
115115
var generic = generics[generic_command];
116116

117-
command_controller.addHandler(generic_command, function(command) {
117+
command_controller.addHandler(generic_command, function(command, handler) {
118118
var event_obj = {};
119119
var event_keys = Object.keys(generic);
120120
var val;
@@ -137,14 +137,14 @@ module.exports = function AddCommandHandlers(command_controller) {
137137
if (event_obj.channel) {
138138
// Extract the group from any errors targetted towards channels with a statusmsg prefix
139139
// Eg. @#channel
140-
var parsed = this.network.extractTargetGroup(event_obj.channel);
140+
var parsed = handler.network.extractTargetGroup(event_obj.channel);
141141
if (parsed) {
142142
event_obj.channel = parsed.target;
143143
event_obj.target_group = parsed.target_group;
144144
}
145145
}
146146

147-
this.emit(generic.event, event_obj);
147+
handler.emit(generic.event, event_obj);
148148
});
149149
});
150150
};

0 commit comments

Comments
 (0)