omnomirc.js 30 KB

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