omnomirc.js 32 KB

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