/* OmnomIRC COPYRIGHT 2010,2011 Netham45 OmnomIRC3 rewrite COPYRIGHT 2013 Nathaniel 'Eeems' van Diepen This file is part of OmnomIRC. OmnomIRC is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OmnomIRC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OmnomIRC. If not, see . */ (function(window,$,io,undefined){ "use strict"; var $o = window.OmnomIRC = window.$o = function(){ return 'Version: '+version; }, event = function(msg,type){ if(settings.debug){ type=exists(type)?type:'event'; switch(type){ case 'ready':type='document_ready';break; } log('['+type.toUpperCase()+'] '+msg); } }, emit = window.emit = function(type,data){ if($o.chat.connected()){ socket.emit.apply(socket,arguments); }else{ if(tabs.length > 0){ $o.msg('Disconnected, cannot do anything'); } } }, noop = function(){}, log = function(){ console.log.apply(console,arguments); }, exists = function(object){ return typeof object != 'undefined'; }, prevent = function(e){ e.stopImmediatePropagation(); e.stopPropagation(); e.preventDefault(); return false; }, selectedTab=0, settings = {}, settingsConf = {}, tabs = [], properties = { nick: 'User', sig: '', tabs: tabs, themes: [] }, commands = [ { // names cmd: 'names', fn: function(args){ emit('names',{ name: $o.ui.tabs.current().name }); } }, { // me cmd: 'me', help: 'Say something in third person', fn: function(args){ var i,ret=''; for(i=1;i 1){ $o.ui.tabs.remove(args[1]); }else{ $o.ui.tabs.remove(selectedTab); } } }, { // tabs cmd: 'tabs', fn: function(args){ $o.msg('tabs:'); for(var i in tabs){ $o.msg(' ['+i+'] '+tabs[i].name); } } } ], handles = [ { // names on: 'names', fn: function(data){ var tab = $o.ui.tabs.tab(data.room), users = tab.users, i; tab.users = data.names; if($o.ui.tabs.idForName(data.room) == selectedTab){ $o.ui.render.users(); } $(users).each(function(i,v){ if(v != null){ if(tab.users.indexOf(v.trim()) == -1){ emit('echo',{ room: data.room, message: v+' left the room', from: 0 }); runHook('part',[ v, data.room ]); } } }); $(tab.users).each(function(i,v){ if(v != null){ if(users.indexOf(v.trim()) == -1){ runHook('join',[ v, data.room ]); } } }); } }, { // authorized on: 'authorized', fn: function(data){ properties.nick = data.nick; for(var i in settings.autojoin){ emit('join',{ name: settings.autojoin[i] }); } runHook('authorized'); } }, { // join on: 'join', fn: function(data){ event('joined '+data.name); var flag = tabs.length == 0; $o.ui.tabs.add(data.name); if(flag){ $o.ui.tabs.select(0); } } }, { // reconnect on: 'reconnect', fn: function(data){ event('reconnected'); properties.connected = true; $o.chat.auth(); runHook('reconnect'); emit('echo',{ room: $o.ui.tabs.current().name, from: 0, message: 'reconnected' }); } }, { // connect on: 'connect', fn: function(data){ event('connected'); properties.connected = true; $o.chat.auth(); runHook('connect'); emit('echo',{ room: $o.ui.tabs.current().name, from: 0, message: 'connected' }); } }, { // disconnect on: 'disconnect', fn: function(data){ event('disconnected'); properties.connected = false; runHook('disconnected'); $o.msg('* disconnected'); } }, { // message on: 'message', fn: function(data){ event('recieved message'); var date = new Date(), string, time = date.getTime(), child, i, msg = function(msg){ string = '[]'; child = $('
  • ').html(string+''+ msg .htmlentities() .replace( /(https?:\/\/(([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?))/g, "$1" ) +''); $o.msg({html:child},data.room); }; if(data.from != 0){ msg(' <'+data.from+'> '+data.message); }else{ msg(' * '+data.message); } abbrDate('abbr.date_'+time); if(settings.timestamp == ''){ $('.date_cell').css('visibility','hidden'); }else{ $('.date_cell').css('visibility','visible'); } runHook('message',[ data.message, data.from, data.room ]); } } ], hooks = [ { type: '', hook: 'setting', fn: function(name){ return name != 'colour'; } }, { // load - style type: 'style', hook: 'load', fn: function(){ // STUB event('testing == '+testing,'debug'); } } ], currentPlugin = 0, runHook = function(name,args){ var i,r=true,hook,fn,sandbox = { testing: 'test' }; args=exists(args)?args:[]; for(i in hooks){ hook = hooks[i]; if(hook.hook == name){ fn = (hook.fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,''); fn = 'var ret = true;eval("with(this){ret = ('+fn+').apply(this,arguments);}");return ret;'; try{ r = (new Function(fn)).apply(sandbox,args); }catch(e){ event('Hook failed to run: '+e+"\nFunction that ran: "+fn,'hook_error'); } } if(r == false){ break; } } return r; }, version = '3.0', abbrDate = function(selector){ if(settings.timestamp == 'fuzzy'){ $(selector).timeago(); }else{ $(selector).each(function(){ var timestamp = settings.timestamp, i, text='', date = new Date($(this).attr('title')); if(timestamp == 'exact'){ timestamp = 'H:m:s t'; } for(i=0;i9?'':'0')+date.getMinutes();break; case 's':text+=(date.getSeconds()>9?'':'0')+date.getSeconds();break; case 't':text+=(date.getHours()>11)?'pm':'am';break; default:text+=timestamp[i]; } } $(this).text(text); }).timeago('dispose'); } }, socket,$i,$s,$h,$cl,$c,$tl,hht; $.extend($o,{ version: function(){ return version; }, register: { theme: function(name){ if(-1==$.inArray(properties.themes,name)){ properties.themes.push(name); runHook('theme',[name]); return true; } return false; }, command: function(name,fn,help){ if(-1==$.inArray(commands,name)){ var o = { cmd: name, fn: fn }; if(exists(help)){ o.help = help; } commands.push(o); return true; } return false; }, plugin: function(){ // STUB }, setting: function(name,type,val,validate,values,callback){ if(!exists(settings[name])){ validate = exists(validate)?validate:function(){}; values = exists(values)?values:undefined; callback = exists(callback)?callback:function(){}; settings[name] = val; settingsConf[name] = { validate: validate, callback: callback, type: type, values: values, default: val, name: name } return true; }else{ return false; } }, hook: function(event,fn){ hooks.push({ hook: event, fn: fn }) } }, hook: function(event,fn){ $o.register.hook(event,fn); }, ui: { render: { settings: function(){ var name,setting,frag = document.createDocumentFragment(),item; for(name in settings){ setting = $o.get(name,true); switch(setting.type){ case 'select': item = $('') .attr({ type: 'text', id: 'setting_'+name }) .val(setting.val) .change(function(){ $o.set(this.id.substr(8),$(this).val().split(','),false); }); break; case 'boolean': item = $('') .attr({ type: 'checkbox', id: 'setting_'+name }) .change(function(){ $o.set(this.id.substr(8),$(this).is(':checked'),false); }); if(setting.val){ item.attr('checked','checked'); } break; case 'number': case 'string':default: item = $('') .attr({ type: 'text', id: 'setting_'+name }) .val(setting.val) .change(function(){ $o.set(this.id.substr(8),$(this).val(),false); }); } $(frag).append( $('
  • ') .addClass('row') .append( $('') .text(name) .addClass('cell') ) .append( $('') .append(item) .addClass('cell') ) ); } if(settings.debug){ $(frag).append( $('
  • ') .addClass('row') .attr('id','console-log-controls') .append( $('') .text('Debug Log') .addClass('cell') ) .append( $('') .addClass('cell') .append( $('