| 1 | _kiwi.model.Application = function () {\r |
| 2 | // Set to a reference to this object within initialize()\r |
| 3 | var that = null;\r |
| 4 | \r |
| 5 | // The auto connect details entered into the server select box\r |
| 6 | var auto_connect_details = {};\r |
| 7 | \r |
| 8 | \r |
| 9 | var model = function () {\r |
| 10 | /** Instance of _kiwi.model.PanelList */\r |
| 11 | this.panels = null;\r |
| 12 | \r |
| 13 | /** _kiwi.view.Application */\r |
| 14 | this.view = null;\r |
| 15 | \r |
| 16 | /** _kiwi.view.StatusMessage */\r |
| 17 | this.message = null;\r |
| 18 | \r |
| 19 | /* Address for the kiwi server */\r |
| 20 | this.kiwi_server = null;\r |
| 21 | \r |
| 22 | this.initialize = function (options) {\r |
| 23 | that = this;\r |
| 24 | \r |
| 25 | if (options[0].container) {\r |
| 26 | this.set('container', options[0].container);\r |
| 27 | }\r |
| 28 | \r |
| 29 | // The base url to the kiwi server\r |
| 30 | this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');\r |
| 31 | \r |
| 32 | // Any options sent down from the server\r |
| 33 | this.server_settings = options[0].server_settings || {};\r |
| 34 | \r |
| 35 | // Best guess at where the kiwi server is\r |
| 36 | this.detectKiwiServer();\r |
| 37 | };\r |
| 38 | \r |
| 39 | this.start = function () {\r |
| 40 | // Only debug if set in the querystring\r |
| 41 | if (!getQueryVariable('debug')) {\r |
| 42 | manageDebug(false);\r |
| 43 | } else {\r |
| 44 | //manageDebug(true);\r |
| 45 | }\r |
| 46 | \r |
| 47 | // Set the gateway up\r |
| 48 | _kiwi.gateway = new _kiwi.model.Gateway();\r |
| 49 | this.bindGatewayCommands(_kiwi.gateway);\r |
| 50 | \r |
| 51 | this.initializeClient();\r |
| 52 | this.initializeGlobals();\r |
| 53 | \r |
| 54 | this.view.barsHide(true);\r |
| 55 | \r |
| 56 | this.panels.server.server_login.bind('server_connect', function (event) {\r |
| 57 | var server_login = this,\r |
| 58 | transport_path = '';\r |
| 59 | auto_connect_details = event;\r |
| 60 | \r |
| 61 | server_login.networkConnecting();\r |
| 62 | \r |
| 63 | // Path to get the socket.io transport code\r |
| 64 | transport_path = that.kiwi_server + that.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime());\r |
| 65 | \r |
| 66 | $script(transport_path, function () {\r |
| 67 | if (!window.io) {\r |
| 68 | kiwiServerNotFound();\r |
| 69 | return;\r |
| 70 | }\r |
| 71 | _kiwi.gateway.set('kiwi_server', that.kiwi_server + '/kiwi');\r |
| 72 | _kiwi.gateway.set('nick', event.nick);\r |
| 73 | \r |
| 74 | _kiwi.gateway.connect(event.server, event.port, event.ssl, event.password, function (error) {\r |
| 75 | if (error) {\r |
| 76 | kiwiServerNotFound();\r |
| 77 | }\r |
| 78 | });\r |
| 79 | });\r |
| 80 | });\r |
| 81 | \r |
| 82 | // TODO: Shouldn't really be here but it's not working in the view.. :/\r |
| 83 | // Hack for firefox browers: Focus is not given on this event loop iteration\r |
| 84 | setTimeout(function(){\r |
| 85 | _kiwi.app.panels.server.server_login.$el.find('.nick').select();\r |
| 86 | }, 0);\r |
| 87 | };\r |
| 88 | \r |
| 89 | \r |
| 90 | function kiwiServerNotFound (e) {\r |
| 91 | that.panels.server.server_login.showError();\r |
| 92 | }\r |
| 93 | \r |
| 94 | \r |
| 95 | this.detectKiwiServer = function () {\r |
| 96 | // If running from file, default to localhost:7777 by default\r |
| 97 | if (window.location.protocol === 'file:') {\r |
| 98 | this.kiwi_server = 'http://localhost:7778';\r |
| 99 | } else {\r |
| 100 | // Assume the kiwi server is on the same server\r |
| 101 | this.kiwi_server = window.location.protocol + '//' + window.location.host;\r |
| 102 | }\r |
| 103 | };\r |
| 104 | \r |
| 105 | \r |
| 106 | this.initializeClient = function () {\r |
| 107 | this.view = new _kiwi.view.Application({model: this, el: this.get('container')});\r |
| 108 | \r |
| 109 | /**\r |
| 110 | * Set the UI components up\r |
| 111 | */\r |
| 112 | this.panels = new _kiwi.model.PanelList();\r |
| 113 | \r |
| 114 | this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]});\r |
| 115 | this.bindControllboxCommands(this.controlbox);\r |
| 116 | \r |
| 117 | this.topicbar = new _kiwi.view.TopicBar({el: $('#topic')[0]});\r |
| 118 | \r |
| 119 | new _kiwi.view.AppToolbar({el: $('#toolbar .app_tools')[0]});\r |
| 120 | \r |
| 121 | this.message = new _kiwi.view.StatusMessage({el: $('#status_message')[0]});\r |
| 122 | \r |
| 123 | this.resize_handle = new _kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});\r |
| 124 | \r |
| 125 | this.panels.server.view.show();\r |
| 126 | \r |
| 127 | // Rejigg the UI sizes\r |
| 128 | this.view.doLayout();\r |
| 129 | \r |
| 130 | this.populateDefaultServerSettings();\r |
| 131 | };\r |
| 132 | \r |
| 133 | \r |
| 134 | this.initializeGlobals = function () {\r |
| 135 | _kiwi.global.panels = this.panels;\r |
| 136 | \r |
| 137 | _kiwi.global.components.Applet = _kiwi.model.Applet;\r |
| 138 | _kiwi.global.components.Panel =_kiwi.model.Panel;\r |
| 139 | };\r |
| 140 | \r |
| 141 | \r |
| 142 | this.populateDefaultServerSettings = function () {\r |
| 143 | var parts;\r |
| 144 | var defaults = {\r |
| 145 | nick: getQueryVariable('nick') || '',\r |
| 146 | server: '',\r |
| 147 | port: 6667,\r |
| 148 | ssl: false,\r |
| 149 | channel: window.location.hash || '#chat',\r |
| 150 | channel_key: ''\r |
| 151 | };\r |
| 152 | var uricheck;\r |
| 153 | \r |
| 154 | \r |
| 155 | /**\r |
| 156 | * Get any settings set by the server\r |
| 157 | * These settings may be changed in the server selection dialog\r |
| 158 | */\r |
| 159 | if (this.server_settings.client) {\r |
| 160 | if (this.server_settings.client.nick)\r |
| 161 | defaults.nick = this.server_settings.client.nick;\r |
| 162 | \r |
| 163 | if (this.server_settings.client.server)\r |
| 164 | defaults.server = this.server_settings.client.server;\r |
| 165 | \r |
| 166 | if (this.server_settings.client.port)\r |
| 167 | defaults.port = this.server_settings.client.port;\r |
| 168 | \r |
| 169 | if (this.server_settings.client.ssl)\r |
| 170 | defaults.ssl = this.server_settings.client.ssl;\r |
| 171 | \r |
| 172 | if (this.server_settings.client.channel)\r |
| 173 | defaults.channel = this.server_settings.client.channel;\r |
| 174 | }\r |
| 175 | \r |
| 176 | \r |
| 177 | \r |
| 178 | /**\r |
| 179 | * Get any settings passed in the URL\r |
| 180 | * These settings may be changed in the server selection dialog\r |
| 181 | */\r |
| 182 | \r |
| 183 | // Process the URL part by part, extracting as we go\r |
| 184 | parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');\r |
| 185 | \r |
| 186 | if (parts.length > 0) {\r |
| 187 | parts.shift();\r |
| 188 | \r |
| 189 | if (parts.length > 0 && parts[0]) {\r |
| 190 | // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path.\r |
| 191 | uricheck = parts[0].substr(0, 7).toLowerCase();\r |
| 192 | if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {\r |
| 193 | parts[0] = decodeURIComponent(parts[0]);\r |
| 194 | // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r |
| 195 | uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r |
| 196 | /*\r |
| 197 | uricheck[1] = ssl (optional)\r |
| 198 | uricheck[2] = host\r |
| 199 | uricheck[3] = port (optional)\r |
| 200 | uricheck[4] = channel (optional)\r |
| 201 | uricheck[5] = channel key (optional, channel must also be set)\r |
| 202 | */\r |
| 203 | if (uricheck) {\r |
| 204 | if (typeof uricheck[1] !== 'undefined') {\r |
| 205 | defaults.ssl = true;\r |
| 206 | if (defaults.port === 6667) {\r |
| 207 | defaults.port = 6697;\r |
| 208 | }\r |
| 209 | }\r |
| 210 | defaults.server = uricheck[2];\r |
| 211 | if (typeof uricheck[3] !== 'undefined') {\r |
| 212 | defaults.port = uricheck[3];\r |
| 213 | }\r |
| 214 | if (typeof uricheck[4] !== 'undefined') {\r |
| 215 | defaults.channel = '#' + uricheck[4];\r |
| 216 | if (typeof uricheck[5] !== 'undefined') {\r |
| 217 | defaults.channel_key = uricheck[5];\r |
| 218 | }\r |
| 219 | }\r |
| 220 | }\r |
| 221 | parts = [];\r |
| 222 | } else {\r |
| 223 | // Extract the port+ssl if we find one\r |
| 224 | if (parts[0].search(/:/) > 0) {\r |
| 225 | defaults.port = parts[0].substring(parts[0].search(/:/) + 1);\r |
| 226 | defaults.server = parts[0].substring(0, parts[0].search(/:/));\r |
| 227 | if (defaults.port[0] === '+') {\r |
| 228 | defaults.port = parseInt(defaults.port.substring(1), 10);\r |
| 229 | defaults.ssl = true;\r |
| 230 | } else {\r |
| 231 | defaults.ssl = false;\r |
| 232 | }\r |
| 233 | \r |
| 234 | } else {\r |
| 235 | defaults.server = parts[0];\r |
| 236 | }\r |
| 237 | \r |
| 238 | parts.shift();\r |
| 239 | }\r |
| 240 | }\r |
| 241 | \r |
| 242 | if (parts.length > 0 && parts[0]) {\r |
| 243 | defaults.channel = '#' + parts[0];\r |
| 244 | parts.shift();\r |
| 245 | }\r |
| 246 | }\r |
| 247 | \r |
| 248 | // If any settings have been given by the server.. override any auto detected settings\r |
| 249 | /**\r |
| 250 | * Get any server restrictions as set in the server config\r |
| 251 | * These settings can not be changed in the server selection dialog\r |
| 252 | */\r |
| 253 | if (this.server_settings && this.server_settings.connection) {\r |
| 254 | if (this.server_settings.connection.server) {\r |
| 255 | defaults.server = this.server_settings.connection.server;\r |
| 256 | }\r |
| 257 | \r |
| 258 | if (this.server_settings.connection.port) {\r |
| 259 | defaults.port = this.server_settings.connection.port;\r |
| 260 | }\r |
| 261 | \r |
| 262 | if (this.server_settings.connection.ssl) {\r |
| 263 | defaults.ssl = this.server_settings.connection.ssl;\r |
| 264 | }\r |
| 265 | \r |
| 266 | if (this.server_settings.connection.channel) {\r |
| 267 | defaults.channel = this.server_settings.connection.channel;\r |
| 268 | }\r |
| 269 | \r |
| 270 | if (this.server_settings.connection.nick) {\r |
| 271 | defaults.nick = this.server_settings.connection.nick;\r |
| 272 | }\r |
| 273 | }\r |
| 274 | \r |
| 275 | // Set any random numbers if needed\r |
| 276 | defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());\r |
| 277 | \r |
| 278 | // Populate the server select box with defaults\r |
| 279 | this.panels.server.server_login.populateFields(defaults);\r |
| 280 | };\r |
| 281 | \r |
| 282 | \r |
| 283 | this.bindGatewayCommands = function (gw) {\r |
| 284 | gw.on('onmotd', function (event) {\r |
| 285 | that.panels.server.addMsg(_kiwi.gateway.get('name'), event.msg, 'motd');\r |
| 286 | });\r |
| 287 | \r |
| 288 | \r |
| 289 | gw.on('onconnect', function (event) {\r |
| 290 | that.view.barsShow();\r |
| 291 | \r |
| 292 | if (auto_connect_details.channel) {\r |
| 293 | that.controlbox.processInput('/JOIN ' + auto_connect_details.channel + ' ' + auto_connect_details.channel_key);\r |
| 294 | }\r |
| 295 | });\r |
| 296 | \r |
| 297 | \r |
| 298 | (function () {\r |
| 299 | var gw_stat = 0;\r |
| 300 | \r |
| 301 | gw.on('disconnect', function (event) {\r |
| 302 | var msg = 'You have been disconnected. Attempting to reconnect for you..';\r |
| 303 | that.message.text(msg, {timeout: 10000});\r |
| 304 | \r |
| 305 | // Mention the disconnection on every channel\r |
| 306 | $.each(_kiwi.app.panels.models, function (idx, panel) {\r |
| 307 | if (!panel || !panel.isChannel()) return;\r |
| 308 | panel.addMsg('', msg, 'action quit');\r |
| 309 | });\r |
| 310 | _kiwi.app.panels.server.addMsg('', msg, 'action quit');\r |
| 311 | \r |
| 312 | gw_stat = 1;\r |
| 313 | });\r |
| 314 | gw.on('reconnecting', function (event) {\r |
| 315 | msg = 'You have been disconnected. Attempting to reconnect again in ' + (event.delay/1000) + ' seconds..';\r |
| 316 | _kiwi.app.panels.server.addMsg('', msg, 'action quit');\r |
| 317 | });\r |
| 318 | gw.on('connect', function (event) {\r |
| 319 | if (gw_stat !== 1) return;\r |
| 320 | \r |
| 321 | var msg = 'It\'s OK, you\'re connected again :)';\r |
| 322 | that.message.text(msg, {timeout: 5000});\r |
| 323 | \r |
| 324 | // Mention the disconnection on every channel\r |
| 325 | $.each(_kiwi.app.panels.models, function (idx, panel) {\r |
| 326 | if (!panel || !panel.isChannel()) return;\r |
| 327 | panel.addMsg('', msg, 'action join');\r |
| 328 | });\r |
| 329 | _kiwi.app.panels.server.addMsg('', msg, 'action join');\r |
| 330 | \r |
| 331 | gw_stat = 0;\r |
| 332 | });\r |
| 333 | })();\r |
| 334 | \r |
| 335 | \r |
| 336 | gw.on('onjoin', function (event) {\r |
| 337 | var c, members, user;\r |
| 338 | c = that.panels.getByName(event.channel);\r |
| 339 | if (!c) {\r |
| 340 | c = new _kiwi.model.Channel({name: event.channel});\r |
| 341 | that.panels.add(c);\r |
| 342 | }\r |
| 343 | \r |
| 344 | members = c.get('members');\r |
| 345 | if (!members) return;\r |
| 346 | \r |
| 347 | user = new _kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname});\r |
| 348 | members.add(user);\r |
| 349 | // TODO: highlight the new channel in some way\r |
| 350 | });\r |
| 351 | \r |
| 352 | \r |
| 353 | gw.on('onpart', function (event) {\r |
| 354 | var channel, members, user,\r |
| 355 | part_options = {};\r |
| 356 | \r |
| 357 | part_options.type = 'part';\r |
| 358 | part_options.message = event.message || '';\r |
| 359 | \r |
| 360 | channel = that.panels.getByName(event.channel);\r |
| 361 | if (!channel) return;\r |
| 362 | \r |
| 363 | // If this is us, close the panel\r |
| 364 | if (event.nick === _kiwi.gateway.get('nick')) {\r |
| 365 | channel.close();\r |
| 366 | return;\r |
| 367 | }\r |
| 368 | \r |
| 369 | members = channel.get('members');\r |
| 370 | if (!members) return;\r |
| 371 | \r |
| 372 | user = members.getByNick(event.nick);\r |
| 373 | if (!user) return;\r |
| 374 | \r |
| 375 | members.remove(user, part_options);\r |
| 376 | });\r |
| 377 | \r |
| 378 | \r |
| 379 | gw.on('onquit', function (event) {\r |
| 380 | var member, members,\r |
| 381 | quit_options = {};\r |
| 382 | \r |
| 383 | quit_options.type = 'quit';\r |
| 384 | quit_options.message = event.message || '';\r |
| 385 | \r |
| 386 | $.each(that.panels.models, function (index, panel) {\r |
| 387 | if (!panel.isChannel()) return;\r |
| 388 | \r |
| 389 | member = panel.get('members').getByNick(event.nick);\r |
| 390 | if (member) {\r |
| 391 | panel.get('members').remove(member, quit_options);\r |
| 392 | }\r |
| 393 | });\r |
| 394 | });\r |
| 395 | \r |
| 396 | \r |
| 397 | gw.on('onkick', function (event) {\r |
| 398 | var channel, members, user,\r |
| 399 | part_options = {};\r |
| 400 | \r |
| 401 | part_options.type = 'kick';\r |
| 402 | part_options.by = event.nick;\r |
| 403 | part_options.message = event.message || '';\r |
| 404 | \r |
| 405 | channel = that.panels.getByName(event.channel);\r |
| 406 | if (!channel) return;\r |
| 407 | \r |
| 408 | members = channel.get('members');\r |
| 409 | if (!members) return;\r |
| 410 | \r |
| 411 | user = members.getByNick(event.kicked);\r |
| 412 | if (!user) return;\r |
| 413 | \r |
| 414 | members.remove(user, part_options);\r |
| 415 | \r |
| 416 | if (event.kicked === _kiwi.gateway.get('nick')) {\r |
| 417 | members.reset([]);\r |
| 418 | }\r |
| 419 | \r |
| 420 | });\r |
| 421 | \r |
| 422 | \r |
| 423 | gw.on('onmsg', function (event) {\r |
| 424 | var panel,\r |
| 425 | is_pm = (event.channel == _kiwi.gateway.get('nick'));\r |
| 426 | \r |
| 427 | // An ignored user? don't do anything with it\r |
| 428 | if (gw.isNickIgnored(event.nick)) {\r |
| 429 | return;\r |
| 430 | }\r |
| 431 | \r |
| 432 | if (is_pm) {\r |
| 433 | // If a panel isn't found for this PM, create one\r |
| 434 | panel = that.panels.getByName(event.nick);\r |
| 435 | if (!panel) {\r |
| 436 | panel = new _kiwi.model.Query({name: event.nick});\r |
| 437 | that.panels.add(panel);\r |
| 438 | }\r |
| 439 | \r |
| 440 | } else {\r |
| 441 | // If a panel isn't found for this channel, reroute to the\r |
| 442 | // server panel\r |
| 443 | panel = that.panels.getByName(event.channel);\r |
| 444 | if (!panel) {\r |
| 445 | panel = that.panels.server;\r |
| 446 | }\r |
| 447 | }\r |
| 448 | \r |
| 449 | panel.addMsg(event.nick, event.msg);\r |
| 450 | });\r |
| 451 | \r |
| 452 | \r |
| 453 | gw.on('onctcp_request', function (event) {\r |
| 454 | // An ignored user? don't do anything with it\r |
| 455 | if (gw.isNickIgnored(event.nick)) {\r |
| 456 | return;\r |
| 457 | }\r |
| 458 | \r |
| 459 | // Reply to a TIME ctcp\r |
| 460 | if (event.msg.toUpperCase() === 'TIME') {\r |
| 461 | gw.ctcp(false, event.type, event.nick, (new Date()).toString());\r |
| 462 | }\r |
| 463 | });\r |
| 464 | \r |
| 465 | \r |
| 466 | gw.on('onctcp_response', function (event) {\r |
| 467 | // An ignored user? don't do anything with it\r |
| 468 | if (gw.isNickIgnored(event.nick)) {\r |
| 469 | return;\r |
| 470 | }\r |
| 471 | \r |
| 472 | that.panels.server.addMsg('[' + event.nick + ']', 'CTCP ' + event.msg);\r |
| 473 | });\r |
| 474 | \r |
| 475 | \r |
| 476 | gw.on('onnotice', function (event) {\r |
| 477 | var panel;\r |
| 478 | \r |
| 479 | // An ignored user? don't do anything with it\r |
| 480 | if (event.nick && gw.isNickIgnored(event.nick)) {\r |
| 481 | return;\r |
| 482 | }\r |
| 483 | \r |
| 484 | // Find a panel for the destination(channel) or who its from\r |
| 485 | panel = that.panels.getByName(event.target) || that.panels.getByName(event.nick);\r |
| 486 | if (!panel) {\r |
| 487 | panel = that.panels.server;\r |
| 488 | }\r |
| 489 | \r |
| 490 | panel.addMsg('[' + (event.nick||'') + ']', event.msg);\r |
| 491 | });\r |
| 492 | \r |
| 493 | \r |
| 494 | gw.on('onaction', function (event) {\r |
| 495 | var panel,\r |
| 496 | is_pm = (event.channel == _kiwi.gateway.get('nick'));\r |
| 497 | \r |
| 498 | // An ignored user? don't do anything with it\r |
| 499 | if (gw.isNickIgnored(event.nick)) {\r |
| 500 | return;\r |
| 501 | }\r |
| 502 | \r |
| 503 | if (is_pm) {\r |
| 504 | // If a panel isn't found for this PM, create one\r |
| 505 | panel = that.panels.getByName(event.nick);\r |
| 506 | if (!panel) {\r |
| 507 | panel = new _kiwi.model.Channel({name: event.nick});\r |
| 508 | that.panels.add(panel);\r |
| 509 | }\r |
| 510 | \r |
| 511 | } else {\r |
| 512 | // If a panel isn't found for this channel, reroute to the\r |
| 513 | // server panel\r |
| 514 | panel = that.panels.getByName(event.channel);\r |
| 515 | if (!panel) {\r |
| 516 | panel = that.panels.server;\r |
| 517 | }\r |
| 518 | }\r |
| 519 | \r |
| 520 | panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action');\r |
| 521 | });\r |
| 522 | \r |
| 523 | \r |
| 524 | gw.on('ontopic', function (event) {\r |
| 525 | var c;\r |
| 526 | c = that.panels.getByName(event.channel);\r |
| 527 | if (!c) return;\r |
| 528 | \r |
| 529 | // Set the channels topic\r |
| 530 | c.set('topic', event.topic);\r |
| 531 | \r |
| 532 | // If this is the active channel, update the topic bar too\r |
| 533 | if (c.get('name') === _kiwi.app.panels.active.get('name')) {\r |
| 534 | that.topicbar.setCurrentTopic(event.topic);\r |
| 535 | }\r |
| 536 | });\r |
| 537 | \r |
| 538 | \r |
| 539 | gw.on('ontopicsetby', function (event) {\r |
| 540 | var c, when;\r |
| 541 | c = that.panels.getByName(event.channel);\r |
| 542 | if (!c) return;\r |
| 543 | \r |
| 544 | when = formatDate(new Date(event.when * 1000));\r |
| 545 | c.addMsg('', 'Topic set by ' + event.nick + ' at ' + when, 'topic');\r |
| 546 | });\r |
| 547 | \r |
| 548 | \r |
| 549 | gw.on('onuserlist', function (event) {\r |
| 550 | var channel;\r |
| 551 | channel = that.panels.getByName(event.channel);\r |
| 552 | \r |
| 553 | // If we didn't find a channel for this, may aswell leave\r |
| 554 | if (!channel) return;\r |
| 555 | \r |
| 556 | channel.temp_userlist = channel.temp_userlist || [];\r |
| 557 | _.each(event.users, function (item) {\r |
| 558 | var user = new _kiwi.model.Member({nick: item.nick, modes: item.modes});\r |
| 559 | channel.temp_userlist.push(user);\r |
| 560 | });\r |
| 561 | });\r |
| 562 | \r |
| 563 | \r |
| 564 | gw.on('onuserlist_end', function (event) {\r |
| 565 | var channel;\r |
| 566 | channel = that.panels.getByName(event.channel);\r |
| 567 | \r |
| 568 | // If we didn't find a channel for this, may aswell leave\r |
| 569 | if (!channel) return;\r |
| 570 | \r |
| 571 | // Update the members list with the new list\r |
| 572 | channel.get('members').reset(channel.temp_userlist || []);\r |
| 573 | \r |
| 574 | // Clear the temporary userlist\r |
| 575 | delete channel.temp_userlist;\r |
| 576 | });\r |
| 577 | \r |
| 578 | \r |
| 579 | gw.on('onmode', function (event) {\r |
| 580 | var channel, i, prefixes, members, member, find_prefix;\r |
| 581 | \r |
| 582 | // Build a nicely formatted string to be displayed to a regular human\r |
| 583 | function friendlyModeString (event_modes, alt_target) {\r |
| 584 | var modes = {}, return_string;\r |
| 585 | \r |
| 586 | // If no default given, use the main event info\r |
| 587 | if (!event_modes) {\r |
| 588 | event_modes = event.modes;\r |
| 589 | alt_target = event.target;\r |
| 590 | }\r |
| 591 | \r |
| 592 | // Reformat the mode object to make it easier to work with\r |
| 593 | _.each(event_modes, function (mode){\r |
| 594 | var param = mode.param || alt_target || '';\r |
| 595 | \r |
| 596 | // Make sure we have some modes for this param\r |
| 597 | if (!modes[param]) {\r |
| 598 | modes[param] = {'+':'', '-':''};\r |
| 599 | }\r |
| 600 | \r |
| 601 | modes[param][mode.mode[0]] += mode.mode.substr(1);\r |
| 602 | });\r |
| 603 | \r |
| 604 | // Put the string together from each mode\r |
| 605 | return_string = [];\r |
| 606 | _.each(modes, function (modeset, param) {\r |
| 607 | var str = '';\r |
| 608 | if (modeset['+']) str += '+' + modeset['+'];\r |
| 609 | if (modeset['-']) str += '-' + modeset['-'];\r |
| 610 | return_string.push(str + ' ' + param);\r |
| 611 | });\r |
| 612 | return_string = return_string.join(', ');\r |
| 613 | \r |
| 614 | return return_string;\r |
| 615 | }\r |
| 616 | \r |
| 617 | \r |
| 618 | channel = that.panels.getByName(event.target);\r |
| 619 | if (channel) {\r |
| 620 | prefixes = _kiwi.gateway.get('user_prefixes');\r |
| 621 | find_prefix = function (p) {\r |
| 622 | return event.modes[i].mode[1] === p.mode;\r |
| 623 | };\r |
| 624 | for (i = 0; i < event.modes.length; i++) {\r |
| 625 | if (_.any(prefixes, find_prefix)) {\r |
| 626 | if (!members) {\r |
| 627 | members = channel.get('members');\r |
| 628 | }\r |
| 629 | member = members.getByNick(event.modes[i].param);\r |
| 630 | if (!member) {\r |
| 631 | console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target);\r |
| 632 | return;\r |
| 633 | } else {\r |
| 634 | if (event.modes[i].mode[0] === '+') {\r |
| 635 | member.addMode(event.modes[i].mode[1]);\r |
| 636 | } else if (event.modes[i].mode[0] === '-') {\r |
| 637 | member.removeMode(event.modes[i].mode[1]);\r |
| 638 | }\r |
| 639 | members.sort();\r |
| 640 | //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');\r |
| 641 | }\r |
| 642 | } else {\r |
| 643 | // Channel mode being set\r |
| 644 | // TODO: Store this somewhere?\r |
| 645 | //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');\r |
| 646 | }\r |
| 647 | }\r |
| 648 | \r |
| 649 | channel.addMsg('', '== ' + event.nick + ' sets mode ' + friendlyModeString(), 'action mode');\r |
| 650 | } else {\r |
| 651 | // This is probably a mode being set on us.\r |
| 652 | if (event.target.toLowerCase() === _kiwi.gateway.get("nick").toLowerCase()) {\r |
| 653 | that.panels.server.addMsg('', '== ' + event.nick + ' set mode ' + friendlyModeString(), 'action mode');\r |
| 654 | } else {\r |
| 655 | console.log('MODE command recieved for unknown target %s: ', event.target, event);\r |
| 656 | }\r |
| 657 | }\r |
| 658 | });\r |
| 659 | \r |
| 660 | \r |
| 661 | gw.on('onnick', function (event) {\r |
| 662 | var member;\r |
| 663 | \r |
| 664 | $.each(that.panels.models, function (index, panel) {\r |
| 665 | if (!panel.isChannel()) return;\r |
| 666 | \r |
| 667 | member = panel.get('members').getByNick(event.nick);\r |
| 668 | if (member) {\r |
| 669 | member.set('nick', event.newnick);\r |
| 670 | panel.addMsg('', '== ' + event.nick + ' is now known as ' + event.newnick, 'action nick');\r |
| 671 | }\r |
| 672 | });\r |
| 673 | });\r |
| 674 | \r |
| 675 | \r |
| 676 | gw.on('onwhois', function (event) {\r |
| 677 | /*globals secondsToTime */\r |
| 678 | var logon_date, idle_time = '', panel;\r |
| 679 | \r |
| 680 | if (event.end) {\r |
| 681 | return;\r |
| 682 | }\r |
| 683 | \r |
| 684 | if (typeof event.idle !== 'undefined') {\r |
| 685 | idle_time = secondsToTime(parseInt(event.idle, 10));\r |
| 686 | idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0");\r |
| 687 | }\r |
| 688 | \r |
| 689 | panel = _kiwi.app.panels.active;\r |
| 690 | if (event.ident) {\r |
| 691 | panel.addMsg(event.nick, event.nick + ' [' + event.nick + '!' + event.ident + '@' + event.host + '] * ' + event.msg, 'whois');\r |
| 692 | } else if (event.chans) {\r |
| 693 | panel.addMsg(event.nick, 'Channels: ' + event.chans, 'whois');\r |
| 694 | } else if (event.irc_server) {\r |
| 695 | panel.addMsg(event.nick, 'Connected to server: ' + event.irc_server, 'whois');\r |
| 696 | } else if (event.msg) {\r |
| 697 | panel.addMsg(event.nick, event.msg, 'whois');\r |
| 698 | } else if (event.logon) {\r |
| 699 | logon_date = new Date();\r |
| 700 | logon_date.setTime(event.logon * 1000);\r |
| 701 | logon_date = formatDate(logon_date);\r |
| 702 | \r |
| 703 | panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois');\r |
| 704 | } else {\r |
| 705 | panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois');\r |
| 706 | }\r |
| 707 | });\r |
| 708 | \r |
| 709 | gw.on('onaway', function (event) {\r |
| 710 | $.each(that.panels.models, function (index, panel) {\r |
| 711 | if (!panel.isChannel()) return;\r |
| 712 | \r |
| 713 | member = panel.get('members').getByNick(event.nick);\r |
| 714 | if (member) {\r |
| 715 | member.set('away', !(!event.trailing));\r |
| 716 | }\r |
| 717 | });\r |
| 718 | });\r |
| 719 | \r |
| 720 | \r |
| 721 | gw.on('onlist_start', function (data) {\r |
| 722 | if (_kiwi.app.channel_list) {\r |
| 723 | _kiwi.app.channel_list.close();\r |
| 724 | delete _kiwi.app.channel_list;\r |
| 725 | }\r |
| 726 | \r |
| 727 | var panel = new _kiwi.model.Applet(),\r |
| 728 | applet = new _kiwi.applets.Chanlist();\r |
| 729 | \r |
| 730 | panel.load(applet);\r |
| 731 | \r |
| 732 | _kiwi.app.panels.add(panel);\r |
| 733 | panel.view.show();\r |
| 734 | \r |
| 735 | _kiwi.app.channel_list = applet;\r |
| 736 | });\r |
| 737 | \r |
| 738 | \r |
| 739 | gw.on('onlist_channel', function (data) {\r |
| 740 | // TODO: Put this listener within the applet itself\r |
| 741 | _kiwi.app.channel_list.addChannel(data.chans);\r |
| 742 | });\r |
| 743 | \r |
| 744 | \r |
| 745 | gw.on('onlist_end', function (data) {\r |
| 746 | // TODO: Put this listener within the applet itself\r |
| 747 | delete _kiwi.app.channel_list;\r |
| 748 | });\r |
| 749 | \r |
| 750 | \r |
| 751 | gw.on('onirc_error', function (data) {\r |
| 752 | var panel, tmp;\r |
| 753 | \r |
| 754 | if (data.channel !== undefined && !(panel = _kiwi.app.panels.getByName(data.channel))) {\r |
| 755 | panel = _kiwi.app.panels.server;\r |
| 756 | }\r |
| 757 | \r |
| 758 | switch (data.error) {\r |
| 759 | case 'banned_from_channel':\r |
| 760 | panel.addMsg(' ', '== You are banned from ' + data.channel + '. ' + data.reason, 'status');\r |
| 761 | _kiwi.app.message.text('You are banned from ' + data.channel + '. ' + data.reason);\r |
| 762 | break;\r |
| 763 | case 'bad_channel_key':\r |
| 764 | panel.addMsg(' ', '== Bad channel key for ' + data.channel, 'status');\r |
| 765 | _kiwi.app.message.text('Bad channel key or password for ' + data.channel);\r |
| 766 | break;\r |
| 767 | case 'invite_only_channel':\r |
| 768 | panel.addMsg(' ', '== ' + data.channel + ' is invite only.', 'status');\r |
| 769 | _kiwi.app.message.text(data.channel + ' is invite only');\r |
| 770 | break;\r |
| 771 | case 'channel_is_full':\r |
| 772 | panel.addMsg(' ', '== ' + data.channel + ' is full.', 'status');\r |
| 773 | _kiwi.app.message.text(data.channel + ' is full');\r |
| 774 | break;\r |
| 775 | case 'chanop_privs_needed':\r |
| 776 | panel.addMsg(' ', '== ' + data.reason, 'status');\r |
| 777 | _kiwi.app.message.text(data.reason + ' (' + data.channel + ')');\r |
| 778 | break;\r |
| 779 | case 'no_such_nick':\r |
| 780 | tmp = _kiwi.app.panels.getByName(data.nick);\r |
| 781 | if (tmp) {\r |
| 782 | tmp.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');\r |
| 783 | } else {\r |
| 784 | _kiwi.app.panels.server.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');\r |
| 785 | }\r |
| 786 | break;\r |
| 787 | case 'nickname_in_use':\r |
| 788 | _kiwi.app.panels.server.addMsg(' ', '== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');\r |
| 789 | if (_kiwi.app.panels.server !== _kiwi.app.panels.active) {\r |
| 790 | _kiwi.app.message.text('The nickname "' + data.nick + '" is already in use. Please select a new nickname');\r |
| 791 | }\r |
| 792 | \r |
| 793 | // Only show the nickchange component if the controlbox is open\r |
| 794 | if (that.controlbox.$el.css('display') !== 'none') {\r |
| 795 | (new _kiwi.view.NickChangeBox()).render();\r |
| 796 | }\r |
| 797 | \r |
| 798 | case 'password_mismatch':\r |
| 799 | _kiwi.app.panels.server.addMsg(' ', '== Incorrect password given', 'status');\r |
| 800 | break;\r |
| 801 | default:\r |
| 802 | // We don't know what data contains, so don't do anything with it.\r |
| 803 | //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');\r |
| 804 | }\r |
| 805 | });\r |
| 806 | };\r |
| 807 | \r |
| 808 | \r |
| 809 | \r |
| 810 | /**\r |
| 811 | * Bind to certain commands that may be typed into the control box\r |
| 812 | */\r |
| 813 | this.bindControllboxCommands = function (controlbox) {\r |
| 814 | // Default aliases\r |
| 815 | $.extend(controlbox.preprocessor.aliases, {\r |
| 816 | // General aliases\r |
| 817 | '/p': '/part $1+',\r |
| 818 | '/me': '/action $1+',\r |
| 819 | '/j': '/join $1+',\r |
| 820 | '/q': '/query $1+',\r |
| 821 | \r |
| 822 | // Op related aliases\r |
| 823 | '/op': '/quote mode $channel +o $1+',\r |
| 824 | '/deop': '/quote mode $channel -o $1+',\r |
| 825 | '/hop': '/quote mode $channel +h $1+',\r |
| 826 | '/dehop': '/quote mode $channel -h $1+',\r |
| 827 | '/voice': '/quote mode $channel +v $1+',\r |
| 828 | '/devoice': '/quote mode $channel -v $1+',\r |
| 829 | '/k': '/kick $channel $1+',\r |
| 830 | \r |
| 831 | // Misc aliases\r |
| 832 | '/slap': '/me slaps $1 around a bit with a large trout'\r |
| 833 | });\r |
| 834 | \r |
| 835 | controlbox.on('unknown_command', unknownCommand);\r |
| 836 | \r |
| 837 | controlbox.on('command', allCommands);\r |
| 838 | controlbox.on('command:msg', msgCommand);\r |
| 839 | \r |
| 840 | controlbox.on('command:action', actionCommand);\r |
| 841 | \r |
| 842 | controlbox.on('command:join', joinCommand);\r |
| 843 | \r |
| 844 | controlbox.on('command:part', partCommand);\r |
| 845 | \r |
| 846 | controlbox.on('command:nick', function (ev) {\r |
| 847 | _kiwi.gateway.changeNick(ev.params[0]);\r |
| 848 | });\r |
| 849 | \r |
| 850 | controlbox.on('command:query', queryCommand);\r |
| 851 | \r |
| 852 | controlbox.on('command:topic', topicCommand);\r |
| 853 | \r |
| 854 | controlbox.on('command:notice', noticeCommand);\r |
| 855 | \r |
| 856 | controlbox.on('command:quote', quoteCommand);\r |
| 857 | \r |
| 858 | controlbox.on('command:kick', kickCommand);\r |
| 859 | \r |
| 860 | controlbox.on('command:clear', clearCommand);\r |
| 861 | \r |
| 862 | controlbox.on('command:ctcp', ctcpCommand);\r |
| 863 | \r |
| 864 | \r |
| 865 | controlbox.on('command:css', function (ev) {\r |
| 866 | var queryString = '?reload=' + new Date().getTime();\r |
| 867 | $('link[rel="stylesheet"]').each(function () {\r |
| 868 | this.href = this.href.replace(/\?.*|$/, queryString);\r |
| 869 | });\r |
| 870 | });\r |
| 871 | \r |
| 872 | controlbox.on('command:js', function (ev) {\r |
| 873 | if (!ev.params[0]) return;\r |
| 874 | $script(ev.params[0] + '?' + (new Date().getTime()));\r |
| 875 | });\r |
| 876 | \r |
| 877 | \r |
| 878 | controlbox.on('command:set', function (ev) {\r |
| 879 | if (!ev.params[0]) return;\r |
| 880 | \r |
| 881 | var setting = ev.params[0],\r |
| 882 | value;\r |
| 883 | \r |
| 884 | // Do we have a second param to set a value?\r |
| 885 | if (ev.params[1]) {\r |
| 886 | ev.params.shift();\r |
| 887 | \r |
| 888 | value = ev.params.join(' ');\r |
| 889 | \r |
| 890 | // If we're setting a true boolean value..\r |
| 891 | if (value === 'true')\r |
| 892 | value = true;\r |
| 893 | \r |
| 894 | // If we're setting a false boolean value..\r |
| 895 | if (value === 'false')\r |
| 896 | value = false;\r |
| 897 | \r |
| 898 | // If we're setting a number..\r |
| 899 | if (parseInt(value, 10).toString() === value)\r |
| 900 | value = parseInt(value, 10);\r |
| 901 | \r |
| 902 | _kiwi.global.settings.set(setting, value);\r |
| 903 | }\r |
| 904 | \r |
| 905 | // Read the value to the user\r |
| 906 | _kiwi.app.panels.active.addMsg('', setting + ' = ' + _kiwi.global.settings.get(setting).toString());\r |
| 907 | });\r |
| 908 | \r |
| 909 | \r |
| 910 | controlbox.on('command:save', function (ev) {\r |
| 911 | _kiwi.global.settings.save();\r |
| 912 | _kiwi.app.panels.active.addMsg('', 'Settings have been saved');\r |
| 913 | });\r |
| 914 | \r |
| 915 | \r |
| 916 | controlbox.on('command:alias', function (ev) {\r |
| 917 | var name, rule;\r |
| 918 | \r |
| 919 | // No parameters passed so list them\r |
| 920 | if (!ev.params[1]) {\r |
| 921 | $.each(controlbox.preprocessor.aliases, function (name, rule) {\r |
| 922 | _kiwi.app.panels.server.addMsg(' ', name + ' => ' + rule);\r |
| 923 | });\r |
| 924 | return;\r |
| 925 | }\r |
| 926 | \r |
| 927 | // Deleting an alias?\r |
| 928 | if (ev.params[0] === 'del' || ev.params[0] === 'delete') {\r |
| 929 | name = ev.params[1];\r |
| 930 | if (name[0] !== '/') name = '/' + name;\r |
| 931 | delete controlbox.preprocessor.aliases[name];\r |
| 932 | return;\r |
| 933 | }\r |
| 934 | \r |
| 935 | // Add the alias\r |
| 936 | name = ev.params[0];\r |
| 937 | ev.params.shift();\r |
| 938 | rule = ev.params.join(' ');\r |
| 939 | \r |
| 940 | // Make sure the name starts with a slash\r |
| 941 | if (name[0] !== '/') name = '/' + name;\r |
| 942 | \r |
| 943 | // Now actually add the alias\r |
| 944 | controlbox.preprocessor.aliases[name] = rule;\r |
| 945 | });\r |
| 946 | \r |
| 947 | \r |
| 948 | controlbox.on('command:ignore', function (ev) {\r |
| 949 | var list = _kiwi.gateway.get('ignore_list');\r |
| 950 | \r |
| 951 | // No parameters passed so list them\r |
| 952 | if (!ev.params[0]) {\r |
| 953 | if (list.length > 0) {\r |
| 954 | _kiwi.app.panels.active.addMsg(' ', 'Ignored nicks:');\r |
| 955 | $.each(list, function (idx, ignored_pattern) {\r |
| 956 | _kiwi.app.panels.active.addMsg(' ', ignored_pattern);\r |
| 957 | });\r |
| 958 | } else {\r |
| 959 | _kiwi.app.panels.active.addMsg(' ', 'Not ignoring anybody');\r |
| 960 | }\r |
| 961 | return;\r |
| 962 | }\r |
| 963 | \r |
| 964 | // We have a parameter, so add it\r |
| 965 | list.push(ev.params[0]);\r |
| 966 | _kiwi.gateway.set('ignore_list', list);\r |
| 967 | _kiwi.app.panels.active.addMsg(' ', 'Ignoring ' + ev.params[0]);\r |
| 968 | });\r |
| 969 | \r |
| 970 | \r |
| 971 | controlbox.on('command:unignore', function (ev) {\r |
| 972 | var list = _kiwi.gateway.get('ignore_list');\r |
| 973 | \r |
| 974 | if (!ev.params[0]) {\r |
| 975 | _kiwi.app.panels.active.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');\r |
| 976 | return;\r |
| 977 | }\r |
| 978 | \r |
| 979 | list = _.reject(list, function(pattern) {\r |
| 980 | return pattern === ev.params[0];\r |
| 981 | });\r |
| 982 | \r |
| 983 | _kiwi.gateway.set('ignore_list', list);\r |
| 984 | \r |
| 985 | _kiwi.app.panels.active.addMsg(' ', 'Stopped ignoring ' + ev.params[0]);\r |
| 986 | });\r |
| 987 | \r |
| 988 | \r |
| 989 | controlbox.on('command:applet', appletCommand);\r |
| 990 | controlbox.on('command:settings', settingsCommand);\r |
| 991 | controlbox.on('command:script', scriptCommand);\r |
| 992 | };\r |
| 993 | \r |
| 994 | // A fallback action. Send a raw command to the server\r |
| 995 | function unknownCommand (ev) {\r |
| 996 | var raw_cmd = ev.command + ' ' + ev.params.join(' ');\r |
| 997 | console.log('RAW: ' + raw_cmd);\r |
| 998 | _kiwi.gateway.raw(raw_cmd);\r |
| 999 | }\r |
| 1000 | \r |
| 1001 | function allCommands (ev) {}\r |
| 1002 | \r |
| 1003 | function joinCommand (ev) {\r |
| 1004 | var channel, channel_names;\r |
| 1005 | \r |
| 1006 | channel_names = ev.params.join(' ').split(',');\r |
| 1007 | \r |
| 1008 | $.each(channel_names, function (index, channel_name_key) {\r |
| 1009 | // We may have a channel key so split it off\r |
| 1010 | var spli = channel_name_key.split(' '),\r |
| 1011 | channel_name = spli[0],\r |
| 1012 | channel_key = spli[1] || '';\r |
| 1013 | \r |
| 1014 | // Trim any whitespace off the name\r |
| 1015 | channel_name = channel_name.trim();\r |
| 1016 | \r |
| 1017 | // If not a valid channel name, display a warning\r |
| 1018 | if (!that.isChannelName(channel_name)) {\r |
| 1019 | _kiwi.app.panels.server.addMsg('', channel_name + ' is not a valid channel name');\r |
| 1020 | _kiwi.app.message.text(channel_name + ' is not a valid channel name', {timeout: 5000});\r |
| 1021 | return;\r |
| 1022 | }\r |
| 1023 | \r |
| 1024 | // Check if we have the panel already. If not, create it\r |
| 1025 | channel = that.panels.getByName(channel_name);\r |
| 1026 | if (!channel) {\r |
| 1027 | channel = new _kiwi.model.Channel({name: channel_name});\r |
| 1028 | _kiwi.app.panels.add(channel);\r |
| 1029 | }\r |
| 1030 | \r |
| 1031 | _kiwi.gateway.join(channel_name, channel_key);\r |
| 1032 | });\r |
| 1033 | \r |
| 1034 | if (channel) channel.view.show();\r |
| 1035 | \r |
| 1036 | }\r |
| 1037 | \r |
| 1038 | function queryCommand (ev) {\r |
| 1039 | var destination, panel;\r |
| 1040 | \r |
| 1041 | destination = ev.params[0];\r |
| 1042 | \r |
| 1043 | // Check if we have the panel already. If not, create it\r |
| 1044 | panel = that.panels.getByName(destination);\r |
| 1045 | if (!panel) {\r |
| 1046 | panel = new _kiwi.model.Query({name: destination});\r |
| 1047 | _kiwi.app.panels.add(panel);\r |
| 1048 | }\r |
| 1049 | \r |
| 1050 | if (panel) panel.view.show();\r |
| 1051 | \r |
| 1052 | }\r |
| 1053 | \r |
| 1054 | function msgCommand (ev) {\r |
| 1055 | var destination = ev.params[0],\r |
| 1056 | panel = that.panels.getByName(destination) || that.panels.server;\r |
| 1057 | \r |
| 1058 | ev.params.shift();\r |
| 1059 | \r |
| 1060 | panel.addMsg(_kiwi.gateway.get('nick'), ev.params.join(' '));\r |
| 1061 | _kiwi.gateway.privmsg(destination, ev.params.join(' '));\r |
| 1062 | }\r |
| 1063 | \r |
| 1064 | function actionCommand (ev) {\r |
| 1065 | if (_kiwi.app.panels.active === _kiwi.app.panels.server) {\r |
| 1066 | return;\r |
| 1067 | }\r |
| 1068 | \r |
| 1069 | var panel = _kiwi.app.panels.active;\r |
| 1070 | panel.addMsg('', '* ' + _kiwi.gateway.get('nick') + ' ' + ev.params.join(' '), 'action');\r |
| 1071 | _kiwi.gateway.action(panel.get('name'), ev.params.join(' '));\r |
| 1072 | }\r |
| 1073 | \r |
| 1074 | function partCommand (ev) {\r |
| 1075 | if (ev.params.length === 0) {\r |
| 1076 | _kiwi.gateway.part(_kiwi.app.panels.active.get('name'));\r |
| 1077 | } else {\r |
| 1078 | _.each(ev.params, function (channel) {\r |
| 1079 | _kiwi.gateway.part(channel);\r |
| 1080 | });\r |
| 1081 | }\r |
| 1082 | // TODO: More responsive = close tab now, more accurate = leave until part event\r |
| 1083 | //_kiwi.app.panels.remove(_kiwi.app.panels.active);\r |
| 1084 | }\r |
| 1085 | \r |
| 1086 | function topicCommand (ev) {\r |
| 1087 | var channel_name;\r |
| 1088 | \r |
| 1089 | if (ev.params.length === 0) return;\r |
| 1090 | \r |
| 1091 | if (that.isChannelName(ev.params[0])) {\r |
| 1092 | channel_name = ev.params[0];\r |
| 1093 | ev.params.shift();\r |
| 1094 | } else {\r |
| 1095 | channel_name = _kiwi.app.panels.active.get('name');\r |
| 1096 | }\r |
| 1097 | \r |
| 1098 | _kiwi.gateway.topic(channel_name, ev.params.join(' '));\r |
| 1099 | }\r |
| 1100 | \r |
| 1101 | function noticeCommand (ev) {\r |
| 1102 | var destination;\r |
| 1103 | \r |
| 1104 | // Make sure we have a destination and some sort of message\r |
| 1105 | if (ev.params.length <= 1) return;\r |
| 1106 | \r |
| 1107 | destination = ev.params[0];\r |
| 1108 | ev.params.shift();\r |
| 1109 | \r |
| 1110 | _kiwi.gateway.notice(destination, ev.params.join(' '));\r |
| 1111 | }\r |
| 1112 | \r |
| 1113 | function quoteCommand (ev) {\r |
| 1114 | var raw = ev.params.join(' ');\r |
| 1115 | _kiwi.gateway.raw(raw);\r |
| 1116 | }\r |
| 1117 | \r |
| 1118 | function kickCommand (ev) {\r |
| 1119 | var nick, panel = _kiwi.app.panels.active;\r |
| 1120 | \r |
| 1121 | if (!panel.isChannel()) return;\r |
| 1122 | \r |
| 1123 | // Make sure we have a nick\r |
| 1124 | if (ev.params.length === 0) return;\r |
| 1125 | \r |
| 1126 | nick = ev.params[0];\r |
| 1127 | ev.params.shift();\r |
| 1128 | \r |
| 1129 | _kiwi.gateway.kick(panel.get('name'), nick, ev.params.join(' '));\r |
| 1130 | }\r |
| 1131 | \r |
| 1132 | function clearCommand (ev) {\r |
| 1133 | // Can't clear a server or applet panel\r |
| 1134 | if (_kiwi.app.panels.active.isServer() || _kiwi.app.panels.active.isApplet()) {\r |
| 1135 | return;\r |
| 1136 | }\r |
| 1137 | \r |
| 1138 | if (_kiwi.app.panels.active.clearMessages) {\r |
| 1139 | _kiwi.app.panels.active.clearMessages();\r |
| 1140 | }\r |
| 1141 | }\r |
| 1142 | \r |
| 1143 | function ctcpCommand(ev) {\r |
| 1144 | var target, type;\r |
| 1145 | \r |
| 1146 | // Make sure we have a target and a ctcp type (eg. version, time)\r |
| 1147 | if (ev.params.length < 2) return;\r |
| 1148 | \r |
| 1149 | target = ev.params[0];\r |
| 1150 | ev.params.shift();\r |
| 1151 | \r |
| 1152 | type = ev.params[0];\r |
| 1153 | ev.params.shift();\r |
| 1154 | \r |
| 1155 | _kiwi.gateway.ctcp(true, type, target, ev.params.join(' '));\r |
| 1156 | }\r |
| 1157 | \r |
| 1158 | function settingsCommand (ev) {\r |
| 1159 | var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');\r |
| 1160 | settings.view.show();\r |
| 1161 | }\r |
| 1162 | \r |
| 1163 | function scriptCommand (ev) {\r |
| 1164 | var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');\r |
| 1165 | editor.view.show();\r |
| 1166 | }\r |
| 1167 | \r |
| 1168 | function appletCommand (ev) {\r |
| 1169 | if (!ev.params[0]) return;\r |
| 1170 | \r |
| 1171 | var panel = new _kiwi.model.Applet();\r |
| 1172 | \r |
| 1173 | if (ev.params[1]) {\r |
| 1174 | // Url and name given\r |
| 1175 | panel.load(ev.params[0], ev.params[1]);\r |
| 1176 | } else {\r |
| 1177 | // Load a pre-loaded applet\r |
| 1178 | if (_kiwi.applets[ev.params[0]]) {\r |
| 1179 | panel.load(new _kiwi.applets[ev.params[0]]());\r |
| 1180 | } else {\r |
| 1181 | _kiwi.app.panels.server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');\r |
| 1182 | return;\r |
| 1183 | }\r |
| 1184 | }\r |
| 1185 | \r |
| 1186 | _kiwi.app.panels.add(panel);\r |
| 1187 | panel.view.show();\r |
| 1188 | }\r |
| 1189 | \r |
| 1190 | \r |
| 1191 | \r |
| 1192 | \r |
| 1193 | \r |
| 1194 | this.isChannelName = function (channel_name) {\r |
| 1195 | var channel_prefix = _kiwi.gateway.get('channel_prefix');\r |
| 1196 | \r |
| 1197 | if (!channel_name || !channel_name.length) return false;\r |
| 1198 | return (channel_prefix.indexOf(channel_name[0]) > -1);\r |
| 1199 | };\r |
| 1200 | \r |
| 1201 | };\r |
| 1202 | \r |
| 1203 | \r |
| 1204 | model = Backbone.Model.extend(new model());\r |
| 1205 | \r |
| 1206 | return new model(arguments);\r |
| 1207 | };\r |