Client: New plugin manager implementation
[KiwiIRC.git] / client / assets / dev / model_application.js
CommitLineData
696a66f8
D
1kiwi.model.Application = function () {\r
2 // Set to a reference to this object within initialize()\r
3 var that = null;\r
4\r
5 // The auto connect details entered into the server select box\r
6 var auto_connect_details = {};\r
7\r
8\r
9 var model = function () {\r
10 /** Instance of kiwi.model.PanelList */\r
11 this.panels = null;\r
12\r
13 /** kiwi.view.Application */\r
14 this.view = null;\r
15\r
16 /** kiwi.view.StatusMessage */\r
17 this.message = null;\r
18\r
19 /* Address for the kiwi server */\r
20 this.kiwi_server = null;\r
21\r
22 this.initialize = function (options) {\r
23 that = this;\r
24\r
25 if (options[0].container) {\r
26 this.set('container', options[0].container);\r
27 }\r
28\r
29 // The base url to the kiwi server\r
8b0eb787 30 this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');\r
696a66f8
D
31\r
32 // Best guess at where the kiwi server is\r
33 this.detectKiwiServer();\r
890a9b91
D
34\r
35 // Load the plugin manager\r
36 this.plugins = new kiwi.model.PluginManager();\r
696a66f8
D
37 };\r
38\r
39 this.start = function () {\r
40 // Only debug if set in the querystring\r
41 if (!getQueryVariable('debug')) {\r
42 manageDebug(false);\r
43 } else {\r
44 //manageDebug(true);\r
45 }\r
46 \r
47 // Set the gateway up\r
48 kiwi.gateway = new kiwi.model.Gateway();\r
49 this.bindGatewayCommands(kiwi.gateway);\r
50\r
51 this.initializeClient();\r
52 this.initializeGlobals();\r
53\r
54 this.view.barsHide(true);\r
55\r
56 this.panels.server.server_login.bind('server_connect', function (event) {\r
8b0eb787
D
57 var server_login = this,\r
58 transport_path = '';\r
696a66f8
D
59 auto_connect_details = event;\r
60\r
61 server_login.networkConnecting();\r
62 \r
8b0eb787
D
63 // Path to get the socket.io transport code\r
64 transport_path = that.kiwi_server + that.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime());\r
65 \r
66 $script(transport_path, function () {\r
696a66f8
D
67 if (!window.io) {\r
68 kiwiServerNotFound();\r
69 return;\r
70 }\r
71 kiwi.gateway.set('kiwi_server', that.kiwi_server + '/kiwi');\r
72 kiwi.gateway.set('nick', event.nick);\r
73 \r
d2d91c10
D
74 kiwi.gateway.connect(event.server, event.port, event.ssl, event.password, function (error) {\r
75 if (error) {\r
76 kiwiServerNotFound();\r
77 }\r
78 });\r
696a66f8
D
79 });\r
80 });\r
81\r
82 // TODO: Shouldn't really be here but it's not working in the view.. :/\r
83 // Hack for firefox browers: Focus is not given on this event loop iteration\r
84 setTimeout(function(){\r
85 kiwi.app.panels.server.server_login.$el.find('.nick').select();\r
86 }, 0);\r
87 };\r
88\r
89\r
90 function kiwiServerNotFound (e) {\r
91 that.panels.server.server_login.showError();\r
92 }\r
93\r
94\r
95 this.detectKiwiServer = function () {\r
96 // If running from file, default to localhost:7777 by default\r
97 if (window.location.protocol === 'file:') {\r
98 this.kiwi_server = 'http://localhost:7778';\r
99 } else {\r
100 // Assume the kiwi server is on the same server\r
101 this.kiwi_server = window.location.protocol + '//' + window.location.host;\r
102 }\r
103 };\r
104\r
105\r
106 this.initializeClient = function () {\r
107 this.view = new kiwi.view.Application({model: this, el: this.get('container')});\r
108 \r
109 /**\r
110 * Set the UI components up\r
111 */\r
112 this.panels = new kiwi.model.PanelList();\r
113\r
114 this.controlbox = new kiwi.view.ControlBox({el: $('#controlbox')[0]});\r
115 this.bindControllboxCommands(this.controlbox);\r
116\r
117 this.topicbar = new kiwi.view.TopicBar({el: $('#topic')[0]});\r
118\r
7de3dd03
D
119 new kiwi.view.AppToolbar({el: $('#toolbar .app_tools')[0]});\r
120\r
696a66f8
D
121 this.message = new kiwi.view.StatusMessage({el: $('#status_message')[0]});\r
122\r
123 this.resize_handle = new kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});\r
124 \r
125 this.panels.server.view.show();\r
126\r
127 // Rejigg the UI sizes\r
128 this.view.doLayout();\r
129\r
130 this.populateDefaultServerSettings();\r
131 };\r
132\r
133\r
134 this.initializeGlobals = function () {\r
135 kiwi.global.control = this.controlbox;\r
136 };\r
137\r
138\r
139 this.populateDefaultServerSettings = function () {\r
140 var parts;\r
141 var defaults = {\r
142 nick: getQueryVariable('nick') || 'kiwi_' + Math.ceil(Math.random() * 10000).toString(),\r
143 server: 'irc.kiwiirc.com',\r
144 port: 6667,\r
145 ssl: false,\r
2f54e55e
JA
146 channel: window.location.hash || '#kiwiirc',\r
147 channel_key: ''\r
696a66f8 148 };\r
2f54e55e 149 var uricheck;\r
696a66f8
D
150\r
151 // Process the URL part by part, extracting as we go\r
152 parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');\r
153\r
154 if (parts.length > 0) {\r
155 parts.shift();\r
156\r
157 if (parts.length > 0 && parts[0]) {\r
4f8da37d 158 // 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
159 uricheck = parts[0].substr(0, 7).toLowerCase();\r
160 if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {\r
161 parts[0] = decodeURIComponent(parts[0]);\r
2f54e55e
JA
162 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]\r
163 uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);\r
4f8da37d
JA
164 /*\r
165 uricheck[1] = ssl (optional)\r
166 uricheck[2] = host\r
167 uricheck[3] = port (optional)\r
168 uricheck[4] = channel (optional)\r
169 uricheck[5] = channel key (optional, channel must also be set)\r
170 */\r
2f54e55e 171 if (uricheck) {\r
4f8da37d 172 if (typeof uricheck[1] !== 'undefined') {\r
2f54e55e
JA
173 defaults.ssl = true;\r
174 if (defaults.port === 6667) {\r
175 defaults.port = 6697;\r
176 }\r
177 }\r
178 defaults.server = uricheck[2];\r
4f8da37d 179 if (typeof uricheck[3] !== 'undefined') {\r
2f54e55e
JA
180 defaults.port = uricheck[3];\r
181 }\r
4f8da37d 182 if (typeof uricheck[4] !== 'undefined') {\r
2f54e55e 183 defaults.channel = '#' + uricheck[4];\r
4f8da37d 184 if (typeof uricheck[5] !== 'undefined') {\r
2f54e55e
JA
185 defaults.channel_key = uricheck[5];\r
186 }\r
187 }\r
188 }\r
2f54e55e
JA
189 parts = [];\r
190 } else {\r
191 // Extract the port+ssl if we find one\r
192 if (parts[0].search(/:/) > 0) {\r
193 defaults.port = parts[0].substring(parts[0].search(/:/) + 1);\r
194 defaults.server = parts[0].substring(0, parts[0].search(/:/));\r
195 if (defaults.port[0] === '+') {\r
196 defaults.port = parseInt(defaults.port.substring(1), 10);\r
197 defaults.ssl = true;\r
198 } else {\r
199 defaults.ssl = false;\r
200 }\r
201\r
696a66f8 202 } else {\r
2f54e55e 203 defaults.server = parts[0];\r
696a66f8
D
204 }\r
205\r
2f54e55e 206 parts.shift();\r
696a66f8 207 }\r
696a66f8
D
208 }\r
209\r
210 if (parts.length > 0 && parts[0]) {\r
211 defaults.channel = '#' + parts[0];\r
212 parts.shift();\r
213 }\r
214 }\r
215\r
216 // Set any random numbers if needed\r
217 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());\r
218\r
219 // Populate the server select box with defaults\r
220 this.panels.server.server_login.populateFields(defaults);\r
221 };\r
222\r
223\r
224 this.bindGatewayCommands = function (gw) {\r
225 gw.on('onmotd', function (event) {\r
226 that.panels.server.addMsg(kiwi.gateway.get('name'), event.msg, 'motd');\r
227 });\r
228\r
229\r
230 gw.on('onconnect', function (event) {\r
231 that.view.barsShow();\r
232 \r
233 if (auto_connect_details.channel) {\r
2f54e55e 234 that.controlbox.processInput('/JOIN ' + auto_connect_details.channel + ' ' + auto_connect_details.channel_key);\r
696a66f8
D
235 }\r
236 });\r
237\r
238\r
239 (function () {\r
240 var gw_stat = 0;\r
241\r
242 gw.on('disconnect', function (event) {\r
243 var msg = 'You have been disconnected. Attempting to reconnect for you..';\r
244 that.message.text(msg, {timeout: 10000});\r
245\r
246 // Mention the disconnection on every channel\r
247 $.each(kiwi.app.panels.models, function (idx, panel) {\r
248 if (!panel || !panel.isChannel()) return;\r
249 panel.addMsg('', msg, 'action quit');\r
250 });\r
251 kiwi.app.panels.server.addMsg('', msg, 'action quit');\r
252\r
253 gw_stat = 1;\r
254 });\r
255 gw.on('reconnecting', function (event) {\r
256 msg = 'You have been disconnected. Attempting to reconnect again in ' + (event.delay/1000) + ' seconds..';\r
257 kiwi.app.panels.server.addMsg('', msg, 'action quit');\r
258 });\r
259 gw.on('connect', function (event) {\r
260 if (gw_stat !== 1) return;\r
261\r
262 var msg = 'It\'s OK, you\'re connected again :)';\r
263 that.message.text(msg, {timeout: 5000});\r
264\r
265 // Mention the disconnection on every channel\r
266 $.each(kiwi.app.panels.models, function (idx, panel) {\r
267 if (!panel || !panel.isChannel()) return;\r
268 panel.addMsg('', msg, 'action join');\r
269 });\r
270 kiwi.app.panels.server.addMsg('', msg, 'action join');\r
271\r
272 gw_stat = 0;\r
273 });\r
274 })();\r
275\r
276\r
277 gw.on('onjoin', function (event) {\r
278 var c, members, user;\r
279 c = that.panels.getByName(event.channel);\r
280 if (!c) {\r
281 c = new kiwi.model.Channel({name: event.channel});\r
282 that.panels.add(c);\r
283 }\r
284\r
285 members = c.get('members');\r
286 if (!members) return;\r
287\r
288 user = new kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname});\r
289 members.add(user);\r
290 // TODO: highlight the new channel in some way\r
291 });\r
292\r
293\r
294 gw.on('onpart', function (event) {\r
295 var channel, members, user,\r
296 part_options = {};\r
297\r
298 part_options.type = 'part';\r
299 part_options.message = event.message || '';\r
300\r
301 channel = that.panels.getByName(event.channel);\r
302 if (!channel) return;\r
303\r
304 // If this is us, close the panel\r
305 if (event.nick === kiwi.gateway.get('nick')) {\r
306 channel.close();\r
307 return;\r
308 }\r
309\r
310 members = channel.get('members');\r
311 if (!members) return;\r
312\r
313 user = members.getByNick(event.nick);\r
314 if (!user) return;\r
315\r
316 members.remove(user, part_options);\r
317 });\r
318\r
319\r
320 gw.on('onquit', function (event) {\r
321 var member, members,\r
322 quit_options = {};\r
323\r
324 quit_options.type = 'quit';\r
325 quit_options.message = event.message || '';\r
326\r
327 $.each(that.panels.models, function (index, panel) {\r
328 if (!panel.isChannel()) return;\r
329\r
330 member = panel.get('members').getByNick(event.nick);\r
331 if (member) {\r
332 panel.get('members').remove(member, quit_options);\r
333 }\r
334 });\r
335 });\r
336\r
337\r
338 gw.on('onkick', function (event) {\r
339 var channel, members, user,\r
340 part_options = {};\r
341\r
342 part_options.type = 'kick';\r
343 part_options.by = event.nick;\r
344 part_options.message = event.message || '';\r
345\r
346 channel = that.panels.getByName(event.channel);\r
347 if (!channel) return;\r
348\r
349 members = channel.get('members');\r
350 if (!members) return;\r
351\r
352 user = members.getByNick(event.kicked);\r
353 if (!user) return;\r
354\r
355 members.remove(user, part_options);\r
356\r
357 if (event.kicked === kiwi.gateway.get('nick')) {\r
358 members.reset([]);\r
359 }\r
360 \r
361 });\r
362\r
363\r
364 gw.on('onmsg', function (event) {\r
365 var panel,\r
366 is_pm = (event.channel == kiwi.gateway.get('nick'));\r
367\r
368 if (is_pm) {\r
369 // If a panel isn't found for this PM, create one\r
370 panel = that.panels.getByName(event.nick);\r
371 if (!panel) {\r
b9c64fcd 372 panel = new kiwi.model.Query({name: event.nick});\r
696a66f8
D
373 that.panels.add(panel);\r
374 }\r
375\r
376 } else {\r
377 // If a panel isn't found for this channel, reroute to the\r
378 // server panel\r
379 panel = that.panels.getByName(event.channel);\r
380 if (!panel) {\r
381 panel = that.panels.server;\r
382 }\r
383 }\r
384 \r
385 panel.addMsg(event.nick, event.msg);\r
386 });\r
387\r
388\r
389 gw.on('onnotice', function (event) {\r
390 var panel;\r
391\r
392 // Find a panel for the destination(channel) or who its from\r
393 panel = that.panels.getByName(event.target) || that.panels.getByName(event.nick);\r
394 if (!panel) {\r
395 panel = that.panels.server;\r
396 }\r
397\r
398 panel.addMsg('[' + (event.nick||'') + ']', event.msg);\r
399 });\r
400\r
401\r
402 gw.on('onaction', function (event) {\r
403 var panel,\r
404 is_pm = (event.channel == kiwi.gateway.get('nick'));\r
405\r
406 if (is_pm) {\r
407 // If a panel isn't found for this PM, create one\r
408 panel = that.panels.getByName(event.nick);\r
409 if (!panel) {\r
410 panel = new kiwi.model.Channel({name: event.nick});\r
411 that.panels.add(panel);\r
412 }\r
413\r
414 } else {\r
415 // If a panel isn't found for this channel, reroute to the\r
416 // server panel\r
417 panel = that.panels.getByName(event.channel);\r
418 if (!panel) {\r
419 panel = that.panels.server;\r
420 }\r
421 }\r
422\r
423 panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action');\r
424 });\r
425\r
426\r
427 gw.on('ontopic', function (event) {\r
428 var c;\r
429 c = that.panels.getByName(event.channel);\r
430 if (!c) return;\r
431\r
432 // Set the channels topic\r
433 c.set('topic', event.topic);\r
434\r
435 // If this is the active channel, update the topic bar too\r
436 if (c.get('name') === kiwi.app.panels.active.get('name')) {\r
437 that.topicbar.setCurrentTopic(event.topic);\r
438 }\r
439 });\r
440\r
441\r
442 gw.on('ontopicsetby', function (event) {\r
443 var c, when;\r
444 c = that.panels.getByName(event.channel);\r
445 if (!c) return;\r
446\r
447 when = formatDate(new Date(event.when * 1000));\r
448 c.addMsg('', 'Topic set by ' + event.nick + ' at ' + when, 'topic');\r
449 });\r
450\r
451\r
452 gw.on('onuserlist', function (event) {\r
453 var channel;\r
454 channel = that.panels.getByName(event.channel);\r
455\r
456 // If we didn't find a channel for this, may aswell leave\r
457 if (!channel) return;\r
458\r
459 channel.temp_userlist = channel.temp_userlist || [];\r
460 _.each(event.users, function (item) {\r
461 var user = new kiwi.model.Member({nick: item.nick, modes: item.modes});\r
462 channel.temp_userlist.push(user);\r
463 });\r
464 });\r
465\r
466\r
467 gw.on('onuserlist_end', function (event) {\r
468 var channel;\r
469 channel = that.panels.getByName(event.channel);\r
470\r
471 // If we didn't find a channel for this, may aswell leave\r
472 if (!channel) return;\r
473\r
474 // Update the members list with the new list\r
475 channel.get('members').reset(channel.temp_userlist || []);\r
476\r
477 // Clear the temporary userlist\r
478 delete channel.temp_userlist;\r
479 });\r
480\r
481\r
482 gw.on('onmode', function (event) {\r
483 var channel, i, prefixes, members, member, find_prefix;\r
484 \r
485 // Build a nicely formatted string to be displayed to a regular human\r
486 function friendlyModeString (event_modes, alt_target) {\r
487 var modes = {}, return_string;\r
488\r
489 // If no default given, use the main event info\r
490 if (!event_modes) {\r
491 event_modes = event.modes;\r
492 alt_target = event.target;\r
493 }\r
494\r
495 // Reformat the mode object to make it easier to work with\r
496 _.each(event_modes, function (mode){\r
497 var param = mode.param || alt_target || '';\r
498\r
499 // Make sure we have some modes for this param\r
500 if (!modes[param]) {\r
501 modes[param] = {'+':'', '-':''};\r
502 }\r
503\r
504 modes[param][mode.mode[0]] += mode.mode.substr(1);\r
505 });\r
506\r
507 // Put the string together from each mode\r
508 return_string = [];\r
509 _.each(modes, function (modeset, param) {\r
510 var str = '';\r
511 if (modeset['+']) str += '+' + modeset['+'];\r
512 if (modeset['-']) str += '-' + modeset['-'];\r
513 return_string.push(str + ' ' + param);\r
514 });\r
515 return_string = return_string.join(', ');\r
516\r
517 return return_string;\r
518 }\r
519\r
520\r
521 channel = that.panels.getByName(event.target);\r
522 if (channel) {\r
523 prefixes = kiwi.gateway.get('user_prefixes');\r
524 find_prefix = function (p) {\r
525 return event.modes[i].mode[1] === p.mode;\r
526 };\r
527 for (i = 0; i < event.modes.length; i++) {\r
528 if (_.any(prefixes, find_prefix)) {\r
529 if (!members) {\r
530 members = channel.get('members');\r
531 }\r
532 member = members.getByNick(event.modes[i].param);\r
533 if (!member) {\r
534 console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target);\r
535 return;\r
536 } else {\r
537 if (event.modes[i].mode[0] === '+') {\r
538 member.addMode(event.modes[i].mode[1]);\r
539 } else if (event.modes[i].mode[0] === '-') {\r
540 member.removeMode(event.modes[i].mode[1]);\r
541 }\r
542 members.sort();\r
543 //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode');\r
544 }\r
545 } else {\r
546 // Channel mode being set\r
547 // TODO: Store this somewhere?\r
548 //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode');\r
549 }\r
550 }\r
551\r
552 channel.addMsg('', '== ' + event.nick + ' sets mode ' + friendlyModeString(), 'action mode');\r
553 } else {\r
554 // This is probably a mode being set on us.\r
555 if (event.target.toLowerCase() === kiwi.gateway.get("nick").toLowerCase()) {\r
556 that.panels.server.addMsg('', '== ' + event.nick + ' set mode ' + friendlyModeString(), 'action mode');\r
557 } else {\r
558 console.log('MODE command recieved for unknown target %s: ', event.target, event);\r
559 }\r
560 }\r
561 });\r
562\r
563\r
564 gw.on('onnick', function (event) {\r
565 var member;\r
566\r
567 $.each(that.panels.models, function (index, panel) {\r
568 if (!panel.isChannel()) return;\r
569\r
570 member = panel.get('members').getByNick(event.nick);\r
571 if (member) {\r
572 member.set('nick', event.newnick);\r
573 panel.addMsg('', '== ' + event.nick + ' is now known as ' + event.newnick, 'action nick');\r
574 }\r
575 });\r
576 });\r
577\r
578\r
579 gw.on('onwhois', function (event) {\r
580 /*globals secondsToTime */\r
581 var logon_date, idle_time = '', panel;\r
582\r
583 if (event.end) {\r
584 return;\r
585 }\r
586\r
587 if (typeof event.idle !== 'undefined') {\r
588 idle_time = secondsToTime(parseInt(event.idle, 10));\r
589 idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0");\r
590 }\r
591\r
592 panel = kiwi.app.panels.active;\r
593 if (event.ident) {\r
594 panel.addMsg(event.nick, 'is ' + event.nick + '!' + event.ident + '@' + event.host + ' * ' + event.msg, 'whois');\r
595 } else if (event.chans) {\r
596 panel.addMsg(event.nick, 'on ' + event.chans, 'whois');\r
0cfbecfc 597 } else if (event.irc_server) {\r
696a66f8
D
598 panel.addMsg(event.nick, 'using ' + event.server, 'whois');\r
599 } else if (event.msg) {\r
600 panel.addMsg(event.nick, event.msg, 'whois');\r
601 } else if (event.logon) {\r
602 logon_date = new Date();\r
603 logon_date.setTime(event.logon * 1000);\r
604 logon_date = formatDate(logon_date);\r
605\r
606 panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois');\r
607 } else {\r
608 panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois');\r
609 }\r
610 });\r
611\r
612\r
613 gw.on('onlist_start', function (data) {\r
614 if (kiwi.app.channel_list) {\r
615 kiwi.app.channel_list.close();\r
616 delete kiwi.app.channel_list;\r
617 }\r
618\r
619 var panel = new kiwi.model.Applet(),\r
620 applet = new kiwi.applets.Chanlist();\r
621\r
622 panel.load(applet);\r
623 \r
624 kiwi.app.panels.add(panel);\r
625 panel.view.show();\r
626 \r
627 kiwi.app.channel_list = applet;\r
628 });\r
629\r
630\r
631 gw.on('onlist_channel', function (data) {\r
632 // TODO: Put this listener within the applet itself\r
633 kiwi.app.channel_list.addChannel(data.chans);\r
634 });\r
635\r
636\r
637 gw.on('onlist_end', function (data) {\r
638 // TODO: Put this listener within the applet itself\r
639 delete kiwi.app.channel_list;\r
640 });\r
641\r
642\r
643 gw.on('onirc_error', function (data) {\r
644 var panel, tmp;\r
645\r
646 if (data.channel !== undefined && !(panel = kiwi.app.panels.getByName(data.channel))) {\r
647 panel = kiwi.app.panels.server;\r
648 }\r
649\r
650 switch (data.error) {\r
651 case 'banned_from_channel':\r
652 panel.addMsg(' ', '== You are banned from ' + data.channel + '. ' + data.reason, 'status');\r
653 kiwi.app.message.text('You are banned from ' + data.channel + '. ' + data.reason);\r
654 break;\r
655 case 'bad_channel_key':\r
656 panel.addMsg(' ', '== Bad channel key for ' + data.channel, 'status');\r
657 kiwi.app.message.text('Bad channel key or password for ' + data.channel);\r
658 break;\r
659 case 'invite_only_channel':\r
660 panel.addMsg(' ', '== ' + data.channel + ' is invite only.', 'status');\r
661 kiwi.app.message.text(data.channel + ' is invite only');\r
662 break;\r
663 case 'channel_is_full':\r
664 panel.addMsg(' ', '== ' + data.channel + ' is full.', 'status');\r
665 kiwi.app.message.text(data.channel + ' is full');\r
666 break;\r
667 case 'chanop_privs_needed':\r
668 panel.addMsg(' ', '== ' + data.reason, 'status');\r
669 kiwi.app.message.text(data.reason + ' (' + data.channel + ')');\r
670 break;\r
671 case 'no_such_nick':\r
672 tmp = kiwi.app.panels.getByName(data.nick);\r
673 if (tmp) {\r
674 tmp.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');\r
675 } else {\r
676 kiwi.app.panels.server.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status');\r
677 }\r
678 break;\r
679 case 'nickname_in_use':\r
680 kiwi.app.panels.server.addMsg(' ', '== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');\r
681 if (kiwi.app.panels.server !== kiwi.app.panels.active) {\r
682 kiwi.app.message.text('The nickname "' + data.nick + '" is already in use. Please select a new nickname');\r
683 }\r
684\r
685 // Only show the nickchange component if the controlbox is open\r
686 if (that.controlbox.$el.css('display') !== 'none') {\r
687 (new kiwi.view.NickChangeBox()).render();\r
688 }\r
689\r
690 break;\r
691 default:\r
692 // We don't know what data contains, so don't do anything with it.\r
693 //kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status');\r
694 }\r
695 });\r
696 };\r
697\r
698\r
699\r
700 /**\r
701 * Bind to certain commands that may be typed into the control box\r
702 */\r
703 this.bindControllboxCommands = function (controlbox) {\r
704 // Default aliases\r
705 $.extend(controlbox.preprocessor.aliases, {\r
706 // General aliases\r
707 '/p': '/part $1+',\r
708 '/me': '/action $1+',\r
709 '/j': '/join $1+',\r
710 '/q': '/query $1+',\r
711\r
712 // Op related aliases\r
713 '/op': '/quote mode $channel +o $1+',\r
714 '/deop': '/quote mode $channel -o $1+',\r
715 '/hop': '/quote mode $channel +h $1+',\r
716 '/dehop': '/quote mode $channel -h $1+',\r
717 '/voice': '/quote mode $channel +v $1+',\r
718 '/devoice': '/quote mode $channel -v $1+',\r
aefbc5e1 719 '/k': '/kick $channel $1+',\r
696a66f8
D
720\r
721 // Misc aliases\r
70c7e208 722 '/slap': '/me slaps $1 around a bit with a large trout'\r
696a66f8
D
723 });\r
724\r
725 controlbox.on('unknown_command', unknownCommand);\r
726\r
727 controlbox.on('command', allCommands);\r
728 controlbox.on('command_msg', msgCommand);\r
729\r
730 controlbox.on('command_action', actionCommand);\r
731\r
732 controlbox.on('command_join', joinCommand);\r
733\r
734 controlbox.on('command_part', partCommand);\r
735\r
736 controlbox.on('command_nick', function (ev) {\r
737 kiwi.gateway.changeNick(ev.params[0]);\r
738 });\r
739\r
740 controlbox.on('command_query', queryCommand);\r
741\r
742 controlbox.on('command_topic', topicCommand);\r
743\r
744 controlbox.on('command_notice', noticeCommand);\r
745\r
746 controlbox.on('command_quote', quoteCommand);\r
747\r
748 controlbox.on('command_kick', kickCommand);\r
749\r
750\r
751 controlbox.on('command_css', function (ev) {\r
752 var queryString = '?reload=' + new Date().getTime();\r
753 $('link[rel="stylesheet"]').each(function () {\r
754 this.href = this.href.replace(/\?.*|$/, queryString);\r
755 });\r
756 });\r
757\r
758 controlbox.on('command_js', function (ev) {\r
759 if (!ev.params[0]) return;\r
760 $script(ev.params[0] + '?' + (new Date().getTime()));\r
761 });\r
762\r
763 controlbox.on('command_alias', function (ev) {\r
764 var name, rule;\r
765\r
766 // No parameters passed so list them\r
767 if (!ev.params[1]) {\r
768 $.each(controlbox.preprocessor.aliases, function (name, rule) {\r
769 kiwi.app.panels.server.addMsg(' ', name + ' => ' + rule);\r
770 });\r
771 return;\r
772 }\r
773\r
774 // Deleting an alias?\r
775 if (ev.params[0] === 'del' || ev.params[0] === 'delete') {\r
776 name = ev.params[1];\r
777 if (name[0] !== '/') name = '/' + name;\r
778 delete controlbox.preprocessor.aliases[name];\r
779 return;\r
780 }\r
781\r
782 // Add the alias\r
783 name = ev.params[0];\r
784 ev.params.shift();\r
785 rule = ev.params.join(' ');\r
786\r
787 // Make sure the name starts with a slash\r
788 if (name[0] !== '/') name = '/' + name;\r
789\r
790 // Now actually add the alias\r
791 controlbox.preprocessor.aliases[name] = rule;\r
792 });\r
793\r
794 controlbox.on('command_applet', appletCommand);\r
795 controlbox.on('command_settings', settingsCommand);\r
796 };\r
797\r
798 // A fallback action. Send a raw command to the server\r
799 function unknownCommand (ev) {\r
800 var raw_cmd = ev.command + ' ' + ev.params.join(' ');\r
801 console.log('RAW: ' + raw_cmd);\r
802 kiwi.gateway.raw(raw_cmd);\r
803 }\r
804\r
805 function allCommands (ev) {}\r
806\r
807 function joinCommand (ev) {\r
808 var channel, channel_names;\r
809\r
810 channel_names = ev.params.join(' ').split(',');\r
811\r
812 $.each(channel_names, function (index, channel_name) {\r
813 // Trim any whitespace off the name\r
814 channel_name = channel_name.trim();\r
815\r
816 // Check if we have the panel already. If not, create it\r
817 channel = that.panels.getByName(channel_name);\r
818 if (!channel) {\r
819 channel = new kiwi.model.Channel({name: channel_name});\r
820 kiwi.app.panels.add(channel);\r
821 }\r
822\r
823 kiwi.gateway.join(channel_name);\r
824 });\r
825\r
826 if (channel) channel.view.show();\r
827 \r
828 }\r
829\r
830 function queryCommand (ev) {\r
831 var destination, panel;\r
832\r
833 destination = ev.params[0];\r
834\r
835 // Check if we have the panel already. If not, create it\r
836 panel = that.panels.getByName(destination);\r
837 if (!panel) {\r
b9c64fcd 838 panel = new kiwi.model.Query({name: destination});\r
696a66f8
D
839 kiwi.app.panels.add(panel);\r
840 }\r
841\r
842 if (panel) panel.view.show();\r
843 \r
844 }\r
845\r
846 function msgCommand (ev) {\r
847 var destination = ev.params[0],\r
848 panel = that.panels.getByName(destination) || that.panels.server;\r
849\r
850 ev.params.shift();\r
851\r
852 panel.addMsg(kiwi.gateway.get('nick'), ev.params.join(' '));\r
853 kiwi.gateway.privmsg(destination, ev.params.join(' '));\r
854 }\r
855\r
856 function actionCommand (ev) {\r
857 if (kiwi.app.panels.active === kiwi.app.panels.server) {\r
858 return;\r
859 }\r
860\r
861 var panel = kiwi.app.panels.active;\r
862 panel.addMsg('', '* ' + kiwi.gateway.get('nick') + ' ' + ev.params.join(' '), 'action');\r
863 kiwi.gateway.action(panel.get('name'), ev.params.join(' '));\r
864 }\r
865\r
866 function partCommand (ev) {\r
867 if (ev.params.length === 0) {\r
868 kiwi.gateway.part(kiwi.app.panels.active.get('name'));\r
869 } else {\r
870 _.each(ev.params, function (channel) {\r
871 kiwi.gateway.part(channel);\r
872 });\r
873 }\r
874 // TODO: More responsive = close tab now, more accurate = leave until part event\r
875 //kiwi.app.panels.remove(kiwi.app.panels.active);\r
876 }\r
877\r
878 function topicCommand (ev) {\r
879 var channel_name;\r
880\r
881 if (ev.params.length === 0) return;\r
882\r
883 if (that.isChannelName(ev.params[0])) {\r
884 channel_name = ev.params[0];\r
885 ev.params.shift();\r
886 } else {\r
887 channel_name = kiwi.app.panels.active.get('name');\r
888 }\r
889\r
890 kiwi.gateway.topic(channel_name, ev.params.join(' '));\r
891 }\r
892\r
893 function noticeCommand (ev) {\r
894 var destination;\r
895\r
896 // Make sure we have a destination and some sort of message\r
897 if (ev.params.length <= 1) return;\r
898\r
899 destination = ev.params[0];\r
900 ev.params.shift();\r
901\r
902 kiwi.gateway.notice(destination, ev.params.join(' '));\r
903 }\r
904\r
905 function quoteCommand (ev) {\r
906 var raw = ev.params.join(' ');\r
907 kiwi.gateway.raw(raw);\r
908 }\r
909\r
910 function kickCommand (ev) {\r
911 var nick, panel = kiwi.app.panels.active;\r
912\r
913 if (!panel.isChannel()) return;\r
914\r
915 // Make sure we have a nick\r
916 if (ev.params.length === 0) return;\r
917\r
918 nick = ev.params[0];\r
919 ev.params.shift();\r
920\r
921 kiwi.gateway.kick(panel.get('name'), nick, ev.params.join(' '));\r
922 }\r
923\r
924 function settingsCommand (ev) {\r
925 var panel = new kiwi.model.Applet();\r
926 panel.load(new kiwi.applets.Settings());\r
927 \r
928 kiwi.app.panels.add(panel);\r
929 panel.view.show();\r
930 }\r
931\r
932 function appletCommand (ev) {\r
933 if (!ev.params[0]) return;\r
934\r
935 var panel = new kiwi.model.Applet();\r
936\r
937 if (ev.params[1]) {\r
938 // Url and name given\r
939 panel.load(ev.params[0], ev.params[1]);\r
940 } else {\r
941 // Load a pre-loaded applet\r
942 if (kiwi.applets[ev.params[0]]) {\r
943 panel.load(new kiwi.applets[ev.params[0]]());\r
944 } else {\r
945 kiwi.app.panels.server.addMsg('', 'Applet "' + ev.params[0] + '" does not exist');\r
946 return;\r
947 }\r
948 }\r
949 \r
950 kiwi.app.panels.add(panel);\r
951 panel.view.show();\r
952 }\r
953\r
954\r
955\r
956\r
957\r
958 this.isChannelName = function (channel_name) {\r
959 var channel_prefix = kiwi.gateway.get('channel_prefix');\r
960\r
961 if (!channel_name || !channel_name.length) return false;\r
962 return (channel_prefix.indexOf(channel_name[0]) > -1);\r
963 };\r
964\r
965 };\r
966\r
967\r
968 model = Backbone.Model.extend(new model());\r
969\r
970 return new model(arguments);\r
971};\r