omnomirc.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. OmnomIRC COPYRIGHT 2010,2011 Netham45
  3. OmnomIRC3 rewrite COPYRIGHT 2013 Nathaniel 'Eeems' van Diepen
  4. This file is part of OmnomIRC.
  5. OmnomIRC is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. OmnomIRC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with OmnomIRC. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. (function(window,$,io,undefined){
  17. "use strict";
  18. var $o = window.OmnomIRC = window.$o = function(){
  19. return 'Version: '+version;
  20. },
  21. event = function(msg,type){
  22. if(settings.debug){
  23. type=exists(type)?type:'event';
  24. switch(type){
  25. case 'ready':type='document_ready';break;
  26. }
  27. log('['+type.toUpperCase()+'] '+msg);
  28. }
  29. },
  30. emit = function(type,data){
  31. if($o.chat.connected()){
  32. socket.emit.apply(socket,arguments);
  33. }else{
  34. if(tabs.length > 0){
  35. $o.msg('Disconnected, cannot do anything');
  36. }
  37. }
  38. },
  39. noop = function(){},
  40. log = function(){
  41. console.log.apply(console,arguments);
  42. },
  43. exists = function(object){
  44. return typeof object != 'undefined';
  45. },
  46. prevent = function(e){
  47. e.stopImmediatePropagation();
  48. e.stopPropagation();
  49. e.preventDefault();
  50. return false;
  51. },
  52. selectedTab=0,
  53. settings = {},
  54. settingsConf = {},
  55. tabs = [],
  56. properties = {
  57. nick: 'User',
  58. sig: '',
  59. tabs: tabs,
  60. themes: []
  61. },
  62. commands = [
  63. { // names
  64. cmd: 'names',
  65. fn: function(args){
  66. emit('names',{
  67. name: $o.ui.tabs.current().name
  68. });
  69. }
  70. },
  71. { // me
  72. cmd: 'me',
  73. help: 'Say something in third person',
  74. fn: function(args){
  75. var i,ret='';
  76. for(i=1;i<args.length;i++){
  77. ret += ' '+args[i];
  78. }
  79. emit('message',{
  80. from: 0,
  81. message: properties.nick+' '+ret,
  82. room: $o.ui.tabs.current().name
  83. });
  84. }
  85. },
  86. { // connect
  87. cmd: 'connect',
  88. fn: function(){
  89. if(!$o.chat.connected()){
  90. $o.chat.connect();
  91. }
  92. }
  93. },
  94. { // disconnect
  95. cmd: 'disconnect',
  96. fn: function(){
  97. $o.disconnect();
  98. }
  99. },
  100. { // nick
  101. cmd: 'nick',
  102. fn: function(args){
  103. $o.set('nick',args[1]);
  104. }
  105. },
  106. { // help
  107. cmd: 'help',
  108. fn: function(args){
  109. if(exists(args[1])){
  110. var m = 'Commands:',i;
  111. for(i in commands){
  112. m += ' '+commands[i].cmd;
  113. }
  114. $o.msg(m);
  115. }else{
  116. var i,cmd;
  117. for(i in commands){
  118. cmd = commands[i];
  119. if(cmd.cmd == args[1] && exists(cmd.help)){
  120. $o.msg('Command /'+cmd.cmd+': '+cmd.help);
  121. return;
  122. }
  123. }
  124. $o.send('/help');
  125. }
  126. }
  127. },
  128. { // open
  129. cmd: 'open',
  130. fn: function(args){
  131. $o.ui.tabs.add(args[1]);
  132. }
  133. },
  134. { // clear
  135. cmd: 'clear',
  136. fn: function(args){
  137. $o.ui.tabs.current().clear();
  138. }
  139. },
  140. { // close
  141. cmd: 'close',
  142. fn: function(args){
  143. if(args.length > 1){
  144. $o.ui.tabs.remove(args[1]);
  145. }else{
  146. $o.ui.tabs.remove(selectedTab);
  147. }
  148. }
  149. },
  150. { // tabs
  151. cmd: 'tabs',
  152. fn: function(args){
  153. $o.msg('tabs:');
  154. for(var i in tabs){
  155. $o.msg(' ['+i+'] '+tabs[i].name);
  156. }
  157. }
  158. }
  159. ],
  160. handles = [
  161. { // names
  162. on: 'names',
  163. fn: function(data){
  164. var tab = $o.ui.tabs.tab(data.room),
  165. users = tab.users,
  166. i;
  167. tab.users = data.names;
  168. if($o.ui.tabs.idForName(data.room) == selectedTab){
  169. $o.ui.render.users();
  170. }
  171. $(users).each(function(i,v){
  172. if(v != null){
  173. if(tab.users.indexOf(v.trim()) == -1){
  174. emit('echo',{
  175. room: data.room,
  176. message: v+' left the room',
  177. from: 0
  178. });
  179. runHook('part',[
  180. v,
  181. data.room
  182. ]);
  183. }
  184. }
  185. });
  186. $(tab.users).each(function(i,v){
  187. if(v != null){
  188. if(users.indexOf(v.trim()) == -1){
  189. runHook('join',[
  190. v,
  191. data.room
  192. ]);
  193. }
  194. }
  195. });
  196. }
  197. },
  198. { // authorized
  199. on: 'authorized',
  200. fn: function(data){
  201. event('Authorized');
  202. properties.nick = data.nick;
  203. for(var i in settings.autojoin){
  204. emit('join',{
  205. name: settings.autojoin[i]
  206. });
  207. }
  208. runHook('authorized');
  209. }
  210. },
  211. { // join
  212. on: 'join',
  213. fn: function(data){
  214. event('joined '+data.name);
  215. var flag = tabs.length == 0;
  216. $o.ui.tabs.add(data.name);
  217. if(flag){
  218. $o.ui.tabs.select(0);
  219. }
  220. }
  221. },
  222. { // reconnect
  223. on: 'reconnect',
  224. fn: function(data){
  225. event('reconnected');
  226. properties.connected = true;
  227. $o.chat.auth();
  228. runHook('reconnect');
  229. emit('echo',{
  230. room: $o.ui.tabs.current().name,
  231. from: 0,
  232. message: 'reconnected'
  233. });
  234. }
  235. },
  236. { // connect
  237. on: 'connect',
  238. fn: function(data){
  239. event('connected');
  240. properties.connected = true;
  241. $o.chat.auth();
  242. runHook('connect');
  243. emit('echo',{
  244. room: $o.ui.tabs.current().name,
  245. from: 0,
  246. message: 'connected'
  247. });
  248. }
  249. },
  250. { // disconnect
  251. on: 'disconnect',
  252. fn: function(data){
  253. event('disconnected');
  254. properties.connected = false;
  255. runHook('disconnected');
  256. $o.msg('* disconnected');
  257. }
  258. },
  259. { // message
  260. on: 'message',
  261. fn: function(data){
  262. event('recieved message');
  263. var date = new Date(),
  264. string,
  265. time = date.getTime(),
  266. child,
  267. i,
  268. msg = function(msg){
  269. string = '<span class="cell date_cell">[<abbr class="date date_'+time+'" title="'+date.toISOString()+'"></abbr>]</span>';
  270. child = $('<li>').html(string+'<span class="cell">'+
  271. msg
  272. .htmlentities()
  273. .replace(
  274. /(https?:\/\/(([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?))/g,
  275. "<a href=\"$1\" title=\"\">$1</a>"
  276. )
  277. +'</span>');
  278. $o.msg({html:child},data.room);
  279. };
  280. if(data.from != 0){
  281. msg(' <'+data.from+'> '+data.message);
  282. }else{
  283. msg(' * '+data.message);
  284. }
  285. abbrDate('abbr.date_'+time);
  286. if(settings.timestamp == ''){
  287. $('.date_cell').css('visibility','hidden');
  288. }else{
  289. $('.date_cell').css('visibility','visible');
  290. }
  291. runHook('message',[
  292. data.message,
  293. data.from,
  294. data.room
  295. ]);
  296. }
  297. }
  298. ],
  299. hooks = [
  300. { // setting - setting
  301. type: 'setting',
  302. hook: 'setting',
  303. fn: function(name){
  304. return name != 'colour';
  305. }
  306. }
  307. ],
  308. plugins = [],
  309. pluginSandbox = {
  310. $: window.jQuery,
  311. jQuery: window.jQuery,
  312. $o: $o
  313. },
  314. currentPlugin = 0,
  315. Sandbox = function(sandbox){
  316. var i,o = {};
  317. for(i in window){
  318. o[i] = undefined;
  319. }
  320. o.window = o;
  321. for(i in sandbox){
  322. o[i] = sandbox[i];
  323. }
  324. return o;
  325. },
  326. runHook = function(name,args){
  327. var i,r=true,hook,fn,sandbox = {
  328. jQuery: window.jQuery,
  329. $: window.jQuery,
  330. $o: window.OmnomIRC,
  331. OmnomIRC: window.OmnomIRC
  332. };
  333. args=exists(args)?args:[];
  334. for(i in hooks){
  335. hook = hooks[i];
  336. if(hook.hook == name){
  337. r = runInSandbox(hook.fn,sandbox,args,true);
  338. }
  339. if(r == false){
  340. break;
  341. }
  342. }
  343. return r;
  344. },
  345. runInSandbox = function(fn,sandbox,args,isFn){
  346. args = exists(args)?args:[];
  347. sandbox = sandbox instanceof Sandbox?sandbox:new Sandbox(sandbox);
  348. var r = false;
  349. fn = (fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'');
  350. if(!exists(isFn) || !isFn){
  351. fn = 'function(){'+fn+'}';
  352. }
  353. fn = 'var ret = true;eval("with(this){ret = ('+fn+').apply(this,arguments);}");return ret;';
  354. try{
  355. r = (new Function(fn)).apply(sandbox,args);
  356. }catch(e){
  357. event('Sandboxed function failed to run: '+e+"\nFunction that ran: "+fn,'sandbox_error');
  358. }
  359. return r;
  360. },
  361. version = '3.0',
  362. abbrDate = function(selector){
  363. if(settings.timestamp == 'fuzzy'){
  364. $(selector).timeago();
  365. }else{
  366. $(selector).each(function(){
  367. var timestamp = settings.timestamp,
  368. i,
  369. text='',
  370. date = new Date($(this).attr('title'));
  371. if(timestamp == 'exact'){
  372. timestamp = 'H:m:s t';
  373. }
  374. for(i=0;i<timestamp.length;i++){
  375. switch(timestamp[i]){
  376. case 'H':text+=((date.getHours()+11)%12)+1;break;
  377. case 'h':text+=date.getHours();break;
  378. case 'm':text+=(date.getMinutes()>9?'':'0')+date.getMinutes();break;
  379. case 's':text+=(date.getSeconds()>9?'':'0')+date.getSeconds();break;
  380. case 't':text+=(date.getHours()>11)?'pm':'am';break;
  381. default:text+=timestamp[i];
  382. }
  383. }
  384. $(this).text(text);
  385. }).timeago('dispose');
  386. }
  387. },
  388. socket,$i,$s,$h,$cl,$c,$tl,hht;
  389. $.extend($o,{
  390. version: function(){
  391. return version;
  392. },
  393. register: {
  394. theme: function(name){
  395. if(-1==$.inArray(properties.themes,name)){
  396. properties.themes.push(name);
  397. runHook('theme',[name]);
  398. return true;
  399. }
  400. return false;
  401. },
  402. command: function(name,fn,help){
  403. if(-1==$.inArray(commands,name)){
  404. var o = {
  405. cmd: name,
  406. fn: fn
  407. };
  408. if(exists(help)){
  409. o.help = help;
  410. }
  411. commands.push(o);
  412. return true;
  413. }
  414. return false;
  415. },
  416. plugin: function(name){
  417. if(!exists(plugins[name])){
  418. plugins[name] = {
  419. name: name,
  420. loaded: false,
  421. started: false
  422. };
  423. return true;
  424. }
  425. return false;
  426. },
  427. setting: function(name,type,val,validate,values,callback){
  428. if(!exists(settings[name])){
  429. validate = exists(validate)?validate:function(){};
  430. values = exists(values)?values:undefined;
  431. callback = exists(callback)?callback:function(){};
  432. settings[name] = val;
  433. settingsConf[name] = {
  434. validate: validate,
  435. callback: callback,
  436. type: type,
  437. values: values,
  438. default: val,
  439. name: name
  440. }
  441. return true;
  442. }else{
  443. return false;
  444. }
  445. },
  446. hook: function(event,fn,type){
  447. type=exists(type)?type:'hook';
  448. hooks.push({
  449. hook: event,
  450. fn: fn,
  451. type: type
  452. })
  453. }
  454. },
  455. plugin: {
  456. register: function(name){
  457. return $o.register.plugin(name);
  458. },
  459. start: function(name){
  460. if(exists(plugins[name])){
  461. var plugin = plugins[name];
  462. if(plugin.started){
  463. $o.plugin.stop(name);
  464. }
  465. event('Starting plugin '+name);
  466. if(!plugin.loaded){
  467. $.ajax('data/plugins/'+name+'/script.js',{
  468. dataType: 'text',
  469. success: function(data){
  470. plugin.script = data;
  471. runInSandbox(data,pluginSandbox);
  472. plugin.started = true;
  473. }
  474. });
  475. }else{
  476. runInSandbox(plugin.script,pluginSandbox);
  477. plugin.started = true;
  478. }
  479. return true;
  480. }else{
  481. return false;
  482. }
  483. },
  484. stop: function(name){
  485. if(exists(plugins[name])){
  486. event('Stopping plugin '+name);
  487. var i;
  488. for(i in hooks){
  489. if(hooks[i].type == 'plugin' && hooks[i].plugin == name){
  490. hooks.spice(i,1);
  491. }
  492. }
  493. // TODO, remove other things
  494. plugins[name] = undefined;
  495. delete plugins[name];
  496. }else{
  497. return false;
  498. }
  499. },
  500. remove: function(name){
  501. if($o.plugin.stop(name)){
  502. plugin[name] = undefined;
  503. delete plugin[name];
  504. return true;
  505. }else{
  506. return false;
  507. }
  508. },
  509. dir: function(name){
  510. return 'data/plugins/'+name+'/';
  511. }
  512. },
  513. hook: function(event,fn){
  514. $o.register.hook(event,fn);
  515. },
  516. ui: {
  517. render: {
  518. settings: function(){
  519. var name,setting,frag = document.createDocumentFragment(),item;
  520. for(name in settings){
  521. setting = $o.get(name,true);
  522. switch(setting.type){
  523. case 'select':
  524. item = $('<select>')
  525. .attr('id','setting_'+name)
  526. .change(function(){
  527. $o.set(this.id.substr(8),$(this).find(':selected').text().trim(),false);
  528. });
  529. for(var i in setting.values){
  530. item.append(
  531. $('<option>')
  532. .text(setting.values[i])
  533. );
  534. }
  535. item.find(':contains('+setting.val+')').attr('selected','selected');
  536. break;
  537. case 'array':
  538. item = $('<input>')
  539. .attr({
  540. type: 'text',
  541. id: 'setting_'+name
  542. })
  543. .val(setting.val)
  544. .change(function(){
  545. $o.set(this.id.substr(8),$(this).val().split(','),false);
  546. });
  547. break;
  548. case 'boolean':
  549. item = $('<input>')
  550. .attr({
  551. type: 'checkbox',
  552. id: 'setting_'+name
  553. })
  554. .change(function(){
  555. $o.set(this.id.substr(8),$(this).is(':checked'),false);
  556. });
  557. if(setting.val){
  558. item.attr('checked','checked');
  559. }
  560. break;
  561. case 'number':
  562. case 'string':default:
  563. item = $('<input>')
  564. .attr({
  565. type: 'text',
  566. id: 'setting_'+name
  567. })
  568. .val(setting.val)
  569. .change(function(){
  570. $o.set(this.id.substr(8),$(this).val(),false);
  571. });
  572. }
  573. $(frag).append(
  574. $('<li>')
  575. .addClass('row')
  576. .append(
  577. $('<span>')
  578. .text(name)
  579. .addClass('cell')
  580. )
  581. .append(
  582. $('<span>')
  583. .append(item)
  584. .addClass('cell')
  585. )
  586. );
  587. }
  588. if(settings.debug){
  589. $(frag).append(
  590. $('<li>')
  591. .addClass('row')
  592. .attr('id','console-log-controls')
  593. .append(
  594. $('<span>')
  595. .text('Debug Log')
  596. .addClass('cell')
  597. )
  598. .append(
  599. $('<span>')
  600. .addClass('cell')
  601. .append(
  602. $('<button>')
  603. .text('Show')
  604. .click(function(){
  605. $(this).text($('#console-log').is(':visible')?'Show':'Hide');
  606. $('#console-log').toggle();
  607. $('#console-log-clear').toggle();
  608. $('#content').toggle();
  609. })
  610. )
  611. .append(
  612. $('<button>')
  613. .text('Clear')
  614. .attr('id','console-log-clear')
  615. .hide()
  616. .click(function(){
  617. $('#console-log-pre').html('');
  618. })
  619. )
  620. )
  621. );
  622. }else{
  623. $('#console-log-pre').html('');
  624. $('#console-log').hide();
  625. $('#content').show();
  626. }
  627. $('#settings-list').html(frag);
  628. },
  629. users: function(){
  630. event('Rendering userlist');
  631. var $ul = $('#user-list').html(''),
  632. i,
  633. names = $o.ui.tabs.current().users;
  634. for(i in names){
  635. $ul.append(
  636. $('<li>').text(names[i])
  637. );
  638. }
  639. },
  640. tab: function(){
  641. $cl.html($($o.ui.tabs.current().body).clone());
  642. },
  643. tablist: function(){
  644. $tl.html('');
  645. var i,tab;
  646. for(i in tabs){
  647. tab = $o.ui.tabs.obj(i);
  648. if(i==selectedTab){
  649. tab.addClass('clicked');
  650. $('#title').text(tabs[i].name);
  651. $('#topic').text(tabs[i].topic);
  652. }
  653. $tl.append(tab);
  654. }
  655. if($tl.get(0).scrollHeight-20 != $tl.scrollTop()){
  656. $('#tabs-scroll-right').removeClass('disabled');
  657. }
  658. if($tl.scrollTop() != 0){
  659. $('#tabs-scroll-left').removeClass('disabled');
  660. }
  661. }
  662. },
  663. tabs: {
  664. add: function(name){
  665. event('Tab added: '+name);
  666. if(!(function(){
  667. for(var i in tabs){
  668. if(name==tabs[i].name){
  669. return true;
  670. }
  671. }
  672. return false;
  673. })()){
  674. var scroll = $.localStorage('tabs'),
  675. i,
  676. frag = document.createDocumentFragment(),
  677. id = tabs.length;
  678. for(i in scroll){
  679. if(scroll[i].name == name){
  680. scroll[i].body = $(scroll[i].body).slice(-10);
  681. $(frag)
  682. .append(scroll[i].body)
  683. .append(
  684. $('<li>').html('<span class="to_remove">-- loaded old scrollback for '+scroll[i].date+' --</span>')
  685. )
  686. .children()
  687. .children('.remove')
  688. .remove();
  689. $(frag)
  690. .children()
  691. .children('.to_remove')
  692. .removeClass('to_remove')
  693. .addClass('remove');
  694. event('loading old tab scrollback for '+name+' last saved +'+scroll[i].date);
  695. }
  696. }
  697. tabs.push({
  698. name: name,
  699. body: frag,
  700. date: new Date(),
  701. send: function(msg){
  702. $o.chat.send(msg,$o.ui.tabs.tab(id).name);
  703. },
  704. close: function(){
  705. $o.ui.tabs.remove(id);
  706. },
  707. users: [],
  708. names: function(){
  709. emit('names',{
  710. name: $o.ui.tabs.tab(id).name
  711. });
  712. },
  713. select: function(){
  714. $o.ui.tabs.select(id);
  715. },
  716. clear: function(){
  717. $cl.html('');
  718. $o.ui.tabs.tab(id).body = document.createDocumentFragment();
  719. emit('echo',{
  720. room: $o.ui.tabs.tab(id).name,
  721. message: 'messages cleared',
  722. from: 0
  723. });
  724. }
  725. });
  726. $tl.append($o.ui.tabs.obj(id));
  727. $o.ui.render.tablist();
  728. $o.ui.render.users();
  729. }else{
  730. event('Attempted to add an existing tab');
  731. }
  732. },
  733. remove: function(name){
  734. if(typeof name == 'number'){
  735. name = tabs[name].name;
  736. }
  737. for(var id=0;id<tabs.length;id++){
  738. if($o.ui.tabs.tab(id).name == name){
  739. event('Tab removed: '+$o.ui.tabs.tab(id).name);
  740. emit('part',{
  741. name: $o.ui.tabs.tab(id).name
  742. });
  743. tabs.splice(id,1);
  744. if(selectedTab==id&&selectedTab>0){
  745. selectedTab--;
  746. }
  747. break;
  748. }
  749. }
  750. $o.ui.render.tablist();
  751. $cl.html($o.ui.tabs.current().body);
  752. $o.ui.render.users();
  753. },
  754. selected: function(){
  755. return selectedTab;
  756. },
  757. idForName: function(name){
  758. for(var i in tabs){
  759. if(tabs[i].name == name){
  760. return i;
  761. }
  762. }
  763. return false;
  764. },
  765. tab: function(id){
  766. if(typeof id == 'string' && !id.isNumber()){
  767. id = $o.ui.tabs.idForName(id);
  768. if(!id) return false;
  769. }
  770. return exists(tabs[id])?tabs[id]:false;
  771. },
  772. dom: function(id){
  773. if(typeof id == 'string' && !id.isNumber()){
  774. id = $o.ui.tabs.idForName(id);
  775. if(!id) return false;
  776. }
  777. return typeof tabs[id] == 'undefined'?false:tabs[id].body;
  778. },
  779. obj: function(id){
  780. if(exists(id)){
  781. if(typeof id == 'string' && !id.isNumber()){
  782. id = $o.ui.tabs.idForName(id);
  783. if(!id) return;
  784. }
  785. return $('<div>')
  786. .addClass('tab')
  787. .text($o.ui.tabs.tab(id).name)
  788. .mouseup(function(e){
  789. switch(e.which){
  790. case 1: // RMB
  791. if($(this).data('id')!=selectedTab){
  792. $o.ui.tabs.select($(this).data('id'));
  793. return prevent(e);
  794. }
  795. break;
  796. case 2: // MMB
  797. $(this).children('span.close-button').click();
  798. return prevent(e);
  799. break;
  800. case 3: // LMB
  801. return prevent(e);
  802. break;
  803. default:
  804. return prevent(e);
  805. }
  806. })
  807. .append(
  808. $('<span>')
  809. .addClass('close-button')
  810. .click(function(){
  811. $o.ui.tabs.remove(id);
  812. return false;
  813. })
  814. .css({
  815. 'position': 'absolute',
  816. 'background-color': 'inherit',
  817. 'top': 0,
  818. 'right': 0
  819. })
  820. .html('&times;')
  821. )
  822. .data('id',id);
  823. }
  824. },
  825. select: function(id){
  826. if(typeof id == 'string' && !id.isNumber()){
  827. id = $o.ui.tabs.idForName(id);
  828. if(!id) return false;
  829. }
  830. event(id+' '+$o.ui.tabs.tab(id).name,'tab_select');
  831. if(id<tabs.length&&id>=0){
  832. selectedTab=id;
  833. }
  834. $tl.children('.clicked').removeClass('clicked');
  835. $($tl.children().get(id)).addClass('clicked');
  836. $('#title').text($o.ui.tabs.tab(id).name);
  837. $('#topic').text($o.ui.tabs.tab(id).topic);
  838. $cl.html($($o.ui.tabs.tab(id).body).clone());
  839. abbrDate('abbr.date');
  840. $o.ui.render.users();
  841. setTimeout(function scrollContent(){
  842. if($c.scrollTop()+$c.height() < $c.prop('scrollHeight')){
  843. $c.scrollTop($c.scrollTop()+1);
  844. setTimeout(scrollContent,settings.scrollspeed);
  845. }else{
  846. event('scrolling stopped');
  847. }
  848. },settings.scrollspeed);
  849. },
  850. current: function(){
  851. if(tabs.length > 0 && tabs.length > selectedTab){
  852. return tabs[selectedTab];
  853. }else{
  854. return {
  855. name: '',
  856. body: document.createDocumentFragment(),
  857. date: new Date(),
  858. send: noop,
  859. close: noop,
  860. users: [],
  861. names: noop,
  862. select: noop,
  863. clear: noop
  864. }
  865. }
  866. }
  867. },
  868. },
  869. chat: {
  870. connect: function(server){
  871. if($o.chat.connected()){
  872. $o.disconnect();
  873. }
  874. event('Connecting');
  875. if(!exists(server)){
  876. server = settings.server;
  877. }
  878. socket = window.socket = io.connect(server);
  879. for(var i in handles){
  880. socket.on(handles[i].on,handles[i].fn);
  881. }
  882. $o.chat.auth();
  883. },
  884. disconnect: function(){
  885. if($o.chat.connected()){
  886. event('Disconnecting');
  887. socket.disconnect();
  888. }
  889. },
  890. connected: function(){
  891. return exists(socket)?properties.connected:false;
  892. },
  893. send: function(msg,room){
  894. if(!exists(room)){
  895. room = $o.ui.tabs.current().name;
  896. }
  897. if(msg !== ''){
  898. if(msg[0] == '/' && msg[1] != '/'){
  899. var args = msg.split(' '),
  900. cmd = args[0].substr(1),
  901. i;
  902. event(msg,'command');
  903. for(i in commands){
  904. if(commands[i].cmd == cmd){
  905. commands[i].fn(args);
  906. return;
  907. }
  908. }
  909. $o.msg(cmd+' is not a valid command.');
  910. }else{
  911. if(msg[0]+msg[1] == '//'){
  912. msg = msg.substr(1);
  913. }
  914. event(msg,'send');
  915. if(runHook('send',[msg,room])){
  916. emit('message',{
  917. message: msg,
  918. room: room,
  919. from: properties.nick
  920. });
  921. }
  922. }
  923. }
  924. },
  925. auth: function(){
  926. if(settings.nick == ''){
  927. $o.set('nick','User');
  928. return;
  929. }
  930. emit('auth',{
  931. nick: settings.nick
  932. // TODO - send authorization info
  933. });
  934. }
  935. },
  936. get: function(name,formatted){
  937. if(!exists(formatted)){
  938. return exists(settings[name])?settings[name]:false;
  939. }else{
  940. if(exists(settingsConf[name]) && exists(settings[name])){
  941. var r = $.extend({},settingsConf[name]);
  942. r.val = settings[name];
  943. r.validate = undefined;
  944. r.callback = undefined;
  945. delete r['validate'];
  946. delete r['callback'];
  947. return r;
  948. }else{
  949. return false;
  950. }
  951. }
  952. },
  953. set: function(name,value,render){
  954. if(exists(settings[name])){
  955. var setting;
  956. if(exists(settingsConf[name])){
  957. setting = $.extend({},settingsConf[name]);
  958. }else{
  959. setting = {
  960. val: setting[name],
  961. callback: function(){},
  962. validate: function(){},
  963. values: undefined,
  964. type: typeof setting[name]
  965. }
  966. }
  967. if(
  968. (exists(setting.values) && $.inArray(value,setting.values) == -1) ||
  969. setting.validate(settings[name],value,setting.values,name) == false ||
  970. !runHook('setting',[
  971. name,
  972. settings[name],
  973. value,
  974. $o.get(name,true).values
  975. ])
  976. ){
  977. if(exists(render)){
  978. $o.ui.render.settings();
  979. }
  980. return false;
  981. }
  982. settings[name] = value;
  983. $.localStorage('settings',JSON.stringify(settings));
  984. setting.callback(value,name,exists(render));
  985. if(exists(render)){
  986. $o.ui.render.settings();
  987. }
  988. return true;
  989. }else{
  990. if(exists(render)){
  991. $o.ui.render.settings();
  992. }
  993. return false;
  994. }
  995. },
  996. prop: function(name){
  997. return exists(properties[name])?properties[name]:null;
  998. },
  999. send: function(msg){
  1000. $o.chat.send(msg);
  1001. },
  1002. msg: function(msg,tabName){
  1003. var frag;
  1004. if(!exists(tabName) || tabName == $o.ui.tabs.current().name || !exists($o.ui.tabs.tab(tabName).body)){
  1005. frag = document.createDocumentFragment();
  1006. }else{
  1007. frag = $o.ui.tabs.tab(tabName).body;
  1008. }
  1009. try{
  1010. switch(typeof msg){
  1011. case 'string':
  1012. $(frag).append($('<li>').html(msg.htmlentities()));
  1013. break;
  1014. case 'object':
  1015. if(!exists(msg.html)){
  1016. $(frag).append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  1017. }else{
  1018. $(frag).append(msg.html);
  1019. }
  1020. break;
  1021. }
  1022. }catch(e){event('Failed to add message','error')}
  1023. if(tabs.length > 0){
  1024. $($o.ui.tabs.tab(tabName).body || $o.ui.tabs.current().body).append(frag);
  1025. }
  1026. var scroll = [],i,html;
  1027. for(i in tabs){
  1028. html = '';
  1029. $(tabs[i].body).children().each(function(){
  1030. html += this.outerHTML;
  1031. });
  1032. scroll.push({
  1033. name: tabs[i].name,
  1034. body: html,
  1035. date: new Date().toString()
  1036. });
  1037. }
  1038. $.localStorage('tabs',scroll);
  1039. if(!exists(tabName) || tabName == $o.ui.tabs.current().name){
  1040. $o.ui.tabs.select(selectedTab);
  1041. }
  1042. },
  1043. event: function(event_name,message){
  1044. event(message,event_name);
  1045. }
  1046. });
  1047. (function(settings){
  1048. var i,s;
  1049. for(i in settings){
  1050. s = settings[i];
  1051. $o.register.setting(i,s.type,s.val,s['validate'],s['values'],s['callback']);
  1052. }
  1053. })({
  1054. colour: {
  1055. type: 'boolean',
  1056. val: false
  1057. },
  1058. debug: {
  1059. type: 'boolean',
  1060. val: false,
  1061. callback: function(v,s,r){
  1062. if(r){
  1063. $o.ui.render.settings();
  1064. }
  1065. }
  1066. },
  1067. timestamp: {
  1068. type: 'string',
  1069. val: 'exact',
  1070. callback: function(v,s){
  1071. abbrDate('abbr.date');
  1072. if(v == ''){
  1073. $('.date_cell').css('visibility','hidden');
  1074. }else{
  1075. $('.date_cell').css('visibility','visible');
  1076. }
  1077. }
  1078. },
  1079. server: {
  1080. type: 'string',
  1081. val: location.origin
  1082. },
  1083. autoconnect: {
  1084. type: 'boolean',
  1085. val: true
  1086. },
  1087. autojoin: {
  1088. type: 'array',
  1089. val: [
  1090. '#omnimaga',
  1091. '#omnimaga-fr',
  1092. '#irp'
  1093. ]
  1094. },
  1095. scrollspeed: {
  1096. type: 'number',
  1097. val: 10
  1098. },
  1099. theme: {
  1100. type: 'select',
  1101. val: 'default',
  1102. values: properties.themes,
  1103. validate: function(o,n,v,s){
  1104. runHook('untheme',[o,n]);
  1105. },
  1106. callback: function(v,s,r){
  1107. if($('link[id="theme-style"]').attr('href') != 'data/themes/'+v+'/style.css'){
  1108. event('Loading theme '+v);
  1109. runHook('theme',[v]);
  1110. $('link[id="theme-style"]').attr({
  1111. id: 'theme-style',
  1112. rel: 'stylesheet',
  1113. href: 'data/themes/'+v+'/style.css'
  1114. });
  1115. var i,h;
  1116. for(i in hooks){
  1117. h = hooks[i];
  1118. if(h.type == 'style'){
  1119. hooks.splice(i,1);
  1120. }
  1121. }
  1122. $.ajax('data/themes/'+v+'/script.js',{
  1123. dataType: 'text',
  1124. success: function(data){
  1125. var sandbox = {
  1126. load: function(fn){
  1127. fn();
  1128. },
  1129. unload: function(fn){
  1130. $o.register.hook('untheme',"function(o,n){if(o == '"+v+"'){("+(fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'')+")();}}",'style');
  1131. },
  1132. $: window.jQuery,
  1133. $o: window.OmnomIRC,
  1134. OmnomIRC: window.OmnomIRC,
  1135. };
  1136. runInSandbox(data,sandbox);
  1137. }
  1138. });
  1139. }
  1140. }
  1141. },
  1142. nick: {
  1143. type: 'string',
  1144. val: 'User',
  1145. callback: function(){
  1146. $o.chat.auth();
  1147. }
  1148. }
  1149. });
  1150. $.extend(pluginSandbox,$o);
  1151. String.prototype.htmlentities = function(){
  1152. return this
  1153. .replace(/&/g, '&amp;')
  1154. .replace(/</g, '&lt;')
  1155. .replace(/>/g, '&gt;')
  1156. .replace(/\n/g,'<br/>')
  1157. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  1158. .replace(/\s/g, '&nbsp;')
  1159. .replace(/"/g, '&quot;');
  1160. };
  1161. $(document).ready(function(){
  1162. $.extend(settings,$.parseJSON($.localStorage('settings')));
  1163. $.localStorage('settings',JSON.stringify(settings));
  1164. settingsConf['theme'].callback(settings['theme'],'theme',true);
  1165. $i = $('#input');
  1166. $s = $('#send');
  1167. $cl = $('#content-list');
  1168. $c = $('#content');
  1169. $tl = $('#tabs-list');
  1170. $h = $('#head');
  1171. $s.click(function(){
  1172. if(!$s.hasClass('clicked')){
  1173. $s.addClass('clicked');
  1174. setTimeout(function(){
  1175. $s.removeClass('clicked');
  1176. },500);
  1177. }
  1178. $o.send($i.val());
  1179. $i.val('');
  1180. });
  1181. $i.keypress(function(e){
  1182. if(e.keyCode == 13){
  1183. if(!$s.hasClass('clicked')){
  1184. $s.addClass('clicked');
  1185. setTimeout(function(){
  1186. $s.removeClass('clicked');
  1187. },500);
  1188. }
  1189. $o.send($i.val());
  1190. $i.val('');
  1191. }
  1192. });
  1193. $('#settings, #users').click(function(){
  1194. $(this).addClass('open');
  1195. $(this).children('.close-button').show();
  1196. }).hover(function(){
  1197. $(this).addClass('hovered');
  1198. },function(){
  1199. $(this).removeClass('hovered');
  1200. }).children('.close-button').click(function(){
  1201. $(this).parent().removeClass('open');
  1202. $(this).hide();
  1203. return false;
  1204. }).hide();
  1205. $('#users').hoverIntent({
  1206. out: function(){
  1207. $(this).removeClass('open');
  1208. $(this).children('.close-button').hide();
  1209. },
  1210. timeout: 1000
  1211. });
  1212. $('#content').click(function(){
  1213. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  1214. $('#settings, #users').children('.close-button').hide()
  1215. });
  1216. $('.unselectable').attr('unselectable','on');
  1217. $.contextMenu({
  1218. selector: 'div.tab',
  1219. items: {
  1220. add: {
  1221. name: 'New Tab',
  1222. icon: 'add',
  1223. callback: function(){
  1224. $(this).contextMenu('hide');
  1225. $o.ui.tabs.add(prompt('Channel'));
  1226. }
  1227. },
  1228. s1: '',
  1229. close: {
  1230. name: 'Close',
  1231. icon: 'delete',
  1232. callback: function(){
  1233. $(this).contextMenu('hide');
  1234. $o.ui.tabs.remove($(this).data('id'));
  1235. }
  1236. }
  1237. },
  1238. zIndex: 99999,
  1239. trigger: 'right'
  1240. });
  1241. $.contextMenu({
  1242. selector: '#tabs-list',
  1243. items: {
  1244. add: {
  1245. name: 'New Tab',
  1246. icon: 'add',
  1247. callback: function(){
  1248. $(this).contextMenu('hide');
  1249. $o.ui.tabs.add(prompt('channel'));
  1250. }
  1251. }
  1252. },
  1253. zIndex: 99999,
  1254. trigger: 'right'
  1255. });
  1256. $('#tabs-scroll-right').click(function(){
  1257. event('scroll right');
  1258. $tl.scrollTop(($tl.scrollTop()||0)+20);
  1259. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  1260. $('#tabs-scroll-right').addClass('disabled');
  1261. }
  1262. $('#tabs-scroll-left').removeClass('disabled');
  1263. });
  1264. $('#tabs-scroll-left').click(function(){
  1265. event('scroll left');
  1266. $tl.scrollTop(($tl.scrollTop()||0)-20);
  1267. if($tl.scrollTop() == 0){
  1268. $('#tabs-scroll-left').addClass('disabled');
  1269. }
  1270. $('#tabs-scroll-right').removeClass('disabled');
  1271. });
  1272. (function scrollup(){
  1273. $('#tabs-scroll-left').click();
  1274. if($tl.scrollTop() != 0){
  1275. setTimeout(scrollup,10);
  1276. }
  1277. })();
  1278. event('Date '+new Date,'ready');
  1279. $h.addClass('hovered');
  1280. setTimeout(function(){
  1281. $h.removeClass('hovered');
  1282. },1000);
  1283. $o.ui.render.settings();
  1284. if(settings.autoconnect){
  1285. $o.chat.connect();
  1286. }
  1287. // Check for script updates and update if required
  1288. (function checkScripts(){
  1289. for(var i in document.scripts){
  1290. (function(el,src){
  1291. if(exists(src) && el.innerHTML == ''){
  1292. $.ajax(src,{
  1293. success: function(source){
  1294. if(exists($(el).data('source')) && $(el).data('source') != source){
  1295. event('Reloading','update');
  1296. location.reload();
  1297. }
  1298. $(el).data('source',source);
  1299. },
  1300. dataType: 'text'
  1301. });
  1302. }
  1303. })(document.scripts[i],document.scripts[i].src);
  1304. }
  1305. var s = $('link');
  1306. for(i in s){
  1307. (function(el,href){
  1308. if(exists(href) && el.innerHTML == ''){
  1309. $.ajax(href,{
  1310. success: function(source){
  1311. if(exists($(el).data('source')) && $(el).data('source') != source){
  1312. event('Updating CSS','update');
  1313. if(!exists($(el).data('href'))){
  1314. $(el).data('href',href);
  1315. }
  1316. if($(el).data('href') != href){
  1317. href = $(el).data('href');
  1318. }
  1319. el.href += '?reload='+new Date().getTime();
  1320. }
  1321. $(el).data('source',source);
  1322. },
  1323. dataType: 'text'
  1324. });
  1325. }
  1326. })(s[i],s[i].href);
  1327. }
  1328. setTimeout(checkScripts,1000*30);
  1329. })();
  1330. });
  1331. window.io = null;
  1332. runHook('load');
  1333. })(window,jQuery,io);
  1334. if (!Date.prototype.toISOString) {
  1335. Date.prototype.toISOString = function() {
  1336. function pad(n) { return n < 10 ? '0' + n : n }
  1337. return this.getUTCFullYear() + '-'
  1338. + pad(this.getUTCMonth() + 1) + '-'
  1339. + pad(this.getUTCDate()) + 'T'
  1340. + pad(this.getUTCHours()) + ':'
  1341. + pad(this.getUTCMinutes()) + ':'
  1342. + pad(this.getUTCSeconds()) + 'Z';
  1343. };
  1344. }
  1345. if(!String.prototype.isNumber){
  1346. String.prototype.isNumber = function(){return /^\d+$/.test(this);}
  1347. }