/* Jappix - An open social platform These are the Jappix Mobile lightweight JS script ------------------------------------------------- License: AGPL Author: Valérian Saliou Last revision: 10/07/11 */ /* BEGIN CONNECTION FUNCTIONS */ function doLogin(aForm) { try { // Reset the panels resetPanel(); // Get the values var xid = aForm.xid.value; var username, domain; // A domain is specified if(xid.indexOf('@') != -1) { username = getXIDNick(xid); domain = getXIDHost(xid); // Domain is locked and not the same if((LOCK_HOST == 'on') && (domain != HOST_MAIN)) { showThis('error'); return false; } } // No "@" in the XID, we should add the default domain else { username = xid; domain = HOST_MAIN; } var pwd = aForm.pwd.value; var reg = false; if(aForm.reg) reg = aForm.reg.checked; // Enough parameters if(username && domain && pwd) { // Show the info notification showThis('info'); // We define the http binding parameters oArgs = new Object(); if(HOST_BOSH_MAIN) oArgs.httpbase = HOST_BOSH_MAIN; else oArgs.httpbase = HOST_BOSH; // Check BOSH origin BOSH_SAME_ORIGIN = isSameOrigin(oArgs.httpbase); // We create the new http-binding connection con = new JSJaCHttpBindingConnection(oArgs); // And we handle everything that happen con.registerHandler('message', handleMessage); con.registerHandler('presence', handlePresence); con.registerHandler('iq', handleIQ); con.registerHandler('onconnect', handleConnected); con.registerHandler('onerror', handleError); con.registerHandler('ondisconnect', handleDisconnected); // We retrieve what the user typed in the login inputs oArgs = new Object(); oArgs.username = username; oArgs.domain = domain; oArgs.resource = JAPPIX_RESOURCE + ' Mobile (' + (new Date()).getTime() + ')'; oArgs.pass = pwd; oArgs.secure = true; oArgs.xmllang = XML_LANG; // Register? if(reg) oArgs.register = true; // We connect ! con.connect(oArgs); } // Not enough parameters else showThis('error'); } catch(e) { // An error happened resetPanel('error'); } finally { return false; } } function doLogout() { con.disconnect(); } /* END CONNECTION FUNCTIONS */ /* BEGIN SHOW/HIDE FUNCTIONS */ function showThis(id) { document.getElementById(id).style.display = 'block'; } function hideThis(id) { document.getElementById(id).style.display = 'none'; } function resetPanel(id) { // Hide the opened panels hideThis('info'); hideThis('error'); //Show the target panel if(id) showThis(id); } function resetDOM() { // Reset the "secret" input values document.getElementById('pwd').value = ''; // Remove the useless DOM elements var body = document.getElementsByTagName('body')[0]; body.removeChild(document.getElementById('talk')); body.removeChild(document.getElementById('chat')); } /* END SHOW/HIDE FUNCTIONS */ /* BEGIN SYSTEM FUNCTIONS */ function exists(id) { if(!document.getElementById(id)) return false; else return true; } function _e(string) { return string; } function encodeOnclick(str) { return str.replace(/'/g, '\\$&').replace(/"/g, '"'); } function getJappixLocation() { var url = window.location.href; // If the URL has variables, remove them if(url.indexOf('?') != -1) url = url.split('?')[0]; if(url.indexOf('#') != -1) url = url.split('#')[0]; // No "/" at the end if(!url.match(/(.+)\/$/)) url += '/'; return url; } /* END SYSTEM FUNCTIONS */ /* BEGIN HANDLING FUNCTIONS */ function handleMessage(msg) { var type = msg.getType(); if(type == 'chat' || type == 'normal') { // Get the body var body = msg.getBody(); if(body) { // Get the values var xid = cutResource(msg.getFrom()); var hash = hex_md5(xid); var nick = getNick(xid, hash); // No nickname? if(!nick) nick = xid; // Create the chat if it does not exist chat(xid, nick); // Display the message displayMessage(xid, body, nick, hash); } } } function handlePresence(pre) { // Define the variables var xid = cutResource(pre.getFrom()); var hash = hex_md5(xid); var type = pre.getType(); var show = pre.getShow(); // Online buddy: show it! if(!type) { showThis('buddy-' + hash); // Display the correct presence switch(show) { case 'chat': displayPresence(hash, show); break; case 'away': displayPresence(hash, show); break; case 'xa': displayPresence(hash, show); break; case 'dnd': displayPresence(hash, show); break; default: displayPresence(hash, 'available'); break; } } else hideThis('buddy-' + hash); } function handleIQ(iq) { // Get the content var iqFrom = iq.getFrom(); var iqID = iq.getID(); var iqQueryXMLNS = iq.getQueryXMLNS(); var iqType = iq.getType(); // Create the response if((iqType == 'get') && ((iqQueryXMLNS == NS_DISCO_INFO) || (iqQueryXMLNS == NS_VERSION))) { var iqResponse = new JSJaCIQ(); iqResponse.setID(iqID); iqResponse.setTo(iqFrom); iqResponse.setType('result'); } // Disco#infos query if((iqQueryXMLNS == NS_DISCO_INFO) && (iqType == 'get')) { /* REF: http://xmpp.org/extensions/xep-0030.html */ var iqQuery = iqResponse.setQuery(NS_DISCO_INFO); // We set the name of the client iqQuery.appendChild(iq.appendNode('identity', { 'category': 'client', 'type': 'mobile', 'name': 'Jappix Mobile' })); // We set all the supported features var fArray = new Array( NS_DISCO_INFO, NS_VERSION ); for(i in fArray) iqQuery.appendChild(iq.buildNode('feature', {'var': fArray[i]})); con.send(iqResponse); } // Software version query else if((iqQueryXMLNS == NS_VERSION) && (iqType == 'get')) { /* REF: http://xmpp.org/extensions/xep-0092.html */ var iqQuery = iqResponse.setQuery(NS_VERSION); iqQuery.appendChild(iq.buildNode('name', 'Jappix Mobile')); iqQuery.appendChild(iq.buildNode('version', JAPPIX_VERSION)); iqQuery.appendChild(iq.buildNode('os', BrowserDetect.OS)); con.send(iqResponse); } } function handleConnected() { // Reset the elements hideThis('home'); resetPanel(); // Create the talk page document.getElementsByTagName('body')[0].innerHTML += '
' + nick + '
'; chat.appendChild(oneChat); } function chat(xid, nick) { var hash = hex_md5(xid); // If the chat was not yet opened if(!exists('chat-' + hash)) { // No nick? if(!nick) nick = getNick(xid, hash); // Create the chat createChat(xid, nick, hash); } // Switch to the chat chatSwitch('chat-' + hash); return false; } /* END CHAT FUNCTIONS */ /* BEGIN PRESENCE FUNCTIONS */ function displayPresence(hash, show) { document.getElementById('buddy-' + hash).setAttribute('class', 'one-buddy ' + show); } /* END PRESENCE FUNCTIONS */ /* BEGIN DOCUMENT EVENTS FUNCTIONS */ onbeforeunload = doLogout; /* END DOCUMENT EVENTS FUNCTIONS */