Client: #toolbar change to .toolbar
[KiwiIRC.git] / client / assets / dev / model_application.js
CommitLineData
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 78\r
21e3d147
D
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
f2bb5380
D
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
4047ee2b 94 connection_dialog.view.$el.slideUp(function() {\r
4047ee2b
D
95 connection_dialog.view.dispose();\r
96 connection_dialog = null;\r
97\r
f2bb5380
D
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
696a66f8 106 this.initializeClient = function () {\r
eaaf73b0 107 this.view = new _kiwi.view.Application({model: this, el: this.get('container')});\r
c966123a 108\r
f2bb5380 109 // Applets panel list\r
c966123a
D
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
ce13508b
D
114 /**\r
115 * Set the UI components up\r
116 */\r
eaaf73b0 117 this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]});\r
696a66f8
D
118 this.bindControllboxCommands(this.controlbox);\r
119\r
eaaf73b0 120 this.topicbar = new _kiwi.view.TopicBar({el: $('#topic')[0]});\r
696a66f8 121\r
4f99a29b 122 new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r
7de3dd03 123\r
eaaf73b0 124 this.message = new _kiwi.view.StatusMessage({el: $('#status_message')[0]});\r
696a66f8 125\r
eaaf73b0 126 this.resize_handle = new _kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});\r
696a66f8
D
127\r
128 // Rejigg the UI sizes\r
129 this.view.doLayout();\r
696a66f8
D
130 };\r
131\r
132\r
133 this.initializeGlobals = function () {\r
6d5faa6e 134 _kiwi.global.connections = this.connections;\r
a9bc007f
D
135\r
136 _kiwi.global.panels = this.panels;\r
137 _kiwi.global.panels.applets = this.applet_panels;\r
138\r
6228b635
D
139 _kiwi.global.components.Applet = _kiwi.model.Applet;\r
140 _kiwi.global.components.Panel =_kiwi.model.Panel;\r
696a66f8
D
141 };\r
142\r
143\r
f2bb5380 144 this.populateDefaultServerSettings = function (new_connection_dialog) {\r
696a66f8
D
145 var parts;\r
146 var defaults = {\r
5e84a85a 147 nick: '',\r
46f41dfb 148 server: '',\r
696a66f8
D
149 port: 6667,\r
150 ssl: false,\r
5e84a85a 151 channel: '#chat',\r
2f54e55e 152 channel_key: ''\r
696a66f8 153 };\r
2f54e55e 154 var uricheck;\r
696a66f8 155\r
76391784
D
156\r
157 /**\r
158 * Get any settings set by the server\r
5e84a85a 159 * These settings may be changed in the server selection dialog or via URL parameters\r
76391784
D
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
5e84a85a
D
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
696a66f8
D
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
4f8da37d 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
2f54e55e
JA
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
2f54e55e
JA
204 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r
205 uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r
4f8da37d
JA
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
2f54e55e 213 if (uricheck) {\r
4f8da37d 214 if (typeof uricheck[1] !== 'undefined') {\r
2f54e55e
JA
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
4f8da37d 221 if (typeof uricheck[3] !== 'undefined') {\r
2f54e55e
JA
222 defaults.port = uricheck[3];\r
223 }\r
4f8da37d 224 if (typeof uricheck[4] !== 'undefined') {\r
2f54e55e 225 defaults.channel = '#' + uricheck[4];\r
4f8da37d 226 if (typeof uricheck[5] !== 'undefined') {\r
2f54e55e
JA
227 defaults.channel_key = uricheck[5];\r
228 }\r
229 }\r
230 }\r
2f54e55e
JA
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
696a66f8 244 } else {\r
2f54e55e 245 defaults.server = parts[0];\r
696a66f8
D
246 }\r
247\r
2f54e55e 248 parts.shift();\r
696a66f8 249 }\r
696a66f8
D
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
93e84f75 258 // If any settings have been given by the server.. override any auto detected settings\r
76391784
D
259 /**\r
260 * Get any server restrictions as set in the server config\r
46f41dfb 261 * These settings can not be changed in the server selection dialog\r
76391784 262 */\r
93e84f75
D
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
93e84f75 284\r
696a66f8
D
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
f2bb5380 289 new_connection_dialog.view.populateFields(defaults);\r
696a66f8
D
290 };\r
291\r
292\r
6d5faa6e 293 this.panels = (function() {\r
c966123a
D
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
6d5faa6e
D
308\r
309 // Active panels / server\r
310 panels.active = this.connections.active_panel;\r
a9bc007f
D
311 panels.server = this.connections.active_connection ?\r
312 this.connections.active_connection.panels.server :\r
313 null;\r
6d5faa6e
D
314\r
315 return panels;\r
316 };\r
317\r
318 _.extend(fn, Backbone.Events);\r
319\r
320 return fn;\r
321 })();\r
e4de4648
D
322\r
323\r
696a66f8 324 this.bindGatewayCommands = function (gw) {\r
696a66f8
D
325 gw.on('onconnect', function (event) {\r
326 that.view.barsShow();\r
ce13508b 327\r
696a66f8 328 if (auto_connect_details.channel) {\r
2f54e55e 329 that.controlbox.processInput('/JOIN ' + auto_connect_details.channel + ' ' + auto_connect_details.channel_key);\r
696a66f8
D
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
343cd967
PV
341 that.view.$el.removeClass('connected');\r
342\r
696a66f8 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 quit');\r
347 });\r
6d5faa6e 348 _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action quit');\r
696a66f8
D
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
6d5faa6e 354 _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action quit');\r
696a66f8 355 });\r
343cd967
PV
356 gw.on('onconnect', function (event) {\r
357 that.view.$el.addClass('connected');\r
696a66f8
D
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
6d5faa6e 364 $.each(_kiwi.app.connections.getByConnectionId(0).panels.models, function (idx, panel) {\r
696a66f8
D
365 if (!panel || !panel.isChannel()) return;\r
366 panel.addMsg('', msg, 'action join');\r
367 });\r
6d5faa6e 368 _kiwi.app.connections.getByConnectionId(0).panels.server.addMsg('', msg, 'action join');\r
696a66f8
D
369\r
370 gw_stat = 0;\r
371 });\r
372 })();\r
373\r
696a66f8
D
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
aefbc5e1 397 '/k': '/kick $channel $1+',\r
696a66f8
D
398\r
399 // Misc aliases\r
70c7e208 400 '/slap': '/me slaps $1 around a bit with a large trout'\r
696a66f8
D
401 });\r
402\r
403 controlbox.on('unknown_command', unknownCommand);\r
404\r
405 controlbox.on('command', allCommands);\r
6c58c492 406 controlbox.on('command:msg', msgCommand);\r
696a66f8 407\r
6c58c492 408 controlbox.on('command:action', actionCommand);\r
696a66f8 409\r
6c58c492 410 controlbox.on('command:join', joinCommand);\r
696a66f8 411\r
6c58c492 412 controlbox.on('command:part', partCommand);\r
696a66f8 413\r
6c58c492 414 controlbox.on('command:nick', function (ev) {\r
e7d65587 415 _kiwi.gateway.changeNick(null, ev.params[0]);\r
696a66f8
D
416 });\r
417\r
6c58c492 418 controlbox.on('command:query', queryCommand);\r
696a66f8 419\r
6c58c492 420 controlbox.on('command:topic', topicCommand);\r
696a66f8 421\r
6c58c492 422 controlbox.on('command:notice', noticeCommand);\r
696a66f8 423\r
6c58c492 424 controlbox.on('command:quote', quoteCommand);\r
696a66f8 425\r
6c58c492 426 controlbox.on('command:kick', kickCommand);\r
696a66f8 427\r
6c58c492 428 controlbox.on('command:clear', clearCommand);\r
648e2c01 429\r
d8fb3499 430 controlbox.on('command:ctcp', ctcpCommand);\r
b1042f3d 431\r
cdbf2b6f
D
432 controlbox.on('command:server', serverCommand);\r
433\r
696a66f8 434\r
6c58c492 435 controlbox.on('command:css', function (ev) {\r
696a66f8
D
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
6c58c492 442 controlbox.on('command:js', function (ev) {\r
696a66f8
D
443 if (!ev.params[0]) return;\r
444 $script(ev.params[0] + '?' + (new Date().getTime()));\r
445 });\r
446\r
600bc234 447 \r
6c58c492 448 controlbox.on('command:set', function (ev) {\r
600bc234
D
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
9383cc07
D
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
600bc234
D
472 _kiwi.global.settings.set(setting, value);\r
473 }\r
474\r
475 // Read the value to the user\r
6d5faa6e 476 _kiwi.app.panels().active.addMsg('', setting + ' = ' + _kiwi.global.settings.get(setting).toString());\r
600bc234
D
477 });\r
478\r
479\r
6c58c492 480 controlbox.on('command:save', function (ev) {\r
600bc234 481 _kiwi.global.settings.save();\r
6d5faa6e 482 _kiwi.app.panels().active.addMsg('', 'Settings have been saved');\r
600bc234
D
483 });\r
484\r
485\r
6c58c492 486 controlbox.on('command:alias', function (ev) {\r
696a66f8
D
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
6d5faa6e 492 _kiwi.app.panels().server.addMsg(' ', name + ' => ' + rule);\r
696a66f8
D
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
f3091277
D
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
6d5faa6e 524 _kiwi.app.panels().active.addMsg(' ', 'Ignored nicks:');\r
f3091277 525 $.each(list, function (idx, ignored_pattern) {\r
6d5faa6e 526 _kiwi.app.panels().active.addMsg(' ', ignored_pattern);\r
f3091277
D
527 });\r
528 } else {\r
6d5faa6e 529 _kiwi.app.panels().active.addMsg(' ', 'Not ignoring anybody');\r
f3091277
D
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
6d5faa6e 537 _kiwi.app.panels().active.addMsg(' ', 'Ignoring ' + ev.params[0]);\r
f3091277
D
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
6d5faa6e 545 _kiwi.app.panels().active.addMsg(' ', 'Specifiy which nick you wish to stop ignoring');\r
f3091277
D
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
6d5faa6e 555 _kiwi.app.panels().active.addMsg(' ', 'Stopped ignoring ' + ev.params[0]);\r
f3091277
D
556 });\r
557\r
558\r
6c58c492
D
559 controlbox.on('command:applet', appletCommand);\r
560 controlbox.on('command:settings', settingsCommand);\r
22373da6 561 controlbox.on('command:script', scriptCommand);\r
696a66f8
D
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
e7d65587 568 _kiwi.gateway.raw(null, raw_cmd);\r
696a66f8
D
569 }\r
570\r
571 function allCommands (ev) {}\r
572\r
573 function joinCommand (ev) {\r
18818abd 574 var panels, channel_names;\r
696a66f8
D
575\r
576 channel_names = ev.params.join(' ').split(',');\r
18818abd 577 panels = that.connections.active_connection.createAndJoinChannels(channel_names);\r
696a66f8 578\r
18818abd
D
579 // Show the last channel if we have one\r
580 if (panels)\r
581 panels[panels.length - 1].view.show();\r
696a66f8
D
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
6d5faa6e 590 panel = that.connections.active_connection.panels.getByName(destination);\r
696a66f8 591 if (!panel) {\r
eaaf73b0 592 panel = new _kiwi.model.Query({name: destination});\r
6d5faa6e 593 that.connections.active_connection.panels.add(panel);\r
696a66f8
D
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
6d5faa6e 602 panel = that.connections.active_connection.panels.getByName(destination) || that.panels().server;\r
696a66f8
D
603\r
604 ev.params.shift();\r
605\r
e4de4648 606 panel.addMsg(_kiwi.app.connections.active_connection.get('nick'), ev.params.join(' '));\r
e7d65587 607 _kiwi.gateway.privmsg(null, destination, ev.params.join(' '));\r
696a66f8
D
608 }\r
609\r
610 function actionCommand (ev) {\r
6d5faa6e 611 if (_kiwi.app.panels().active.isServer()) {\r
696a66f8
D
612 return;\r
613 }\r
614\r
6d5faa6e 615 var panel = _kiwi.app.panels().active;\r
e4de4648 616 panel.addMsg('', '* ' + _kiwi.app.connections.active_connection.get('nick') + ' ' + ev.params.join(' '), 'action');\r
e7d65587 617 _kiwi.gateway.action(null, panel.get('name'), ev.params.join(' '));\r
696a66f8
D
618 }\r
619\r
620 function partCommand (ev) {\r
621 if (ev.params.length === 0) {\r
e7d65587 622 _kiwi.gateway.part(null, _kiwi.app.panels().active.get('name'));\r
696a66f8
D
623 } else {\r
624 _.each(ev.params, function (channel) {\r
e7d65587 625 _kiwi.gateway.part(null, channel);\r
696a66f8
D
626 });\r
627 }\r
696a66f8
D
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
6d5faa6e 639 channel_name = _kiwi.app.panels().active.get('name');\r
696a66f8
D
640 }\r
641\r
e7d65587 642 _kiwi.gateway.topic(null, channel_name, ev.params.join(' '));\r
696a66f8
D
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
e7d65587 654 _kiwi.gateway.notice(null, destination, ev.params.join(' '));\r
696a66f8
D
655 }\r
656\r
657 function quoteCommand (ev) {\r
658 var raw = ev.params.join(' ');\r
e7d65587 659 _kiwi.gateway.raw(null, raw);\r
696a66f8
D
660 }\r
661\r
662 function kickCommand (ev) {\r
6d5faa6e 663 var nick, panel = _kiwi.app.panels().active;\r
696a66f8
D
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
e7d65587 673 _kiwi.gateway.kick(null, panel.get('name'), nick, ev.params.join(' '));\r
696a66f8
D
674 }\r
675\r
648e2c01
D
676 function clearCommand (ev) {\r
677 // Can't clear a server or applet panel\r
6d5faa6e 678 if (_kiwi.app.panels().active.isServer() || _kiwi.app.panels().active.isApplet()) {\r
648e2c01
D
679 return;\r
680 }\r
681\r
6d5faa6e
D
682 if (_kiwi.app.panels().active.clearMessages) {\r
683 _kiwi.app.panels().active.clearMessages();\r
648e2c01
D
684 }\r
685 }\r
686\r
b1042f3d
D
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
e7d65587 699 _kiwi.gateway.ctcp(null, true, type, target, ev.params.join(' '));\r
b1042f3d
D
700 }\r
701\r
696a66f8 702 function settingsCommand (ev) {\r
da848c4f
D
703 var settings = _kiwi.model.Applet.loadOnce('kiwi_settings');\r
704 settings.view.show();\r
696a66f8
D
705 }\r
706\r
22373da6
D
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
696a66f8
D
712 function appletCommand (ev) {\r
713 if (!ev.params[0]) return;\r
714\r
eaaf73b0 715 var panel = new _kiwi.model.Applet();\r
696a66f8
D
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
eaaf73b0
D
722 if (_kiwi.applets[ev.params[0]]) {\r
723 panel.load(new _kiwi.applets[ev.params[0]]());\r
696a66f8 724 } else {\r
6d5faa6e 725 _kiwi.app.panels().server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');\r
696a66f8
D
726 return;\r
727 }\r
728 }\r
729 \r
6d5faa6e 730 _kiwi.app.connections.active_connection.panels.add(panel);\r
696a66f8
D
731 panel.view.show();\r
732 }\r
733\r
734\r
cdbf2b6f
D
735 function serverCommand (ev) {\r
736 var server, port, ssl, password, nick,\r
737 tmp;\r
738\r
3735e390
D
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
cdbf2b6f
D
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
696a66f8
D
799\r
800\r
801\r
802 this.isChannelName = function (channel_name) {\r
eaaf73b0 803 var channel_prefix = _kiwi.gateway.get('channel_prefix');\r
696a66f8
D
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
fb321ba0 809\r
696a66f8
D
810 };\r
811\r
812\r
813 model = Backbone.Model.extend(new model());\r
814\r
815 return new model(arguments);\r
816};\r