omnomirc.js 28 KB

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