omnomirc.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. var $o = window.OmnomIRC = window.$o = function(){
  18. return 'Version: '+$o.version
  19. },
  20. event = function(msg,type){
  21. type=typeof type == 'undefined'?'event':type;
  22. switch(type){
  23. case 'ready':type='document_ready';break;
  24. }
  25. log('['+type.toUpperCase()+'] '+msg);
  26. },
  27. log = function(){
  28. console.log.apply(console,arguments);
  29. },
  30. exists = function(object){
  31. return typeof object != 'undefined';
  32. },
  33. prevent = function(e){
  34. e.stopImmediatePropagation();
  35. e.stopPropagation();
  36. e.preventDefault();
  37. return false;
  38. },
  39. selectedTab=0,
  40. settings = {
  41. colour: false,
  42. timestamp: '',
  43. server: location.origin,
  44. autojoin: [
  45. '#omnimaga',
  46. '#omnimaga-fr',
  47. '#irp'
  48. ],
  49. scrollspeed: 100
  50. },
  51. tabs = [],
  52. properties = {
  53. nick: 'User',
  54. sig: '',
  55. tabs: tabs
  56. },
  57. commands = [
  58. {
  59. cmd: 'names',
  60. fn: function(args){
  61. socket.emit('names',{
  62. name: tabs[selectedTab].name
  63. });
  64. }
  65. },
  66. {
  67. cmd: 'me',
  68. fn: function(args){
  69. var i,ret='';
  70. for(i=1;i<args.length;i++){
  71. ret += args[i];
  72. }
  73. socket.emit('message',{
  74. from: 0,
  75. message: properties.nick+' '+ret,
  76. room: tabs[selectedTab].name
  77. });
  78. }
  79. },
  80. {
  81. cmd: 'nick',
  82. fn: function(args){
  83. properties.nick = args[1];
  84. $o.auth();
  85. }
  86. },
  87. {
  88. cmd: 'help',
  89. fn: function(args){
  90. var m = 'Commands:',i;
  91. for(i in commands){
  92. m += ' '+commands[i].cmd;
  93. }
  94. $o.msg(m);
  95. }
  96. },
  97. {
  98. cmd: 'open',
  99. fn: function(args){
  100. tabs.push({
  101. name: args[1],
  102. title: args[2],
  103. topic: 'Topic for '+args[2]
  104. });
  105. $o.refreshTabs();
  106. }
  107. },
  108. {
  109. cmd: 'clear',
  110. fn: function(args){
  111. $cl.html('');
  112. tabs[selectedTab].body = document.createDocumentFragment();
  113. }
  114. },
  115. {
  116. cmd: 'close',
  117. fn: function(args){
  118. if(args.length > 1){
  119. $o.removeTab(args[1]);
  120. }else{
  121. $o.removeTab(selectedTab);
  122. }
  123. }
  124. },
  125. {
  126. cmd: 'tabs',
  127. fn: function(args){
  128. $o.msg('tabs:');
  129. for(var i in tabs){
  130. $o.msg(' ['+i+'] '+tabs[i].name);
  131. }
  132. }
  133. }
  134. ],
  135. handles = [
  136. {
  137. on: 'names',
  138. fn: function(data){
  139. tabs[$o.tabIdForName(data.room)].names = data.names;
  140. if($o.tabIdForName(data.room) == selectedTab){
  141. $o.renderUsers();
  142. }
  143. }
  144. },
  145. {
  146. on: 'authorized',
  147. fn: function(data){
  148. properties.nick = data.nick;
  149. for(var i in settings.autojoin){
  150. socket.emit('join',{
  151. name: settings.autojoin[i]
  152. });
  153. }
  154. }
  155. },
  156. {
  157. on: 'join',
  158. fn: function(data){
  159. event('joined '+data.name);
  160. var flag = tabs.length == 0;
  161. $o.addTab(data.name,data.title);
  162. if(flag){
  163. $o.selectTab(0);
  164. }
  165. }
  166. },
  167. {
  168. on: 'reconnect',
  169. fn: function(data){
  170. event('reconnected');
  171. $o.auth();
  172. }
  173. },
  174. {
  175. on: 'message',
  176. fn: function(data){
  177. event('recieved message');
  178. var date = new Date(),
  179. string,
  180. time = date.getTime(),
  181. child,
  182. i,
  183. msg = function(msg){
  184. if(settings.timestamp == ''){
  185. string = '<span class="cell"></span>';
  186. }else{
  187. string = '<span class="cell">[<abbr class="date date_'+time+'" title="'+date.toISOString()+'"></abbr>]</span>';
  188. }
  189. child = $('<li>').html(string+'<span class="cell">'+msg.htmlentities().replace(
  190. /(https?:\/\/(([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?))/g,
  191. "<a href=\"$1\" title=\"\">$1</a>"
  192. )+'</span>');
  193. $o.msg({html:child},data.room);
  194. };
  195. if(data.from != 0){
  196. msg(' <'+data.from+'> '+data.message);
  197. }else{
  198. msg(' * '+data.message);
  199. }
  200. abbrDate('abbr.date_'+time);
  201. }
  202. }
  203. ],
  204. hooks = [
  205. {
  206. type: 'style',
  207. hook: 'load',
  208. fn: function(){
  209. }
  210. }
  211. ],
  212. abbrDate = function(selector){
  213. if(settings.timestamp == 'fuzzy'){
  214. $(selector).timeago();
  215. }else{
  216. $(selector).each(function(){
  217. var timestamp = settings.timestamp,
  218. i,
  219. text='',
  220. date = new Date($(this).attr('title'));
  221. if(timestamp == 'exact'){
  222. timestamp = 'H:m:s t';
  223. }
  224. for(i=0;i<timestamp.length;i++){
  225. switch(timestamp[i]){
  226. case 'H':text+=((date.getHours()+11)%12)+1;break;
  227. case 'h':text+=date.getHours();break;
  228. case 'm':text+=(date.getMinutes()>9?'':'0')+date.getMinutes();break;
  229. case 's':text+=(date.getSeconds()>9?'':'0')+date.getSeconds();break;
  230. case 't':text+=(date.getHours()>11)?'pm':'am';break;
  231. default:text+=timestamp[i];
  232. }
  233. }
  234. $(this).text(text);
  235. });
  236. }
  237. },
  238. socket,$i,$s,$h,$cl,$tl,hht;
  239. $.extend($o,{
  240. version: '3.0',
  241. connect: function(server){
  242. if($o.connected()){
  243. socket.disconnect();
  244. socket = undefined;
  245. }
  246. if(typeof server == 'undefined'){
  247. server = settings.server;
  248. }
  249. socket = io.connect(server);
  250. for(var i in handles){
  251. socket.on(handles[i].on,handles[i].fn);
  252. }
  253. $o.auth();
  254. },
  255. auth: function(){
  256. socket.emit('auth',{
  257. nick: properties.nick
  258. // TODO - send authorization info
  259. });
  260. },
  261. connected: function(){
  262. return typeof socket != 'undefined';
  263. },
  264. get: function(name,formatted){
  265. if(typeof formatted == 'undefined'){
  266. return exists(settings[name])?settings[name]:false;
  267. }else{
  268. var val = $o.get(name),
  269. type,
  270. values = false;
  271. switch(name){
  272. case 'autojoin':type = 'array';break;
  273. case 'timestamp':type = 'string';break;
  274. default:
  275. type = typeof val;
  276. }
  277. return {
  278. type: type,
  279. val: val,
  280. values: values,
  281. name: name
  282. };
  283. }
  284. },
  285. set: function(name,value,render){
  286. if(exists(settings[name])){
  287. settings[name] = value;
  288. $.localStorage('settings',JSON.stringify(settings));
  289. switch(name){
  290. case 'timestamp':abbrDate('abbr.date');break;
  291. }
  292. if(typeof render == 'undefined'){
  293. $o.renderSettings();
  294. }
  295. return true;
  296. }else{
  297. return false;
  298. }
  299. },
  300. renderSettings: function(){
  301. var name,setting,frag = document.createDocumentFragment(),item;
  302. for(name in settings){
  303. setting = $o.get(name,true);
  304. switch(setting.type){
  305. case 'array':
  306. item = $('<input>')
  307. .attr({
  308. type: 'text',
  309. id: 'setting_'+name
  310. })
  311. .val(setting.val)
  312. .change(function(){
  313. $o.set(this.id.substr(8),$(this).val().split(','),false);
  314. });
  315. break;
  316. case 'boolean':
  317. item = $('<input>')
  318. .attr({
  319. type: 'checkbox',
  320. id: 'setting_'+name
  321. })
  322. .change(function(){
  323. $o.set(this.id.substr(8),$(this).is(':checked'),false);
  324. });
  325. if(setting.val){
  326. item.attr('checked','checked');
  327. }
  328. break;
  329. case 'number':
  330. case 'string':default:
  331. item = $('<input>')
  332. .attr({
  333. type: 'text',
  334. id: 'setting_'+name
  335. })
  336. .val(setting.val)
  337. .change(function(){
  338. $o.set(this.id.substr(8),$(this).val(),false);
  339. });
  340. }
  341. $(frag).append(
  342. $('<li>')
  343. .addClass('row')
  344. .append(
  345. $('<span>')
  346. .text(name)
  347. .addClass('cell')
  348. )
  349. .append(
  350. $('<span>')
  351. .append(item)
  352. .addClass('cell')
  353. )
  354. );
  355. }
  356. $('#settings-list').html(frag);
  357. },
  358. renderUsers: function(){
  359. event('Rendering userlist');
  360. var $ul = $('#user-list').html(''),
  361. i,
  362. names = tabs[selectedTab].names;
  363. for(i in names){
  364. $ul.append(
  365. $('<li>').text(names[i])
  366. );
  367. }
  368. },
  369. selectedTab: function(){
  370. return selectedTab;
  371. },
  372. prop: function(name){
  373. return exists(properties[name])?properties[name]:null;
  374. },
  375. send: function(msg){
  376. if(msg !== ''){
  377. if(msg[0] == '/' && msg[1] != '/'){
  378. var args = msg.split(' '),
  379. cmd = args[0].substr(1),
  380. i;
  381. event(msg,'command');
  382. for(i in commands){
  383. if(commands[i].cmd == cmd){
  384. commands[i].fn(args);
  385. return;
  386. }
  387. }
  388. $o.msg(cmd+' is not a valid command.');
  389. }else{
  390. event(msg,'send');
  391. socket.emit('message',{
  392. message: msg,
  393. room: tabs[selectedTab].name,
  394. from: properties.nick
  395. });
  396. }
  397. }
  398. },
  399. msg: function(msg,tabName){
  400. var frag;
  401. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  402. frag = document.createDocumentFragment();
  403. }else{
  404. frag = tabs[$o.tabIdForName(tabName)].body;
  405. }
  406. switch(typeof msg){
  407. case 'string':
  408. $(frag).append($('<li>').html(msg.htmlentities()));
  409. break;
  410. case 'object':
  411. if(typeof msg.html == 'undefined'){
  412. $(frag).append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  413. }else{
  414. $(frag).append(msg.html);
  415. }
  416. break;
  417. }
  418. $(tabs[$o.tabIdForName(tabName) || selectedTab].body).append(frag);
  419. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  420. $o.selectTab(selectedTab);
  421. }
  422. },
  423. event: function(event_name,message){
  424. event(message,event_name);
  425. },
  426. selectTab: function(id){
  427. event(id+' '+tabs[id].name,'tab_select');
  428. if(id<tabs.length&&id>=0){
  429. selectedTab=id;
  430. }
  431. $tl.children('.clicked').removeClass('clicked');
  432. $($tl.children().get(id)).addClass('clicked');
  433. $('#title').text(tabs[id].title);
  434. $('#topic').text(tabs[id].topic);
  435. $cl.html($(tabs[id].body).clone());
  436. abbrDate('abbr.date');
  437. $o.renderUsers();
  438. setTimeout(function scrollContent(){
  439. if($cl.scrollTop() < $cl[0].scrollHeight){
  440. $cl.scrollTop($cl.scrollTop()+1);
  441. setTimeout(scrollContent,settings.scrollspeed);
  442. }
  443. },settings.scrollspeed);
  444. },
  445. tabIdForName: function(name){
  446. for(var i in tabs){
  447. if(tabs[i].name == name){
  448. return i;
  449. }
  450. }
  451. return false;
  452. },
  453. tabDOM: function(id){
  454. return tabs[id].body;
  455. },
  456. addTab: function(name,title){
  457. event('Tab added: '+name+' with title '+title);
  458. if(!(function(){
  459. for(var i in tabs){
  460. if(name==tabs[i].name){
  461. return true;
  462. }
  463. }
  464. return false;
  465. })()){
  466. tabs.push({
  467. name: name,
  468. title: title,
  469. body: document.createDocumentFragment(),
  470. names: []
  471. });
  472. $tl.append($o.tabObj(tabs.length-1));
  473. $o.refreshTabs();
  474. $o.renderUsers();
  475. }else{
  476. event('Attempted to add an existing tab');
  477. }
  478. },
  479. removeTab: function(id){
  480. if(typeof tabs[id] != 'undefined'){
  481. event('Tab removed: '+tabs[id].name);
  482. socket.emit('part',{
  483. name: tabs[id].name
  484. });
  485. tabs.splice(id,1);
  486. if(selectedTab==id&&selectedTab>0){
  487. selectedTab--;
  488. }
  489. }
  490. $o.refreshTabs();
  491. $cl.html(tabs[selectedTab].body);
  492. $o.renderUsers();
  493. },
  494. tabObj: function(id){
  495. if(typeof id !== 'undefined'){
  496. return $('<div>')
  497. .addClass('tab')
  498. .text(tabs[id].title)
  499. .mouseup(function(e){
  500. switch(e.which){
  501. case 1: // RMB
  502. if($(this).data('id')!=selectedTab){
  503. $o.selectTab($(this).data('id'));
  504. return prevent(e);
  505. }
  506. break;
  507. case 2: // MMB
  508. $(this).children('span.close-button').click();
  509. return prevent(e);
  510. break;
  511. case 3: // LMB
  512. return prevent(e);
  513. break;
  514. default:
  515. return prevent(e);
  516. }
  517. })
  518. .append(
  519. $('<span>')
  520. .addClass('close-button')
  521. .click(function(){
  522. $o.removeTab(id);
  523. return false;
  524. })
  525. .css({
  526. 'position': 'absolute',
  527. 'background-color': 'inherit',
  528. 'top': 0,
  529. 'right': 0
  530. })
  531. .html('&times;')
  532. )
  533. .data('id',id);
  534. }
  535. },
  536. refreshTabs: function(){
  537. $tl.html('');
  538. var i,tab;
  539. for(i in tabs){
  540. tab = $o.tabObj(i);
  541. if(i==selectedTab){
  542. tab.addClass('clicked');
  543. $('#title').text(tabs[i].title);
  544. $('#topic').text(tabs[i].topic);
  545. }
  546. $tl.append(tab);
  547. }
  548. if($tl.get(0).scrollHeight-20 != $tl.scrollTop()){
  549. $('#tabs-scroll-right').removeClass('disabled');
  550. }
  551. if($tl.scrollTop() != 0){
  552. $('#tabs-scroll-left').removeClass('disabled');
  553. }
  554. }
  555. });
  556. String.prototype.htmlentities = function(){
  557. return this.replace(/&/g, '&amp;').replace(/\s/g, '&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  558. };
  559. $(document).ready(function(){
  560. $.extend(settings,$.parseJSON($.localStorage('settings')));
  561. $.localStorage('settings',JSON.stringify(settings));
  562. $i = $('#input');
  563. $s = $('#send');
  564. $cl = $('#content-list');
  565. $tl = $('#tabs-list');
  566. $h = $('#head');
  567. $s.click(function(){
  568. if(!$s.hasClass('clicked')){
  569. $s.addClass('clicked');
  570. setTimeout(function(){
  571. $s.removeClass('clicked');
  572. },500);
  573. }
  574. $o.send($i.val());
  575. $i.val('');
  576. });
  577. $i.keypress(function(e){
  578. if(e.keyCode == 13){
  579. if(!$s.hasClass('clicked')){
  580. $s.addClass('clicked');
  581. setTimeout(function(){
  582. $s.removeClass('clicked');
  583. },500);
  584. }
  585. $o.send($i.val());
  586. $i.val('');
  587. }
  588. });
  589. $('#settings, #users').click(function(){
  590. $(this).addClass('open');
  591. $(this).children('.close-button').show();
  592. }).hover(function(){
  593. $(this).addClass('hovered');
  594. },function(){
  595. $(this).removeClass('hovered');
  596. }).children('.close-button').click(function(){
  597. $(this).parent().removeClass('open');
  598. $(this).hide();
  599. return false;
  600. }).hide();
  601. $('#users').hoverIntent({
  602. out: function(){
  603. $(this).removeClass('open');
  604. $(this).children('.close-button').hide();
  605. },
  606. timeout: 1000
  607. });
  608. $('#content').click(function(){
  609. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  610. $('#settings, #users').children('.close-button').hide()
  611. });
  612. $('.unselectable').attr('unselectable','on');
  613. $.contextMenu({
  614. selector: 'div.tab',
  615. items: {
  616. add: {
  617. name: 'New Tab',
  618. icon: 'add',
  619. callback: function(){
  620. $(this).contextMenu('hide');
  621. var title = prompt('Title');
  622. tabs.push({
  623. name: prompt('channel'),
  624. title: title,
  625. topic: 'Topic for '+title
  626. });
  627. $o.refreshTabs();
  628. }
  629. },
  630. s1: '',
  631. close: {
  632. name: 'Close',
  633. icon: 'delete',
  634. callback: function(){
  635. $(this).contextMenu('hide');
  636. $o.removeTab($(this).data('id'));
  637. }
  638. }
  639. },
  640. zIndex: 99999,
  641. trigger: 'right'
  642. });
  643. $.contextMenu({
  644. selector: '#tabs-list',
  645. items: {
  646. add: {
  647. name: 'New Tab',
  648. icon: 'add',
  649. callback: function(){
  650. $(this).contextMenu('hide');
  651. var title = prompt('Title');
  652. tabs.push({
  653. name: prompt('channel'),
  654. title: title,
  655. topic: 'Topic for '+title
  656. });
  657. $o.refreshTabs();
  658. }
  659. }
  660. },
  661. zIndex: 99999,
  662. trigger: 'right'
  663. });
  664. $('#tabs-scroll-right').click(function(){
  665. event('scroll right');
  666. $tl.scrollTop(($tl.scrollTop()||0)+20);
  667. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  668. $('#tabs-scroll-right').addClass('disabled');
  669. }
  670. $('#tabs-scroll-left').removeClass('disabled');
  671. });
  672. $('#tabs-scroll-left').click(function(){
  673. event('scroll left');
  674. $tl.scrollTop(($tl.scrollTop()||0)-20);
  675. if($tl.scrollTop() == 0){
  676. $('#tabs-scroll-left').addClass('disabled');
  677. }
  678. $('#tabs-scroll-right').removeClass('disabled');
  679. });
  680. (function scrollup(){
  681. $('#tabs-scroll-left').click();
  682. if($tl.scrollTop() != 0){
  683. setTimeout(scrollup,10);
  684. }
  685. })();
  686. //DEBUG
  687. /* for(var i=0;i<20;i++){
  688. tabs.push({
  689. name: '#Tab'+i,
  690. title: 'Tab '+i,
  691. topic: 'Topic for tab '+i
  692. });
  693. } */
  694. //END DEBUG
  695. event('Date '+new Date,'ready');
  696. $h.addClass('hovered');
  697. setTimeout(function(){
  698. $h.removeClass('hovered');
  699. },1000);
  700. $o.renderSettings();
  701. $o.connect();
  702. });
  703. delete window.io;
  704. })(window,jQuery,io);
  705. if (!Date.prototype.toISOString) {
  706. Date.prototype.toISOString = function() {
  707. function pad(n) { return n < 10 ? '0' + n : n }
  708. return this.getUTCFullYear() + '-'
  709. + pad(this.getUTCMonth() + 1) + '-'
  710. + pad(this.getUTCDate()) + 'T'
  711. + pad(this.getUTCHours()) + ':'
  712. + pad(this.getUTCMinutes()) + ':'
  713. + pad(this.getUTCSeconds()) + 'Z';
  714. };
  715. }