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);
57 this.gateway
.on('disconnect', onDisconnect
, this);
59 this.gateway
.on('nick', function(event
) {
60 if (event
.nick
=== this.get('nick')) {
61 this.set('nick', event
.newnick
);
65 this.gateway
.on('options', onOptions
, this);
66 this.gateway
.on('motd', onMotd
, this);
67 this.gateway
.on('join', onJoin
, this);
68 this.gateway
.on('part', onPart
, this);
69 this.gateway
.on('quit', onQuit
, this);
70 this.gateway
.on('kick', onKick
, this);
71 this.gateway
.on('msg', onMsg
, this);
72 this.gateway
.on('nick', onNick
, this);
73 this.gateway
.on('ctcp_request', onCtcpRequest
, this);
74 this.gateway
.on('ctcp_response', onCtcpResponse
, this);
75 this.gateway
.on('notice', onNotice
, this);
76 this.gateway
.on('action', onAction
, this);
77 this.gateway
.on('topic', onTopic
, this);
78 this.gateway
.on('topicsetby', onTopicSetBy
, this);
79 this.gateway
.on('userlist', onUserlist
, this);
80 this.gateway
.on('userlist_end', onUserlistEnd
, this);
81 this.gateway
.on('mode', onMode
, this);
82 this.gateway
.on('whois', onWhois
, this);
83 this.gateway
.on('away', onAway
, this);
84 this.gateway
.on('list_start', onListStart
, this);
89 * Create panels and join the channel
90 * This will not wait for the join event to create a panel. This
91 * increases responsiveness in case of network lag
93 createAndJoinChannels: function (channels
) {
97 // Multiple channels may come as comma-delimited
98 if (typeof channels
=== 'string') {
99 channels
= channels
.split(',');
102 $.each(channels
, function (index
, channel_name_key
) {
103 // We may have a channel key so split it off
104 var spli
= channel_name_key
.trim().split(' '),
105 channel_name
= spli
[0],
106 channel_key
= spli
[1] || '';
108 // Trim any whitespace off the name
109 channel_name
= channel_name
.trim();
111 // If not a valid channel name, display a warning
112 if (!_kiwi
.app
.isChannelName(channel_name
)) {
113 that
.panels
.server
.addMsg('', channel_name
+ ' is not a valid channel name');
114 _kiwi
.app
.message
.text(channel_name
+ ' is not a valid channel name', {timeout
: 5000});
118 // Check if we have the panel already. If not, create it
119 channel
= that
.panels
.getByName(channel_name
);
121 channel
= new _kiwi
.model
.Channel({name
: channel_name
});
122 that
.panels
.add(channel
);
125 panels
.push(channel
);
127 that
.gateway
.join(channel_name
, channel_key
);
136 function onDisconnect(event
) {
137 $.each(this.panels
.models
, function (index
, panel
) {
138 panel
.addMsg('', 'Disconnected from the IRC network', 'action quit');
144 function onConnect(event
) {
145 var panels
, channel_names
;
147 // Update our nick with what the network gave us
148 this.set('nick', event
.nick
);
150 // Auto joining channels
151 if (this.auto_join
&& this.auto_join
.channel
) {
152 panels
= this.createAndJoinChannels(this.auto_join
.channel
+ ' ' + (this.auto_join
.channel_key
|| ''));
154 // Show the last channel if we have one
156 panels
[panels
.length
- 1].view
.show();
162 function onOptions(event
) {
165 $.each(event
.options
, function (name
, value
) {
168 that
.set('channel_prefix', value
.join(''));
171 that
.set('name', value
);
174 that
.set('user_prefixes', value
);
179 this.set('cap', event
.cap
);
184 function onMotd(event
) {
185 this.panels
.server
.addMsg(this.get('name'), event
.msg
, 'motd');
190 function onJoin(event
) {
191 var c
, members
, user
;
192 c
= this.panels
.getByName(event
.channel
);
194 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
198 members
= c
.get('members');
199 if (!members
) return;
201 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
207 function onPart(event
) {
208 var channel
, members
, user
,
211 part_options
.type
= 'part';
212 part_options
.message
= event
.message
|| '';
214 channel
= this.panels
.getByName(event
.channel
);
215 if (!channel
) return;
217 // If this is us, close the panel
218 if (event
.nick
=== this.get('nick')) {
223 members
= channel
.get('members');
224 if (!members
) return;
226 user
= members
.getByNick(event
.nick
);
229 members
.remove(user
, part_options
);
234 function onQuit(event
) {
238 quit_options
.type
= 'quit';
239 quit_options
.message
= event
.message
|| '';
241 $.each(this.panels
.models
, function (index
, panel
) {
242 if (!panel
.isChannel()) return;
244 member
= panel
.get('members').getByNick(event
.nick
);
246 panel
.get('members').remove(member
, quit_options
);
253 function onKick(event
) {
254 var channel
, members
, user
,
257 part_options
.type
= 'kick';
258 part_options
.by
= event
.nick
;
259 part_options
.message
= event
.message
|| '';
261 channel
= this.panels
.getByName(event
.channel
);
262 if (!channel
) return;
264 members
= channel
.get('members');
265 if (!members
) return;
267 user
= members
.getByNick(event
.kicked
);
270 members
.remove(user
, part_options
);
272 if (event
.kicked
=== this.get('nick')) {
279 function onMsg(event
) {
281 is_pm
= (event
.channel
== this.get('nick'));
283 // An ignored user? don't do anything with it
284 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
289 // If a panel isn't found for this PM, create one
290 panel
= this.panels
.getByName(event
.nick
);
292 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
293 this.panels
.add(panel
);
297 // If a panel isn't found for this channel, reroute to the
299 panel
= this.panels
.getByName(event
.channel
);
301 panel
= this.panels
.server
;
305 panel
.addMsg(event
.nick
, event
.msg
);
310 function onNick(event
) {
313 $.each(this.panels
.models
, function (index
, panel
) {
314 if (panel
.get('name') == event
.nick
)
315 panel
.set('name', event
.newnick
);
317 if (!panel
.isChannel()) return;
319 member
= panel
.get('members').getByNick(event
.nick
);
321 member
.set('nick', event
.newnick
);
322 panel
.addMsg('', '== ' + event
.nick
+ ' is now known as ' + event
.newnick
, 'action nick');
329 function onCtcpRequest(event
) {
330 // An ignored user? don't do anything with it
331 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
335 // Reply to a TIME ctcp
336 if (event
.msg
.toUpperCase() === 'TIME') {
337 this.gateway
.ctcp(null, false, event
.type
, event
.nick
, (new Date()).toString());
343 function onCtcpResponse(event
) {
344 // An ignored user? don't do anything with it
345 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
349 this.panels
.server
.addMsg('[' + event
.nick
+ ']', 'CTCP ' + event
.msg
);
354 function onNotice(event
) {
357 // An ignored user? don't do anything with it
358 if (event
.nick
&& _kiwi
.gateway
.isNickIgnored(event
.nick
)) {
362 // Find a panel for the destination(channel) or who its from
363 panel
= this.panels
.getByName(event
.target
) || this.panels
.getByName(event
.nick
);
365 panel
= this.panels
.server
;
368 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
370 // Show this notice to the active panel if it didn't have a set target
371 if (panel
=== this.panels
.server
)
372 _kiwi
.app
.panels().active
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
377 function onAction(event
) {
379 is_pm
= (event
.channel
== this.get('nick'));
381 // An ignored user? don't do anything with it
382 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
387 // If a panel isn't found for this PM, create one
388 panel
= this.panels
.getByName(event
.nick
);
390 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
391 this.panels
.add(panel
);
395 // If a panel isn't found for this channel, reroute to the
397 panel
= this.panels
.getByName(event
.channel
);
399 panel
= this.panels
.server
;
403 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
408 function onTopic(event
) {
410 c
= this.panels
.getByName(event
.channel
);
413 // Set the channels topic
414 c
.set('topic', event
.topic
);
416 // If this is the active channel, update the topic bar too
417 if (c
.get('name') === this.panels
.active
.get('name')) {
418 _kiwi
.app
.topicbar
.setCurrentTopic(event
.topic
);
424 function onTopicSetBy(event
) {
426 c
= this.panels
.getByName(event
.channel
);
429 when
= formatDate(new Date(event
.when
* 1000));
430 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
435 function onUserlist(event
) {
437 channel
= this.panels
.getByName(event
.channel
);
439 // If we didn't find a channel for this, may aswell leave
440 if (!channel
) return;
442 channel
.temp_userlist
= channel
.temp_userlist
|| [];
443 _
.each(event
.users
, function (item
) {
444 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
445 channel
.temp_userlist
.push(user
);
451 function onUserlistEnd(event
) {
453 channel
= this.panels
.getByName(event
.channel
);
455 // If we didn't find a channel for this, may aswell leave
456 if (!channel
) return;
458 // Update the members list with the new list
459 channel
.get('members').reset(channel
.temp_userlist
|| []);
461 // Clear the temporary userlist
462 delete channel
.temp_userlist
;
467 function onMode(event
) {
468 var channel
, i
, prefixes
, members
, member
, find_prefix
;
470 // Build a nicely formatted string to be displayed to a regular human
471 function friendlyModeString (event_modes
, alt_target
) {
472 var modes
= {}, return_string
;
474 // If no default given, use the main event info
476 event_modes
= event
.modes
;
477 alt_target
= event
.target
;
480 // Reformat the mode object to make it easier to work with
481 _
.each(event_modes
, function (mode
){
482 var param
= mode
.param
|| alt_target
|| '';
484 // Make sure we have some modes for this param
486 modes
[param
] = {'+':'', '-':''};
489 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
492 // Put the string together from each mode
494 _
.each(modes
, function (modeset
, param
) {
496 if (modeset
['+']) str
+= '+' + modeset
['+'];
497 if (modeset
['-']) str
+= '-' + modeset
['-'];
498 return_string
.push(str
+ ' ' + param
);
500 return_string
= return_string
.join(', ');
502 return return_string
;
506 channel
= this.panels
.getByName(event
.target
);
508 prefixes
= this.get('user_prefixes');
509 find_prefix = function (p
) {
510 return event
.modes
[i
].mode
[1] === p
.mode
;
512 for (i
= 0; i
< event
.modes
.length
; i
++) {
513 if (_
.any(prefixes
, find_prefix
)) {
515 members
= channel
.get('members');
517 member
= members
.getByNick(event
.modes
[i
].param
);
519 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
522 if (event
.modes
[i
].mode
[0] === '+') {
523 member
.addMode(event
.modes
[i
].mode
[1]);
524 } else if (event
.modes
[i
].mode
[0] === '-') {
525 member
.removeMode(event
.modes
[i
].mode
[1]);
528 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
531 // Channel mode being set
532 // TODO: Store this somewhere?
533 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
537 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
539 // This is probably a mode being set on us.
540 if (event
.target
.toLowerCase() === this.get("nick").toLowerCase()) {
541 this.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
543 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
550 function onWhois(event
) {
551 var logon_date
, idle_time
= '', panel
;
556 if (typeof event
.idle
!== 'undefined') {
557 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
558 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
561 panel
= _kiwi
.app
.panels().active
;
563 panel
.addMsg(event
.nick
, event
.nick
+ ' [' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ '] * ' + event
.msg
, 'whois');
564 } else if (event
.chans
) {
565 panel
.addMsg(event
.nick
, 'Channels: ' + event
.chans
, 'whois');
566 } else if (event
.irc_server
) {
567 panel
.addMsg(event
.nick
, 'Connected to server: ' + event
.irc_server
, 'whois');
568 } else if (event
.msg
) {
569 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
570 } else if (event
.logon
) {
571 logon_date
= new Date();
572 logon_date
.setTime(event
.logon
* 1000);
573 logon_date
= formatDate(logon_date
);
575 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
576 } else if (event
.away_reason
) {
577 panel
.addMsg(event
.nick
, 'Away: ' + event
.away_reason
, 'whois');
579 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
585 function onAway(event
) {
586 $.each(this.panels
.models
, function (index
, panel
) {
587 if (!panel
.isChannel()) return;
589 member
= panel
.get('members').getByNick(event
.nick
);
591 member
.set('away', !(!event
.trailing
));
598 function onListStart(event
) {
599 var chanlist
= _kiwi
.model
.Applet
.loadOnce('kiwi_chanlist');
600 chanlist
.view
.show();
605 function onIrcError(event
) {
608 if (event
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(event
.channel
))) {
609 panel
= this.panels
.server
;
612 switch (event
.error
) {
613 case 'banned_from_channel':
614 panel
.addMsg(' ', '== You are banned from ' + event
.channel
+ '. ' + event
.reason
, 'status');
615 _kiwi
.app
.message
.text('You are banned from ' + event
.channel
+ '. ' + event
.reason
);
617 case 'bad_channel_key':
618 panel
.addMsg(' ', '== Bad channel key for ' + event
.channel
, 'status');
619 _kiwi
.app
.message
.text('Bad channel key or password for ' + event
.channel
);
621 case 'invite_only_channel':
622 panel
.addMsg(' ', '== ' + event
.channel
+ ' is invite only.', 'status');
623 _kiwi
.app
.message
.text(event
.channel
+ ' is invite only');
625 case 'channel_is_full':
626 panel
.addMsg(' ', '== ' + event
.channel
+ ' is full.', 'status');
627 _kiwi
.app
.message
.text(event
.channel
+ ' is full');
629 case 'chanop_privs_needed':
630 panel
.addMsg(' ', '== ' + event
.reason
, 'status');
631 _kiwi
.app
.message
.text(event
.reason
+ ' (' + event
.channel
+ ')');
634 tmp
= this.panels
.getByName(event
.nick
);
636 tmp
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
638 this.panels
.server
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
641 case 'nickname_in_use':
642 this.panels
.server
.addMsg(' ', '== The nickname ' + event
.nick
+ ' is already in use. Please select a new nickname', 'status');
643 if (this.panels
.server
!== thia
.panels
.active
) {
644 _kiwi
.app
.message
.text('The nickname "' + event
.nick
+ '" is already in use. Please select a new nickname');
647 // Only show the nickchange component if the controlbox is open
648 if (that
.controlbox
.$el
.css('display') !== 'none') {
649 (new _kiwi
.view
.NickChangeBox()).render();
654 case 'password_mismatch':
655 this.panels
.server
.addMsg(' ', '== Incorrect password given', 'status');
658 // We don't know what data contains, so don't do anything with it.
659 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');