omnomirc.js 32 KB

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