omnomirc.js 14 KB

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