26e9a8ee6112bf9c95b89180d12e47dd61cbada9
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('ctcp_request', onCtcpRequest
, this);
72 this.gateway
.on('ctcp_response', onCtcpResponse
, this);
73 this.gateway
.on('notice', onNotice
, this);
74 this.gateway
.on('action', onAction
, this);
75 this.gateway
.on('topic', onTopic
, this);
76 this.gateway
.on('topicsetby', onTopicSetBy
, this);
77 this.gateway
.on('userlist', onUserlist
, this);
78 this.gateway
.on('userlist_end', onUserlistEnd
, this);
79 this.gateway
.on('mode', onMode
, this);
80 this.gateway
.on('whois', onWhois
, this);
81 this.gateway
.on('away', onAway
, this);
82 this.gateway
.on('list_start', onListStart
, this);
87 * Create panels and join the channel
88 * This will not wait for the join event to create a panel. This
89 * increases responsiveness in case of network lag
91 createAndJoinChannels: function (channels
) {
95 // Multiple channels may come as comma-delimited
96 if (typeof channels
=== 'string') {
97 channels
= channels
.split(',');
100 $.each(channels
, function (index
, channel_name_key
) {
101 // We may have a channel key so split it off
102 var spli
= channel_name_key
.trim().split(' '),
103 channel_name
= spli
[0],
104 channel_key
= spli
[1] || '';
106 // Trim any whitespace off the name
107 channel_name
= channel_name
.trim();
109 // If not a valid channel name, display a warning
110 if (!_kiwi
.app
.isChannelName(channel_name
)) {
111 that
.panels
.server
.addMsg('', channel_name
+ ' is not a valid channel name');
112 _kiwi
.app
.message
.text(channel_name
+ ' is not a valid channel name', {timeout
: 5000});
116 // Check if we have the panel already. If not, create it
117 channel
= that
.panels
.getByName(channel_name
);
119 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
120 that
.panels
.add(channel
);
123 panels
.push(channel
);
125 that
.gateway
.join(channel_name
, channel_key
);
134 function onConnect(event
) {
135 var panels
, channel_names
;
137 // Update our nick with what the network gave us
138 this.set('nick', event
.nick
);
140 // Auto joining channels
141 if (this.auto_join
&& this.auto_join
.channel
) {
142 panels
= this.createAndJoinChannels(this.auto_join
.channel
+ ' ' + (this.auto_join
.channel_key
|| ''));
144 // Show the last channel if we have one
146 panels
[panels
.length
- 1].view
.show();
152 function onOptions(event
) {
155 $.each(event
.options
, function (name
, value
) {
158 that
.set('channel_prefix', value
.join(''));
161 that
.set('name', value
);
164 that
.set('user_prefixes', value
);
169 this.set('cap', event
.cap
);
174 function onMotd(event
) {
175 this.panels
.server
.addMsg(this.get('name'), event
.msg
, 'motd');
180 function onJoin(event
) {
181 var c
, members
, user
;
182 c
= this.panels
.getByName(event
.channel
);
184 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
188 members
= c
.get('members');
189 if (!members
) return;
191 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
197 function onPart(event
) {
198 var channel
, members
, user
,
201 part_options
.type
= 'part';
202 part_options
.message
= event
.message
|| '';
204 channel
= this.panels
.getByName(event
.channel
);
205 if (!channel
) return;
207 // If this is us, close the panel
208 if (event
.nick
=== this.get('nick')) {
213 members
= channel
.get('members');
214 if (!members
) return;
216 user
= members
.getByNick(event
.nick
);
219 members
.remove(user
, part_options
);
224 function onQuit(event
) {
228 quit_options
.type
= 'quit';
229 quit_options
.message
= event
.message
|| '';
231 $.each(this.panels
.models
, function (index
, panel
) {
232 if (!panel
.isChannel()) return;
234 member
= panel
.get('members').getByNick(event
.nick
);
236 panel
.get('members').remove(member
, quit_options
);
243 function onKick(event
) {
244 var channel
, members
, user
,
247 part_options
.type
= 'kick';
248 part_options
.by
= event
.nick
;
249 part_options
.message
= event
.message
|| '';
251 channel
= this.panels
.getByName(event
.channel
);
252 if (!channel
) return;
254 members
= channel
.get('members');
255 if (!members
) return;
257 user
= members
.getByNick(event
.kicked
);
260 members
.remove(user
, part_options
);
262 if (event
.kicked
=== this.get('nick')) {
269 function onMsg(event
) {
271 is_pm
= (event
.channel
== this.get('nick'));
273 // An ignored user? don't do anything with it
274 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
279 // If a panel isn't found for this PM, create one
280 panel
= this.panels
.getByName(event
.nick
);
282 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
283 this.panels
.add(panel
);
287 // If a panel isn't found for this channel, reroute to the
289 panel
= this.panels
.getByName(event
.channel
);
291 panel
= this.panels
.server
;
295 panel
.addMsg(event
.nick
, event
.msg
);
300 function onCtcpRequest(event
) {
301 // An ignored user? don't do anything with it
302 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
306 // Reply to a TIME ctcp
307 if (event
.msg
.toUpperCase() === 'TIME') {
308 this.gateway
.ctcp(false, event
.type
, event
.nick
, (new Date()).toString());
314 function onCtcpResponse(event
) {
315 // An ignored user? don't do anything with it
316 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
320 this.panels
.server
.addMsg('[' + event
.nick
+ ']', 'CTCP ' + event
.msg
);
325 function onNotice(event
) {
328 // An ignored user? don't do anything with it
329 if (event
.nick
&& _kiwi
.gateway
.isNickIgnored(event
.nick
)) {
333 // Find a panel for the destination(channel) or who its from
334 panel
= this.panels
.getByName(event
.target
) || this.panels
.getByName(event
.nick
);
336 panel
= this.panels
.server
;
339 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
344 function onAction(event
) {
346 is_pm
= (event
.channel
== this.get('nick'));
348 // An ignored user? don't do anything with it
349 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
354 // If a panel isn't found for this PM, create one
355 panel
= this.panels
.getByName(event
.nick
);
357 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
358 this.panels
.add(panel
);
362 // If a panel isn't found for this channel, reroute to the
364 panel
= this.panels
.getByName(event
.channel
);
366 panel
= this.panels
.server
;
370 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
375 function onTopic(event
) {
377 c
= this.panels
.getByName(event
.channel
);
380 // Set the channels topic
381 c
.set('topic', event
.topic
);
383 // If this is the active channel, update the topic bar too
384 if (c
.get('name') === this.panels
.active
.get('name')) {
385 _kiwi
.app
.topicbar
.setCurrentTopic(event
.topic
);
391 function onTopicSetBy(event
) {
393 c
= this.panels
.getByName(event
.channel
);
396 when
= formatDate(new Date(event
.when
* 1000));
397 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
402 function onUserlist(event
) {
404 channel
= this.panels
.getByName(event
.channel
);
406 // If we didn't find a channel for this, may aswell leave
407 if (!channel
) return;
409 channel
.temp_userlist
= channel
.temp_userlist
|| [];
410 _
.each(event
.users
, function (item
) {
411 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
412 channel
.temp_userlist
.push(user
);
418 function onUserlistEnd(event
) {
420 channel
= this.panels
.getByName(event
.channel
);
422 // If we didn't find a channel for this, may aswell leave
423 if (!channel
) return;
425 // Update the members list with the new list
426 channel
.get('members').reset(channel
.temp_userlist
|| []);
428 // Clear the temporary userlist
429 delete channel
.temp_userlist
;
434 function onMode(event
) {
435 var channel
, i
, prefixes
, members
, member
, find_prefix
;
437 // Build a nicely formatted string to be displayed to a regular human
438 function friendlyModeString (event_modes
, alt_target
) {
439 var modes
= {}, return_string
;
441 // If no default given, use the main event info
443 event_modes
= event
.modes
;
444 alt_target
= event
.target
;
447 // Reformat the mode object to make it easier to work with
448 _
.each(event_modes
, function (mode
){
449 var param
= mode
.param
|| alt_target
|| '';
451 // Make sure we have some modes for this param
453 modes
[param
] = {'+':'', '-':''};
456 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
459 // Put the string together from each mode
461 _
.each(modes
, function (modeset
, param
) {
463 if (modeset
['+']) str
+= '+' + modeset
['+'];
464 if (modeset
['-']) str
+= '-' + modeset
['-'];
465 return_string
.push(str
+ ' ' + param
);
467 return_string
= return_string
.join(', ');
469 return return_string
;
473 channel
= this.panels
.getByName(event
.target
);
475 prefixes
= this.get('user_prefixes');
476 find_prefix = function (p
) {
477 return event
.modes
[i
].mode
[1] === p
.mode
;
479 for (i
= 0; i
< event
.modes
.length
; i
++) {
480 if (_
.any(prefixes
, find_prefix
)) {
482 members
= channel
.get('members');
484 member
= members
.getByNick(event
.modes
[i
].param
);
486 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
489 if (event
.modes
[i
].mode
[0] === '+') {
490 member
.addMode(event
.modes
[i
].mode
[1]);
491 } else if (event
.modes
[i
].mode
[0] === '-') {
492 member
.removeMode(event
.modes
[i
].mode
[1]);
495 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
498 // Channel mode being set
499 // TODO: Store this somewhere?
500 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
504 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
506 // This is probably a mode being set on us.
507 if (event
.target
.toLowerCase() === this.get("nick").toLowerCase()) {
508 this.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
510 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
517 function onWhois(event
) {
518 var logon_date
, idle_time
= '', panel
;
523 if (typeof event
.idle
!== 'undefined') {
524 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
525 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
528 panel
= _kiwi
.app
.panels
.active
;
530 panel
.addMsg(event
.nick
, event
.nick
+ ' [' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ '] * ' + event
.msg
, 'whois');
531 } else if (event
.chans
) {
532 panel
.addMsg(event
.nick
, 'Channels: ' + event
.chans
, 'whois');
533 } else if (event
.irc_server
) {
534 panel
.addMsg(event
.nick
, 'Connected to server: ' + event
.irc_server
, 'whois');
535 } else if (event
.msg
) {
536 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
537 } else if (event
.logon
) {
538 logon_date
= new Date();
539 logon_date
.setTime(event
.logon
* 1000);
540 logon_date
= formatDate(logon_date
);
542 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
544 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
550 function onAway(event
) {
551 $.each(this.panels
.models
, function (index
, panel
) {
552 if (!panel
.isChannel()) return;
554 member
= panel
.get('members').getByNick(event
.nick
);
556 member
.set('away', !(!event
.trailing
));
563 function onListStart(event
) {
564 var chanlist
= _kiwi
.model
.Applet
.loadOnce('kiwi_chanlist');
565 chanlist
.view
.show();
570 function onIrcError(event
) {
573 if (event
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(event
.channel
))) {
574 panel
= this.panels
.server
;
577 switch (event
.error
) {
578 case 'banned_from_channel':
579 panel
.addMsg(' ', '== You are banned from ' + event
.channel
+ '. ' + event
.reason
, 'status');
580 _kiwi
.app
.message
.text('You are banned from ' + event
.channel
+ '. ' + event
.reason
);
582 case 'bad_channel_key':
583 panel
.addMsg(' ', '== Bad channel key for ' + event
.channel
, 'status');
584 _kiwi
.app
.message
.text('Bad channel key or password for ' + event
.channel
);
586 case 'invite_only_channel':
587 panel
.addMsg(' ', '== ' + event
.channel
+ ' is invite only.', 'status');
588 _kiwi
.app
.message
.text(event
.channel
+ ' is invite only');
590 case 'channel_is_full':
591 panel
.addMsg(' ', '== ' + event
.channel
+ ' is full.', 'status');
592 _kiwi
.app
.message
.text(event
.channel
+ ' is full');
594 case 'chanop_privs_needed':
595 panel
.addMsg(' ', '== ' + event
.reason
, 'status');
596 _kiwi
.app
.message
.text(event
.reason
+ ' (' + event
.channel
+ ')');
599 tmp
= this.panels
.getByName(event
.nick
);
601 tmp
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
603 this.panels
.server
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
606 case 'nickname_in_use':
607 this.panels
.server
.addMsg(' ', '== The nickname ' + event
.nick
+ ' is already in use. Please select a new nickname', 'status');
608 if (this.panels
.server
!== thia
.panels
.active
) {
609 _kiwi
.app
.message
.text('The nickname "' + event
.nick
+ '" is already in use. Please select a new nickname');
612 // Only show the nickchange component if the controlbox is open
613 if (that
.controlbox
.$el
.css('display') !== 'none') {
614 (new _kiwi
.view
.NickChangeBox()).render();
619 case 'password_mismatch':
620 this.panels
.server
.addMsg(' ', '== Incorrect password given', 'status');
623 // We don't know what data contains, so don't do anything with it.
624 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');