omnomirc.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. }
  1137. });
  1138. }
  1139. }
  1140. },
  1141. nick: {
  1142. type: 'string',
  1143. val: 'User',
  1144. callback: function(){
  1145. $o.chat.auth();
  1146. }
  1147. }
  1148. });
  1149. $.extend(pluginSandbox,$o);
  1150. String.prototype.htmlentities = function(){
  1151. return this
  1152. .replace(/&/g, '&amp;')
  1153. .replace(/</g, '&lt;')
  1154. .replace(/>/g, '&gt;')
  1155. .replace(/\n/g,'<br/>')
  1156. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  1157. .replace(/\s/g, '&nbsp;')
  1158. .replace(/"/g, '&quot;');
  1159. };
  1160. $(document).ready(function(){
  1161. $.extend(settings,$.parseJSON($.localStorage('settings')));
  1162. $.localStorage('settings',JSON.stringify(settings));
  1163. settingsConf['theme'].callback(settings['theme'],'theme',true);
  1164. $i = $('#input');
  1165. $s = $('#send');
  1166. $cl = $('#content-list');
  1167. $c = $('#content');
  1168. $tl = $('#tabs-list');
  1169. $h = $('#head');
  1170. $s.click(function(){
  1171. if(!$s.hasClass('clicked')){
  1172. $s.addClass('clicked');
  1173. setTimeout(function(){
  1174. $s.removeClass('clicked');
  1175. },500);
  1176. }
  1177. $o.send($i.val());
  1178. $i.val('');
  1179. });
  1180. $i.keypress(function(e){
  1181. if(e.keyCode == 13){
  1182. if(!$s.hasClass('clicked')){
  1183. $s.addClass('clicked');
  1184. setTimeout(function(){
  1185. $s.removeClass('clicked');
  1186. },500);
  1187. }
  1188. $o.send($i.val());
  1189. $i.val('');
  1190. }
  1191. });
  1192. $('#settings, #users').click(function(){
  1193. $(this).addClass('open');
  1194. $(this).children('.close-button').show();
  1195. }).hover(function(){
  1196. $(this).addClass('hovered');
  1197. },function(){
  1198. $(this).removeClass('hovered');
  1199. }).children('.close-button').click(function(){
  1200. $(this).parent().removeClass('open');
  1201. $(this).hide();
  1202. return false;
  1203. }).hide();
  1204. $('#users').hoverIntent({
  1205. out: function(){
  1206. $(this).removeClass('open');
  1207. $(this).children('.close-button').hide();
  1208. },
  1209. timeout: 1000
  1210. });
  1211. $('#content').click(function(){
  1212. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  1213. $('#settings, #users').children('.close-button').hide()
  1214. });
  1215. $('.unselectable').attr('unselectable','on');
  1216. $.contextMenu({
  1217. selector: 'div.tab',
  1218. items: {
  1219. add: {
  1220. name: 'New Tab',
  1221. icon: 'add',
  1222. callback: function(){
  1223. $(this).contextMenu('hide');
  1224. $o.ui.tabs.add(prompt('Channel'));
  1225. }
  1226. },
  1227. s1: '',
  1228. close: {
  1229. name: 'Close',
  1230. icon: 'delete',
  1231. callback: function(){
  1232. $(this).contextMenu('hide');
  1233. $o.ui.tabs.remove($(this).data('id'));
  1234. }
  1235. }
  1236. },
  1237. zIndex: 99999,
  1238. trigger: 'right'
  1239. });
  1240. $.contextMenu({
  1241. selector: '#tabs-list',
  1242. items: {
  1243. add: {
  1244. name: 'New Tab',
  1245. icon: 'add',
  1246. callback: function(){
  1247. $(this).contextMenu('hide');
  1248. $o.ui.tabs.add(prompt('channel'));
  1249. }
  1250. }
  1251. },
  1252. zIndex: 99999,
  1253. trigger: 'right'
  1254. });
  1255. $('#tabs-scroll-right').click(function(){
  1256. event('scroll right');
  1257. $tl.scrollTop(($tl.scrollTop()||0)+20);
  1258. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  1259. $('#tabs-scroll-right').addClass('disabled');
  1260. }
  1261. $('#tabs-scroll-left').removeClass('disabled');
  1262. });
  1263. $('#tabs-scroll-left').click(function(){
  1264. event('scroll left');
  1265. $tl.scrollTop(($tl.scrollTop()||0)-20);
  1266. if($tl.scrollTop() == 0){
  1267. $('#tabs-scroll-left').addClass('disabled');
  1268. }
  1269. $('#tabs-scroll-right').removeClass('disabled');
  1270. });
  1271. (function scrollup(){
  1272. $('#tabs-scroll-left').click();
  1273. if($tl.scrollTop() != 0){
  1274. setTimeout(scrollup,10);
  1275. }
  1276. })();
  1277. event('Date '+new Date,'ready');
  1278. $h.addClass('hovered');
  1279. setTimeout(function(){
  1280. $h.removeClass('hovered');
  1281. },1000);
  1282. $o.ui.render.settings();
  1283. if(settings.autoconnect){
  1284. $o.chat.connect();
  1285. }
  1286. // Check for script updates and update if required
  1287. (function checkScripts(){
  1288. for(var i in document.scripts){
  1289. (function(el,src){
  1290. if(exists(src) && el.innerHTML == ''){
  1291. $.ajax(src,{
  1292. success: function(source){
  1293. if(exists($(el).data('source')) && $(el).data('source') != source){
  1294. event('Reloading','update');
  1295. location.reload();
  1296. }
  1297. $(el).data('source',source);
  1298. },
  1299. dataType: 'text'
  1300. });
  1301. }
  1302. })(document.scripts[i],document.scripts[i].src);
  1303. }
  1304. var s = $('link');
  1305. for(i in s){
  1306. (function(el,href){
  1307. if(exists(href) && el.innerHTML == ''){
  1308. $.ajax(href,{
  1309. success: function(source){
  1310. if(exists($(el).data('source')) && $(el).data('source') != source){
  1311. event('Updating CSS','update');
  1312. if(!exists($(el).data('href'))){
  1313. $(el).data('href',href);
  1314. }
  1315. if($(el).data('href') != href){
  1316. href = $(el).data('href');
  1317. }
  1318. el.href += '?reload='+new Date().getTime();
  1319. }
  1320. $(el).data('source',source);
  1321. },
  1322. dataType: 'text'
  1323. });
  1324. }
  1325. })(s[i],s[i].href);
  1326. }
  1327. setTimeout(checkScripts,1000*30);
  1328. })();
  1329. });
  1330. window.io = null;
  1331. runHook('load');
  1332. })(window,jQuery,io);
  1333. if (!Date.prototype.toISOString) {
  1334. Date.prototype.toISOString = function() {
  1335. function pad(n) { return n < 10 ? '0' + n : n }
  1336. return this.getUTCFullYear() + '-'
  1337. + pad(this.getUTCMonth() + 1) + '-'
  1338. + pad(this.getUTCDate()) + 'T'
  1339. + pad(this.getUTCHours()) + ':'
  1340. + pad(this.getUTCMinutes()) + ':'
  1341. + pad(this.getUTCSeconds()) + 'Z';
  1342. };
  1343. }
  1344. if(!String.prototype.isNumber){
  1345. String.prototype.isNumber = function(){return /^\d+$/.test(this);}
  1346. }