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