| 1 | // Holds anything kiwi client specific (ie. front, gateway, _kiwi.plugs..)\r |
| 2 | /**\r |
| 3 | * @namespace\r |
| 4 | */\r |
| 5 | var _kiwi = {};\r |
| 6 | \r |
| 7 | _kiwi.model = {};\r |
| 8 | _kiwi.view = {};\r |
| 9 | _kiwi.applets = {};\r |
| 10 | \r |
| 11 | \r |
| 12 | /**\r |
| 13 | * A global container for third party access\r |
| 14 | * Will be used to access a limited subset of kiwi functionality\r |
| 15 | * and data (think: plugins)\r |
| 16 | */\r |
| 17 | _kiwi.global = {\r |
| 18 | build_version: '', // Kiwi IRC version this is built from (Set from index.html)\r |
| 19 | settings: undefined, // Instance of _kiwi.model.DataStore\r |
| 20 | plugins: undefined,\r |
| 21 | utils: undefined, // TODO: Re-usable methods\r |
| 22 | user: undefined, // TODO: Limited user methods\r |
| 23 | server: undefined, // TODO: Limited server methods\r |
| 24 | \r |
| 25 | // TODO: think of a better term for this as it will also refer to queries\r |
| 26 | channels: undefined, // TODO: Limited access to panels list\r |
| 27 | \r |
| 28 | addMediaMessageType: function(match, buildHtml) {\r |
| 29 | _kiwi.view.MediaMessage.addType(match, buildHtml);\r |
| 30 | },\r |
| 31 | \r |
| 32 | // Event managers for plugins\r |
| 33 | components: {\r |
| 34 | EventComponent: function(event_source, proxy_event_name) {\r |
| 35 | function proxyEvent(event_name, event_data) {\r |
| 36 | if (proxy_event_name !== 'all') {\r |
| 37 | event_data = event_name.event_data;\r |
| 38 | event_name = event_name.event_name;\r |
| 39 | }\r |
| 40 | \r |
| 41 | this.trigger(event_name, event_data);\r |
| 42 | }\r |
| 43 | \r |
| 44 | // The event we are to proxy\r |
| 45 | proxy_event_name = proxy_event_name || 'all';\r |
| 46 | \r |
| 47 | \r |
| 48 | _.extend(this, Backbone.Events);\r |
| 49 | this._source = event_source;\r |
| 50 | \r |
| 51 | // Proxy the events to this dispatcher\r |
| 52 | event_source.on(proxy_event_name, proxyEvent, this);\r |
| 53 | \r |
| 54 | // Clean up this object\r |
| 55 | this.dispose = function () {\r |
| 56 | event_source.off(proxy_event_name, proxyEvent);\r |
| 57 | this.off();\r |
| 58 | delete this.event_source;\r |
| 59 | };\r |
| 60 | },\r |
| 61 | \r |
| 62 | Network: function(connection_id) {\r |
| 63 | var connection_event;\r |
| 64 | \r |
| 65 | if (typeof connection_id !== 'undefined') {\r |
| 66 | connection_event = 'connection:' + connection_id.toString();\r |
| 67 | }\r |
| 68 | \r |
| 69 | var obj = new this.EventComponent(_kiwi.gateway, connection_event);\r |
| 70 | var funcs = {\r |
| 71 | kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',\r |
| 72 | part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',\r |
| 73 | notice: 'notice', msg: 'privmsg', changeNick: 'changeNick',\r |
| 74 | channelInfo: 'channelInfo', mode: 'mode'\r |
| 75 | };\r |
| 76 | \r |
| 77 | // Proxy each gateway method\r |
| 78 | _.each(funcs, function(gateway_fn, func_name) {\r |
| 79 | obj[func_name] = function() {\r |
| 80 | var fn_name = gateway_fn;\r |
| 81 | \r |
| 82 | // Add connection_id to the argument list\r |
| 83 | var args = Array.prototype.slice.call(arguments, 0);\r |
| 84 | args.unshift(connection_id);\r |
| 85 | \r |
| 86 | // Call the gateway function on behalf of this connection\r |
| 87 | return _kiwi.gateway[fn_name].apply(_kiwi.gateway, args);\r |
| 88 | };\r |
| 89 | });\r |
| 90 | \r |
| 91 | return obj;\r |
| 92 | },\r |
| 93 | \r |
| 94 | ControlInput: function() {\r |
| 95 | var obj = new this.EventComponent(_kiwi.app.controlbox);\r |
| 96 | var funcs = {\r |
| 97 | run: 'processInput', addPluginIcon: 'addPluginIcon'\r |
| 98 | };\r |
| 99 | \r |
| 100 | _.each(funcs, function(controlbox_fn, func_name) {\r |
| 101 | obj[func_name] = function() {\r |
| 102 | var fn_name = controlbox_fn;\r |
| 103 | return _kiwi.app.controlbox[fn_name].apply(_kiwi.app.controlbox, arguments);\r |
| 104 | };\r |
| 105 | });\r |
| 106 | \r |
| 107 | return obj;\r |
| 108 | }\r |
| 109 | },\r |
| 110 | \r |
| 111 | // Entry point to start the kiwi application\r |
| 112 | init: function (opts, callback) {\r |
| 113 | var continueStart, locale;\r |
| 114 | opts = opts || {};\r |
| 115 | \r |
| 116 | continueInit = function (locale, s, xhr) {\r |
| 117 | if (locale) {\r |
| 118 | _kiwi.global.i18n = new Jed(locale);\r |
| 119 | } else {\r |
| 120 | _kiwi.global.i18n = new Jed();\r |
| 121 | }\r |
| 122 | \r |
| 123 | _kiwi.app = new _kiwi.model.Application(opts);\r |
| 124 | \r |
| 125 | // Start the client up\r |
| 126 | _kiwi.app.initializeInterfaces();\r |
| 127 | \r |
| 128 | // Now everything has started up, load the plugin manager for third party plugins\r |
| 129 | _kiwi.global.plugins = new _kiwi.model.PluginManager();\r |
| 130 | \r |
| 131 | callback && callback();\r |
| 132 | };\r |
| 133 | \r |
| 134 | // Set up the settings datastore\r |
| 135 | _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');\r |
| 136 | _kiwi.global.settings.load();\r |
| 137 | \r |
| 138 | // Set the window title\r |
| 139 | window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';\r |
| 140 | \r |
| 141 | locale = _kiwi.global.settings.get('locale');\r |
| 142 | if (!locale) {\r |
| 143 | $.getJSON(opts.base_path + '/assets/locales/magic.json', continueInit);\r |
| 144 | } else {\r |
| 145 | $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueInit);\r |
| 146 | }\r |
| 147 | },\r |
| 148 | \r |
| 149 | start: function() {\r |
| 150 | _kiwi.app.showStartup();\r |
| 151 | },\r |
| 152 | \r |
| 153 | // Allow plugins to change the startup applet\r |
| 154 | registerStartupApplet: function(startup_applet_name) {\r |
| 155 | _kiwi.app.startup_applet_name = startup_applet_name;\r |
| 156 | },\r |
| 157 | \r |
| 158 | /**\r |
| 159 | * Open a new IRC connection\r |
| 160 | * @param {Object} connection_details {nick, host, port, ssl, password, options}\r |
| 161 | * @param {Function} callback function(err, network){}\r |
| 162 | */\r |
| 163 | newIrcConnection: function(connection_details, callback) {\r |
| 164 | _kiwi.gateway.newConnection(connection_details, callback);\r |
| 165 | },\r |
| 166 | \r |
| 167 | \r |
| 168 | /**\r |
| 169 | * Taking settings from the server and URL, extract the default server/channel/nick settings\r |
| 170 | */\r |
| 171 | defaultServerSettings: function () {\r |
| 172 | var parts;\r |
| 173 | var defaults = {\r |
| 174 | nick: '',\r |
| 175 | server: '',\r |
| 176 | port: 6667,\r |
| 177 | ssl: false,\r |
| 178 | channel: '',\r |
| 179 | channel_key: ''\r |
| 180 | };\r |
| 181 | var uricheck;\r |
| 182 | \r |
| 183 | \r |
| 184 | /**\r |
| 185 | * Get any settings set by the server\r |
| 186 | * These settings may be changed in the server selection dialog or via URL parameters\r |
| 187 | */\r |
| 188 | if (_kiwi.app.server_settings.client) {\r |
| 189 | if (_kiwi.app.server_settings.client.nick)\r |
| 190 | defaults.nick = _kiwi.app.server_settings.client.nick;\r |
| 191 | \r |
| 192 | if (_kiwi.app.server_settings.client.server)\r |
| 193 | defaults.server = _kiwi.app.server_settings.client.server;\r |
| 194 | \r |
| 195 | if (_kiwi.app.server_settings.client.port)\r |
| 196 | defaults.port = _kiwi.app.server_settings.client.port;\r |
| 197 | \r |
| 198 | if (_kiwi.app.server_settings.client.ssl)\r |
| 199 | defaults.ssl = _kiwi.app.server_settings.client.ssl;\r |
| 200 | \r |
| 201 | if (_kiwi.app.server_settings.client.channel)\r |
| 202 | defaults.channel = _kiwi.app.server_settings.client.channel;\r |
| 203 | \r |
| 204 | if (_kiwi.app.server_settings.client.channel_key)\r |
| 205 | defaults.channel_key = _kiwi.app.server_settings.client.channel_key;\r |
| 206 | }\r |
| 207 | \r |
| 208 | \r |
| 209 | \r |
| 210 | /**\r |
| 211 | * Get any settings passed in the URL\r |
| 212 | * These settings may be changed in the server selection dialog\r |
| 213 | */\r |
| 214 | \r |
| 215 | // Any query parameters first\r |
| 216 | if (getQueryVariable('nick'))\r |
| 217 | defaults.nick = getQueryVariable('nick');\r |
| 218 | \r |
| 219 | if (window.location.hash)\r |
| 220 | defaults.channel = window.location.hash;\r |
| 221 | \r |
| 222 | \r |
| 223 | // Process the URL part by part, extracting as we go\r |
| 224 | parts = window.location.pathname.toString().replace(_kiwi.app.get('base_path'), '').split('/');\r |
| 225 | \r |
| 226 | if (parts.length > 0) {\r |
| 227 | parts.shift();\r |
| 228 | \r |
| 229 | if (parts.length > 0 && parts[0]) {\r |
| 230 | // 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 |
| 231 | uricheck = parts[0].substr(0, 7).toLowerCase();\r |
| 232 | if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {\r |
| 233 | parts[0] = decodeURIComponent(parts[0]);\r |
| 234 | // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r |
| 235 | uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r |
| 236 | /*\r |
| 237 | uricheck[1] = ssl (optional)\r |
| 238 | uricheck[2] = host\r |
| 239 | uricheck[3] = port (optional)\r |
| 240 | uricheck[4] = channel (optional)\r |
| 241 | uricheck[5] = channel key (optional, channel must also be set)\r |
| 242 | */\r |
| 243 | if (uricheck) {\r |
| 244 | if (typeof uricheck[1] !== 'undefined') {\r |
| 245 | defaults.ssl = true;\r |
| 246 | if (defaults.port === 6667) {\r |
| 247 | defaults.port = 6697;\r |
| 248 | }\r |
| 249 | }\r |
| 250 | defaults.server = uricheck[2];\r |
| 251 | if (typeof uricheck[3] !== 'undefined') {\r |
| 252 | defaults.port = uricheck[3];\r |
| 253 | }\r |
| 254 | if (typeof uricheck[4] !== 'undefined') {\r |
| 255 | defaults.channel = '#' + uricheck[4];\r |
| 256 | if (typeof uricheck[5] !== 'undefined') {\r |
| 257 | defaults.channel_key = uricheck[5];\r |
| 258 | }\r |
| 259 | }\r |
| 260 | }\r |
| 261 | parts = [];\r |
| 262 | } else {\r |
| 263 | // Extract the port+ssl if we find one\r |
| 264 | if (parts[0].search(/:/) > 0) {\r |
| 265 | defaults.port = parts[0].substring(parts[0].search(/:/) + 1);\r |
| 266 | defaults.server = parts[0].substring(0, parts[0].search(/:/));\r |
| 267 | if (defaults.port[0] === '+') {\r |
| 268 | defaults.port = parseInt(defaults.port.substring(1), 10);\r |
| 269 | defaults.ssl = true;\r |
| 270 | } else {\r |
| 271 | defaults.ssl = false;\r |
| 272 | }\r |
| 273 | \r |
| 274 | } else {\r |
| 275 | defaults.server = parts[0];\r |
| 276 | }\r |
| 277 | \r |
| 278 | parts.shift();\r |
| 279 | }\r |
| 280 | }\r |
| 281 | \r |
| 282 | if (parts.length > 0 && parts[0]) {\r |
| 283 | defaults.channel = '#' + parts[0];\r |
| 284 | parts.shift();\r |
| 285 | }\r |
| 286 | }\r |
| 287 | \r |
| 288 | // If any settings have been given by the server.. override any auto detected settings\r |
| 289 | /**\r |
| 290 | * Get any server restrictions as set in the server config\r |
| 291 | * These settings can not be changed in the server selection dialog\r |
| 292 | */\r |
| 293 | if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) {\r |
| 294 | if (_kiwi.app.server_settings.connection.server) {\r |
| 295 | defaults.server = _kiwi.app.server_settings.connection.server;\r |
| 296 | }\r |
| 297 | \r |
| 298 | if (_kiwi.app.server_settings.connection.port) {\r |
| 299 | defaults.port = _kiwi.app.server_settings.connection.port;\r |
| 300 | }\r |
| 301 | \r |
| 302 | if (_kiwi.app.server_settings.connection.ssl) {\r |
| 303 | defaults.ssl = _kiwi.app.server_settings.connection.ssl;\r |
| 304 | }\r |
| 305 | \r |
| 306 | if (_kiwi.app.server_settings.connection.channel) {\r |
| 307 | defaults.channel = _kiwi.app.server_settings.connection.channel;\r |
| 308 | }\r |
| 309 | \r |
| 310 | if (_kiwi.app.server_settings.connection.channel_key) {\r |
| 311 | defaults.channel_key = _kiwi.app.server_settings.connection.channel_key;\r |
| 312 | }\r |
| 313 | \r |
| 314 | if (_kiwi.app.server_settings.connection.nick) {\r |
| 315 | defaults.nick = _kiwi.app.server_settings.connection.nick;\r |
| 316 | }\r |
| 317 | }\r |
| 318 | \r |
| 319 | // Set any random numbers if needed\r |
| 320 | defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());\r |
| 321 | \r |
| 322 | if (getQueryVariable('encoding'))\r |
| 323 | defaults.encoding = getQueryVariable('encoding');\r |
| 324 | \r |
| 325 | return defaults;\r |
| 326 | },\r |
| 327 | };\r |
| 328 | \r |
| 329 | \r |
| 330 | \r |
| 331 | // If within a closure, expose the kiwi globals\r |
| 332 | if (typeof global !== 'undefined') {\r |
| 333 | global.kiwi = _kiwi.global;\r |
| 334 | } else {\r |
| 335 | // Not within a closure so set a var in the current scope\r |
| 336 | var kiwi = _kiwi.global;\r |
| 337 | } |