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