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