Commit | Line | Data |
---|---|---|
ce13508b D |
1 | (function () { |
2 | ||
3 | _kiwi.model.Network = Backbone.Model.extend({ | |
4 | defaults: { | |
5 | connection_id: 0, | |
6 | /** | |
7 | * The name of the network | |
8 | * @type String | |
9 | */ | |
10 | name: 'Network', | |
11 | ||
12 | /** | |
13 | * The address (URL) of the network | |
14 | * @type String | |
15 | */ | |
16 | address: '', | |
17 | ||
d6eec6ed D |
18 | /** |
19 | * The port for the network | |
20 | * @type Int | |
21 | */ | |
22 | port: 6667, | |
23 | ||
24 | /** | |
25 | * If this network uses SSL | |
26 | * @type Bool | |
27 | */ | |
28 | ssl: false, | |
29 | ||
30 | /** | |
31 | * The password to connect to this network | |
32 | * @type String | |
33 | */ | |
34 | password: '', | |
35 | ||
ce13508b D |
36 | /** |
37 | * The current nickname | |
38 | * @type String | |
39 | */ | |
40 | nick: '', | |
41 | ||
42 | /** | |
43 | * The channel prefix for this network | |
44 | * @type String | |
45 | */ | |
46 | channel_prefix: '#', | |
47 | ||
48 | /** | |
49 | * The user prefixes for channel owner/admin/op/voice etc. on this network | |
50 | * @type Array | |
51 | */ | |
d772b95d D |
52 | user_prefixes: [ |
53 | {symbol: '~', mode: 'q'}, | |
54 | {symbol: '&', mode: 'a'}, | |
55 | {symbol: '@', mode: 'o'}, | |
56 | {symbol: '%', mode: 'h'}, | |
57 | {symbol: '+', mode: 'v'} | |
58 | ], | |
425efe7a JA |
59 | |
60 | /** | |
61 | * List of nicks we are ignoring | |
62 | * @type Array | |
63 | */ | |
64 | ignore_list: [] | |
ce13508b D |
65 | }, |
66 | ||
67 | ||
68 | initialize: function () { | |
d6eec6ed D |
69 | // If we already have a connection, bind our events |
70 | if (typeof this.get('connection_id') !== 'undefined') { | |
71 | this.gateway = _kiwi.global.components.Network(this.get('connection_id')); | |
72 | this.bindGatewayEvents(); | |
73 | } | |
ce13508b | 74 | |
c62a9570 | 75 | // Create our panel list (tabs) |
0e546dd4 | 76 | this.panels = new _kiwi.model.PanelList([], this); |
c62a9570 | 77 | //this.panels.network = this; |
0e546dd4 D |
78 | |
79 | // Automatically create a server tab | |
80 | var server_panel = new _kiwi.model.Server({name: 'Server'}); | |
81 | this.panels.add(server_panel); | |
c966123a | 82 | this.panels.server = this.panels.active = server_panel; |
ce13508b D |
83 | }, |
84 | ||
85 | ||
d6eec6ed D |
86 | reconnect: function(callback_fn) { |
87 | var that = this, | |
88 | server_info = { | |
89 | nick: this.get('nick'), | |
90 | host: this.get('address'), | |
91 | port: this.get('port'), | |
92 | ssl: this.get('ssl'), | |
93 | password: this.get('password') | |
94 | }; | |
95 | ||
96 | _kiwi.gateway.makeIrcConnection(server_info, function(err, connection_id) { | |
97 | if (!err) { | |
98 | that.gateway.dispose(); | |
99 | ||
100 | that.set('connection_id', connection_id); | |
101 | that.gateway = _kiwi.global.components.Network(that.get('connection_id')); | |
102 | that.bindGatewayEvents(); | |
103 | ||
104 | callback_fn && callback_fn(err); | |
105 | ||
106 | } else { | |
107 | console.log("_kiwi.gateway.socket.on('error')", {reason: err}); | |
108 | callback_fn && callback_fn(err); | |
109 | } | |
110 | }); | |
111 | }, | |
112 | ||
113 | ||
ce13508b | 114 | bindGatewayEvents: function () { |
0e546dd4 | 115 | //this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);}); |
ce13508b | 116 | |
4047ee2b | 117 | this.gateway.on('connect', onConnect, this); |
a36b7eb6 | 118 | this.gateway.on('disconnect', onDisconnect, this); |
ce13508b D |
119 | |
120 | this.gateway.on('nick', function(event) { | |
2ffd1291 | 121 | if (event.nick === this.get('nick')) { |
ce13508b D |
122 | this.set('nick', event.newnick); |
123 | } | |
124 | }, this); | |
125 | ||
126 | this.gateway.on('options', onOptions, this); | |
127 | this.gateway.on('motd', onMotd, this); | |
128 | this.gateway.on('join', onJoin, this); | |
129 | this.gateway.on('part', onPart, this); | |
130 | this.gateway.on('quit', onQuit, this); | |
131 | this.gateway.on('kick', onKick, this); | |
132 | this.gateway.on('msg', onMsg, this); | |
e2c54b3e | 133 | this.gateway.on('nick', onNick, this); |
ce13508b D |
134 | this.gateway.on('ctcp_request', onCtcpRequest, this); |
135 | this.gateway.on('ctcp_response', onCtcpResponse, this); | |
136 | this.gateway.on('notice', onNotice, this); | |
137 | this.gateway.on('action', onAction, this); | |
138 | this.gateway.on('topic', onTopic, this); | |
139 | this.gateway.on('topicsetby', onTopicSetBy, this); | |
140 | this.gateway.on('userlist', onUserlist, this); | |
141 | this.gateway.on('userlist_end', onUserlistEnd, this); | |
72db27e4 | 142 | this.gateway.on('banlist', onBanlist, this); |
ce13508b D |
143 | this.gateway.on('mode', onMode, this); |
144 | this.gateway.on('whois', onWhois, this); | |
5989f3f4 | 145 | this.gateway.on('whowas', onWhowas, this); |
ce13508b D |
146 | this.gateway.on('away', onAway, this); |
147 | this.gateway.on('list_start', onListStart, this); | |
f8240e98 | 148 | this.gateway.on('irc_error', onIrcError, this); |
3d753975 | 149 | this.gateway.on('unknown_command', onUnknownCommand, this); |
72db27e4 | 150 | this.gateway.on('channel_info', onChannelInfo, this); |
18818abd D |
151 | }, |
152 | ||
153 | ||
154 | /** | |
155 | * Create panels and join the channel | |
156 | * This will not wait for the join event to create a panel. This | |
157 | * increases responsiveness in case of network lag | |
158 | */ | |
159 | createAndJoinChannels: function (channels) { | |
160 | var that = this, | |
161 | panels = []; | |
162 | ||
d6eec6ed | 163 | // Multiple channels may come as comma-delimited |
18818abd D |
164 | if (typeof channels === 'string') { |
165 | channels = channels.split(','); | |
166 | } | |
167 | ||
168 | $.each(channels, function (index, channel_name_key) { | |
169 | // We may have a channel key so split it off | |
170 | var spli = channel_name_key.trim().split(' '), | |
171 | channel_name = spli[0], | |
172 | channel_key = spli[1] || ''; | |
173 | ||
174 | // Trim any whitespace off the name | |
175 | channel_name = channel_name.trim(); | |
176 | ||
b8bba51e | 177 | // Add channel_prefix in front of the first channel if missing |
0db58ad7 D |
178 | if (that.get('channel_prefix').indexOf(channel_name[0]) === -1) { |
179 | // Could be many prefixes but '#' is highly likely the required one | |
180 | channel_name = '#' + channel_name; | |
18818abd D |
181 | } |
182 | ||
183 | // Check if we have the panel already. If not, create it | |
184 | channel = that.panels.getByName(channel_name); | |
185 | if (!channel) { | |
72db27e4 | 186 | channel = new _kiwi.model.Channel({name: channel_name, network: that}); |
18818abd D |
187 | that.panels.add(channel); |
188 | } | |
189 | ||
190 | panels.push(channel); | |
191 | ||
192 | that.gateway.join(channel_name, channel_key); | |
193 | }); | |
194 | ||
b62f086f | 195 | |
18818abd | 196 | return panels; |
24d27c8c D |
197 | }, |
198 | ||
199 | ||
200 | /** | |
201 | * Join all the open channels we have open | |
202 | * Reconnecting to a network would typically call this. | |
203 | */ | |
204 | rejoinAllChannels: function() { | |
205 | var that = this; | |
206 | ||
207 | this.panels.forEach(function(panel) { | |
208 | if (!panel.isChannel()) | |
209 | return; | |
210 | ||
211 | that.gateway.join(panel.get('name')); | |
212 | }); | |
425efe7a JA |
213 | }, |
214 | ||
215 | isChannelName: function (channel_name) { | |
216 | var channel_prefix = this.get('channel_prefix'); | |
217 | ||
218 | if (!channel_name || !channel_name.length) return false; | |
219 | return (channel_prefix.indexOf(channel_name[0]) > -1); | |
220 | }, | |
221 | ||
222 | // Check a nick alongside our ignore list | |
223 | isNickIgnored: function (nick) { | |
224 | var idx, list = this.get('ignore_list'); | |
225 | var pattern, regex; | |
226 | ||
227 | for (idx = 0; idx < list.length; idx++) { | |
228 | pattern = list[idx].replace(/([.+^$[\]\\(){}|-])/g, "\\$1") | |
229 | .replace('*', '.*') | |
230 | .replace('?', '.'); | |
231 | ||
232 | regex = new RegExp(pattern, 'i'); | |
233 | if (regex.test(nick)) return true; | |
234 | } | |
235 | ||
236 | return false; | |
ce13508b D |
237 | } |
238 | }); | |
239 | ||
240 | ||
d6eec6ed | 241 | |
a36b7eb6 D |
242 | function onDisconnect(event) { |
243 | $.each(this.panels.models, function (index, panel) { | |
2e6dd28a | 244 | panel.addMsg('', styleText('network_disconnected', {'%T': translateText('client_models_network_disconnected', [])}), 'action quit'); |
a36b7eb6 D |
245 | }); |
246 | } | |
247 | ||
248 | ||
ce13508b | 249 | |
4047ee2b | 250 | function onConnect(event) { |
18818abd D |
251 | var panels, channel_names; |
252 | ||
253 | // Update our nick with what the network gave us | |
4047ee2b D |
254 | this.set('nick', event.nick); |
255 | ||
24d27c8c D |
256 | // If this is a re-connection then we may have some channels to re-join |
257 | this.rejoinAllChannels(); | |
258 | ||
18818abd | 259 | // Auto joining channels |
4047ee2b | 260 | if (this.auto_join && this.auto_join.channel) { |
852c230c | 261 | panels = this.createAndJoinChannels(this.auto_join.channel + ' ' + (this.auto_join.key || '')); |
18818abd D |
262 | |
263 | // Show the last channel if we have one | |
264 | if (panels) | |
265 | panels[panels.length - 1].view.show(); | |
cf3d9759 D |
266 | |
267 | delete this.auto_join; | |
4047ee2b D |
268 | } |
269 | } | |
270 | ||
271 | ||
272 | ||
ce13508b D |
273 | function onOptions(event) { |
274 | var that = this; | |
275 | ||
276 | $.each(event.options, function (name, value) { | |
277 | switch (name) { | |
278 | case 'CHANTYPES': | |
279 | that.set('channel_prefix', value.join('')); | |
280 | break; | |
281 | case 'NETWORK': | |
282 | that.set('name', value); | |
283 | break; | |
284 | case 'PREFIX': | |
285 | that.set('user_prefixes', value); | |
286 | break; | |
287 | } | |
288 | }); | |
289 | ||
290 | this.set('cap', event.cap); | |
291 | } | |
292 | ||
293 | ||
294 | ||
295 | function onMotd(event) { | |
b8fdd572 | 296 | this.panels.server.addMsg(this.get('name'), styleText('motd', {'%T': event.msg}), 'motd'); |
ce13508b D |
297 | } |
298 | ||
299 | ||
300 | ||
301 | function onJoin(event) { | |
302 | var c, members, user; | |
303 | c = this.panels.getByName(event.channel); | |
304 | if (!c) { | |
72db27e4 | 305 | c = new _kiwi.model.Channel({name: event.channel, network: this}); |
ce13508b D |
306 | this.panels.add(c); |
307 | } | |
308 | ||
309 | members = c.get('members'); | |
310 | if (!members) return; | |
311 | ||
11412289 D |
312 | user = new _kiwi.model.Member({ |
313 | nick: event.nick, | |
314 | ident: event.ident, | |
315 | hostname: event.hostname, | |
316 | user_prefixes: this.get('user_prefixes') | |
317 | }); | |
697a76c5 | 318 | members.add(user, {kiwi: event}); |
ce13508b D |
319 | } |
320 | ||
321 | ||
322 | ||
323 | function onPart(event) { | |
324 | var channel, members, user, | |
325 | part_options = {}; | |
326 | ||
327 | part_options.type = 'part'; | |
328 | part_options.message = event.message || ''; | |
697a76c5 | 329 | part_options.time = event.time; |
ce13508b D |
330 | |
331 | channel = this.panels.getByName(event.channel); | |
332 | if (!channel) return; | |
333 | ||
334 | // If this is us, close the panel | |
335 | if (event.nick === this.get('nick')) { | |
336 | channel.close(); | |
337 | return; | |
338 | } | |
339 | ||
340 | members = channel.get('members'); | |
341 | if (!members) return; | |
342 | ||
343 | user = members.getByNick(event.nick); | |
344 | if (!user) return; | |
345 | ||
697a76c5 | 346 | members.remove(user, {kiwi: part_options}); |
ce13508b D |
347 | } |
348 | ||
349 | ||
350 | ||
351 | function onQuit(event) { | |
352 | var member, members, | |
353 | quit_options = {}; | |
354 | ||
355 | quit_options.type = 'quit'; | |
356 | quit_options.message = event.message || ''; | |
697a76c5 | 357 | quit_options.time = event.time; |
ce13508b D |
358 | |
359 | $.each(this.panels.models, function (index, panel) { | |
360 | if (!panel.isChannel()) return; | |
361 | ||
362 | member = panel.get('members').getByNick(event.nick); | |
363 | if (member) { | |
697a76c5 | 364 | panel.get('members').remove(member, {kiwi: quit_options}); |
ce13508b D |
365 | } |
366 | }); | |
367 | } | |
368 | ||
369 | ||
370 | ||
371 | function onKick(event) { | |
372 | var channel, members, user, | |
373 | part_options = {}; | |
374 | ||
375 | part_options.type = 'kick'; | |
376 | part_options.by = event.nick; | |
377 | part_options.message = event.message || ''; | |
697a76c5 JA |
378 | part_options.current_user_kicked = (event.kicked == this.get('nick')); |
379 | part_options.current_user_initiated = (event.nick == this.get('nick')); | |
380 | part_options.time = event.time; | |
ce13508b D |
381 | |
382 | channel = this.panels.getByName(event.channel); | |
383 | if (!channel) return; | |
384 | ||
385 | members = channel.get('members'); | |
386 | if (!members) return; | |
387 | ||
388 | user = members.getByNick(event.kicked); | |
389 | if (!user) return; | |
390 | ||
eed5ac29 | 391 | |
697a76c5 | 392 | members.remove(user, {kiwi: part_options}); |
ce13508b | 393 | |
b8c035b2 | 394 | if (part_options.current_user_kicked) { |
d6eec6ed | 395 | members.reset([]); |
ce13508b D |
396 | } |
397 | } | |
398 | ||
399 | ||
400 | ||
401 | function onMsg(event) { | |
402 | var panel, | |
aef6e2a7 | 403 | is_pm = (event.channel.toLowerCase() == this.get('nick').toLowerCase()); |
ce13508b D |
404 | |
405 | // An ignored user? don't do anything with it | |
425efe7a | 406 | if (this.isNickIgnored(event.nick)) { |
ce13508b D |
407 | return; |
408 | } | |
409 | ||
410 | if (is_pm) { | |
411 | // If a panel isn't found for this PM, create one | |
412 | panel = this.panels.getByName(event.nick); | |
413 | if (!panel) { | |
414 | panel = new _kiwi.model.Query({name: event.nick}); | |
415 | this.panels.add(panel); | |
416 | } | |
417 | ||
418 | } else { | |
419 | // If a panel isn't found for this channel, reroute to the | |
420 | // server panel | |
421 | panel = this.panels.getByName(event.channel); | |
422 | if (!panel) { | |
423 | panel = this.panels.server; | |
424 | } | |
425 | } | |
426 | ||
b8fdd572 | 427 | panel.addMsg(event.nick, styleText('privmsg', {'%T': event.msg}), 'privmsg', {time: event.time}); |
ce13508b D |
428 | } |
429 | ||
430 | ||
431 | ||
e2c54b3e D |
432 | function onNick(event) { |
433 | var member; | |
434 | ||
435 | $.each(this.panels.models, function (index, panel) { | |
436 | if (panel.get('name') == event.nick) | |
437 | panel.set('name', event.newnick); | |
438 | ||
439 | if (!panel.isChannel()) return; | |
440 | ||
441 | member = panel.get('members').getByNick(event.nick); | |
442 | if (member) { | |
443 | member.set('nick', event.newnick); | |
2e6dd28a | 444 | panel.addMsg('', styleText('nick_changed', {'%N': event.nick, '%T': translateText('client_models_network_nickname_changed', [event.newnick]), '%C': name}), 'action nick', {time: event.time}); |
e2c54b3e D |
445 | } |
446 | }); | |
447 | } | |
448 | ||
449 | ||
450 | ||
ce13508b D |
451 | function onCtcpRequest(event) { |
452 | // An ignored user? don't do anything with it | |
425efe7a | 453 | if (this.isNickIgnored(event.nick)) { |
ce13508b D |
454 | return; |
455 | } | |
456 | ||
457 | // Reply to a TIME ctcp | |
458 | if (event.msg.toUpperCase() === 'TIME') { | |
e3691498 | 459 | this.gateway.ctcp(false, event.type, event.nick, (new Date()).toString()); |
ce13508b D |
460 | } |
461 | } | |
462 | ||
463 | ||
464 | ||
465 | function onCtcpResponse(event) { | |
466 | // An ignored user? don't do anything with it | |
425efe7a | 467 | if (this.isNickIgnored(event.nick)) { |
ce13508b D |
468 | return; |
469 | } | |
470 | ||
b8fdd572 | 471 | this.panels.server.addMsg('[' + event.nick + ']', styleText('ctcp', {'%T': event.msg}), 'ctcp', {time: event.time}); |
ce13508b D |
472 | } |
473 | ||
474 | ||
475 | ||
476 | function onNotice(event) { | |
17f187e6 | 477 | var panel, active_panel, channel_name; |
ce13508b D |
478 | |
479 | // An ignored user? don't do anything with it | |
425efe7a | 480 | if (!event.from_server && event.nick && this.isNickIgnored(event.nick)) { |
ce13508b D |
481 | return; |
482 | } | |
483 | ||
484 | // Find a panel for the destination(channel) or who its from | |
0190c2c9 D |
485 | if (!event.from_server) { |
486 | panel = this.panels.getByName(event.target) || this.panels.getByName(event.nick); | |
4550fa1f D |
487 | |
488 | // Forward ChanServ messages to its associated channel | |
b70909a6 | 489 | if (event.nick && event.nick.toLowerCase() == 'chanserv' && event.msg.charAt(0) == '[') { |
4550fa1f D |
490 | channel_name = /\[([^ \]]+)\]/gi.exec(event.msg); |
491 | if (channel_name && channel_name[1]) { | |
492 | channel_name = channel_name[1]; | |
493 | ||
494 | panel = this.panels.getByName(channel_name); | |
495 | } | |
496 | } | |
497 | ||
0190c2c9 D |
498 | if (!panel) { |
499 | panel = this.panels.server; | |
500 | } | |
501 | } else { | |
ce13508b D |
502 | panel = this.panels.server; |
503 | } | |
504 | ||
b8fdd572 | 505 | panel.addMsg('[' + (event.nick||'') + ']', styleText('notice', {'%T': event.msg}), 'notice', {time: event.time}); |
2119ce7a | 506 | |
17f187e6 D |
507 | // Show this notice to the active panel if it didn't have a set target, but only in an active channel or query window |
508 | active_panel = _kiwi.app.panels().active; | |
509 | ||
510 | if (!event.from_server && panel === this.panels.server && active_panel !== this.panels.server) { | |
511 | if (active_panel.isChannel() || active_panel.isQuery()) | |
b8fdd572 | 512 | _kiwi.app.panels().active.addMsg('[' + (event.nick||'') + ']', styleText('notice', {'%T': event.msg}), 'notice', {time: event.time}); |
17f187e6 | 513 | } |
ce13508b D |
514 | } |
515 | ||
516 | ||
517 | ||
518 | function onAction(event) { | |
519 | var panel, | |
aef6e2a7 | 520 | is_pm = (event.channel.toLowerCase() == this.get('nick').toLowerCase()); |
ce13508b D |
521 | |
522 | // An ignored user? don't do anything with it | |
425efe7a | 523 | if (this.isNickIgnored(event.nick)) { |
ce13508b D |
524 | return; |
525 | } | |
526 | ||
527 | if (is_pm) { | |
528 | // If a panel isn't found for this PM, create one | |
529 | panel = this.panels.getByName(event.nick); | |
530 | if (!panel) { | |
72db27e4 | 531 | panel = new _kiwi.model.Channel({name: event.nick, network: this}); |
ce13508b D |
532 | this.panels.add(panel); |
533 | } | |
534 | ||
535 | } else { | |
536 | // If a panel isn't found for this channel, reroute to the | |
537 | // server panel | |
538 | panel = this.panels.getByName(event.channel); | |
539 | if (!panel) { | |
540 | panel = this.panels.server; | |
541 | } | |
542 | } | |
543 | ||
b8fdd572 | 544 | panel.addMsg('', styleText('action', {'%N': event.nick, '%T': event.msg}), 'action', {time: event.time}); |
ce13508b D |
545 | } |
546 | ||
547 | ||
548 | ||
549 | function onTopic(event) { | |
550 | var c; | |
551 | c = this.panels.getByName(event.channel); | |
552 | if (!c) return; | |
553 | ||
554 | // Set the channels topic | |
555 | c.set('topic', event.topic); | |
556 | ||
557 | // If this is the active channel, update the topic bar too | |
558 | if (c.get('name') === this.panels.active.get('name')) { | |
559 | _kiwi.app.topicbar.setCurrentTopic(event.topic); | |
560 | } | |
561 | } | |
562 | ||
563 | ||
564 | ||
565 | function onTopicSetBy(event) { | |
566 | var c, when; | |
567 | c = this.panels.getByName(event.channel); | |
568 | if (!c) return; | |
569 | ||
570 | when = formatDate(new Date(event.when * 1000)); | |
2e6dd28a | 571 | c.addMsg('', styleText('channel_topic_setby', {'%T': translateText('client_models_network_topic', [event.nick, when]), '%C': event.channel}), 'topic'); |
ce13508b D |
572 | } |
573 | ||
574 | ||
575 | ||
72db27e4 D |
576 | function onChannelInfo(event) { |
577 | var channel = this.panels.getByName(event.channel); | |
578 | if (!channel) return; | |
579 | ||
580 | if (event.url) { | |
581 | channel.set('info_url', event.url); | |
582 | } else if (event.modes) { | |
583 | channel.set('info_modes', event.modes); | |
584 | } | |
585 | } | |
586 | ||
587 | ||
588 | ||
ce13508b | 589 | function onUserlist(event) { |
11412289 D |
590 | var that = this, |
591 | channel = this.panels.getByName(event.channel); | |
ce13508b D |
592 | |
593 | // If we didn't find a channel for this, may aswell leave | |
594 | if (!channel) return; | |
595 | ||
596 | channel.temp_userlist = channel.temp_userlist || []; | |
597 | _.each(event.users, function (item) { | |
11412289 D |
598 | var user = new _kiwi.model.Member({ |
599 | nick: item.nick, | |
600 | modes: item.modes, | |
601 | user_prefixes: that.get('user_prefixes') | |
602 | }); | |
ce13508b D |
603 | channel.temp_userlist.push(user); |
604 | }); | |
605 | } | |
606 | ||
607 | ||
608 | ||
609 | function onUserlistEnd(event) { | |
610 | var channel; | |
611 | channel = this.panels.getByName(event.channel); | |
612 | ||
613 | // If we didn't find a channel for this, may aswell leave | |
614 | if (!channel) return; | |
615 | ||
616 | // Update the members list with the new list | |
617 | channel.get('members').reset(channel.temp_userlist || []); | |
618 | ||
619 | // Clear the temporary userlist | |
620 | delete channel.temp_userlist; | |
621 | } | |
622 | ||
623 | ||
624 | ||
72db27e4 | 625 | function onBanlist(event) { |
72db27e4 D |
626 | var channel = this.panels.getByName(event.channel); |
627 | if (!channel) | |
628 | return; | |
629 | ||
630 | channel.set('banlist', event.bans || []); | |
631 | } | |
632 | ||
633 | ||
634 | ||
ce13508b | 635 | function onMode(event) { |
19c15434 D |
636 | var channel, i, prefixes, members, member, find_prefix, |
637 | request_updated_banlist = false; | |
ce13508b D |
638 | |
639 | // Build a nicely formatted string to be displayed to a regular human | |
640 | function friendlyModeString (event_modes, alt_target) { | |
641 | var modes = {}, return_string; | |
642 | ||
643 | // If no default given, use the main event info | |
644 | if (!event_modes) { | |
645 | event_modes = event.modes; | |
646 | alt_target = event.target; | |
647 | } | |
648 | ||
649 | // Reformat the mode object to make it easier to work with | |
650 | _.each(event_modes, function (mode){ | |
651 | var param = mode.param || alt_target || ''; | |
652 | ||
653 | // Make sure we have some modes for this param | |
654 | if (!modes[param]) { | |
655 | modes[param] = {'+':'', '-':''}; | |
656 | } | |
657 | ||
658 | modes[param][mode.mode[0]] += mode.mode.substr(1); | |
659 | }); | |
660 | ||
661 | // Put the string together from each mode | |
662 | return_string = []; | |
663 | _.each(modes, function (modeset, param) { | |
664 | var str = ''; | |
665 | if (modeset['+']) str += '+' + modeset['+']; | |
666 | if (modeset['-']) str += '-' + modeset['-']; | |
667 | return_string.push(str + ' ' + param); | |
668 | }); | |
669 | return_string = return_string.join(', '); | |
670 | ||
671 | return return_string; | |
672 | } | |
673 | ||
674 | ||
675 | channel = this.panels.getByName(event.target); | |
676 | if (channel) { | |
677 | prefixes = this.get('user_prefixes'); | |
678 | find_prefix = function (p) { | |
679 | return event.modes[i].mode[1] === p.mode; | |
680 | }; | |
681 | for (i = 0; i < event.modes.length; i++) { | |
682 | if (_.any(prefixes, find_prefix)) { | |
683 | if (!members) { | |
684 | members = channel.get('members'); | |
685 | } | |
686 | member = members.getByNick(event.modes[i].param); | |
687 | if (!member) { | |
688 | console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target); | |
689 | return; | |
690 | } else { | |
691 | if (event.modes[i].mode[0] === '+') { | |
692 | member.addMode(event.modes[i].mode[1]); | |
693 | } else if (event.modes[i].mode[0] === '-') { | |
694 | member.removeMode(event.modes[i].mode[1]); | |
695 | } | |
696 | members.sort(); | |
ce13508b D |
697 | } |
698 | } else { | |
699 | // Channel mode being set | |
700 | // TODO: Store this somewhere? | |
701 | //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode'); | |
702 | } | |
19c15434 D |
703 | |
704 | // TODO: Be smart, remove this specific ban from the banlist rather than request a whole banlist | |
705 | if (event.modes[i].mode[1] == 'b') | |
706 | request_updated_banlist = true; | |
ce13508b D |
707 | } |
708 | ||
2e6dd28a | 709 | channel.addMsg('', styleText('mode', {'%N': event.nick, '%T': translateText('client_models_network_mode', [friendlyModeString()]), '%C': event.target}), 'action mode', {time: event.time}); |
19c15434 D |
710 | |
711 | // TODO: Be smart, remove the specific ban from the banlist rather than request a whole banlist | |
712 | if (request_updated_banlist) | |
713 | this.gateway.raw('MODE ' + channel.get('name') + ' +b'); | |
714 | ||
ce13508b D |
715 | } else { |
716 | // This is probably a mode being set on us. | |
717 | if (event.target.toLowerCase() === this.get("nick").toLowerCase()) { | |
2e6dd28a | 718 | this.panels.server.addMsg('', styleText('selfmode', {'%N': event.nick, '%T': translateText('client_models_network_mode', [friendlyModeString()]), '%C': event.target}), 'action mode'); |
ce13508b D |
719 | } else { |
720 | console.log('MODE command recieved for unknown target %s: ', event.target, event); | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | ||
726 | ||
727 | function onWhois(event) { | |
728 | var logon_date, idle_time = '', panel; | |
729 | ||
730 | if (event.end) | |
731 | return; | |
732 | ||
733 | if (typeof event.idle !== 'undefined') { | |
734 | idle_time = secondsToTime(parseInt(event.idle, 10)); | |
735 | idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0"); | |
736 | } | |
737 | ||
e7d65587 | 738 | panel = _kiwi.app.panels().active; |
ce13508b | 739 | if (event.ident) { |
b8fdd572 CC |
740 | panel.addMsg(event.nick, styleText('whois_ident', {'%N': event.nick, '%J': event.ident, '%H': event.host, '%T': event.msg}), 'whois'); |
741 | ||
ce13508b | 742 | } else if (event.chans) { |
2e6dd28a | 743 | panel.addMsg(event.nick, styleText('whois_channels', {'%N': event.nick, '%T': translateText('client_models_network_channels', [event.chans])}), 'whois'); |
ce13508b | 744 | } else if (event.irc_server) { |
2e6dd28a | 745 | panel.addMsg(event.nick, styleText('whois_server', {'%N': event.nick, '%T': translateText('client_models_network_server', [event.irc_server, event.server_info])}), 'whois'); |
ce13508b | 746 | } else if (event.msg) { |
b8fdd572 | 747 | panel.addMsg(event.nick, styleText('whois', {'%T': event.msg}), 'whois'); |
ce13508b D |
748 | } else if (event.logon) { |
749 | logon_date = new Date(); | |
750 | logon_date.setTime(event.logon * 1000); | |
751 | logon_date = formatDate(logon_date); | |
752 | ||
2e6dd28a | 753 | panel.addMsg(event.nick, styleText('whois_idle_and_signon', {'%N': event.nick, '%T': translateText('client_models_network_idle_and_signon', [idle_time, logon_date])}), 'whois'); |
f701d5ba | 754 | } else if (event.away_reason) { |
2e6dd28a | 755 | panel.addMsg(event.nick, styleText('whois_away', {'%N': event.nick, '%T': translateText('client_models_network_away', [event.away_reason])}), 'whois'); |
ce13508b | 756 | } else { |
2e6dd28a | 757 | panel.addMsg(event.nick, styleText('whois_idle', {'%N': event.nick, '%T': translateText('client_models_network_idle', [idle_time])}), 'whois'); |
ce13508b D |
758 | } |
759 | } | |
760 | ||
5989f3f4 JA |
761 | function onWhowas(event) { |
762 | var panel; | |
763 | ||
764 | if (event.end) | |
765 | return; | |
766 | ||
767 | panel = _kiwi.app.panels().active; | |
768 | if (event.host) { | |
b8fdd572 | 769 | panel.addMsg(event.nick, styleText('who', {'%N': event.nick, '%J': event.ident, '%H': event.host, '%R': event.real_name, '%T': event.msg}), 'whois'); |
5989f3f4 | 770 | } else { |
2e6dd28a | 771 | panel.addMsg(event.nick, styleText('whois_notfound', {'%N': event.nick, '%T': translateText('client_models_network_nickname_notfound', [])}), 'whois'); |
5989f3f4 JA |
772 | } |
773 | } | |
ce13508b D |
774 | |
775 | ||
776 | function onAway(event) { | |
777 | $.each(this.panels.models, function (index, panel) { | |
778 | if (!panel.isChannel()) return; | |
779 | ||
780 | member = panel.get('members').getByNick(event.nick); | |
781 | if (member) { | |
17dfa698 | 782 | member.set('away', !(!event.reason)); |
ce13508b D |
783 | } |
784 | }); | |
785 | } | |
786 | ||
787 | ||
788 | ||
789 | function onListStart(event) { | |
b24da6ba D |
790 | var chanlist = _kiwi.model.Applet.loadOnce('kiwi_chanlist'); |
791 | chanlist.view.show(); | |
ce13508b D |
792 | } |
793 | ||
794 | ||
795 | ||
796 | function onIrcError(event) { | |
797 | var panel, tmp; | |
798 | ||
f8240e98 | 799 | if (event.channel !== undefined && !(panel = this.panels.getByName(event.channel))) { |
ce13508b D |
800 | panel = this.panels.server; |
801 | } | |
802 | ||
803 | switch (event.error) { | |
804 | case 'banned_from_channel': | |
2e6dd28a | 805 | panel.addMsg(' ', styleText('channel_banned', {'%N': event.nick, '%T': translateText('client_models_network_banned', [event.channel, event.reason]), '%C': event.channel}), 'status'); |
247dd7ac | 806 | _kiwi.app.message.text(_kiwi.global.i18n.translate('client_models_network_banned').fetch(event.channel, event.reason)); |
ce13508b D |
807 | break; |
808 | case 'bad_channel_key': | |
2e6dd28a | 809 | panel.addMsg(' ', styleText('channel_badkey', {'%N': event.nick, '%T': translateText('client_models_network_channel_badkey', [event.channel]), '%C': event.channel}), 'status'); |
247dd7ac | 810 | _kiwi.app.message.text(_kiwi.global.i18n.translate('client_models_network_channel_badkey').fetch(event.channel)); |
ce13508b D |
811 | break; |
812 | case 'invite_only_channel': | |
2e6dd28a | 813 | panel.addMsg(' ', styleText('channel_inviteonly', {'%N': event.nick, '%T': translateText('client_models_network_channel_inviteonly', [event.nick, event.channel]), '%C': event.channel}), 'status'); |
a8675b0b | 814 | _kiwi.app.message.text(event.channel + ' ' + _kiwi.global.i18n.translate('client_models_network_channel_inviteonly').fetch()); |
ce13508b | 815 | break; |
3d753975 | 816 | case 'user_on_channel': |
2e6dd28a | 817 | panel.addMsg(' ', styleText('channel_alreadyin', {'%N': event.nick, '%T': translateText('client_models_network_channel_alreadyin'), '%C': event.channel})); |
3d753975 | 818 | break; |
ce13508b | 819 | case 'channel_is_full': |
2e6dd28a | 820 | panel.addMsg(' ', styleText('channel_limitreached', {'%N': event.nick, '%T': translateText('client_models_network_channel_limitreached', [event.channel]), '%C': event.channel}), 'status'); |
a8675b0b | 821 | _kiwi.app.message.text(event.channel + ' ' + _kiwi.global.i18n.translate('client_models_network_channel_limitreached').fetch(event.channel)); |
ce13508b D |
822 | break; |
823 | case 'chanop_privs_needed': | |
b8fdd572 | 824 | panel.addMsg(' ', styleText('chanop_privs_needed', {'%T': event.reason, '%C': event.channel}), 'status'); |
ce13508b D |
825 | _kiwi.app.message.text(event.reason + ' (' + event.channel + ')'); |
826 | break; | |
827 | case 'no_such_nick': | |
828 | tmp = this.panels.getByName(event.nick); | |
829 | if (tmp) { | |
b8fdd572 | 830 | tmp.addMsg(' ', styleText('no_such_nick', {'%N': event.nick, '%T': event.reason, '%C': event.channel}), 'status'); |
ce13508b | 831 | } else { |
b8fdd572 | 832 | this.panels.server.addMsg(' ', styleText('no_such_nick', {'%N': event.nick, '%T': event.reason, '%C': event.channel}), 'status'); |
ce13508b D |
833 | } |
834 | break; | |
835 | case 'nickname_in_use': | |
2e6dd28a | 836 | this.panels.server.addMsg(' ', styleText('nickname_alreadyinuse', {'%N': event.nick, '%T': translateText('client_models_network_nickname_alreadyinuse', [event.nick]), '%C': event.channel}), 'status'); |
2e06af20 | 837 | if (this.panels.server !== this.panels.active) { |
247dd7ac | 838 | _kiwi.app.message.text(_kiwi.global.i18n.translate('client_models_network_nickname_alreadyinuse').fetch(event.nick)); |
ce13508b D |
839 | } |
840 | ||
841 | // Only show the nickchange component if the controlbox is open | |
2e06af20 | 842 | if (_kiwi.app.controlbox.$el.css('display') !== 'none') { |
ce13508b D |
843 | (new _kiwi.view.NickChangeBox()).render(); |
844 | } | |
845 | ||
846 | break; | |
847 | ||
848 | case 'password_mismatch': | |
2e6dd28a | 849 | this.panels.server.addMsg(' ', styleText('channel_badpassword', {'%N': event.nick, '%T': translateText('client_models_network_badpassword', []), '%C': event.channel}), 'status'); |
ce13508b D |
850 | break; |
851 | default: | |
852 | // We don't know what data contains, so don't do anything with it. | |
853 | //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status'); | |
854 | } | |
855 | } | |
3d753975 D |
856 | |
857 | ||
858 | function onUnknownCommand(event) { | |
859 | var display_params = _.clone(event.params); | |
860 | ||
861 | // A lot of commands have our nick as the first parameter. This is redundant for us | |
862 | if (display_params[0] && display_params[0] == this.get('nick')) { | |
863 | display_params.shift(); | |
864 | } | |
865 | ||
b8fdd572 | 866 | this.panels.server.addMsg('', styleText('unknown_command', {'%T': '[' + event.command + '] ' + display_params.join(', ', '')})); |
3d753975 | 867 | } |
ce13508b D |
868 | } |
869 | ||
eed5ac29 | 870 | )(); |