3 Jappix - An open social platform
4 These are the connection JS script for Jappix
6 -------------------------------------------------
10 Last revision: 29/08/11
14 // Does the user login
15 var CURRENT_SESSION = false;
17 function doLogin(lNick, lServer, lPass, lResource, lPriority, lRemember) {
19 // We remove the not completed class to avoid problems
20 $('#home .loginer input').removeClass('please-complete');
22 // We add the login wait div
25 // We define the http binding parameters
29 oArgs.httpbase = HOST_BOSH_MAIN;
31 oArgs.httpbase = HOST_BOSH;
33 // We create the new http-binding connection
34 con = new JSJaCHttpBindingConnection(oArgs);
36 // And we handle everything that happen
39 // Generate a resource
40 var random_resource = getDB('session', 'resource');
43 random_resource = lResource + ' (' + (new Date()).getTime() + ')';
45 // We retrieve what the user typed in the login inputs
47 oArgs.domain = trim(lServer);
48 oArgs.username = trim(lNick);
49 oArgs.resource = random_resource;
52 oArgs.xmllang = XML_LANG;
54 // Store the resource (for reconnection)
55 setDB('session', 'resource', random_resource);
57 // Generate a session XML to be stored
58 session_xml = '<session><stored>true</stored><domain>' + lServer.htmlEnc() + '</domain><username>' + lNick.htmlEnc() + '</username><resource>' + lResource.htmlEnc() + '</resource><password>' + lPass.htmlEnc() + '</password><priority>' + lPriority.htmlEnc() + '</priority></session>';
60 // Save the session parameters (for reconnect if network issue)
61 CURRENT_SESSION = session_xml;
65 setDB('remember', 'session', 1);
67 // We store the infos of the user into the data-base
68 setDB('priority', 1, lPriority);
73 // Change the page title
76 logThis('Jappix is connecting...', 3);
81 logThis('Error while logging in: ' + e, 1);
86 // Open an unknown error
95 // Handles the user registration
96 function handleRegistered() {
97 logThis('A new account has been registered.', 3);
99 // We remove the waiting image
105 // We show the success information
106 $('#home .registerer .success').fadeIn('fast');
108 // We quit the session
112 // Does the user registration
113 function doRegister(username, domain, pass) {
114 logThis('Trying to register an account...', 3);
117 // We define the http binding parameters
118 oArgs = new Object();
121 oArgs.httpbase = HOST_BOSH_MAIN;
123 oArgs.httpbase = HOST_BOSH;
125 // We create the new http-binding connection
126 con = new JSJaCHttpBindingConnection(oArgs);
128 // We setup the connection !
129 con.registerHandler('onconnect', handleRegistered);
130 con.registerHandler('onerror', handleError);
132 // We retrieve what the user typed in the register inputs
133 oArgs = new Object();
134 oArgs.domain = trim(domain);
135 oArgs.username = trim(username);
136 oArgs.resource = JAPPIX_RESOURCE + ' Register (' + (new Date()).getTime() + ')';
138 oArgs.register = true;
140 oArgs.xmllang = XML_LANG;
144 // We change the registered information text
145 $('#home .homediv.registerer').append(
146 '<div class="info success">' +
147 _e("You have been registered, here is your XMPP address:") + ' <b>' + con.username.htmlEnc() + '@' + con.domain.htmlEnc() + '</b> - <a href="#">' + _e("Login") + '</a>' +
152 $('#home .homediv.registerer .success a').click(function() {
153 return doLogin(con.username, con.domain, con.pass, con.resource, '10', false);
156 // Show the waiting image
159 // Change the page title
173 // Does the user anonymous login
174 function doAnonymous() {
175 logThis('Trying to login anonymously...', 3);
177 var aPath = '#home .anonymouser ';
178 var room = $(aPath + '.room').val();
179 var nick = $(aPath + '.nick').val();
181 // If the form is correctly completed
183 // We remove the not completed class to avoid problems
184 $('#home .anonymouser input').removeClass('please-complete');
186 // Redirect the user to the anonymous room
187 window.location.href = JAPPIX_LOCATION + '?r=' + room + '&n=' + nick;
190 // We check if the form is entirely completed
192 $(aPath + 'input[type=text]').each(function() {
193 var select = $(this);
196 $(document).oneTime(10, function() {
197 select.addClass('please-complete').focus();
200 select.removeClass('please-complete');
207 // Handles the user connected event
208 var CONNECTED = false;
210 function handleConnected() {
211 logThis('Jappix is now connected.', 3);
213 // Connection markers
218 // We hide the home page
223 // Remember the session?
224 if(getDB('remember', 'session'))
225 setPersistent('session', 1, CURRENT_SESSION);
227 // We show the chatting app.
230 // We reset the homepage
231 switchHome('default');
233 // We get all the other things
236 // Set last activity stamp
237 LAST_ACTIVITY = getTimeStamp();
249 // Remove the waiting item
253 // Handles the user disconnected event
254 function handleDisconnected() {
255 logThis('Jappix is now disconnected.', 3);
257 // Normal disconnection
258 if(!CURRENT_SESSION && !CONNECTED)
262 // Setups the normal connection
263 function setupCon(con) {
264 // We setup all the necessary handlers for the connection
265 con.registerHandler('message', handleMessage);
266 con.registerHandler('presence', handlePresence);
267 con.registerHandler('iq', handleIQ);
268 con.registerHandler('onconnect', handleConnected);
269 con.registerHandler('onerror', handleError);
270 con.registerHandler('ondisconnect', handleDisconnected);
273 // Logouts from the server
275 // We are not connected
279 // Disconnect from the XMPP server
282 logThis('Jappix is disconnecting...', 3);
285 // Terminates a session
286 function terminate() {
290 // Clear temporary session storage
293 // Show the waiting item (useful if BOSH is sloooow)
296 // Change the page title
299 // Disconnect from the XMPP server
308 // We show the waiting image
311 // Change the page title
314 // We disconnect from the XMPP server
318 // Creates the reconnect pane
319 var RECONNECT_TRY = 0;
320 var RECONNECT_TIMER = 0;
322 function createReconnect(mode) {
323 logThis('This is not a normal disconnection, show the reconnect pane...', 1);
325 // Reconnect pane not yet displayed?
326 if(!exists('#reconnect')) {
327 // Blur the focused input/textarea/select
328 $('input, select, textarea').blur();
330 // Create the HTML code
331 var html = '<div id="reconnect" class="lock">' +
332 '<div class="pane">' +
333 _e("Due to a network issue, you were disconnected. What do you want to do now?");
335 // Can we cancel reconnection?
337 html += '<a href="#" class="finish cancel">' + _e("Cancel") + '</a>';
339 html += '<a href="#" class="finish reconnect">' + _e("Reconnect") + '</a>' +
343 $('body').append(html);
347 $('#reconnect a.finish.cancel').click(function() {
348 return cancelReconnect();
351 $('#reconnect a.finish.reconnect').click(function() {
352 return acceptReconnect(mode);
355 // Try to reconnect automatically after a while
356 if(RECONNECT_TRY < 5)
357 RECONNECT_TIMER = 5 + (5 * RECONNECT_TRY);
359 RECONNECT_TIMER = 120;
361 // Change the try number
365 $('#reconnect a.finish.reconnect').everyTime('1s', function() {
367 if(RECONNECT_TIMER == 0)
368 return acceptReconnect(mode);
371 if(RECONNECT_TIMER <= 10)
372 $(this).text(_e("Reconnect") + ' (' + RECONNECT_TIMER + ')');
383 // Reconnects the user if he was disconnected (network issue)
386 function acceptReconnect(mode) {
387 logThis('Trying to reconnect the user...', 3);
395 // Reset some various stuffs
396 var groupchats = '#page-engine .page-engine-chan[data-type=groupchat]';
397 $(groupchats + ' .list .role').hide();
398 $(groupchats + ' .one-group, ' + groupchats + ' .list .user').remove();
399 $(groupchats).attr('data-initial', 'false');
402 $('#reconnect a.finish.reconnect').stopTime();
404 // Remove the reconnect pane
405 $('#reconnect').remove();
407 // Try to login again
409 loginFromSession(XMLFromString(CURRENT_SESSION));
410 else if(mode == 'anonymous')
411 anonymousLogin(HOST_ANONYMOUS);
416 // Cancel the reconnection of user account (network issue)
417 function cancelReconnect() {
418 logThis('User has canceled automatic reconnection...', 3);
421 $('#reconnect a.finish.reconnect').stopTime();
423 // Remove the reconnect pane
424 $('#reconnect').remove();
426 // Destroy the talk page
429 // Renitialize the previous session parameters
435 // Clears session reminder database
436 function clearLastSession() {
437 // Clear temporary storage
440 // Clear persistent storage
441 if($(XMLFromString(getPersistent('session', 1))).find('stored').text() == 'true')
442 removePersistent('session', 1);
445 // Resets the connection markers
446 function resetConMarkers() {
447 CURRENT_SESSION = false;
454 // Logins from a saved session
455 function loginFromSession(data) {
457 var session = $(data);
459 // Fire the login event
461 session.find('username').text(),
462 session.find('domain').text(),
463 session.find('password').text(),
464 session.find('resource').text(),
465 session.find('priority').text(),
470 // Quits a session normally
471 function normalQuit() {
472 // Reset our database
475 // We quit the current session
484 // Gets all the users stuffs
485 function getEverything() {
489 getStorage(NS_ROSTERNOTES);
493 function launchConnection() {
494 // Logouts when Jappix is closed
495 $(window).bind('beforeunload', terminate);
497 // Nothing to do when anonymous!
501 // Try to resume a stored session, if not anonymous
502 var session = XMLFromString(getPersistent('session', 1));
504 if($(session).find('stored').text() == 'true') {
508 // Show the waiting icon
512 loginFromSession(session);
514 logThis('Saved session found, resuming it...', 3);
517 // Not connected, maybe a XMPP link is submitted?
518 else if((parent.location.hash != '#OK') && LINK_VARS['x']) {
519 switchHome('loginer');
521 logThis('A XMPP link is set, switch to login page.', 3);
525 // Launch this plugin!
526 $(document).ready(launchConnection);