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 this.panels
= new _kiwi
.model
.PanelList([], this);
43 this.panels
.network
= this;
45 // Automatically create a server tab
46 var server_panel
= new _kiwi
.model
.Server({name
: 'Server'});
47 this.panels
.add(server_panel
);
48 this.panels
.server
= server_panel
;
52 bindGatewayEvents: function () {
53 //this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);});
55 this.gateway
.on('connect', function(event
) {
56 this.set('nick', event
.nick
);
59 this.gateway
.on('nick', function(event
) {
60 if (event
.nick
=== event
.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('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);
89 function onOptions(event
) {
92 $.each(event
.options
, function (name
, value
) {
95 that
.set('channel_prefix', value
.join(''));
98 that
.set('name', value
);
101 that
.set('user_prefixes', value
);
106 this.set('cap', event
.cap
);
111 function onMotd(event
) {
112 this.panels
.server
.addMsg(this.get('name'), event
.msg
, 'motd');
117 function onJoin(event
) {
118 var c
, members
, user
;
119 c
= this.panels
.getByName(event
.channel
);
121 c
= new _kiwi
.model
.Channel({name
: event
.channel
});
125 members
= c
.get('members');
126 if (!members
) return;
128 user
= new _kiwi
.model
.Member({nick
: event
.nick
, ident
: event
.ident
, hostname
: event
.hostname
});
134 function onPart(event
) {
135 var channel
, members
, user
,
138 part_options
.type
= 'part';
139 part_options
.message
= event
.message
|| '';
141 channel
= this.panels
.getByName(event
.channel
);
142 if (!channel
) return;
144 // If this is us, close the panel
145 if (event
.nick
=== this.get('nick')) {
150 members
= channel
.get('members');
151 if (!members
) return;
153 user
= members
.getByNick(event
.nick
);
156 members
.remove(user
, part_options
);
161 function onQuit(event
) {
165 quit_options
.type
= 'quit';
166 quit_options
.message
= event
.message
|| '';
168 $.each(this.panels
.models
, function (index
, panel
) {
169 if (!panel
.isChannel()) return;
171 member
= panel
.get('members').getByNick(event
.nick
);
173 panel
.get('members').remove(member
, quit_options
);
180 function onKick(event
) {
181 var channel
, members
, user
,
184 part_options
.type
= 'kick';
185 part_options
.by
= event
.nick
;
186 part_options
.message
= event
.message
|| '';
188 channel
= this.panels
.getByName(event
.channel
);
189 if (!channel
) return;
191 members
= channel
.get('members');
192 if (!members
) return;
194 user
= members
.getByNick(event
.kicked
);
197 members
.remove(user
, part_options
);
199 if (event
.kicked
=== this.get('nick')) {
206 function onMsg(event
) {
208 is_pm
= (event
.channel
== this.get('nick'));
210 // An ignored user? don't do anything with it
211 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
216 // If a panel isn't found for this PM, create one
217 panel
= this.panels
.getByName(event
.nick
);
219 panel
= new _kiwi
.model
.Query({name
: event
.nick
});
220 this.panels
.add(panel
);
224 // If a panel isn't found for this channel, reroute to the
226 panel
= this.panels
.getByName(event
.channel
);
228 panel
= this.panels
.server
;
232 panel
.addMsg(event
.nick
, event
.msg
);
237 function onCtcpRequest(event
) {
238 // An ignored user? don't do anything with it
239 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
243 // Reply to a TIME ctcp
244 if (event
.msg
.toUpperCase() === 'TIME') {
245 this.gateway
.ctcp(false, event
.type
, event
.nick
, (new Date()).toString());
251 function onCtcpResponse(event
) {
252 // An ignored user? don't do anything with it
253 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
257 this.panels
.server
.addMsg('[' + event
.nick
+ ']', 'CTCP ' + event
.msg
);
262 function onNotice(event
) {
265 // An ignored user? don't do anything with it
266 if (event
.nick
&& _kiwi
.gateway
.isNickIgnored(event
.nick
)) {
270 // Find a panel for the destination(channel) or who its from
271 panel
= this.panels
.getByName(event
.target
) || this.panels
.getByName(event
.nick
);
273 panel
= this.panels
.server
;
276 panel
.addMsg('[' + (event
.nick
||'') + ']', event
.msg
);
281 function onAction(event
) {
283 is_pm
= (event
.channel
== this.get('nick'));
285 // An ignored user? don't do anything with it
286 if (_kiwi
.gateway
.isNickIgnored(event
.nick
)) {
291 // If a panel isn't found for this PM, create one
292 panel
= this.panels
.getByName(event
.nick
);
294 panel
= new _kiwi
.model
.Channel({name
: event
.nick
});
295 this.panels
.add(panel
);
299 // If a panel isn't found for this channel, reroute to the
301 panel
= this.panels
.getByName(event
.channel
);
303 panel
= this.panels
.server
;
307 panel
.addMsg('', '* ' + event
.nick
+ ' ' + event
.msg
, 'action');
312 function onTopic(event
) {
314 c
= this.panels
.getByName(event
.channel
);
317 // Set the channels topic
318 c
.set('topic', event
.topic
);
320 // If this is the active channel, update the topic bar too
321 if (c
.get('name') === this.panels
.active
.get('name')) {
322 _kiwi
.app
.topicbar
.setCurrentTopic(event
.topic
);
328 function onTopicSetBy(event
) {
330 c
= this.panels
.getByName(event
.channel
);
333 when
= formatDate(new Date(event
.when
* 1000));
334 c
.addMsg('', 'Topic set by ' + event
.nick
+ ' at ' + when
, 'topic');
339 function onUserlist(event
) {
341 channel
= this.panels
.getByName(event
.channel
);
343 // If we didn't find a channel for this, may aswell leave
344 if (!channel
) return;
346 channel
.temp_userlist
= channel
.temp_userlist
|| [];
347 _
.each(event
.users
, function (item
) {
348 var user
= new _kiwi
.model
.Member({nick
: item
.nick
, modes
: item
.modes
});
349 channel
.temp_userlist
.push(user
);
355 function onUserlistEnd(event
) {
357 channel
= this.panels
.getByName(event
.channel
);
359 // If we didn't find a channel for this, may aswell leave
360 if (!channel
) return;
362 // Update the members list with the new list
363 channel
.get('members').reset(channel
.temp_userlist
|| []);
365 // Clear the temporary userlist
366 delete channel
.temp_userlist
;
371 function onMode(event
) {
372 var channel
, i
, prefixes
, members
, member
, find_prefix
;
374 // Build a nicely formatted string to be displayed to a regular human
375 function friendlyModeString (event_modes
, alt_target
) {
376 var modes
= {}, return_string
;
378 // If no default given, use the main event info
380 event_modes
= event
.modes
;
381 alt_target
= event
.target
;
384 // Reformat the mode object to make it easier to work with
385 _
.each(event_modes
, function (mode
){
386 var param
= mode
.param
|| alt_target
|| '';
388 // Make sure we have some modes for this param
390 modes
[param
] = {'+':'', '-':''};
393 modes
[param
][mode
.mode
[0]] += mode
.mode
.substr(1);
396 // Put the string together from each mode
398 _
.each(modes
, function (modeset
, param
) {
400 if (modeset
['+']) str
+= '+' + modeset
['+'];
401 if (modeset
['-']) str
+= '-' + modeset
['-'];
402 return_string
.push(str
+ ' ' + param
);
404 return_string
= return_string
.join(', ');
406 return return_string
;
410 channel
= this.panels
.getByName(event
.target
);
412 prefixes
= this.get('user_prefixes');
413 find_prefix = function (p
) {
414 return event
.modes
[i
].mode
[1] === p
.mode
;
416 for (i
= 0; i
< event
.modes
.length
; i
++) {
417 if (_
.any(prefixes
, find_prefix
)) {
419 members
= channel
.get('members');
421 member
= members
.getByNick(event
.modes
[i
].param
);
423 console
.log('MODE command recieved for unknown member %s on channel %s', event
.modes
[i
].param
, event
.target
);
426 if (event
.modes
[i
].mode
[0] === '+') {
427 member
.addMode(event
.modes
[i
].mode
[1]);
428 } else if (event
.modes
[i
].mode
[0] === '-') {
429 member
.removeMode(event
.modes
[i
].mode
[1]);
432 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');
435 // Channel mode being set
436 // TODO: Store this somewhere?
437 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');
441 channel
.addMsg('', '== ' + event
.nick
+ ' sets mode ' + friendlyModeString(), 'action mode');
443 // This is probably a mode being set on us.
444 if (event
.target
.toLowerCase() === this.get("nick").toLowerCase()) {
445 this.panels
.server
.addMsg('', '== ' + event
.nick
+ ' set mode ' + friendlyModeString(), 'action mode');
447 console
.log('MODE command recieved for unknown target %s: ', event
.target
, event
);
454 function onWhois(event
) {
455 var logon_date
, idle_time
= '', panel
;
460 if (typeof event
.idle
!== 'undefined') {
461 idle_time
= secondsToTime(parseInt(event
.idle
, 10));
462 idle_time
= idle_time
.h
.toString().lpad(2, "0") + ':' + idle_time
.m
.toString().lpad(2, "0") + ':' + idle_time
.s
.toString().lpad(2, "0");
465 panel
= _kiwi
.app
.panels
.active
;
467 panel
.addMsg(event
.nick
, event
.nick
+ ' [' + event
.nick
+ '!' + event
.ident
+ '@' + event
.host
+ '] * ' + event
.msg
, 'whois');
468 } else if (event
.chans
) {
469 panel
.addMsg(event
.nick
, 'Channels: ' + event
.chans
, 'whois');
470 } else if (event
.irc_server
) {
471 panel
.addMsg(event
.nick
, 'Connected to server: ' + event
.irc_server
, 'whois');
472 } else if (event
.msg
) {
473 panel
.addMsg(event
.nick
, event
.msg
, 'whois');
474 } else if (event
.logon
) {
475 logon_date
= new Date();
476 logon_date
.setTime(event
.logon
* 1000);
477 logon_date
= formatDate(logon_date
);
479 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
+ ', signed on ' + logon_date
, 'whois');
481 panel
.addMsg(event
.nick
, 'idle for ' + idle_time
, 'whois');
487 function onAway(event
) {
488 $.each(this.panels
.models
, function (index
, panel
) {
489 if (!panel
.isChannel()) return;
491 member
= panel
.get('members').getByNick(event
.nick
);
493 member
.set('away', !(!event
.trailing
));
500 function onListStart(event
) {
501 var chanlist
= _kiwi
.model
.Applet
.loadOnce('kiwi_chanlist');
502 chanlist
.view
.show();
507 function onIrcError(event
) {
510 if (event
.channel
!== undefined && !(panel
= _kiwi
.app
.panels
.getByName(event
.channel
))) {
511 panel
= this.panels
.server
;
514 switch (event
.error
) {
515 case 'banned_from_channel':
516 panel
.addMsg(' ', '== You are banned from ' + event
.channel
+ '. ' + event
.reason
, 'status');
517 _kiwi
.app
.message
.text('You are banned from ' + event
.channel
+ '. ' + event
.reason
);
519 case 'bad_channel_key':
520 panel
.addMsg(' ', '== Bad channel key for ' + event
.channel
, 'status');
521 _kiwi
.app
.message
.text('Bad channel key or password for ' + event
.channel
);
523 case 'invite_only_channel':
524 panel
.addMsg(' ', '== ' + event
.channel
+ ' is invite only.', 'status');
525 _kiwi
.app
.message
.text(event
.channel
+ ' is invite only');
527 case 'channel_is_full':
528 panel
.addMsg(' ', '== ' + event
.channel
+ ' is full.', 'status');
529 _kiwi
.app
.message
.text(event
.channel
+ ' is full');
531 case 'chanop_privs_needed':
532 panel
.addMsg(' ', '== ' + event
.reason
, 'status');
533 _kiwi
.app
.message
.text(event
.reason
+ ' (' + event
.channel
+ ')');
536 tmp
= this.panels
.getByName(event
.nick
);
538 tmp
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
540 this.panels
.server
.addMsg(' ', '== ' + event
.nick
+ ': ' + event
.reason
, 'status');
543 case 'nickname_in_use':
544 this.panels
.server
.addMsg(' ', '== The nickname ' + event
.nick
+ ' is already in use. Please select a new nickname', 'status');
545 if (this.panels
.server
!== thia
.panels
.active
) {
546 _kiwi
.app
.message
.text('The nickname "' + event
.nick
+ '" is already in use. Please select a new nickname');
549 // Only show the nickchange component if the controlbox is open
550 if (that
.controlbox
.$el
.css('display') !== 'none') {
551 (new _kiwi
.view
.NickChangeBox()).render();
556 case 'password_mismatch':
557 this.panels
.server
.addMsg(' ', '== Incorrect password given', 'status');
560 // We don't know what data contains, so don't do anything with it.
561 //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');