omnomirc.js 34 KB

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