3 _kiwi
.model
.Network
= Backbone
.Model
.extend({
7 * The name of the network
13 * The address (URL) of the network
19 * The current nickname
25 * The channel prefix for this network
31 * The user prefixes for channel owner/admin/op/voice etc. on this network
34 user_prefixes
: ['~', '&', '@', '+']
38 initialize: function () {
39 this.gateway
= _kiwi
.global
.components
.Network(this.get('connection_id'));
40 this.bindGatewayEvents();
42 // Create our panel list (tabs)
43 this.panels
= new _kiwi
.model
.PanelList([], this);
44 //this.panels.network = this;
46 // Automatically create a server tab
47 var server_panel
= new _kiwi
.model
.Server({name
: 'Server'});
48 this.panels
.add(server_panel
);
49 this.panels
.server
= this.panels
.active
= server_panel
;
53 bindGatewayEvents: function () {
54 //this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);});
56 this.gateway
.on('connect', onConnect
, this);
58 this.gateway
.on('nick', function(event
) {
59 if (event
.nick
=== this.get('nick')) {
60 this.set('nick', event
.newnick
);
64 this.gateway
.on('options', onOptions
, this);
65 this.gateway
.on('motd', onMotd
, this);
66 this.gateway
.on('join', onJoin
, this);
67 this.gateway
.on('part', onPart
, this);
68 this.gateway
.on('quit', onQuit
, this);
69 this.gateway
.on('kick', onKick
, this);
70 this.gateway
.on('msg', onMsg
, this);
71 this.gateway
.on('nick', onNick
, this);
72 this.gateway
.on('ctcp_request', onCtcpRequest
, this);
73 this.gateway
.on('ctcp_response', onCtcpResponse
, this);
74 this.gateway
.on('notice', onNotice
, this);
75 this.gateway
.on('action', onAction
, this);
76 this.gateway
.on('topic', onTopic
, this);
77 this.gateway
.on('topicsetby', onTopicSetBy
, this);
78 this.gateway
.on('userlist', onUserlist
, this);
79 this.gateway
.on('userlist_end', onUserlistEnd
, this);
80 this.gateway
.on('mode', onMode
, this);
81 this.gateway
.on('whois', onWhois
, this);
82 this.gateway
.on('away', onAway
, this);
83 this.gateway
.on('list_start', onListStart
, this);
88 * Create panels and join the channel
89 * This will not wait for the join event to create a panel. This
90 * increases responsiveness in case of network lag
92 createAndJoinChannels: function (channels
) {
96 // Multiple channels may come as comma-delimited
97 if (typeof channels
=== 'string') {
98 channels
= channels
.split(',');
101 $.each(channels
, function (index
, channel_name_key
) {
102 // We may have a channel key so split it off
103 var spli
= channel_name_key
.trim().split(' '),
104 channel_name
= spli
[0],
105 channel_key
= spli
[1] || '';
107 // Trim any whitespace off the name
108 channel_name
= channel_name
.trim();
110 // If not a valid channel name, display a warning
111 if (!_kiwi
.app
.isChannelName(channel_name
)) {
112 that
.panels
.server
.addMsg('', channel_name
+ ' is not a valid channel name');
113 _kiwi
.app
.message
.text(channel_name
+ ' is not a valid channel name', {timeout
: 5000});
117 // Check if we have the panel already. If not, create it
118 channel
= that
.panels
.getByName(channel_name
);
120 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
121 that
.panels
.add(channel
);
124 panels
.push(channel
);
126 that
.gateway
.join(channel_name
, channel_key
);
135 function onConnect(event
) {
136 var panels
, channel_names
;
138 // Update our nick with what the network gave us
139 this.set('nick', event
.nick
);
141 // Auto joining channels
142 if (this.auto_join
&& this.auto_join
.channel
) {
143 panels
= this.createAndJoinChannels(this.auto_join
.channel
+ ' ' + (this.auto_join
.channel_key
|| ''));
145 // Show the last channel if we have one
147 panels
[panels
.length
- 1].view
.show();
153 function onOptions(event
) {
156 $.each(event
.options
, function (name
, value
) {
159 that
.set('channel_prefix', value
.join(''));
162 that
.set('name', value
);
165 that
.set('user_prefixes', value
);
170 this.set('cap', event
.cap
);
175 function onMotd(event
) {
176 this.panels
.server
.addMsg(this.get('name'), event
.msg
, 'motd');
181 function onJoin(event
) {
182 var c
, members
, user
;
183 c
= this.panels
.getByName(event
.channel
);
185 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
189 members
= c
.get('members');
190 if (!members
) return;
192 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
198 function onPart(event
) {
199 var channel
, members
, user
,
202 part_options
.type
= 'part';
203 part_options
.message
= event
.message
|| '';
205 channel
= this.panels
.getByName(event
.channel
);
206 if (!channel
) return;
208 // If this is us, close the panel
209 if (event
.nick
=== this.get('nick')) {
214 members
= channel
.get('members');
215 if (!members
) return;
217 user
= members
.getByNick(event
.nick
);
220 members
.remove(user
, part_options
);
225 function onQuit(event
) {
229 quit_options
.type
= 'quit';
230 quit_options
.message
= event
.message
|| '';
232 $.each(this.panels
.models
, function (index
, panel
) {
233 if (!panel
.isChannel()) return;
235 member
= panel
.get('members').getByNick(event
.nick
);
237 panel
.get('members').remove(member
, quit_options
);
244 function onKick(event
) {
245 var channel
, members
, user
,
248 part_options
.type
= 'kick';
249 part_options
.by
= event
.nick
;
250 part_options
.message
= event
.message
|| '';
252 channel
= this.panels
.getByName(event
.channel
);
253 if (!channel
) return;
255 members
= channel
.get('members');
256 if (!members
) return;
258 user
= members
.getByNick(event
.kicked
);
261 members
.remove(user
, part_options
);
263 if (event
.kicked
=== this.get('nick')) {
270 function onMsg(event
) {
272 is_pm
= (event
.channel
== this.get('nick'));
274 // An ignored user? don't do anything with it
275 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
280 // If a panel isn't found for this PM, create one
281 panel
= this.panels
.getByName(event
.nick
);
283 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
284 this.panels
.add(panel
);
288 // If a panel isn't found for this channel, reroute to the
290 panel
= this.panels
.getByName(event
.channel
);
292 panel
= this.panels
.server
;
296 panel
.addMsg(event
.nick
, event
.msg
);
301 function onNick(event
) {
304 $.each(this.panels
.models
, function (index
, panel
) {
305 if (panel
.get('name') == event
.nick
)
306 panel
.set('name', event
.newnick
);
308 if (!panel
.isChannel()) return;
310 member
= panel
.get('members').getByNick(event
.nick
);
312 member
.set('nick', event
.newnick
);
313 panel
.addMsg('', '== ' + event
.nick
+ ' is now known as ' + event
.newnick
, 'action nick');
320 function onCtcpRequest(event
) {
321 // An ignored user? don't do anything with it
322 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
326 // Reply to a TIME ctcp
327 if (event
.msg
.toUpperCase() === 'TIME') {
328 this.gateway
.ctcp(null, false, event
.type
, event
.nick
, (new Date()).toString());
334 function onCtcpResponse(event
) {
335 // An ignored user? don't do anything with it
336 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
340 this.panels
.server
.addMsg('[' + event
.nick
+ ']', 'CTCP ' + event
.msg
);
345 function onNotice(event
) {
348 // An ignored user? don't do anything with it
349 if (event
.nick
&& _kiwi
.gateway
.isNickIgnored(event
.nick
)) {
353 // Find a panel for the destination(channel) or who its from
354 panel
= this.panels
.getByName(event
.target
) || this.panels
.getByName(event
.nick
);
356 panel
= this.panels
.server
;
359 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
364 function onAction(event
) {
366 is_pm
= (event
.channel
== this.get('nick'));
368 // An ignored user? don't do anything with it
369 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
374 // If a panel isn't found for this PM, create one
375 panel
= this.panels
.getByName(event
.nick
);
377 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
378 this.panels
.add(panel
);
382 // If a panel isn't found for this channel, reroute to the
384 panel
= this.panels
.getByName(event
.channel
);
386 panel
= this.panels
.server
;
390 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
395 function onTopic(event
) {
397 c
= this.panels
.getByName(event
.channel
);
400 // Set the channels topic
401 c
.set('topic', event
.topic
);
403 // If this is the active channel, update the topic bar too
404 if (c
.get('name') === this.panels
.active
.get('name')) {
405 _kiwi
.app
.topicbar
.setCurrentTopic(event
.topic
);
411 function onTopicSetBy(event
) {
413 c
= this.panels
.getByName(event
.channel
);
416 when
= formatDate(new Date(event
.when
* 1000));
417 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
422 function onUserlist(event
) {
424 channel
= this.panels
.getByName(event
.channel
);
426 // If we didn't find a channel for this, may aswell leave
427 if (!channel
) return;
429 channel
.temp_userlist
= channel
.temp_userlist
|| [];
430 _
.each(event
.users
, function (item
) {
431 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
432 channel
.temp_userlist
.push(user
);
438 function onUserlistEnd(event
) {
440 channel
= this.panels
.getByName(event
.channel
);
442 // If we didn't find a channel for this, may aswell leave
443 if (!channel
) return;
445 // Update the members list with the new list
446 channel
.get('members').reset(channel
.temp_userlist
|| []);
448 // Clear the temporary userlist
449 delete channel
.temp_userlist
;
454 function onMode(event
) {
455 var channel
, i
, prefixes
, members
, member
, find_prefix
;
457 // Build a nicely formatted string to be displayed to a regular human
458 function friendlyModeString (event_modes
, alt_target
) {
459 var modes
= {}, return_string
;
461 // If no default given, use the main event info
463 event_modes
= event
.modes
;
464 alt_target
= event
.target
;
467 // Reformat the mode object to make it easier to work with
468 _
.each(event_modes
, function (mode
){
469 var param
= mode
.param
|| alt_target
|| '';
471 // Make sure we have some modes for this param
473 modes
[param
] = {'+':'', '-':''};
476 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
479 // Put the string together from each mode
481 _
.each(modes
, function (modeset
, param
) {
483 if (modeset
['+']) str
+= '+' + modeset
['+'];
484 if (modeset
['-']) str
+= '-' + modeset
['-'];
485 return_string
.push(str
+ ' ' + param
);
487 return_string
= return_string
.join(', ');
489 return return_string
;
493 channel
= this.panels
.getByName(event
.target
);
495 prefixes
= this.get('user_prefixes');
496 find_prefix = function (p
) {
497 return event
.modes
[i
].mode
[1] === p
.mode
;
499 for (i
= 0; i
< event
.modes
.length
; i
++) {
500 if (_
.any(prefixes
, find_prefix
)) {
502 members
= channel
.get('members');
504 member
= members
.getByNick(event
.modes
[i
].param
);
506 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
509 if (event
.modes
[i
].mode
[0] === '+') {
510 member
.addMode(event
.modes
[i
].mode
[1]);
511 } else if (event
.modes
[i
].mode
[0] === '-') {
512 member
.removeMode(event
.modes
[i
].mode
[1]);
515 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
518 // Channel mode being set
519 // TODO: Store this somewhere?
520 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
524 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
526 // This is probably a mode being set on us.
527 if (event
.target
.toLowerCase() === this.get("nick").toLowerCase()) {
528 this.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
530 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
537 function onWhois(event
) {
538 var logon_date
, idle_time
= '', panel
;
543 if (typeof event
.idle
!== 'undefined') {
544 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
545 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
548 panel
= _kiwi
.app
.panels().active
;
550 panel
.addMsg(event
.nick
, event
.nick
+ ' [' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ '] * ' + event
.msg
, 'whois');
551 } else if (event
.chans
) {
552 panel
.addMsg(event
.nick
, 'Channels: ' + event
.chans
, 'whois');
553 } else if (event
.irc_server
) {
554 panel
.addMsg(event
.nick
, 'Connected to server: ' + event
.irc_server
, 'whois');
555 } else if (event
.msg
) {
556 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
557 } else if (event
.logon
) {
558 logon_date
= new Date();
559 logon_date
.setTime(event
.logon
* 1000);
560 logon_date
= formatDate(logon_date
);
562 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
563 } else if (event
.away_reason
) {
564 panel
.addMsg(event
.nick
, 'Away: ' + event
.away_reason
, 'whois');
566 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
572 function onAway(event
) {
573 $.each(this.panels
.models
, function (index
, panel
) {
574 if (!panel
.isChannel()) return;
576 member
= panel
.get('members').getByNick(event
.nick
);
578 member
.set('away', !(!event
.trailing
));
585 function onListStart(event
) {
586 var chanlist
= _kiwi
.model
.Applet
.loadOnce('kiwi_chanlist');
587 chanlist
.view
.show();
592 function onIrcError(event
) {
595 if (event
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(event
.channel
))) {
596 panel
= this.panels
.server
;
599 switch (event
.error
) {
600 case 'banned_from_channel':
601 panel
.addMsg(' ', '== You are banned from ' + event
.channel
+ '. ' + event
.reason
, 'status');
602 _kiwi
.app
.message
.text('You are banned from ' + event
.channel
+ '. ' + event
.reason
);
604 case 'bad_channel_key':
605 panel
.addMsg(' ', '== Bad channel key for ' + event
.channel
, 'status');
606 _kiwi
.app
.message
.text('Bad channel key or password for ' + event
.channel
);
608 case 'invite_only_channel':
609 panel
.addMsg(' ', '== ' + event
.channel
+ ' is invite only.', 'status');
610 _kiwi
.app
.message
.text(event
.channel
+ ' is invite only');
612 case 'channel_is_full':
613 panel
.addMsg(' ', '== ' + event
.channel
+ ' is full.', 'status');
614 _kiwi
.app
.message
.text(event
.channel
+ ' is full');
616 case 'chanop_privs_needed':
617 panel
.addMsg(' ', '== ' + event
.reason
, 'status');
618 _kiwi
.app
.message
.text(event
.reason
+ ' (' + event
.channel
+ ')');
621 tmp
= this.panels
.getByName(event
.nick
);
623 tmp
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
625 this.panels
.server
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
628 case 'nickname_in_use':
629 this.panels
.server
.addMsg(' ', '== The nickname ' + event
.nick
+ ' is already in use. Please select a new nickname', 'status');
630 if (this.panels
.server
!== thia
.panels
.active
) {
631 _kiwi
.app
.message
.text('The nickname "' + event
.nick
+ '" is already in use. Please select a new nickname');
634 // Only show the nickchange component if the controlbox is open
635 if (that
.controlbox
.$el
.css('display') !== 'none') {
636 (new _kiwi
.view
.NickChangeBox()).render();
641 case 'password_mismatch':
642 this.panels
.server
.addMsg(' ', '== Incorrect password given', 'status');
645 // We don't know what data contains, so don't do anything with it.
646 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');