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