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