| 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 | /** _kiwi.view.Application */\r |
| 11 | this.view = null;\r |
| 12 | \r |
| 13 | /** _kiwi.view.StatusMessage */\r |
| 14 | this.message = null;\r |
| 15 | \r |
| 16 | /* Address for the kiwi server */\r |
| 17 | this.kiwi_server = null;\r |
| 18 | \r |
| 19 | this.initialize = function (options) {\r |
| 20 | that = this;\r |
| 21 | \r |
| 22 | if (options[0].container) {\r |
| 23 | this.set('container', options[0].container);\r |
| 24 | }\r |
| 25 | \r |
| 26 | // The base url to the kiwi server\r |
| 27 | this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');\r |
| 28 | \r |
| 29 | // Any options sent down from the server\r |
| 30 | this.server_settings = options[0].server_settings || {};\r |
| 31 | \r |
| 32 | // Best guess at where the kiwi server is\r |
| 33 | this.detectKiwiServer();\r |
| 34 | \r |
| 35 | // Takes instances of model_network\r |
| 36 | this.connections = new _kiwi.model.NetworkPanelList();\r |
| 37 | };\r |
| 38 | \r |
| 39 | \r |
| 40 | this.start = function () {\r |
| 41 | // Only debug if set in the querystring\r |
| 42 | if (!getQueryVariable('debug')) {\r |
| 43 | manageDebug(false);\r |
| 44 | } else {\r |
| 45 | //manageDebug(true);\r |
| 46 | }\r |
| 47 | \r |
| 48 | // Set the gateway up\r |
| 49 | _kiwi.gateway = new _kiwi.model.Gateway();\r |
| 50 | this.bindGatewayCommands(_kiwi.gateway);\r |
| 51 | \r |
| 52 | this.initializeClient();\r |
| 53 | this.initializeGlobals();\r |
| 54 | \r |
| 55 | this.view.barsHide(true);\r |
| 56 | \r |
| 57 | this.showIntialConenctionDialog();\r |
| 58 | };\r |
| 59 | \r |
| 60 | \r |
| 61 | this.detectKiwiServer = function () {\r |
| 62 | // If running from file, default to localhost:7777 by default\r |
| 63 | if (window.location.protocol === 'file:') {\r |
| 64 | this.kiwi_server = 'http://localhost:7778';\r |
| 65 | } else {\r |
| 66 | // Assume the kiwi server is on the same server\r |
| 67 | this.kiwi_server = window.location.protocol + '//' + window.location.host;\r |
| 68 | }\r |
| 69 | };\r |
| 70 | \r |
| 71 | \r |
| 72 | this.showIntialConenctionDialog = function() {\r |
| 73 | var connection_dialog = new _kiwi.model.NewConnection();\r |
| 74 | this.populateDefaultServerSettings(connection_dialog);\r |
| 75 | \r |
| 76 | connection_dialog.view.$el.addClass('initial');\r |
| 77 | this.view.$el.find('.panel_container:first').append(connection_dialog.view.$el);\r |
| 78 | \r |
| 79 | var $info = $($('#tmpl_new_connection_info').html());\r |
| 80 | \r |
| 81 | if ($info.html()) {\r |
| 82 | connection_dialog.view.infoBoxSet($info);\r |
| 83 | connection_dialog.view.infoBoxShow();\r |
| 84 | }\r |
| 85 | \r |
| 86 | // TODO: Shouldn't really be here but it's not working in the view.. :/\r |
| 87 | // Hack for firefox browers: Focus is not given on this event loop iteration\r |
| 88 | setTimeout(function(){\r |
| 89 | connection_dialog.view.$el.find('.nick').select();\r |
| 90 | }, 0);\r |
| 91 | \r |
| 92 | // Once connected, close this dialog and remove its own event\r |
| 93 | var fn = function() {\r |
| 94 | connection_dialog.view.$el.slideUp(function() {\r |
| 95 | connection_dialog.view.dispose();\r |
| 96 | connection_dialog = null;\r |
| 97 | \r |
| 98 | _kiwi.gateway.off('onconnect', fn);\r |
| 99 | });\r |
| 100 | \r |
| 101 | };\r |
| 102 | _kiwi.gateway.on('onconnect', fn);\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 | // Applets panel list\r |
| 110 | this.applet_panels = new _kiwi.model.PanelList();\r |
| 111 | this.applet_panels.view.$el.addClass('panellist applets');\r |
| 112 | this.view.$el.find('#tabs').append(this.applet_panels.view.$el);\r |
| 113 | \r |
| 114 | /**\r |
| 115 | * Set the UI components up\r |
| 116 | */\r |
| 117 | this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]});\r |
| 118 | this.bindControllboxCommands(this.controlbox);\r |
| 119 | \r |
| 120 | this.topicbar = new _kiwi.view.TopicBar({el: $('#topic')[0]});\r |
| 121 | \r |
| 122 | new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r |
| 123 | \r |
| 124 | this.message = new _kiwi.view.StatusMessage({el: $('#status_message')[0]});\r |
| 125 | \r |
| 126 | this.resize_handle = new _kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});\r |
| 127 | \r |
| 128 | // Rejigg the UI sizes\r |
| 129 | this.view.doLayout();\r |
| 130 | };\r |
| 131 | \r |
| 132 | \r |
| 133 | this.initializeGlobals = function () {\r |
| 134 | _kiwi.global.connections = this.connections;\r |
| 135 | \r |
| 136 | _kiwi.global.panels = this.panels;\r |
| 137 | _kiwi.global.panels.applets = this.applet_panels;\r |
| 138 | \r |
| 139 | _kiwi.global.components.Applet = _kiwi.model.Applet;\r |
| 140 | _kiwi.global.components.Panel =_kiwi.model.Panel;\r |
| 141 | };\r |
| 142 | \r |
| 143 | \r |
| 144 | this.populateDefaultServerSettings = function (new_connection_dialog) {\r |
| 145 | var parts;\r |
| 146 | var defaults = {\r |
| 147 | nick: '',\r |
| 148 | server: '',\r |
| 149 | port: 6667,\r |
| 150 | ssl: false,\r |
| 151 | channel: '#chat',\r |
| 152 | channel_key: ''\r |
| 153 | };\r |
| 154 | var uricheck;\r |
| 155 | \r |
| 156 | \r |
| 157 | /**\r |
| 158 | * Get any settings set by the server\r |
| 159 | * These settings may be changed in the server selection dialog or via URL parameters\r |
| 160 | */\r |
| 161 | if (this.server_settings.client) {\r |
| 162 | if (this.server_settings.client.nick)\r |
| 163 | defaults.nick = this.server_settings.client.nick;\r |
| 164 | \r |
| 165 | if (this.server_settings.client.server)\r |
| 166 | defaults.server = this.server_settings.client.server;\r |
| 167 | \r |
| 168 | if (this.server_settings.client.port)\r |
| 169 | defaults.port = this.server_settings.client.port;\r |
| 170 | \r |
| 171 | if (this.server_settings.client.ssl)\r |
| 172 | defaults.ssl = this.server_settings.client.ssl;\r |
| 173 | \r |
| 174 | if (this.server_settings.client.channel)\r |
| 175 | defaults.channel = this.server_settings.client.channel;\r |
| 176 | }\r |
| 177 | \r |
| 178 | \r |
| 179 | \r |
| 180 | /**\r |
| 181 | * Get any settings passed in the URL\r |
| 182 | * These settings may be changed in the server selection dialog\r |
| 183 | */\r |
| 184 | \r |
| 185 | // Any query parameters first\r |
| 186 | if (getQueryVariable('nick'))\r |
| 187 | defaults.nick = getQueryVariable('nick');\r |
| 188 | \r |
| 189 | if (window.location.hash)\r |
| 190 | defaults.channel = window.location.hash;\r |
| 191 | \r |
| 192 | \r |
| 193 | // Process the URL part by part, extracting as we go\r |
| 194 | parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');\r |
| 195 | \r |
| 196 | if (parts.length > 0) {\r |
| 197 | parts.shift();\r |
| 198 | \r |
| 199 | if (parts.length > 0 && parts[0]) {\r |
| 200 | // 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 |
| 201 | uricheck = parts[0].substr(0, 7).toLowerCase();\r |
| 202 | if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {\r |
| 203 | parts[0] = decodeURIComponent(parts[0]);\r |
| 204 | // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r |
| 205 | uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r |
| 206 | /*\r |
| 207 | uricheck[1] = ssl (optional)\r |
| 208 | uricheck[2] = host\r |
| 209 | uricheck[3] = port (optional)\r |
| 210 | uricheck[4] = channel (optional)\r |
| 211 | uricheck[5] = channel key (optional, channel must also be set)\r |
| 212 | */\r |
| 213 | if (uricheck) {\r |
| 214 | if (typeof uricheck[1] !== 'undefined') {\r |
| 215 | defaults.ssl = true;\r |
| 216 | if (defaults.port === 6667) {\r |
| 217 | defaults.port = 6697;\r |
| 218 | }\r |
| 219 | }\r |
| 220 | defaults.server = uricheck[2];\r |
| 221 | if (typeof uricheck[3] !== 'undefined') {\r |
| 222 | defaults.port = uricheck[3];\r |
| 223 | }\r |
| 224 | if (typeof uricheck[4] !== 'undefined') {\r |
| 225 | defaults.channel = '#' + uricheck[4];\r |
| 226 | if (typeof uricheck[5] !== 'undefined') {\r |
| 227 | defaults.channel_key = uricheck[5];\r |
| 228 | }\r |
| 229 | }\r |
| 230 | }\r |
| 231 | parts = [];\r |
| 232 | } else {\r |
| 233 | // Extract the port+ssl if we find one\r |
| 234 | if (parts[0].search(/:/) > 0) {\r |
| 235 | defaults.port = parts[0].substring(parts[0].search(/:/) + 1);\r |
| 236 | defaults.server = parts[0].substring(0, parts[0].search(/:/));\r |
| 237 | if (defaults.port[0] === '+') {\r |
| 238 | defaults.port = parseInt(defaults.port.substring(1), 10);\r |
| 239 | defaults.ssl = true;\r |
| 240 | } else {\r |
| 241 | defaults.ssl = false;\r |
| 242 | }\r |
| 243 | \r |
| 244 | } else {\r |
| 245 | defaults.server = parts[0];\r |
| 246 | }\r |
| 247 | \r |
| 248 | parts.shift();\r |
| 249 | }\r |
| 250 | }\r |
| 251 | \r |
| 252 | if (parts.length > 0 && parts[0]) {\r |
| 253 | defaults.channel = '#' + parts[0];\r |
| 254 | parts.shift();\r |
| 255 | }\r |
| 256 | }\r |
| 257 | \r |
| 258 | // If any settings have been given by the server.. override any auto detected settings\r |
| 259 | /**\r |
| 260 | * Get any server restrictions as set in the server config\r |
| 261 | * These settings can not be changed in the server selection dialog\r |
| 262 | */\r |
| 263 | if (this.server_settings && this.server_settings.connection) {\r |
| 264 | if (this.server_settings.connection.server) {\r |
| 265 | defaults.server = this.server_settings.connection.server;\r |
| 266 | }\r |
| 267 | \r |
| 268 | if (this.server_settings.connection.port) {\r |
| 269 | defaults.port = this.server_settings.connection.port;\r |
| 270 | }\r |
| 271 | \r |
| 272 | if (this.server_settings.connection.ssl) {\r |
| 273 | defaults.ssl = this.server_settings.connection.ssl;\r |
| 274 | }\r |
| 275 | \r |
| 276 | if (this.server_settings.connection.channel) {\r |
| 277 | defaults.channel = this.server_settings.connection.channel;\r |
| 278 | }\r |
| 279 | \r |
| 280 | if (this.server_settings.connection.nick) {\r |
| 281 | defaults.nick = this.server_settings.connection.nick;\r |
| 282 | }\r |
| 283 | }\r |
| 284 | \r |
| 285 | // Set any random numbers if needed\r |
| 286 | defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());\r |
| 287 | \r |
| 288 | // Populate the server select box with defaults\r |
| 289 | new_connection_dialog.view.populateFields(defaults);\r |
| 290 | };\r |
| 291 | \r |
| 292 | \r |
| 293 | this.panels = (function() {\r |
| 294 | var fn = function(panel_type) {\r |
| 295 | var panels;\r |
| 296 | \r |
| 297 | // Default panel type\r |
| 298 | panel_type = panel_type || 'connections';\r |
| 299 | \r |
| 300 | switch (panel_type) {\r |
| 301 | case 'connections':\r |
| 302 | panels = this.connections.panels();\r |
| 303 | break;\r |
| 304 | case 'applets':\r |
| 305 | panels = this.applet_panels.models;\r |
| 306 | break;\r |
| 307 | }\r |
| 308 | \r |
| 309 | // Active panels / server\r |
| 310 | panels.active = this.connections.active_panel;\r |
| 311 | panels.server = this.connections.active_connection ?\r |
| 312 | this.connections.active_connection.panels.server :\r |
| 313 | null;\r |
| 314 | \r |
| 315 | return panels;\r |
| 316 | };\r |
| 317 | \r |
| 318 | _.extend(fn, Backbone.Events);\r |
| 319 | \r |
| 320 | return fn;\r |
| 321 | })();\r |
| 322 | \r |
| 323 | \r |
| 324 | this.bindGatewayCommands = function (gw) {\r |
| 325 | gw.on('onconnect', function (event) {\r |
| 326 | that.view.barsShow();\r |
| 327 | \r |
| 328 | if (auto_connect_details.channel) {\r |
| 329 | that.controlbox.processInput('/JOIN ' + auto_connect_details.channel + ' ' + auto_connect_details.channel_key);\r |
| 330 | }\r |
| 331 | });\r |
| 332 | \r |
| 333 | \r |
| 334 | (function () {\r |
| 335 | var gw_stat = 0;\r |
| 336 | \r |
| 337 | gw.on('disconnect', function (event) {\r |
| 338 | var msg = 'You have been disconnected. Attempting to reconnect for you..';\r |
| 339 | that.message.text(msg, {timeout: 10000});\r |
| 340 | \r |
| 341 | that.view.$el.removeClass('connected');\r |
| 342 | \r |
| 343 | // Mention the disconnection on every channel\r |
| 344 | $.each(_kiwi.app.connections.getByConnectionId(0).panels.models, function (idx, panel) {\r |
| 345 | if (!panel || !panel.isChannel()) return;\r |
| 346 | panel.addMsg('', msg, 'action quit');\r |
| 347 | });\r |
| 348 | _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action quit');\r |
| 349 | \r |
| 350 | gw_stat = 1;\r |
| 351 | });\r |
| 352 | gw.on('reconnecting', function (event) {\r |
| 353 | msg = 'You have been disconnected. Attempting to reconnect again in ' + (event.delay/1000) + ' seconds..';\r |
| 354 | _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action quit');\r |
| 355 | });\r |
| 356 | gw.on('onconnect', function (event) {\r |
| 357 | that.view.$el.addClass('connected');\r |
| 358 | if (gw_stat !== 1) return;\r |
| 359 | \r |
| 360 | var msg = 'It\'s OK, you\'re connected again :)';\r |
| 361 | that.message.text(msg, {timeout: 5000});\r |
| 362 | \r |
| 363 | // Mention the disconnection on every channel\r |
| 364 | $.each(_kiwi.app.connections.getByConnectionId(0).panels.models, function (idx, panel) {\r |
| 365 | if (!panel || !panel.isChannel()) return;\r |
| 366 | panel.addMsg('', msg, 'action join');\r |
| 367 | });\r |
| 368 | _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action join');\r |
| 369 | \r |
| 370 | gw_stat = 0;\r |
| 371 | });\r |
| 372 | })();\r |
| 373 | \r |
| 374 | };\r |
| 375 | \r |
| 376 | \r |
| 377 | \r |
| 378 | /**\r |
| 379 | * Bind to certain commands that may be typed into the control box\r |
| 380 | */\r |
| 381 | this.bindControllboxCommands = function (controlbox) {\r |
| 382 | // Default aliases\r |
| 383 | $.extend(controlbox.preprocessor.aliases, {\r |
| 384 | // General aliases\r |
| 385 | '/p': '/part $1+',\r |
| 386 | '/me': '/action $1+',\r |
| 387 | '/j': '/join $1+',\r |
| 388 | '/q': '/query $1+',\r |
| 389 | \r |
| 390 | // Op related aliases\r |
| 391 | '/op': '/quote mode $channel +o $1+',\r |
| 392 | '/deop': '/quote mode $channel -o $1+',\r |
| 393 | '/hop': '/quote mode $channel +h $1+',\r |
| 394 | '/dehop': '/quote mode $channel -h $1+',\r |
| 395 | '/voice': '/quote mode $channel +v $1+',\r |
| 396 | '/devoice': '/quote mode $channel -v $1+',\r |
| 397 | '/k': '/kick $channel $1+',\r |
| 398 | \r |
| 399 | // Misc aliases\r |
| 400 | '/slap': '/me slaps $1 around a bit with a large trout'\r |
| 401 | });\r |
| 402 | \r |
| 403 | controlbox.on('unknown_command', unknownCommand);\r |
| 404 | \r |
| 405 | controlbox.on('command', allCommands);\r |
| 406 | controlbox.on('command:msg', msgCommand);\r |
| 407 | \r |
| 408 | controlbox.on('command:action', actionCommand);\r |
| 409 | \r |
| 410 | controlbox.on('command:join', joinCommand);\r |
| 411 | \r |
| 412 | controlbox.on('command:part', partCommand);\r |
| 413 | \r |
| 414 | controlbox.on('command:nick', function (ev) {\r |
| 415 | _kiwi.gateway.changeNick(null, ev.params[0]);\r |
| 416 | });\r |
| 417 | \r |
| 418 | controlbox.on('command:query', queryCommand);\r |
| 419 | \r |
| 420 | controlbox.on('command:topic', topicCommand);\r |
| 421 | \r |
| 422 | controlbox.on('command:notice', noticeCommand);\r |
| 423 | \r |
| 424 | controlbox.on('command:quote', quoteCommand);\r |
| 425 | \r |
| 426 | controlbox.on('command:kick', kickCommand);\r |
| 427 | \r |
| 428 | controlbox.on('command:clear', clearCommand);\r |
| 429 | \r |
| 430 | controlbox.on('command:ctcp', ctcpCommand);\r |
| 431 | \r |
| 432 | controlbox.on('command:server', serverCommand);\r |
| 433 | \r |
| 434 | \r |
| 435 | controlbox.on('command:css', function (ev) {\r |
| 436 | var queryString = '?reload=' + new Date().getTime();\r |
| 437 | $('link[rel="stylesheet"]').each(function () {\r |
| 438 | this.href = this.href.replace(/\?.*|$/, queryString);\r |
| 439 | });\r |
| 440 | });\r |
| 441 | \r |
| 442 | controlbox.on('command:js', function (ev) {\r |
| 443 | if (!ev.params[0]) return;\r |
| 444 | $script(ev.params[0] + '?' + (new Date().getTime()));\r |
| 445 | });\r |
| 446 | \r |
| 447 | \r |
| 448 | controlbox.on('command:set', function (ev) {\r |
| 449 | if (!ev.params[0]) return;\r |
| 450 | \r |
| 451 | var setting = ev.params[0],\r |
| 452 | value;\r |
| 453 | \r |
| 454 | // Do we have a second param to set a value?\r |
| 455 | if (ev.params[1]) {\r |
| 456 | ev.params.shift();\r |
| 457 | \r |
| 458 | value = ev.params.join(' ');\r |
| 459 | \r |
| 460 | // If we're setting a true boolean value..\r |
| 461 | if (value === 'true')\r |
| 462 | value = true;\r |
| 463 | \r |
| 464 | // If we're setting a false boolean value..\r |
| 465 | if (value === 'false')\r |
| 466 | value = false;\r |
| 467 | \r |
| 468 | // If we're setting a number..\r |
| 469 | if (parseInt(value, 10).toString() === value)\r |
| 470 | value = parseInt(value, 10);\r |
| 471 | \r |
| 472 | _kiwi.global.settings.set(setting, value);\r |
| 473 | }\r |
| 474 | \r |
| 475 | // Read the value to the user\r |
| 476 | _kiwi.app.panels().active.addMsg('', setting + ' = ' + _kiwi.global.settings.get(setting).toString());\r |
| 477 | });\r |
| 478 | \r |
| 479 | \r |
| 480 | controlbox.on('command:save', function (ev) {\r |
| 481 | _kiwi.global.settings.save();\r |
| 482 | _kiwi.app.panels().active.addMsg('', 'Settings have been saved');\r |
| 483 | });\r |
| 484 | \r |
| 485 | \r |
| 486 | controlbox.on('command:alias', function (ev) {\r |
| 487 | var name, rule;\r |
| 488 | \r |
| 489 | // No parameters passed so list them\r |
| 490 | if (!ev.params[1]) {\r |
| 491 | $.each(controlbox.preprocessor.aliases, function (name, rule) {\r |
| 492 | _kiwi.app.panels().server.addMsg(' ', name + ' => ' + rule);\r |
| 493 | });\r |
| 494 | return;\r |
| 495 | }\r |
| 496 | \r |
| 497 | // Deleting an alias?\r |
| 498 | if (ev.params[0] === 'del' || ev.params[0] === 'delete') {\r |
| 499 | name = ev.params[1];\r |
| 500 | if (name[0] !== '/') name = '/' + name;\r |
| 501 | delete controlbox.preprocessor.aliases[name];\r |
| 502 | return;\r |
| 503 | }\r |
| 504 | \r |
| 505 | // Add the alias\r |
| 506 | name = ev.params[0];\r |
| 507 | ev.params.shift();\r |
| 508 | rule = ev.params.join(' ');\r |
| 509 | \r |
| 510 | // Make sure the name starts with a slash\r |
| 511 | if (name[0] !== '/') name = '/' + name;\r |
| 512 | \r |
| 513 | // Now actually add the alias\r |
| 514 | controlbox.preprocessor.aliases[name] = rule;\r |
| 515 | });\r |
| 516 | \r |
| 517 | \r |
| 518 | controlbox.on('command:ignore', function (ev) {\r |
| 519 | var list = _kiwi.gateway.get('ignore_list');\r |
| 520 | \r |
| 521 | // No parameters passed so list them\r |
| 522 | if (!ev.params[0]) {\r |
| 523 | if (list.length > 0) {\r |
| 524 | _kiwi.app.panels().active.addMsg(' ', 'Ignored nicks:');\r |
| 525 | $.each(list, function (idx, ignored_pattern) {\r |
| 526 | _kiwi.app.panels().active.addMsg(' ', ignored_pattern);\r |
| 527 | });\r |
| 528 | } else {\r |
| 529 | _kiwi.app.panels().active.addMsg(' ', 'Not ignoring anybody');\r |
| 530 | }\r |
| 531 | return;\r |
| 532 | }\r |
| 533 | \r |
| 534 | // We have a parameter, so add it\r |
| 535 | list.push(ev.params[0]);\r |
| 536 | _kiwi.gateway.set('ignore_list', list);\r |
| 537 | _kiwi.app.panels().active.addMsg(' ', 'Ignoring ' + ev.params[0]);\r |
| 538 | });\r |
| 539 | \r |
| 540 | \r |
| 541 | controlbox.on('command:unignore', function (ev) {\r |
| 542 | var list = _kiwi.gateway.get('ignore_list');\r |
| 543 | \r |
| 544 | if (!ev.params[0]) {\r |
| 545 | _kiwi.app.panels().active.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');\r |
| 546 | return;\r |
| 547 | }\r |
| 548 | \r |
| 549 | list = _.reject(list, function(pattern) {\r |
| 550 | return pattern === ev.params[0];\r |
| 551 | });\r |
| 552 | \r |
| 553 | _kiwi.gateway.set('ignore_list', list);\r |
| 554 | \r |
| 555 | _kiwi.app.panels().active.addMsg(' ', 'Stopped ignoring ' + ev.params[0]);\r |
| 556 | });\r |
| 557 | \r |
| 558 | \r |
| 559 | controlbox.on('command:applet', appletCommand);\r |
| 560 | controlbox.on('command:settings', settingsCommand);\r |
| 561 | controlbox.on('command:script', scriptCommand);\r |
| 562 | };\r |
| 563 | \r |
| 564 | // A fallback action. Send a raw command to the server\r |
| 565 | function unknownCommand (ev) {\r |
| 566 | var raw_cmd = ev.command + ' ' + ev.params.join(' ');\r |
| 567 | console.log('RAW: ' + raw_cmd);\r |
| 568 | _kiwi.gateway.raw(null, raw_cmd);\r |
| 569 | }\r |
| 570 | \r |
| 571 | function allCommands (ev) {}\r |
| 572 | \r |
| 573 | function joinCommand (ev) {\r |
| 574 | var panels, channel_names;\r |
| 575 | \r |
| 576 | channel_names = ev.params.join(' ').split(',');\r |
| 577 | panels = that.connections.active_connection.createAndJoinChannels(channel_names);\r |
| 578 | \r |
| 579 | // Show the last channel if we have one\r |
| 580 | if (panels)\r |
| 581 | panels[panels.length - 1].view.show();\r |
| 582 | }\r |
| 583 | \r |
| 584 | function queryCommand (ev) {\r |
| 585 | var destination, panel;\r |
| 586 | \r |
| 587 | destination = ev.params[0];\r |
| 588 | \r |
| 589 | // Check if we have the panel already. If not, create it\r |
| 590 | panel = that.connections.active_connection.panels.getByName(destination);\r |
| 591 | if (!panel) {\r |
| 592 | panel = new _kiwi.model.Query({name: destination});\r |
| 593 | that.connections.active_connection.panels.add(panel);\r |
| 594 | }\r |
| 595 | \r |
| 596 | if (panel) panel.view.show();\r |
| 597 | \r |
| 598 | }\r |
| 599 | \r |
| 600 | function msgCommand (ev) {\r |
| 601 | var destination = ev.params[0],\r |
| 602 | panel = that.connections.active_connection.panels.getByName(destination) || that.panels().server;\r |
| 603 | \r |
| 604 | ev.params.shift();\r |
| 605 | \r |
| 606 | panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), ev.params.join(' '));\r |
| 607 | _kiwi.gateway.privmsg(null, destination, ev.params.join(' '));\r |
| 608 | }\r |
| 609 | \r |
| 610 | function actionCommand (ev) {\r |
| 611 | if (_kiwi.app.panels().active.isServer()) {\r |
| 612 | return;\r |
| 613 | }\r |
| 614 | \r |
| 615 | var panel = _kiwi.app.panels().active;\r |
| 616 | panel.addMsg('', '* ' + _kiwi.app.connections.active_connection.get('nick') + ' ' + ev.params.join(' '), 'action');\r |
| 617 | _kiwi.gateway.action(null, panel.get('name'), ev.params.join(' '));\r |
| 618 | }\r |
| 619 | \r |
| 620 | function partCommand (ev) {\r |
| 621 | if (ev.params.length === 0) {\r |
| 622 | _kiwi.gateway.part(null, _kiwi.app.panels().active.get('name'));\r |
| 623 | } else {\r |
| 624 | _.each(ev.params, function (channel) {\r |
| 625 | _kiwi.gateway.part(null, channel);\r |
| 626 | });\r |
| 627 | }\r |
| 628 | }\r |
| 629 | \r |
| 630 | function topicCommand (ev) {\r |
| 631 | var channel_name;\r |
| 632 | \r |
| 633 | if (ev.params.length === 0) return;\r |
| 634 | \r |
| 635 | if (that.isChannelName(ev.params[0])) {\r |
| 636 | channel_name = ev.params[0];\r |
| 637 | ev.params.shift();\r |
| 638 | } else {\r |
| 639 | channel_name = _kiwi.app.panels().active.get('name');\r |
| 640 | }\r |
| 641 | \r |
| 642 | _kiwi.gateway.topic(null, channel_name, ev.params.join(' '));\r |
| 643 | }\r |
| 644 | \r |
| 645 | function noticeCommand (ev) {\r |
| 646 | var destination;\r |
| 647 | \r |
| 648 | // Make sure we have a destination and some sort of message\r |
| 649 | if (ev.params.length <= 1) return;\r |
| 650 | \r |
| 651 | destination = ev.params[0];\r |
| 652 | ev.params.shift();\r |
| 653 | \r |
| 654 | _kiwi.gateway.notice(null, destination, ev.params.join(' '));\r |
| 655 | }\r |
| 656 | \r |
| 657 | function quoteCommand (ev) {\r |
| 658 | var raw = ev.params.join(' ');\r |
| 659 | _kiwi.gateway.raw(null, raw);\r |
| 660 | }\r |
| 661 | \r |
| 662 | function kickCommand (ev) {\r |
| 663 | var nick, panel = _kiwi.app.panels().active;\r |
| 664 | \r |
| 665 | if (!panel.isChannel()) return;\r |
| 666 | \r |
| 667 | // Make sure we have a nick\r |
| 668 | if (ev.params.length === 0) return;\r |
| 669 | \r |
| 670 | nick = ev.params[0];\r |
| 671 | ev.params.shift();\r |
| 672 | \r |
| 673 | _kiwi.gateway.kick(null, panel.get('name'), nick, ev.params.join(' '));\r |
| 674 | }\r |
| 675 | \r |
| 676 | function clearCommand (ev) {\r |
| 677 | // Can't clear a server or applet panel\r |
| 678 | if (_kiwi.app.panels().active.isServer() || _kiwi.app.panels().active.isApplet()) {\r |
| 679 | return;\r |
| 680 | }\r |
| 681 | \r |
| 682 | if (_kiwi.app.panels().active.clearMessages) {\r |
| 683 | _kiwi.app.panels().active.clearMessages();\r |
| 684 | }\r |
| 685 | }\r |
| 686 | \r |
| 687 | function ctcpCommand(ev) {\r |
| 688 | var target, type;\r |
| 689 | \r |
| 690 | // Make sure we have a target and a ctcp type (eg. version, time)\r |
| 691 | if (ev.params.length < 2) return;\r |
| 692 | \r |
| 693 | target = ev.params[0];\r |
| 694 | ev.params.shift();\r |
| 695 | \r |
| 696 | type = ev.params[0];\r |
| 697 | ev.params.shift();\r |
| 698 | \r |
| 699 | _kiwi.gateway.ctcp(null, true, type, target, ev.params.join(' '));\r |
| 700 | }\r |
| 701 | \r |
| 702 | function settingsCommand (ev) {\r |
| 703 | var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');\r |
| 704 | settings.view.show();\r |
| 705 | }\r |
| 706 | \r |
| 707 | function scriptCommand (ev) {\r |
| 708 | var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');\r |
| 709 | editor.view.show();\r |
| 710 | }\r |
| 711 | \r |
| 712 | function appletCommand (ev) {\r |
| 713 | if (!ev.params[0]) return;\r |
| 714 | \r |
| 715 | var panel = new _kiwi.model.Applet();\r |
| 716 | \r |
| 717 | if (ev.params[1]) {\r |
| 718 | // Url and name given\r |
| 719 | panel.load(ev.params[0], ev.params[1]);\r |
| 720 | } else {\r |
| 721 | // Load a pre-loaded applet\r |
| 722 | if (_kiwi.applets[ev.params[0]]) {\r |
| 723 | panel.load(new _kiwi.applets[ev.params[0]]());\r |
| 724 | } else {\r |
| 725 | _kiwi.app.panels().server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');\r |
| 726 | return;\r |
| 727 | }\r |
| 728 | }\r |
| 729 | \r |
| 730 | _kiwi.app.connections.active_connection.panels.add(panel);\r |
| 731 | panel.view.show();\r |
| 732 | }\r |
| 733 | \r |
| 734 | \r |
| 735 | function serverCommand (ev) {\r |
| 736 | var server, port, ssl, password, nick,\r |
| 737 | tmp;\r |
| 738 | \r |
| 739 | // If no server address given, show the new connection dialog\r |
| 740 | if (!ev.params[0]) {\r |
| 741 | tmp = new _kiwi.view.MenuBox('New Connection');\r |
| 742 | tmp.addItem('new_connection', new _kiwi.model.NewConnection().view.$el);\r |
| 743 | tmp.show();\r |
| 744 | \r |
| 745 | // Center screen the dialog\r |
| 746 | tmp.$el.offset({\r |
| 747 | top: (that.view.$el.height() / 2) - (tmp.$el.height() / 2),\r |
| 748 | left: (that.view.$el.width() / 2) - (tmp.$el.width() / 2)\r |
| 749 | });\r |
| 750 | \r |
| 751 | return;\r |
| 752 | }\r |
| 753 | \r |
| 754 | // Port given in 'host:port' format and no specific port given after a space\r |
| 755 | if (ev.params[0].indexOf(':') > 0) {\r |
| 756 | tmp = ev.params[0].split(':');\r |
| 757 | server = tmp[0];\r |
| 758 | port = tmp[1];\r |
| 759 | \r |
| 760 | password = ev.params[1] || undefined;\r |
| 761 | \r |
| 762 | } else {\r |
| 763 | // Server + port given as 'host port'\r |
| 764 | server = ev.params[0];\r |
| 765 | port = ev.params[1] || 6667;\r |
| 766 | \r |
| 767 | password = ev.params[2] || undefined;\r |
| 768 | }\r |
| 769 | \r |
| 770 | // + in the port means SSL\r |
| 771 | if (port.toString()[0] === '+') {\r |
| 772 | ssl = true;\r |
| 773 | port = parseInt(port.substring(1), 10);\r |
| 774 | } else {\r |
| 775 | ssl = false;\r |
| 776 | }\r |
| 777 | \r |
| 778 | // Default port if one wasn't found\r |
| 779 | port = port || 6667;\r |
| 780 | \r |
| 781 | // Use the same nick as we currently have\r |
| 782 | nick = _kiwi.app.connections.active_connection.get('nick');\r |
| 783 | \r |
| 784 | _kiwi.app.panels().active.addMsg('', 'Connecting to ' + server + ':' + port.toString() + '..');\r |
| 785 | \r |
| 786 | _kiwi.gateway.newConnection({\r |
| 787 | nick: nick,\r |
| 788 | host: server,\r |
| 789 | port: port,\r |
| 790 | ssl: ssl,\r |
| 791 | password: password\r |
| 792 | }, function(err, new_connection) {\r |
| 793 | if (err)\r |
| 794 | _kiwi.app.panels().active.addMsg('', 'Error connecting to ' + server + ':' + port.toString() + ' (' + err.toString() + ')');\r |
| 795 | });\r |
| 796 | }\r |
| 797 | \r |
| 798 | \r |
| 799 | \r |
| 800 | \r |
| 801 | \r |
| 802 | this.isChannelName = function (channel_name) {\r |
| 803 | var channel_prefix = _kiwi.gateway.get('channel_prefix');\r |
| 804 | \r |
| 805 | if (!channel_name || !channel_name.length) return false;\r |
| 806 | return (channel_prefix.indexOf(channel_name[0]) > -1);\r |
| 807 | };\r |
| 808 | \r |
| 809 | \r |
| 810 | };\r |
| 811 | \r |
| 812 | \r |
| 813 | model = Backbone.Model.extend(new model());\r |
| 814 | \r |
| 815 | return new model(arguments);\r |
| 816 | };\r |