| 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 | \r |
| 6 | var model = function () {\r |
| 7 | /** _kiwi.view.Application */\r |
| 8 | this.view = null;\r |
| 9 | \r |
| 10 | /** _kiwi.view.StatusMessage */\r |
| 11 | this.message = null;\r |
| 12 | \r |
| 13 | /* Address for the kiwi server */\r |
| 14 | this.kiwi_server = null;\r |
| 15 | \r |
| 16 | this.initialize = function (options) {\r |
| 17 | that = this;\r |
| 18 | \r |
| 19 | if (options[0].container) {\r |
| 20 | this.set('container', options[0].container);\r |
| 21 | }\r |
| 22 | \r |
| 23 | // The base url to the kiwi server\r |
| 24 | this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');\r |
| 25 | \r |
| 26 | // Path for the settings.json file\r |
| 27 | this.set('settings_path', options[0].settings_path ?\r |
| 28 | options[0].settings_path :\r |
| 29 | this.get('base_path') + '/assets/settings.json'\r |
| 30 | );\r |
| 31 | \r |
| 32 | // Any options sent down from the server\r |
| 33 | this.server_settings = options[0].server_settings || {};\r |
| 34 | this.translations = options[0].translations || {};\r |
| 35 | \r |
| 36 | // Best guess at where the kiwi server is\r |
| 37 | this.detectKiwiServer();\r |
| 38 | \r |
| 39 | // Set any default settings before anything else is applied\r |
| 40 | if (this.server_settings && this.server_settings.client && this.server_settings.client.settings) {\r |
| 41 | this.applyDefaultClientSettings(this.server_settings.client.settings);\r |
| 42 | }\r |
| 43 | };\r |
| 44 | \r |
| 45 | \r |
| 46 | this.start = function () {\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.showIntialConenctionDialog();\r |
| 57 | };\r |
| 58 | \r |
| 59 | \r |
| 60 | this.detectKiwiServer = function () {\r |
| 61 | // If running from file, default to localhost:7777 by default\r |
| 62 | if (window.location.protocol === 'file:') {\r |
| 63 | this.kiwi_server = 'http://localhost:7778';\r |
| 64 | } else {\r |
| 65 | // Assume the kiwi server is on the same server\r |
| 66 | this.kiwi_server = window.location.protocol + '//' + window.location.host;\r |
| 67 | }\r |
| 68 | };\r |
| 69 | \r |
| 70 | \r |
| 71 | this.showIntialConenctionDialog = function() {\r |
| 72 | var connection_dialog = new _kiwi.model.NewConnection();\r |
| 73 | this.populateDefaultServerSettings(connection_dialog);\r |
| 74 | \r |
| 75 | connection_dialog.view.$el.addClass('initial');\r |
| 76 | this.view.$el.find('.panel_container:first').append(connection_dialog.view.$el);\r |
| 77 | \r |
| 78 | var $info = $($('#tmpl_new_connection_info').html().trim());\r |
| 79 | \r |
| 80 | if ($info.html()) {\r |
| 81 | connection_dialog.view.infoBoxSet($info);\r |
| 82 | connection_dialog.view.infoBoxShow();\r |
| 83 | }\r |
| 84 | \r |
| 85 | // TODO: Shouldn't really be here but it's not working in the view.. :/\r |
| 86 | // Hack for firefox browers: Focus is not given on this event loop iteration\r |
| 87 | setTimeout(function(){\r |
| 88 | connection_dialog.view.$el.find('.nick').select();\r |
| 89 | }, 0);\r |
| 90 | \r |
| 91 | // Once connected, close this dialog and remove its own event\r |
| 92 | var fn = function() {\r |
| 93 | connection_dialog.view.$el.slideUp(function() {\r |
| 94 | connection_dialog.view.dispose();\r |
| 95 | connection_dialog = null;\r |
| 96 | \r |
| 97 | _kiwi.gateway.off('onconnect', fn);\r |
| 98 | });\r |
| 99 | \r |
| 100 | };\r |
| 101 | _kiwi.gateway.on('onconnect', fn);\r |
| 102 | };\r |
| 103 | \r |
| 104 | \r |
| 105 | this.initializeClient = function () {\r |
| 106 | this.view = new _kiwi.view.Application({model: this, el: this.get('container')});\r |
| 107 | \r |
| 108 | // Takes instances of model_network\r |
| 109 | this.connections = new _kiwi.model.NetworkPanelList();\r |
| 110 | \r |
| 111 | // Applets panel list\r |
| 112 | this.applet_panels = new _kiwi.model.PanelList();\r |
| 113 | this.applet_panels.view.$el.addClass('panellist applets');\r |
| 114 | this.view.$el.find('.tabs').append(this.applet_panels.view.$el);\r |
| 115 | \r |
| 116 | /**\r |
| 117 | * Set the UI components up\r |
| 118 | */\r |
| 119 | this.controlbox = new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]});\r |
| 120 | this.bindControllboxCommands(this.controlbox);\r |
| 121 | \r |
| 122 | this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});\r |
| 123 | \r |
| 124 | new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r |
| 125 | \r |
| 126 | this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});\r |
| 127 | \r |
| 128 | this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});\r |
| 129 | \r |
| 130 | // Rejigg the UI sizes\r |
| 131 | this.view.doLayout();\r |
| 132 | };\r |
| 133 | \r |
| 134 | \r |
| 135 | this.initializeGlobals = function () {\r |
| 136 | _kiwi.global.connections = this.connections;\r |
| 137 | \r |
| 138 | _kiwi.global.panels = this.panels;\r |
| 139 | _kiwi.global.panels.applets = this.applet_panels;\r |
| 140 | \r |
| 141 | _kiwi.global.components.Applet = _kiwi.model.Applet;\r |
| 142 | _kiwi.global.components.Panel =_kiwi.model.Panel;\r |
| 143 | };\r |
| 144 | \r |
| 145 | \r |
| 146 | this.applyDefaultClientSettings = function (settings) {\r |
| 147 | _.each(settings, function (value, setting) {\r |
| 148 | if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r |
| 149 | _kiwi.global.settings.set(setting, value);\r |
| 150 | }\r |
| 151 | });\r |
| 152 | };\r |
| 153 | \r |
| 154 | \r |
| 155 | this.populateDefaultServerSettings = function (new_connection_dialog) {\r |
| 156 | var parts;\r |
| 157 | var defaults = {\r |
| 158 | nick: '',\r |
| 159 | server: '',\r |
| 160 | port: 6667,\r |
| 161 | ssl: false,\r |
| 162 | channel: '#chat',\r |
| 163 | channel_key: ''\r |
| 164 | };\r |
| 165 | var uricheck;\r |
| 166 | \r |
| 167 | \r |
| 168 | /**\r |
| 169 | * Get any settings set by the server\r |
| 170 | * These settings may be changed in the server selection dialog or via URL parameters\r |
| 171 | */\r |
| 172 | if (this.server_settings.client) {\r |
| 173 | if (this.server_settings.client.nick)\r |
| 174 | defaults.nick = this.server_settings.client.nick;\r |
| 175 | \r |
| 176 | if (this.server_settings.client.server)\r |
| 177 | defaults.server = this.server_settings.client.server;\r |
| 178 | \r |
| 179 | if (this.server_settings.client.port)\r |
| 180 | defaults.port = this.server_settings.client.port;\r |
| 181 | \r |
| 182 | if (this.server_settings.client.ssl)\r |
| 183 | defaults.ssl = this.server_settings.client.ssl;\r |
| 184 | \r |
| 185 | if (this.server_settings.client.channel)\r |
| 186 | defaults.channel = this.server_settings.client.channel;\r |
| 187 | \r |
| 188 | if (this.server_settings.client.channel_key)\r |
| 189 | defaults.channel_key = this.server_settings.client.channel_key;\r |
| 190 | }\r |
| 191 | \r |
| 192 | \r |
| 193 | \r |
| 194 | /**\r |
| 195 | * Get any settings passed in the URL\r |
| 196 | * These settings may be changed in the server selection dialog\r |
| 197 | */\r |
| 198 | \r |
| 199 | // Any query parameters first\r |
| 200 | if (getQueryVariable('nick'))\r |
| 201 | defaults.nick = getQueryVariable('nick');\r |
| 202 | \r |
| 203 | if (window.location.hash)\r |
| 204 | defaults.channel = window.location.hash;\r |
| 205 | \r |
| 206 | \r |
| 207 | // Process the URL part by part, extracting as we go\r |
| 208 | parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');\r |
| 209 | \r |
| 210 | if (parts.length > 0) {\r |
| 211 | parts.shift();\r |
| 212 | \r |
| 213 | if (parts.length > 0 && parts[0]) {\r |
| 214 | // 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 |
| 215 | uricheck = parts[0].substr(0, 7).toLowerCase();\r |
| 216 | if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {\r |
| 217 | parts[0] = decodeURIComponent(parts[0]);\r |
| 218 | // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r |
| 219 | uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r |
| 220 | /*\r |
| 221 | uricheck[1] = ssl (optional)\r |
| 222 | uricheck[2] = host\r |
| 223 | uricheck[3] = port (optional)\r |
| 224 | uricheck[4] = channel (optional)\r |
| 225 | uricheck[5] = channel key (optional, channel must also be set)\r |
| 226 | */\r |
| 227 | if (uricheck) {\r |
| 228 | if (typeof uricheck[1] !== 'undefined') {\r |
| 229 | defaults.ssl = true;\r |
| 230 | if (defaults.port === 6667) {\r |
| 231 | defaults.port = 6697;\r |
| 232 | }\r |
| 233 | }\r |
| 234 | defaults.server = uricheck[2];\r |
| 235 | if (typeof uricheck[3] !== 'undefined') {\r |
| 236 | defaults.port = uricheck[3];\r |
| 237 | }\r |
| 238 | if (typeof uricheck[4] !== 'undefined') {\r |
| 239 | defaults.channel = '#' + uricheck[4];\r |
| 240 | if (typeof uricheck[5] !== 'undefined') {\r |
| 241 | defaults.channel_key = uricheck[5];\r |
| 242 | }\r |
| 243 | }\r |
| 244 | }\r |
| 245 | parts = [];\r |
| 246 | } else {\r |
| 247 | // Extract the port+ssl if we find one\r |
| 248 | if (parts[0].search(/:/) > 0) {\r |
| 249 | defaults.port = parts[0].substring(parts[0].search(/:/) + 1);\r |
| 250 | defaults.server = parts[0].substring(0, parts[0].search(/:/));\r |
| 251 | if (defaults.port[0] === '+') {\r |
| 252 | defaults.port = parseInt(defaults.port.substring(1), 10);\r |
| 253 | defaults.ssl = true;\r |
| 254 | } else {\r |
| 255 | defaults.ssl = false;\r |
| 256 | }\r |
| 257 | \r |
| 258 | } else {\r |
| 259 | defaults.server = parts[0];\r |
| 260 | }\r |
| 261 | \r |
| 262 | parts.shift();\r |
| 263 | }\r |
| 264 | }\r |
| 265 | \r |
| 266 | if (parts.length > 0 && parts[0]) {\r |
| 267 | defaults.channel = '#' + parts[0];\r |
| 268 | parts.shift();\r |
| 269 | }\r |
| 270 | }\r |
| 271 | \r |
| 272 | // If any settings have been given by the server.. override any auto detected settings\r |
| 273 | /**\r |
| 274 | * Get any server restrictions as set in the server config\r |
| 275 | * These settings can not be changed in the server selection dialog\r |
| 276 | */\r |
| 277 | if (this.server_settings && this.server_settings.connection) {\r |
| 278 | if (this.server_settings.connection.server) {\r |
| 279 | defaults.server = this.server_settings.connection.server;\r |
| 280 | }\r |
| 281 | \r |
| 282 | if (this.server_settings.connection.port) {\r |
| 283 | defaults.port = this.server_settings.connection.port;\r |
| 284 | }\r |
| 285 | \r |
| 286 | if (this.server_settings.connection.ssl) {\r |
| 287 | defaults.ssl = this.server_settings.connection.ssl;\r |
| 288 | }\r |
| 289 | \r |
| 290 | if (this.server_settings.connection.channel) {\r |
| 291 | defaults.channel = this.server_settings.connection.channel;\r |
| 292 | }\r |
| 293 | \r |
| 294 | if (this.server_settings.connection.channel_key) {\r |
| 295 | defaults.channel_key = this.server_settings.connection.channel_key;\r |
| 296 | }\r |
| 297 | \r |
| 298 | if (this.server_settings.connection.nick) {\r |
| 299 | defaults.nick = this.server_settings.connection.nick;\r |
| 300 | }\r |
| 301 | }\r |
| 302 | \r |
| 303 | // Set any random numbers if needed\r |
| 304 | defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());\r |
| 305 | \r |
| 306 | if (getQueryVariable('encoding'))\r |
| 307 | defaults.encoding = getQueryVariable('encoding');\r |
| 308 | \r |
| 309 | // Populate the server select box with defaults\r |
| 310 | new_connection_dialog.view.populateFields(defaults);\r |
| 311 | };\r |
| 312 | \r |
| 313 | \r |
| 314 | this.panels = (function() {\r |
| 315 | var fn = function(panel_type) {\r |
| 316 | var panels;\r |
| 317 | \r |
| 318 | // Default panel type\r |
| 319 | panel_type = panel_type || 'connections';\r |
| 320 | \r |
| 321 | switch (panel_type) {\r |
| 322 | case 'connections':\r |
| 323 | panels = this.connections.panels();\r |
| 324 | break;\r |
| 325 | case 'applets':\r |
| 326 | panels = this.applet_panels.models;\r |
| 327 | break;\r |
| 328 | }\r |
| 329 | \r |
| 330 | // Active panels / server\r |
| 331 | panels.active = this.connections.active_panel;\r |
| 332 | panels.server = this.connections.active_connection ?\r |
| 333 | this.connections.active_connection.panels.server :\r |
| 334 | null;\r |
| 335 | \r |
| 336 | return panels;\r |
| 337 | };\r |
| 338 | \r |
| 339 | _.extend(fn, Backbone.Events);\r |
| 340 | \r |
| 341 | return fn;\r |
| 342 | })();\r |
| 343 | \r |
| 344 | \r |
| 345 | this.bindGatewayCommands = function (gw) {\r |
| 346 | var that = this;\r |
| 347 | \r |
| 348 | gw.on('onconnect', function (event) {\r |
| 349 | that.view.barsShow();\r |
| 350 | });\r |
| 351 | \r |
| 352 | \r |
| 353 | /**\r |
| 354 | * Handle the reconnections to the kiwi server\r |
| 355 | */\r |
| 356 | (function () {\r |
| 357 | // 0 = non-reconnecting state. 1 = reconnecting state.\r |
| 358 | var gw_stat = 0;\r |
| 359 | \r |
| 360 | // If the current or upcoming disconnect was planned\r |
| 361 | var unplanned_disconnect = false;\r |
| 362 | \r |
| 363 | gw.on('disconnect', function (event) {\r |
| 364 | unplanned_disconnect = !gw.disconnect_requested;\r |
| 365 | \r |
| 366 | if (unplanned_disconnect) {\r |
| 367 | var msg = _kiwi.global.i18n.translate('client_models_application_reconnecting').fetch() + '...';\r |
| 368 | that.message.text(msg, {timeout: 10000});\r |
| 369 | }\r |
| 370 | \r |
| 371 | that.view.$el.removeClass('connected');\r |
| 372 | \r |
| 373 | // Mention the disconnection on every channel\r |
| 374 | _kiwi.app.connections.forEach(function(connection) {\r |
| 375 | connection.panels.server.addMsg('', msg, 'action quit');\r |
| 376 | \r |
| 377 | connection.panels.forEach(function(panel) {\r |
| 378 | if (!panel.isChannel())\r |
| 379 | return;\r |
| 380 | \r |
| 381 | panel.addMsg('', msg, 'action quit');\r |
| 382 | });\r |
| 383 | });\r |
| 384 | \r |
| 385 | gw_stat = 1;\r |
| 386 | });\r |
| 387 | \r |
| 388 | \r |
| 389 | gw.on('reconnecting', function (event) {\r |
| 390 | var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_in_x_seconds').fetch(event.delay/1000) + '...';\r |
| 391 | \r |
| 392 | // Only need to mention the repeating re-connection messages on server panels\r |
| 393 | _kiwi.app.connections.forEach(function(connection) {\r |
| 394 | connection.panels.server.addMsg('', msg, 'action quit');\r |
| 395 | });\r |
| 396 | });\r |
| 397 | \r |
| 398 | \r |
| 399 | gw.on('onconnect', function (event) {\r |
| 400 | that.view.$el.addClass('connected');\r |
| 401 | if (gw_stat !== 1) return;\r |
| 402 | \r |
| 403 | if (unplanned_disconnect) {\r |
| 404 | var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_successfully').fetch() + ':)';\r |
| 405 | that.message.text(msg, {timeout: 5000});\r |
| 406 | }\r |
| 407 | \r |
| 408 | // Mention the re-connection on every channel\r |
| 409 | _kiwi.app.connections.forEach(function(connection) {\r |
| 410 | connection.panels.server.addMsg('', msg, 'action join');\r |
| 411 | \r |
| 412 | connection.panels.forEach(function(panel) {\r |
| 413 | if (!panel.isChannel())\r |
| 414 | return;\r |
| 415 | \r |
| 416 | panel.addMsg('', msg, 'action join');\r |
| 417 | });\r |
| 418 | });\r |
| 419 | \r |
| 420 | gw_stat = 0;\r |
| 421 | });\r |
| 422 | })();\r |
| 423 | \r |
| 424 | \r |
| 425 | gw.on('kiwi:reconfig', function () {\r |
| 426 | $.getJSON(that.get('settings_path'), function (data) {\r |
| 427 | that.server_settings = data.server_settings || {};\r |
| 428 | that.translations = data.translations || {};\r |
| 429 | });\r |
| 430 | });\r |
| 431 | \r |
| 432 | \r |
| 433 | gw.on('kiwi:jumpserver', function (data) {\r |
| 434 | var serv;\r |
| 435 | // No server set? Then nowhere to jump to.\r |
| 436 | if (typeof data.kiwi_server === 'undefined')\r |
| 437 | return;\r |
| 438 | \r |
| 439 | serv = data.kiwi_server;\r |
| 440 | \r |
| 441 | // Strip any trailing slash from the end\r |
| 442 | if (serv[serv.length-1] === '/')\r |
| 443 | serv = serv.substring(0, serv.length-1);\r |
| 444 | \r |
| 445 | _kiwi.app.kiwi_server = serv;\r |
| 446 | \r |
| 447 | // Force the jumpserver now?\r |
| 448 | if (data.force) {\r |
| 449 | // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r |
| 450 | var jump_server_interval = Math.random() * (360 - 300) + 300;\r |
| 451 | \r |
| 452 | // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r |
| 453 | var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r |
| 454 | that.message.text(msg, {timeout: 10000});\r |
| 455 | \r |
| 456 | setTimeout(function forcedReconnect() {\r |
| 457 | var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r |
| 458 | that.message.text(msg, {timeout: 8000});\r |
| 459 | \r |
| 460 | setTimeout(function forcedReconnectPartTwo() {\r |
| 461 | _kiwi.gateway.set('kiwi_server', _kiwi.app.kiwi_server);\r |
| 462 | \r |
| 463 | _kiwi.gateway.reconnect(function() {\r |
| 464 | // Reconnect all the IRC connections\r |
| 465 | that.connections.forEach(function(con){ con.reconnect(); });\r |
| 466 | });\r |
| 467 | }, 5000);\r |
| 468 | \r |
| 469 | }, jump_server_interval * 1000);\r |
| 470 | }\r |
| 471 | });\r |
| 472 | };\r |
| 473 | \r |
| 474 | \r |
| 475 | \r |
| 476 | /**\r |
| 477 | * Bind to certain commands that may be typed into the control box\r |
| 478 | */\r |
| 479 | this.bindControllboxCommands = function (controlbox) {\r |
| 480 | // Default aliases\r |
| 481 | $.extend(controlbox.preprocessor.aliases, {\r |
| 482 | // General aliases\r |
| 483 | '/p': '/part $1+',\r |
| 484 | '/me': '/action $1+',\r |
| 485 | '/j': '/join $1+',\r |
| 486 | '/q': '/query $1+',\r |
| 487 | '/w': '/whois $1+',\r |
| 488 | '/raw': '/quote $1+',\r |
| 489 | \r |
| 490 | // Op related aliases\r |
| 491 | '/op': '/quote mode $channel +o $1+',\r |
| 492 | '/deop': '/quote mode $channel -o $1+',\r |
| 493 | '/hop': '/quote mode $channel +h $1+',\r |
| 494 | '/dehop': '/quote mode $channel -h $1+',\r |
| 495 | '/voice': '/quote mode $channel +v $1+',\r |
| 496 | '/devoice': '/quote mode $channel -v $1+',\r |
| 497 | '/k': '/kick $channel $1+',\r |
| 498 | \r |
| 499 | // Misc aliases\r |
| 500 | '/slap': '/me slaps $1 around a bit with a large trout'\r |
| 501 | });\r |
| 502 | \r |
| 503 | controlbox.on('unknown_command', unknownCommand);\r |
| 504 | \r |
| 505 | controlbox.on('command', allCommands);\r |
| 506 | controlbox.on('command:msg', msgCommand);\r |
| 507 | \r |
| 508 | controlbox.on('command:action', actionCommand);\r |
| 509 | \r |
| 510 | controlbox.on('command:join', joinCommand);\r |
| 511 | \r |
| 512 | controlbox.on('command:part', partCommand);\r |
| 513 | \r |
| 514 | controlbox.on('command:nick', function (ev) {\r |
| 515 | _kiwi.gateway.changeNick(null, ev.params[0]);\r |
| 516 | });\r |
| 517 | \r |
| 518 | controlbox.on('command:query', queryCommand);\r |
| 519 | \r |
| 520 | controlbox.on('command:invite', inviteCommand);\r |
| 521 | \r |
| 522 | controlbox.on('command:topic', topicCommand);\r |
| 523 | \r |
| 524 | controlbox.on('command:notice', noticeCommand);\r |
| 525 | \r |
| 526 | controlbox.on('command:quote', quoteCommand);\r |
| 527 | \r |
| 528 | controlbox.on('command:kick', kickCommand);\r |
| 529 | \r |
| 530 | controlbox.on('command:clear', clearCommand);\r |
| 531 | \r |
| 532 | controlbox.on('command:ctcp', ctcpCommand);\r |
| 533 | \r |
| 534 | controlbox.on('command:server', serverCommand);\r |
| 535 | \r |
| 536 | controlbox.on('command:whois', whoisCommand);\r |
| 537 | \r |
| 538 | controlbox.on('command:whowas', whowasCommand);\r |
| 539 | \r |
| 540 | controlbox.on('command:encoding', encodingCommand);\r |
| 541 | \r |
| 542 | controlbox.on('command:css', function (ev) {\r |
| 543 | var queryString = '?reload=' + new Date().getTime();\r |
| 544 | $('link[rel="stylesheet"]').each(function () {\r |
| 545 | this.href = this.href.replace(/\?.*|$/, queryString);\r |
| 546 | });\r |
| 547 | });\r |
| 548 | \r |
| 549 | controlbox.on('command:js', function (ev) {\r |
| 550 | if (!ev.params[0]) return;\r |
| 551 | $script(ev.params[0] + '?' + (new Date().getTime()));\r |
| 552 | });\r |
| 553 | \r |
| 554 | \r |
| 555 | controlbox.on('command:set', function (ev) {\r |
| 556 | if (!ev.params[0]) return;\r |
| 557 | \r |
| 558 | var setting = ev.params[0],\r |
| 559 | value;\r |
| 560 | \r |
| 561 | // Do we have a second param to set a value?\r |
| 562 | if (ev.params[1]) {\r |
| 563 | ev.params.shift();\r |
| 564 | \r |
| 565 | value = ev.params.join(' ');\r |
| 566 | \r |
| 567 | // If we're setting a true boolean value..\r |
| 568 | if (value === 'true')\r |
| 569 | value = true;\r |
| 570 | \r |
| 571 | // If we're setting a false boolean value..\r |
| 572 | if (value === 'false')\r |
| 573 | value = false;\r |
| 574 | \r |
| 575 | // If we're setting a number..\r |
| 576 | if (parseInt(value, 10).toString() === value)\r |
| 577 | value = parseInt(value, 10);\r |
| 578 | \r |
| 579 | _kiwi.global.settings.set(setting, value);\r |
| 580 | }\r |
| 581 | \r |
| 582 | // Read the value to the user\r |
| 583 | _kiwi.app.panels().active.addMsg('', setting + ' = ' + _kiwi.global.settings.get(setting).toString());\r |
| 584 | });\r |
| 585 | \r |
| 586 | \r |
| 587 | controlbox.on('command:save', function (ev) {\r |
| 588 | _kiwi.global.settings.save();\r |
| 589 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_settings_saved').fetch());\r |
| 590 | });\r |
| 591 | \r |
| 592 | \r |
| 593 | controlbox.on('command:alias', function (ev) {\r |
| 594 | var name, rule;\r |
| 595 | \r |
| 596 | // No parameters passed so list them\r |
| 597 | if (!ev.params[1]) {\r |
| 598 | $.each(controlbox.preprocessor.aliases, function (name, rule) {\r |
| 599 | _kiwi.app.panels().server.addMsg(' ', name + ' => ' + rule);\r |
| 600 | });\r |
| 601 | return;\r |
| 602 | }\r |
| 603 | \r |
| 604 | // Deleting an alias?\r |
| 605 | if (ev.params[0] === 'del' || ev.params[0] === 'delete') {\r |
| 606 | name = ev.params[1];\r |
| 607 | if (name[0] !== '/') name = '/' + name;\r |
| 608 | delete controlbox.preprocessor.aliases[name];\r |
| 609 | return;\r |
| 610 | }\r |
| 611 | \r |
| 612 | // Add the alias\r |
| 613 | name = ev.params[0];\r |
| 614 | ev.params.shift();\r |
| 615 | rule = ev.params.join(' ');\r |
| 616 | \r |
| 617 | // Make sure the name starts with a slash\r |
| 618 | if (name[0] !== '/') name = '/' + name;\r |
| 619 | \r |
| 620 | // Now actually add the alias\r |
| 621 | controlbox.preprocessor.aliases[name] = rule;\r |
| 622 | });\r |
| 623 | \r |
| 624 | \r |
| 625 | controlbox.on('command:ignore', function (ev) {\r |
| 626 | var list = _kiwi.gateway.get('ignore_list');\r |
| 627 | \r |
| 628 | // No parameters passed so list them\r |
| 629 | if (!ev.params[0]) {\r |
| 630 | if (list.length > 0) {\r |
| 631 | _kiwi.app.panels().active.addMsg(' ', _kiwi.global.i18n.translate('client_models_application_ignore_title').fetch() + ':');\r |
| 632 | $.each(list, function (idx, ignored_pattern) {\r |
| 633 | _kiwi.app.panels().active.addMsg(' ', ignored_pattern);\r |
| 634 | });\r |
| 635 | } else {\r |
| 636 | _kiwi.app.panels().active.addMsg(' ', _kiwi.global.i18n.translate('client_models_application_ignore_none').fetch());\r |
| 637 | }\r |
| 638 | return;\r |
| 639 | }\r |
| 640 | \r |
| 641 | // We have a parameter, so add it\r |
| 642 | list.push(ev.params[0]);\r |
| 643 | _kiwi.gateway.set('ignore_list', list);\r |
| 644 | _kiwi.app.panels().active.addMsg(' ', _kiwi.global.i18n.translate('client_models_application_ignore_nick').fetch(ev.params[0]));\r |
| 645 | });\r |
| 646 | \r |
| 647 | \r |
| 648 | controlbox.on('command:unignore', function (ev) {\r |
| 649 | var list = _kiwi.gateway.get('ignore_list');\r |
| 650 | \r |
| 651 | if (!ev.params[0]) {\r |
| 652 | _kiwi.app.panels().active.addMsg(' ', _kiwi.global.i18n.translate('client_models_application_ignore_stop_notice').fetch());\r |
| 653 | return;\r |
| 654 | }\r |
| 655 | \r |
| 656 | list = _.reject(list, function(pattern) {\r |
| 657 | return pattern === ev.params[0];\r |
| 658 | });\r |
| 659 | \r |
| 660 | _kiwi.gateway.set('ignore_list', list);\r |
| 661 | \r |
| 662 | _kiwi.app.panels().active.addMsg(' ', _kiwi.global.i18n.translate('client_models_application_ignore_stopped').fetch(ev.params[0]));\r |
| 663 | });\r |
| 664 | \r |
| 665 | \r |
| 666 | controlbox.on('command:applet', appletCommand);\r |
| 667 | controlbox.on('command:settings', settingsCommand);\r |
| 668 | controlbox.on('command:script', scriptCommand);\r |
| 669 | };\r |
| 670 | \r |
| 671 | // A fallback action. Send a raw command to the server\r |
| 672 | function unknownCommand (ev) {\r |
| 673 | var raw_cmd = ev.command + ' ' + ev.params.join(' ');\r |
| 674 | console.log('RAW: ' + raw_cmd);\r |
| 675 | _kiwi.gateway.raw(null, raw_cmd);\r |
| 676 | }\r |
| 677 | \r |
| 678 | function allCommands (ev) {}\r |
| 679 | \r |
| 680 | function joinCommand (ev) {\r |
| 681 | var panels, channel_names;\r |
| 682 | \r |
| 683 | channel_names = ev.params.join(' ').split(',');\r |
| 684 | panels = that.connections.active_connection.createAndJoinChannels(channel_names);\r |
| 685 | \r |
| 686 | // Show the last channel if we have one\r |
| 687 | if (panels.length)\r |
| 688 | panels[panels.length - 1].view.show();\r |
| 689 | }\r |
| 690 | \r |
| 691 | function queryCommand (ev) {\r |
| 692 | var destination, message, panel;\r |
| 693 | \r |
| 694 | destination = ev.params[0];\r |
| 695 | ev.params.shift();\r |
| 696 | \r |
| 697 | message = ev.params.join(' ');\r |
| 698 | \r |
| 699 | // Check if we have the panel already. If not, create it\r |
| 700 | panel = that.connections.active_connection.panels.getByName(destination);\r |
| 701 | if (!panel) {\r |
| 702 | panel = new _kiwi.model.Query({name: destination});\r |
| 703 | that.connections.active_connection.panels.add(panel);\r |
| 704 | }\r |
| 705 | \r |
| 706 | if (panel) panel.view.show();\r |
| 707 | \r |
| 708 | if (message) {\r |
| 709 | that.connections.active_connection.gateway.msg(panel.get('name'), message);\r |
| 710 | panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), message);\r |
| 711 | }\r |
| 712 | \r |
| 713 | }\r |
| 714 | \r |
| 715 | function msgCommand (ev) {\r |
| 716 | var message,\r |
| 717 | destination = ev.params[0],\r |
| 718 | panel = that.connections.active_connection.panels.getByName(destination) || that.panels().server;\r |
| 719 | \r |
| 720 | ev.params.shift();\r |
| 721 | message = formatToIrcMsg(ev.params.join(' '));\r |
| 722 | \r |
| 723 | panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), message);\r |
| 724 | _kiwi.gateway.privmsg(null, destination, message);\r |
| 725 | }\r |
| 726 | \r |
| 727 | function actionCommand (ev) {\r |
| 728 | if (_kiwi.app.panels().active.isServer()) {\r |
| 729 | return;\r |
| 730 | }\r |
| 731 | \r |
| 732 | var panel = _kiwi.app.panels().active;\r |
| 733 | panel.addMsg('', '* ' + _kiwi.app.connections.active_connection.get('nick') + ' ' + ev.params.join(' '), 'action');\r |
| 734 | _kiwi.gateway.action(null, panel.get('name'), ev.params.join(' '));\r |
| 735 | }\r |
| 736 | \r |
| 737 | function partCommand (ev) {\r |
| 738 | if (ev.params.length === 0) {\r |
| 739 | _kiwi.gateway.part(null, _kiwi.app.panels().active.get('name'));\r |
| 740 | } else {\r |
| 741 | _.each(ev.params, function (channel) {\r |
| 742 | _kiwi.gateway.part(null, channel);\r |
| 743 | });\r |
| 744 | }\r |
| 745 | }\r |
| 746 | \r |
| 747 | function topicCommand (ev) {\r |
| 748 | var channel_name;\r |
| 749 | \r |
| 750 | if (ev.params.length === 0) return;\r |
| 751 | \r |
| 752 | if (that.isChannelName(ev.params[0])) {\r |
| 753 | channel_name = ev.params[0];\r |
| 754 | ev.params.shift();\r |
| 755 | } else {\r |
| 756 | channel_name = _kiwi.app.panels().active.get('name');\r |
| 757 | }\r |
| 758 | \r |
| 759 | _kiwi.gateway.topic(null, channel_name, ev.params.join(' '));\r |
| 760 | }\r |
| 761 | \r |
| 762 | function noticeCommand (ev) {\r |
| 763 | var destination;\r |
| 764 | \r |
| 765 | // Make sure we have a destination and some sort of message\r |
| 766 | if (ev.params.length <= 1) return;\r |
| 767 | \r |
| 768 | destination = ev.params[0];\r |
| 769 | ev.params.shift();\r |
| 770 | \r |
| 771 | _kiwi.gateway.notice(null, destination, ev.params.join(' '));\r |
| 772 | }\r |
| 773 | \r |
| 774 | function quoteCommand (ev) {\r |
| 775 | var raw = ev.params.join(' ');\r |
| 776 | _kiwi.gateway.raw(null, raw);\r |
| 777 | }\r |
| 778 | \r |
| 779 | function kickCommand (ev) {\r |
| 780 | var nick, panel = _kiwi.app.panels().active;\r |
| 781 | \r |
| 782 | if (!panel.isChannel()) return;\r |
| 783 | \r |
| 784 | // Make sure we have a nick\r |
| 785 | if (ev.params.length === 0) return;\r |
| 786 | \r |
| 787 | nick = ev.params[0];\r |
| 788 | ev.params.shift();\r |
| 789 | \r |
| 790 | _kiwi.gateway.kick(null, panel.get('name'), nick, ev.params.join(' '));\r |
| 791 | }\r |
| 792 | \r |
| 793 | function clearCommand (ev) {\r |
| 794 | // Can't clear a server or applet panel\r |
| 795 | if (_kiwi.app.panels().active.isServer() || _kiwi.app.panels().active.isApplet()) {\r |
| 796 | return;\r |
| 797 | }\r |
| 798 | \r |
| 799 | if (_kiwi.app.panels().active.clearMessages) {\r |
| 800 | _kiwi.app.panels().active.clearMessages();\r |
| 801 | }\r |
| 802 | }\r |
| 803 | \r |
| 804 | function ctcpCommand(ev) {\r |
| 805 | var target, type;\r |
| 806 | \r |
| 807 | // Make sure we have a target and a ctcp type (eg. version, time)\r |
| 808 | if (ev.params.length < 2) return;\r |
| 809 | \r |
| 810 | target = ev.params[0];\r |
| 811 | ev.params.shift();\r |
| 812 | \r |
| 813 | type = ev.params[0];\r |
| 814 | ev.params.shift();\r |
| 815 | \r |
| 816 | _kiwi.gateway.ctcp(null, true, type, target, ev.params.join(' '));\r |
| 817 | }\r |
| 818 | \r |
| 819 | function settingsCommand (ev) {\r |
| 820 | var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');\r |
| 821 | settings.view.show();\r |
| 822 | }\r |
| 823 | \r |
| 824 | function scriptCommand (ev) {\r |
| 825 | var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');\r |
| 826 | editor.view.show();\r |
| 827 | }\r |
| 828 | \r |
| 829 | function appletCommand (ev) {\r |
| 830 | if (!ev.params[0]) return;\r |
| 831 | \r |
| 832 | var panel = new _kiwi.model.Applet();\r |
| 833 | \r |
| 834 | if (ev.params[1]) {\r |
| 835 | // Url and name given\r |
| 836 | panel.load(ev.params[0], ev.params[1]);\r |
| 837 | } else {\r |
| 838 | // Load a pre-loaded applet\r |
| 839 | if (_kiwi.applets[ev.params[0]]) {\r |
| 840 | panel.load(new _kiwi.applets[ev.params[0]]());\r |
| 841 | } else {\r |
| 842 | _kiwi.app.panels().server.addMsg('', _kiwi.global.i18n.translate('client_models_application_applet_notfound').fetch(ev.params[0]));\r |
| 843 | return;\r |
| 844 | }\r |
| 845 | }\r |
| 846 | \r |
| 847 | _kiwi.app.connections.active_connection.panels.add(panel);\r |
| 848 | panel.view.show();\r |
| 849 | }\r |
| 850 | \r |
| 851 | \r |
| 852 | \r |
| 853 | function inviteCommand (ev) {\r |
| 854 | var nick, channel;\r |
| 855 | \r |
| 856 | // A nick must be specified\r |
| 857 | if (!ev.params[0])\r |
| 858 | return;\r |
| 859 | \r |
| 860 | // Can only invite into channels\r |
| 861 | if (!_kiwi.app.panels().active.isChannel())\r |
| 862 | return;\r |
| 863 | \r |
| 864 | nick = ev.params[0];\r |
| 865 | channel = _kiwi.app.panels().active.get('name');\r |
| 866 | \r |
| 867 | _kiwi.app.connections.active_connection.gateway.raw('INVITE ' + nick + ' ' + channel);\r |
| 868 | \r |
| 869 | _kiwi.app.panels().active.addMsg('', '== ' + nick + ' has been invited to ' + channel, 'action');\r |
| 870 | }\r |
| 871 | \r |
| 872 | \r |
| 873 | function whoisCommand (ev) {\r |
| 874 | var nick;\r |
| 875 | \r |
| 876 | if (ev.params[0]) {\r |
| 877 | nick = ev.params[0];\r |
| 878 | } else if (_kiwi.app.panels().active.isQuery()) {\r |
| 879 | nick = _kiwi.app.panels().active.get('name');\r |
| 880 | }\r |
| 881 | \r |
| 882 | if (nick)\r |
| 883 | _kiwi.app.connections.active_connection.gateway.raw('WHOIS ' + nick + ' ' + nick);\r |
| 884 | }\r |
| 885 | \r |
| 886 | \r |
| 887 | function whowasCommand (ev) {\r |
| 888 | var nick;\r |
| 889 | \r |
| 890 | if (ev.params[0]) {\r |
| 891 | nick = ev.params[0];\r |
| 892 | } else if (_kiwi.app.panels().active.isQuery()) {\r |
| 893 | nick = _kiwi.app.panels().active.get('name');\r |
| 894 | }\r |
| 895 | \r |
| 896 | if (nick)\r |
| 897 | _kiwi.app.connections.active_connection.gateway.raw('WHOWAS ' + nick);\r |
| 898 | }\r |
| 899 | \r |
| 900 | function encodingCommand (ev) {\r |
| 901 | if (ev.params[0]) {\r |
| 902 | _kiwi.gateway.setEncoding(null, ev.params[0], function (success) {\r |
| 903 | if (success) {\r |
| 904 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_encoding_changed').fetch(ev.params[0]));\r |
| 905 | } else {\r |
| 906 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_encoding_invalid').fetch(ev.params[0]));\r |
| 907 | }\r |
| 908 | });\r |
| 909 | } else {\r |
| 910 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_encoding_notspecified').fetch());\r |
| 911 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_encoding_usage').fetch());\r |
| 912 | }\r |
| 913 | }\r |
| 914 | \r |
| 915 | function serverCommand (ev) {\r |
| 916 | var server, port, ssl, password, nick,\r |
| 917 | tmp;\r |
| 918 | \r |
| 919 | // If no server address given, show the new connection dialog\r |
| 920 | if (!ev.params[0]) {\r |
| 921 | tmp = new _kiwi.view.MenuBox(_kiwi.global.i18n.translate('client_models_application_connection_create').fetch());\r |
| 922 | tmp.addItem('new_connection', new _kiwi.model.NewConnection().view.$el);\r |
| 923 | tmp.show();\r |
| 924 | \r |
| 925 | // Center screen the dialog\r |
| 926 | tmp.$el.offset({\r |
| 927 | top: (that.view.$el.height() / 2) - (tmp.$el.height() / 2),\r |
| 928 | left: (that.view.$el.width() / 2) - (tmp.$el.width() / 2)\r |
| 929 | });\r |
| 930 | \r |
| 931 | return;\r |
| 932 | }\r |
| 933 | \r |
| 934 | // Port given in 'host:port' format and no specific port given after a space\r |
| 935 | if (ev.params[0].indexOf(':') > 0) {\r |
| 936 | tmp = ev.params[0].split(':');\r |
| 937 | server = tmp[0];\r |
| 938 | port = tmp[1];\r |
| 939 | \r |
| 940 | password = ev.params[1] || undefined;\r |
| 941 | \r |
| 942 | } else {\r |
| 943 | // Server + port given as 'host port'\r |
| 944 | server = ev.params[0];\r |
| 945 | port = ev.params[1] || 6667;\r |
| 946 | \r |
| 947 | password = ev.params[2] || undefined;\r |
| 948 | }\r |
| 949 | \r |
| 950 | // + in the port means SSL\r |
| 951 | if (port.toString()[0] === '+') {\r |
| 952 | ssl = true;\r |
| 953 | port = parseInt(port.substring(1), 10);\r |
| 954 | } else {\r |
| 955 | ssl = false;\r |
| 956 | }\r |
| 957 | \r |
| 958 | // Default port if one wasn't found\r |
| 959 | port = port || 6667;\r |
| 960 | \r |
| 961 | // Use the same nick as we currently have\r |
| 962 | nick = _kiwi.app.connections.active_connection.get('nick');\r |
| 963 | \r |
| 964 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_connection_connecting').fetch(server, port.toString()));\r |
| 965 | \r |
| 966 | _kiwi.gateway.newConnection({\r |
| 967 | nick: nick,\r |
| 968 | host: server,\r |
| 969 | port: port,\r |
| 970 | ssl: ssl,\r |
| 971 | password: password\r |
| 972 | }, function(err, new_connection) {\r |
| 973 | if (err)\r |
| 974 | _kiwi.app.panels().active.addMsg('', _kiwi.global.i18n.translate('client_models_application_connection_error').fetch(server, port.toString(), err.toString()));\r |
| 975 | });\r |
| 976 | }\r |
| 977 | \r |
| 978 | \r |
| 979 | \r |
| 980 | \r |
| 981 | \r |
| 982 | this.isChannelName = function (channel_name) {\r |
| 983 | var channel_prefix = _kiwi.gateway.get('channel_prefix');\r |
| 984 | \r |
| 985 | if (!channel_name || !channel_name.length) return false;\r |
| 986 | return (channel_prefix.indexOf(channel_name[0]) > -1);\r |
| 987 | };\r |
| 988 | \r |
| 989 | \r |
| 990 | };\r |
| 991 | \r |
| 992 | \r |
| 993 | model = Backbone.Model.extend(new model());\r |
| 994 | \r |
| 995 | return new model(arguments);\r |
| 996 | };\r |