omnomirc.js 34 KB

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