omnomirc.js 33 KB

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