omnomirc.js 33 KB

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