omnomirc.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. OmnomIRC COPYRIGHT 2010,2011 Netham45
  3. OmnomIRC3 rewrite COPYRIGHT 2013 Nathaniel 'Eeems' van Diepen
  4. This file is part of OmnomIRC.
  5. OmnomIRC is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. OmnomIRC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with OmnomIRC. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. (function(window,$,io,undefined){
  17. "use strict";
  18. var $o = window.OmnomIRC = window.$o = function(){
  19. return 'Version: '+version;
  20. },
  21. event = function(msg,type){
  22. if(settings.debug){
  23. type=exists(type)?type:'event';
  24. switch(type){
  25. case 'ready':type='document_ready';break;
  26. }
  27. log('['+type.toUpperCase()+'] '+msg);
  28. }
  29. },
  30. emit = function(type,data){
  31. if($o.chat.connected()){
  32. socket.emit.apply(socket,arguments);
  33. }else{
  34. if(tabs.length > 0){
  35. $o.msg('Disconnected, cannot do anything');
  36. }
  37. }
  38. },
  39. noop = function(){},
  40. log = function(){
  41. console.log.apply(console,arguments);
  42. },
  43. exists = function(object){
  44. return typeof object != 'undefined';
  45. },
  46. prevent = function(e){
  47. e.stopImmediatePropagation();
  48. e.stopPropagation();
  49. e.preventDefault();
  50. return false;
  51. },
  52. selectedTab=0,
  53. settings = {},
  54. settingsConf = {},
  55. tabs = [],
  56. properties = {
  57. nick: 'User',
  58. sig: '',
  59. tabs: tabs,
  60. themes: []
  61. },
  62. commands = [
  63. { // names
  64. cmd: 'names',
  65. fn: function(args){
  66. emit('names',{
  67. name: $o.ui.tabs.current().name
  68. });
  69. }
  70. },
  71. { // me
  72. cmd: 'me',
  73. help: 'Say something in third person',
  74. fn: function(args){
  75. var i,ret='';
  76. for(i=1;i<args.length;i++){
  77. ret += ' '+args[i];
  78. }
  79. emit('message',{
  80. from: 0,
  81. message: properties.nick+' '+ret,
  82. room: $o.ui.tabs.current().name
  83. });
  84. }
  85. },
  86. { // connect
  87. cmd: 'connect',
  88. fn: function(){
  89. if(!$o.chat.connected()){
  90. $o.chat.connect();
  91. }
  92. }
  93. },
  94. { // disconnect
  95. cmd: 'disconnect',
  96. fn: function(){
  97. $o.disconnect();
  98. }
  99. },
  100. { // nick
  101. cmd: 'nick',
  102. fn: function(args){
  103. $o.set('nick',args[1]);
  104. }
  105. },
  106. { // help
  107. cmd: 'help',
  108. fn: function(args){
  109. if(!exists(args[1])){
  110. var m = 'Commands:',i;
  111. for(i in commands){
  112. m += ' '+commands[i].cmd;
  113. }
  114. $o.msg(m);
  115. }else{
  116. var i,cmd;
  117. for(i in commands){
  118. cmd = commands[i];
  119. if(cmd.cmd == args[1] && exists(cmd.help)){
  120. $o.msg('Command /'+cmd.cmd+': '+cmd.help);
  121. return;
  122. }
  123. }
  124. }
  125. }
  126. },
  127. { // open
  128. cmd: 'open',
  129. fn: function(args){
  130. $o.ui.tabs.add(args[1]);
  131. }
  132. },
  133. { // clear
  134. cmd: 'clear',
  135. fn: function(args){
  136. $o.ui.tabs.current().clear();
  137. }
  138. },
  139. { // close
  140. cmd: 'close',
  141. fn: function(args){
  142. if(args.length > 1){
  143. $o.ui.tabs.remove(args[1]);
  144. }else{
  145. $o.ui.tabs.remove(selectedTab);
  146. }
  147. }
  148. },
  149. { // tabs
  150. cmd: 'tabs',
  151. fn: function(args){
  152. $o.msg('tabs:');
  153. for(var i in tabs){
  154. $o.msg(' ['+i+'] '+tabs[i].name);
  155. }
  156. }
  157. }
  158. ],
  159. handles = [
  160. { // names
  161. on: 'names',
  162. fn: function(data){
  163. var tab = $o.ui.tabs.tab(data.room),
  164. users = tab.users,
  165. i;
  166. tab.users = data.names;
  167. if($o.ui.tabs.idForName(data.room) == selectedTab){
  168. $o.ui.render.users();
  169. }
  170. $(users).each(function(i,v){
  171. if(v != null){
  172. if(tab.users.indexOf(v.trim()) == -1){
  173. emit('echo',{
  174. room: data.room,
  175. message: v+' left the room',
  176. from: 0
  177. });
  178. runHook('part',[
  179. v,
  180. data.room
  181. ]);
  182. }
  183. }
  184. });
  185. $(tab.users).each(function(i,v){
  186. if(v != null){
  187. if(users.indexOf(v.trim()) == -1){
  188. runHook('join',[
  189. v,
  190. data.room
  191. ]);
  192. }
  193. }
  194. });
  195. }
  196. },
  197. { // authorized
  198. on: 'authorized',
  199. fn: function(data){
  200. event('Authorized');
  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. $o: $o
  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. if(runHook('send',[msg,room])){
  915. emit('message',{
  916. message: msg,
  917. room: room,
  918. from: properties.nick
  919. });
  920. }
  921. }
  922. }
  923. },
  924. auth: function(){
  925. if(settings.nick == ''){
  926. $o.set('nick','User');
  927. return;
  928. }
  929. emit('auth',{
  930. nick: settings.nick
  931. // TODO - send authorization info
  932. });
  933. }
  934. },
  935. get: function(name,formatted){
  936. if(!exists(formatted)){
  937. return exists(settings[name])?settings[name]:false;
  938. }else{
  939. if(exists(settingsConf[name]) && exists(settings[name])){
  940. var r = $.extend({},settingsConf[name]);
  941. r.val = settings[name];
  942. r.validate = undefined;
  943. r.callback = undefined;
  944. delete r['validate'];
  945. delete r['callback'];
  946. return r;
  947. }else{
  948. return false;
  949. }
  950. }
  951. },
  952. set: function(name,value,render){
  953. if(exists(settings[name])){
  954. var setting;
  955. if(exists(settingsConf[name])){
  956. setting = $.extend({},settingsConf[name]);
  957. }else{
  958. setting = {
  959. val: setting[name],
  960. callback: function(){},
  961. validate: function(){},
  962. values: undefined,
  963. type: typeof setting[name]
  964. }
  965. }
  966. if(
  967. (exists(setting.values) && $.inArray(value,setting.values) == -1) ||
  968. setting.validate(settings[name],value,setting.values,name) == false ||
  969. !runHook('setting',[
  970. name,
  971. settings[name],
  972. value,
  973. $o.get(name,true).values
  974. ])
  975. ){
  976. if(exists(render)){
  977. $o.ui.render.settings();
  978. }
  979. return false;
  980. }
  981. settings[name] = value;
  982. $.localStorage('settings',JSON.stringify(settings));
  983. setting.callback(value,name,exists(render));
  984. if(exists(render)){
  985. $o.ui.render.settings();
  986. }
  987. return true;
  988. }else{
  989. if(exists(render)){
  990. $o.ui.render.settings();
  991. }
  992. return false;
  993. }
  994. },
  995. prop: function(name){
  996. return exists(properties[name])?properties[name]:null;
  997. },
  998. send: function(msg){
  999. $o.chat.send(msg);
  1000. },
  1001. msg: function(msg,tabName){
  1002. var frag;
  1003. if(!exists(tabName) || tabName == $o.ui.tabs.current().name || !exists($o.ui.tabs.tab(tabName).body)){
  1004. frag = document.createDocumentFragment();
  1005. }else{
  1006. frag = $o.ui.tabs.tab(tabName).body;
  1007. }
  1008. try{
  1009. switch(typeof msg){
  1010. case 'string':
  1011. $(frag).append($('<li>').html(msg.htmlentities()));
  1012. break;
  1013. case 'object':
  1014. if(!exists(msg.html)){
  1015. $(frag).append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  1016. }else{
  1017. $(frag).append(msg.html);
  1018. }
  1019. break;
  1020. }
  1021. }catch(e){event('Failed to add message','error')}
  1022. if(tabs.length > 0){
  1023. $($o.ui.tabs.tab(tabName).body || $o.ui.tabs.current().body).append(frag);
  1024. }
  1025. var scroll = [],i,html;
  1026. for(i in tabs){
  1027. html = '';
  1028. $(tabs[i].body).children().each(function(){
  1029. html += this.outerHTML;
  1030. });
  1031. scroll.push({
  1032. name: tabs[i].name,
  1033. body: html,
  1034. date: new Date().toString()
  1035. });
  1036. }
  1037. $.localStorage('tabs',scroll);
  1038. if(!exists(tabName) || tabName == $o.ui.tabs.current().name){
  1039. $o.ui.tabs.select(selectedTab);
  1040. }
  1041. },
  1042. event: function(event_name,message){
  1043. event(message,event_name);
  1044. }
  1045. });
  1046. (function(settings){
  1047. var i,s;
  1048. for(i in settings){
  1049. s = settings[i];
  1050. $o.register.setting(i,s.type,s.val,s['validate'],s['values'],s['callback']);
  1051. }
  1052. })({
  1053. colour: {
  1054. type: 'boolean',
  1055. val: false
  1056. },
  1057. debug: {
  1058. type: 'boolean',
  1059. val: false,
  1060. callback: function(v,s,r){
  1061. if(r){
  1062. $o.ui.render.settings();
  1063. }
  1064. }
  1065. },
  1066. timestamp: {
  1067. type: 'string',
  1068. val: 'exact',
  1069. callback: function(v,s){
  1070. abbrDate('abbr.date');
  1071. if(v == ''){
  1072. $('.date_cell').css('visibility','hidden');
  1073. }else{
  1074. $('.date_cell').css('visibility','visible');
  1075. }
  1076. }
  1077. },
  1078. server: {
  1079. type: 'string',
  1080. val: location.origin
  1081. },
  1082. autoconnect: {
  1083. type: 'boolean',
  1084. val: true
  1085. },
  1086. autojoin: {
  1087. type: 'array',
  1088. val: [
  1089. '#omnimaga',
  1090. '#omnimaga-fr',
  1091. '#irp'
  1092. ]
  1093. },
  1094. scrollspeed: {
  1095. type: 'number',
  1096. val: 10
  1097. },
  1098. theme: {
  1099. type: 'select',
  1100. val: 'default',
  1101. values: properties.themes,
  1102. validate: function(o,n,v,s){
  1103. runHook('untheme',[o,n]);
  1104. },
  1105. callback: function(v,s,r){
  1106. if($('link[id="theme-style"]').attr('href') != 'data/themes/'+v+'/style.css'){
  1107. event('Loading theme '+v);
  1108. runHook('theme',[v]);
  1109. $('link[id="theme-style"]').attr({
  1110. id: 'theme-style',
  1111. rel: 'stylesheet',
  1112. href: 'data/themes/'+v+'/style.css'
  1113. });
  1114. var i,h;
  1115. for(i in hooks){
  1116. h = hooks[i];
  1117. if(h.type == 'style'){
  1118. hooks.splice(i,1);
  1119. }
  1120. }
  1121. $.ajax('data/themes/'+v+'/script.js',{
  1122. dataType: 'text',
  1123. success: function(data){
  1124. var sandbox = {
  1125. load: function(fn){
  1126. fn();
  1127. },
  1128. unload: function(fn){
  1129. $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');
  1130. },
  1131. $: window.jQuery,
  1132. $o: window.OmnomIRC,
  1133. OmnomIRC: window.OmnomIRC,
  1134. };
  1135. runInSandbox(data,sandbox);
  1136. runHook('themechange',[v]);
  1137. }
  1138. });
  1139. }
  1140. }
  1141. },
  1142. nick: {
  1143. type: 'string',
  1144. val: 'User',
  1145. callback: function(){
  1146. $o.chat.auth();
  1147. }
  1148. }
  1149. });
  1150. $.extend(pluginSandbox,$o);
  1151. String.prototype.htmlentities = function(){
  1152. return this
  1153. .replace(/&/g, '&amp;')
  1154. .replace(/</g, '&lt;')
  1155. .replace(/>/g, '&gt;')
  1156. .replace(/\n/g,'<br/>')
  1157. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  1158. .replace(/\s/g, '&nbsp;')
  1159. .replace(/"/g, '&quot;');
  1160. };
  1161. $(document).ready(function(){
  1162. $.extend(settings,$.parseJSON($.localStorage('settings')));
  1163. $.localStorage('settings',JSON.stringify(settings));
  1164. settingsConf['theme'].callback(settings['theme'],'theme',true);
  1165. $i = $('#input');
  1166. $s = $('#send');
  1167. $cl = $('#content-list');
  1168. $c = $('#content');
  1169. $tl = $('#tabs-list');
  1170. $h = $('#head');
  1171. $s.click(function(){
  1172. if(!$s.hasClass('clicked')){
  1173. $s.addClass('clicked');
  1174. setTimeout(function(){
  1175. $s.removeClass('clicked');
  1176. },500);
  1177. }
  1178. $o.send($i.val());
  1179. $i.val('');
  1180. });
  1181. $i.keypress(function(e){
  1182. if(e.keyCode == 13){
  1183. if(!$s.hasClass('clicked')){
  1184. $s.addClass('clicked');
  1185. setTimeout(function(){
  1186. $s.removeClass('clicked');
  1187. },500);
  1188. }
  1189. $o.send($i.val());
  1190. $i.val('');
  1191. }
  1192. });
  1193. $('#settings, #users').click(function(){
  1194. $(this).addClass('open');
  1195. $(this).children('.close-button').show();
  1196. }).hover(function(){
  1197. $(this).addClass('hovered');
  1198. },function(){
  1199. $(this).removeClass('hovered');
  1200. }).children('.close-button').click(function(){
  1201. $(this).parent().removeClass('open');
  1202. $(this).hide();
  1203. return false;
  1204. }).hide();
  1205. $('#users').hoverIntent({
  1206. out: function(){
  1207. $(this).removeClass('open');
  1208. $(this).children('.close-button').hide();
  1209. },
  1210. timeout: 1000
  1211. });
  1212. $('#content').click(function(){
  1213. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  1214. $('#settings, #users').children('.close-button').hide()
  1215. });
  1216. $('.unselectable').attr('unselectable','on');
  1217. $.contextMenu({
  1218. selector: 'div.tab',
  1219. items: {
  1220. add: {
  1221. name: 'New Tab',
  1222. icon: 'add',
  1223. callback: function(){
  1224. $(this).contextMenu('hide');
  1225. $o.ui.tabs.add(prompt('Channel'));
  1226. }
  1227. },
  1228. s1: '',
  1229. close: {
  1230. name: 'Close',
  1231. icon: 'delete',
  1232. callback: function(){
  1233. $(this).contextMenu('hide');
  1234. $o.ui.tabs.remove($(this).data('id'));
  1235. }
  1236. }
  1237. },
  1238. zIndex: 99999,
  1239. trigger: 'right'
  1240. });
  1241. $.contextMenu({
  1242. selector: '#tabs-list',
  1243. items: {
  1244. add: {
  1245. name: 'New Tab',
  1246. icon: 'add',
  1247. callback: function(){
  1248. $(this).contextMenu('hide');
  1249. $o.ui.tabs.add(prompt('channel'));
  1250. }
  1251. }
  1252. },
  1253. zIndex: 99999,
  1254. trigger: 'right'
  1255. });
  1256. $('#tabs-scroll-right').click(function(){
  1257. event('scroll right');
  1258. $tl.scrollTop(($tl.scrollTop()||0)+20);
  1259. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  1260. $('#tabs-scroll-right').addClass('disabled');
  1261. }
  1262. $('#tabs-scroll-left').removeClass('disabled');
  1263. });
  1264. $('#tabs-scroll-left').click(function(){
  1265. event('scroll left');
  1266. $tl.scrollTop(($tl.scrollTop()||0)-20);
  1267. if($tl.scrollTop() == 0){
  1268. $('#tabs-scroll-left').addClass('disabled');
  1269. }
  1270. $('#tabs-scroll-right').removeClass('disabled');
  1271. });
  1272. (function scrollup(){
  1273. $('#tabs-scroll-left').click();
  1274. if($tl.scrollTop() != 0){
  1275. setTimeout(scrollup,10);
  1276. }
  1277. })();
  1278. event('Date '+new Date,'ready');
  1279. $h.addClass('hovered');
  1280. setTimeout(function(){
  1281. $h.removeClass('hovered');
  1282. },1000);
  1283. $o.ui.render.settings();
  1284. if(settings.autoconnect){
  1285. $o.chat.connect();
  1286. }
  1287. // Check for script updates and update if required
  1288. (function checkScripts(){
  1289. for(var i in document.scripts){
  1290. (function(el,src){
  1291. if(exists(src) && el.innerHTML == ''){
  1292. $.ajax(src,{
  1293. success: function(source){
  1294. if(exists($(el).data('source')) && $(el).data('source') != source){
  1295. event('Reloading','update');
  1296. location.reload();
  1297. }
  1298. $(el).data('source',source);
  1299. },
  1300. dataType: 'text'
  1301. });
  1302. }
  1303. })(document.scripts[i],document.scripts[i].src);
  1304. }
  1305. var s = $('link');
  1306. for(i in s){
  1307. (function(el,href){
  1308. if(exists(href) && el.innerHTML == ''){
  1309. $.ajax(href,{
  1310. success: function(source){
  1311. if(exists($(el).data('source')) && $(el).data('source') != source){
  1312. event('Updating CSS','update');
  1313. if(!exists($(el).data('href'))){
  1314. $(el).data('href',href);
  1315. }
  1316. if($(el).data('href') != href){
  1317. href = $(el).data('href');
  1318. }
  1319. el.href += '?reload='+new Date().getTime();
  1320. }
  1321. $(el).data('source',source);
  1322. },
  1323. dataType: 'text'
  1324. });
  1325. }
  1326. })(s[i],s[i].href);
  1327. }
  1328. setTimeout(checkScripts,1000*30);
  1329. })();
  1330. });
  1331. window.io = null;
  1332. runHook('load');
  1333. })(window,jQuery,io);
  1334. if (!Date.prototype.toISOString) {
  1335. Date.prototype.toISOString = function() {
  1336. function pad(n) { return n < 10 ? '0' + n : n }
  1337. return this.getUTCFullYear() + '-'
  1338. + pad(this.getUTCMonth() + 1) + '-'
  1339. + pad(this.getUTCDate()) + 'T'
  1340. + pad(this.getUTCHours()) + ':'
  1341. + pad(this.getUTCMinutes()) + ':'
  1342. + pad(this.getUTCSeconds()) + 'Z';
  1343. };
  1344. }
  1345. if(!String.prototype.isNumber){
  1346. String.prototype.isNumber = function(){return /^\d+$/.test(this);}
  1347. }