]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/mobile.js
GContact moved to src
[friendica-addons.git] / jappixmini / jappix / js / mobile.js
1 /*
2
3 Jappix - An open social platform
4 These are the Jappix Mobile lightweight JS script
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 10/07/11
11
12 */
13
14 /* BEGIN CONNECTION FUNCTIONS */
15
16 function doLogin(aForm) {
17         try {
18                 // Reset the panels
19                 resetPanel();
20                 
21                 // Get the values
22                 var xid = aForm.xid.value;
23                 var username, domain;
24                 
25                 // A domain is specified
26                 if(xid.indexOf('@') != -1) {
27                         username = getXIDNick(xid);
28                         domain = getXIDHost(xid);
29                         
30                         // Domain is locked and not the same
31                         if((LOCK_HOST == 'on') && (domain != HOST_MAIN)) {
32                                 showThis('error');
33                                 
34                                 return false;
35                         }
36                 }
37                 
38                 // No "@" in the XID, we should add the default domain
39                 else {
40                         username = xid;
41                         domain = HOST_MAIN;
42                 }
43                 
44                 var pwd = aForm.pwd.value;
45                 var reg = false;
46                 
47                 if(aForm.reg)
48                         reg = aForm.reg.checked;
49                 
50                 // Enough parameters
51                 if(username && domain && pwd) {
52                         // Show the info notification
53                         showThis('info');
54                         
55                         // We define the http binding parameters
56                         oArgs = new Object();
57                         
58                         if(HOST_BOSH_MAIN)
59                                 oArgs.httpbase = HOST_BOSH_MAIN;
60                         else
61                                 oArgs.httpbase = HOST_BOSH;
62                         
63                         // We create the new http-binding connection
64                         con = new JSJaCHttpBindingConnection(oArgs);
65                         
66                         // And we handle everything that happen
67                         con.registerHandler('message', handleMessage);
68                         con.registerHandler('presence', handlePresence);
69                         con.registerHandler('iq', handleIQ);
70                         con.registerHandler('onconnect', handleConnected);
71                         con.registerHandler('onerror', handleError);
72                         con.registerHandler('ondisconnect', handleDisconnected);
73                         
74                         // We retrieve what the user typed in the login inputs
75                         oArgs = new Object();
76                         oArgs.username = username;
77                         oArgs.domain = domain;
78                         oArgs.resource = JAPPIX_RESOURCE + ' Mobile (' + (new Date()).getTime() + ')';
79                         oArgs.pass = pwd;
80                         oArgs.secure = true;
81                         oArgs.xmllang = XML_LANG;
82                         
83                         // Register?
84                         if(reg)
85                                 oArgs.register = true;
86                         
87                         // We connect !
88                         con.connect(oArgs);
89                 }
90                 
91                 // Not enough parameters
92                 else
93                         showThis('error');
94         }
95         
96         catch(e) {
97                 // An error happened
98                 resetPanel('error');
99         }
100         
101         finally {
102                 return false;
103         }
104 }
105
106 function doLogout() {
107         con.disconnect();
108 }
109
110 /* END CONNECTION FUNCTIONS */
111
112 /* BEGIN SHOW/HIDE FUNCTIONS */
113
114 function showThis(id) {
115         document.getElementById(id).style.display = 'block';
116 }
117
118 function hideThis(id) {
119         document.getElementById(id).style.display = 'none';
120 }
121
122 function resetPanel(id) {
123         // Hide the opened panels
124         hideThis('info');
125         hideThis('error');
126         
127         //Show the target panel
128         if(id)
129                 showThis(id);
130 }
131
132 function resetDOM() {
133         // Reset the "secret" input values
134         document.getElementById('pwd').value = '';
135         
136         // Remove the useless DOM elements
137         var body = document.getElementsByTagName('body')[0];
138         body.removeChild(document.getElementById('talk'));
139         body.removeChild(document.getElementById('chat'));
140 }
141
142 /* END SHOW/HIDE FUNCTIONS */
143
144 /* BEGIN SYSTEM FUNCTIONS */
145
146 function exists(id) {
147         if(!document.getElementById(id))
148                 return false;
149         else
150                 return true;
151 }
152
153 function _e(string) {
154         return string;
155 }
156
157 function encodeOnclick(str) {
158         return str.replace(/'/g, '\\$&').replace(/"/g, '"');
159 }
160
161 function getJappixLocation() {
162         var url = window.location.href;
163         
164         // If the URL has variables, remove them
165         if(url.indexOf('?') != -1)
166                 url = url.split('?')[0];
167         if(url.indexOf('#') != -1)
168                 url = url.split('#')[0];
169         
170         // No "/" at the end
171         if(!url.match(/(.+)\/$/))
172                 url += '/';
173         
174         return url;
175 }
176
177 /* END SYSTEM FUNCTIONS */
178
179 /* BEGIN HANDLING FUNCTIONS */
180
181 function handleMessage(msg) {
182         var type = msg.getType();
183         
184         if(type == 'chat' || type == 'normal') {
185                 // Get the body
186                 var body = msg.getBody();
187                 
188                 if(body) {
189                         // Get the values
190                         var xid = cutResource(msg.getFrom());
191                         var hash = hex_md5(xid);
192                         var nick = getNick(xid, hash);
193                         
194                         // No nickname?
195                         if(!nick)
196                                 nick = xid;
197                 
198                         // Create the chat if it does not exist
199                         chat(xid, nick);
200                 
201                         // Display the message
202                         displayMessage(xid, body, nick, hash);
203                 }
204         }
205 }
206
207 function handlePresence(pre) {
208         // Define the variables
209         var xid = cutResource(pre.getFrom());
210         var hash = hex_md5(xid);
211         var type = pre.getType();
212         var show = pre.getShow();
213         
214         // Online buddy: show it!
215         if(!type) {
216                 showThis('buddy-' + hash);
217                 
218                 // Display the correct presence
219                 switch(show) {
220                         case 'chat':
221                                 displayPresence(hash, show);
222                                 break;
223                         
224                         case 'away':
225                                 displayPresence(hash, show);
226                                 break;
227                         
228                         case 'xa':
229                                 displayPresence(hash, show);
230                                 break;
231                         
232                         case 'dnd':
233                                 displayPresence(hash, show);
234                                 break;
235                         
236                         default:
237                                 displayPresence(hash, 'available');
238                                 break;
239                 }
240         }
241         
242         else
243                 hideThis('buddy-' + hash);
244 }
245
246 function handleIQ(iq) {
247         // Get the content
248         var iqFrom = iq.getFrom();
249         var iqID = iq.getID();
250         var iqQueryXMLNS = iq.getQueryXMLNS();
251         var iqType = iq.getType();
252         
253         // Create the response
254         if((iqType == 'get') && ((iqQueryXMLNS == NS_DISCO_INFO) || (iqQueryXMLNS == NS_VERSION))) {
255                 var iqResponse = new JSJaCIQ();
256                 iqResponse.setID(iqID);
257                 iqResponse.setTo(iqFrom);
258                 iqResponse.setType('result');
259         }
260         
261         // Disco#infos query
262         if((iqQueryXMLNS == NS_DISCO_INFO) && (iqType == 'get')) {
263                 /* REF: http://xmpp.org/extensions/xep-0030.html */
264                 
265                 var iqQuery = iqResponse.setQuery(NS_DISCO_INFO);
266                 
267                 // We set the name of the client
268                 iqQuery.appendChild(iq.appendNode('identity', {
269                         'category': 'client',
270                         'type': 'mobile',
271                         'name': 'Jappix Mobile'
272                 }));
273                 
274                 // We set all the supported features
275                 var fArray = new Array(
276                         NS_DISCO_INFO,
277                         NS_VERSION
278                 );
279                 
280                 for(i in fArray)
281                         iqQuery.appendChild(iq.buildNode('feature', {'var': fArray[i]}));
282                 
283                 con.send(iqResponse);
284         }
285         
286         // Software version query
287         else if((iqQueryXMLNS == NS_VERSION) && (iqType == 'get')) {
288                 /* REF: http://xmpp.org/extensions/xep-0092.html */
289                 
290                 var iqQuery = iqResponse.setQuery(NS_VERSION);
291                 
292                 iqQuery.appendChild(iq.buildNode('name', 'Jappix Mobile'));
293                 iqQuery.appendChild(iq.buildNode('version', JAPPIX_VERSION));
294                 iqQuery.appendChild(iq.buildNode('os', BrowserDetect.OS));
295                 
296                 con.send(iqResponse);
297         }
298 }
299
300 function handleConnected() {
301         // Reset the elements
302         hideThis('home');
303         resetPanel();
304         
305         // Create the talk page
306         document.getElementsByTagName('body')[0].innerHTML +=
307         '<div id="talk">' + 
308                 '<div class="header">' + 
309                         '<div class="mobile-images"></div>' + 
310                         '<button onclick="doLogout();">' + _e("Disconnect") + '</button>' + 
311                 '</div>' + 
312                 
313                 '<div id="roster"></div>' + 
314         '</div>' + 
315         
316         '<div id="chat">' + 
317                 '<div class="header">' + 
318                         '<div class="mobile-images"></div>' + 
319                         '<button onclick="returnToRoster();">' + _e("Previous") + '</button>' + 
320                 '</div>' + 
321                 
322                 '<div id="chans"></div>' + 
323         '</div>';
324         
325         // Get the roster items
326         getRoster();
327 }
328
329 function handleError(error) {
330         resetPanel('error');
331 }
332
333 function handleDisconnected() {
334         // Reset the elements
335         resetDOM();
336         
337         // Show the home page
338         showThis('home');
339 }
340
341 function handleRoster(iq) {
342         // Error: send presence anyway
343         if(!iq || (iq.getType() != 'result'))
344                 return sendPresence('', 'available', 1);
345         
346         // Define some pre-vars
347         var current, xid, nick, oneBuddy, oneID, hash;
348         var roster = document.getElementById('roster');
349         
350         // Get roster items
351         var iqNode = iq.getNode();
352         var bItems = iqNode.getElementsByTagName('item');
353         
354         // Display each elements from the roster
355         for(var i = 0; i < bItems.length; i++) {
356                 // Get the values
357                 current = iqNode.getElementsByTagName('item').item(i);
358                 xid = current.getAttribute('jid').htmlEnc();
359                 nick = current.getAttribute('name');
360                 hash = hex_md5(xid);
361                 
362                 // No defined nick?
363                 if(!nick)
364                         nick = getDirectNick(xid);
365                 
366                 // Display the values
367                 oneBuddy = document.createElement('a');
368                 oneID = 'buddy-' + hash;
369                 oneBuddy.setAttribute('href', '#');
370                 oneBuddy.setAttribute('id', oneID);
371                 oneBuddy.setAttribute('class', 'one-buddy');
372                 oneBuddy.setAttribute('onclick', 'return chat(\'' + encodeOnclick(xid) + '\', \'' + encodeOnclick(nick) + '\');');
373                 oneBuddy.innerHTML = nick.htmlEnc();
374                 roster.appendChild(oneBuddy);
375         }
376         
377         // Start handling buddies presence
378         sendPresence('', 'available', 1);
379 }
380
381 /* END HANDLING FUNCTIONS */
382
383 /* BEGIN SENDING FUNCTIONS */
384
385 function sendMessage(aForm) {
386         try {
387                 var body = aForm.body.value;
388                 var xid = aForm.xid.value;
389                 var hash = hex_md5(xid);
390                 
391                 if(body && xid) {
392                         // Send the message
393                         var aMsg = new JSJaCMessage();
394                         aMsg.setTo(xid);
395                         aMsg.setType('chat');
396                         aMsg.setBody(body);
397                         con.send(aMsg);
398                         
399                         // Clear our input
400                         aForm.body.value = '';
401                         
402                         // Display the message we sent
403                         displayMessage(xid, body, 'me', hash);
404                 }
405         }
406         
407         finally {
408                 return false;
409         }
410 }
411
412 function sendPresence(type, show, priority, status) {
413         var presence = new JSJaCPresence();
414         
415         if(type)
416                 presence.setType(type);
417         if(show)
418                 presence.setShow(show);
419         if(priority)
420                 presence.setPriority(priority);
421         if(status)
422                 presence.setStatus(status);
423         
424         con.send(presence);
425 }
426
427 /* END SENDING FUNCTIONS */
428
429 /* BEGIN GETTING FUNCTIONS */
430
431 function getRoster() {
432         iq = new JSJaCIQ();
433         iq.setType('get');
434         iq.setQuery(NS_ROSTER);
435         
436         con.send(iq, handleRoster);
437 }
438
439 function getDirectNick(xid) {
440         return explodeThis('@', xid, 0);
441 }
442
443 function getNick(xid, hash) {
444         var path = 'buddy-' + hash;
445         
446         if(exists(path))
447                 return document.getElementById(path).innerHTML;
448         
449         else
450                 getDirectNick(xid);
451 }
452
453 /* END GETTING FUNCTIONS */
454
455 /* BEGIN RESOURCES FUNCTIONS */
456 function explodeThis(toEx, toStr, i) {
457         // Get the index of our char to explode
458         var index = toStr.indexOf(toEx);
459         
460         // We split if necessary the string
461         if(index != -1) {
462                 if(i == 0)
463                         toStr = toStr.substr(0, index);
464                 else
465                         toStr = toStr.substr(index + 1);
466         }
467         
468         // We return the value
469         return toStr;
470 }
471
472 function cutResource(aXID) {
473         return explodeThis('/', aXID, 0);
474 }
475
476 function getXIDNick(aXID) {
477         return explodeThis('@', aXID, 0);
478 }
479
480 function getXIDHost(aXID) {
481         return explodeThis('@', aXID, 1);
482 }
483 /* END RESOURCES FUNCTIONS */
484
485 /* BEGIN CHAT FUNCTIONS */
486
487 function filter(msg) {
488         var msg = msg
489         
490         // Encode in HTML
491         .htmlEnc()
492         
493         // Highlighted text
494         .replace(/(\s|^)\*(.+)\*(\s|$)/gi,'$1<em>$2</em>$3');
495         
496         // Links
497         msg = applyLinks(msg, 'mini');
498         
499         return msg;
500 }
501
502 function displayMessage(xid, body, nick, hash) {
503         // Get the path
504         var path = 'content-' + hash;
505         
506         // Display the message
507         html = '<span><b';
508         
509         if(nick == 'me')
510                 html += ' class="me">' + _e("You");
511         else
512                 html += ' class="him">' + nick;
513         
514         html += '</b> ' + filter(body) + '</span>';
515         
516         document.getElementById(path).innerHTML += html;
517         
518         // Scroll to the last element
519         document.getElementById(path).lastChild.scrollIntoView();
520 }
521
522 function returnToRoster() {
523         // Hide the chats
524         hideThis('chat');
525         
526         // Show the roster
527         showThis('talk');
528 }
529
530 function chatSwitch(hash) {
531         // Hide the roster page
532         hideThis('talk');
533         
534         // Hide the other chats
535         var divs = document.getElementsByTagName('div');
536         
537         for(var i = 0; i < divs.length; i++) {
538                 if(divs.item(i).getAttribute('class') == 'one-chat')
539                         divs.item(i).style.display = 'none';
540         }
541         
542         // Show the chat
543         showThis('chat');
544         showThis(hash);
545 }
546
547 function createChat(xid, nick, hash) {
548         // Define the variables
549         var chat = document.getElementById('chans');
550         var oneChat = document.createElement('div');
551         
552         // Apply the DOM modification
553         oneChat.setAttribute('id', 'chat-' + hash);
554         oneChat.setAttribute('class', 'one-chat');
555         oneChat.innerHTML = '<p>' + nick + '</p><div id="content-' + hash + '"></div><form action="#" method="post" onsubmit="return sendMessage(this);"><input type="text" name="body" /><input type="hidden" name="xid" value="' + xid + '" /><input type="submit" class="submit" value="OK" /></form>';
556         chat.appendChild(oneChat);
557 }
558
559 function chat(xid, nick) {
560         var hash = hex_md5(xid);
561         
562         // If the chat was not yet opened
563         if(!exists('chat-' + hash)) {
564                 // No nick?
565                 if(!nick)
566                         nick = getNick(xid, hash);
567                 
568                 // Create the chat
569                 createChat(xid, nick, hash);
570         }
571         
572         // Switch to the chat
573         chatSwitch('chat-' + hash);
574         
575         return false;
576 }
577
578 /* END CHAT FUNCTIONS */
579
580 /* BEGIN PRESENCE FUNCTIONS */
581
582 function displayPresence(hash, show) {
583         document.getElementById('buddy-' + hash).setAttribute('class', 'one-buddy ' + show);
584 }
585
586 /* END PRESENCE FUNCTIONS */
587
588 /* BEGIN DOCUMENT EVENTS FUNCTIONS */
589
590 onbeforeunload = doLogout;
591
592 /* END DOCUMENT EVENTS FUNCTIONS */