omnomirc.js 34 KB

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