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