]> cgit.babelmonkeys.de Git - socialXMPP.git/blobdiff - scripts/basic.js
Small message changes/fixes and consistency change
[socialXMPP.git] / scripts / basic.js
index cc4fde5a693a6ea63da3ec3760b687b0ce040eaa..1ef91154cde0c8947ce8b8640079757c41b66870 100644 (file)
@@ -13,7 +13,7 @@ var appName = 'socialXMPP';
 var roster = new Array();
 
 function log(msg) {
-    var entry = $('<div></div>').append(Strophe.xmlTextNode(msg));
+    var entry = $('<div></div>').append(document.createTextNode(msg));
     $('#log').append(entry);
 }
 
@@ -47,6 +47,18 @@ function populateVCard(e, jid) {
        $("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
        $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
     }
+    $(e).find('TEL:has(HOME)').each(function() {
+        $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('TEL:has(WORK)').each(function() {
+        $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('EMAIL:has(HOME)').each(function() {
+        $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
+    $(e).find('EMAIL:has(WORK)').each(function() {
+        $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
     $('#vCard_container').slideDown("normal");
 }
 
@@ -65,6 +77,11 @@ function _cbVCard(e) {
 }
 
 function _cbOwnVCard(e) {
+    $('#ownInfo').empty();
+    $('#ownInfo').load('vCard.html #vCard', function() {
+    $('#ownInfo').find('div').each(function() {
+       $(this).attr('id', 'own' + $(this).attr('id'));
+    });
     var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
     for (var i=0; i<easy_cases.length; i++) {
        var text = e.getElementsByTagName(easy_cases[i])[0];
@@ -80,7 +97,20 @@ function _cbOwnVCard(e) {
 
        $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
     }
+    $(e).find('TEL:has(HOME)').each(function() {
+        $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('TEL:has(WORK)').each(function() {
+        $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('EMAIL:has(HOME)').each(function() {
+        $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
+    $(e).find('EMAIL:has(WORK)').each(function() {
+        $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
     $('#ownInfo').show();
+    });
 
     return false;
 }
@@ -97,7 +127,6 @@ function getVCard(jid) {
     } else {
        _cbVCard(roster[jid2id(jid)].vCard);
     }
-
 }
 
 function getOwnInfo() {
@@ -129,7 +158,7 @@ function _cbRoster(e) {
        addFriend(entries[item].getAttribute('jid'), nick);
     }
     connection.addHandler(_cbPEP, NS_PEP, 'message');
-    var initialPresence = $pres().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
+    var initialPresence = $pres().c('show').t('online').up().c('status').t('Hy, I am an socialXMPP instance').up().c('priority').t('0').up().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
     connection.send(initialPresence.tree());
 
     return false;
@@ -161,6 +190,29 @@ function _cbDisco(e) {
     return true;
 }
 
+function _cbMessage(msg) {
+    if ($(msg).attr('type') != 'chat')
+       return;
+    var jid = $(msg).attr('from');
+    var id = jid2id($(msg).attr('from')) + 'Chat';
+    var body = $(msg).find('body:first').text();
+    body = escape(body);
+    body = body.replace(/%0A/g, '<br/>');
+    body = body.replace(/%3C/g, '&lt;');
+    body = body.replace(/%3E/g, '&gt;');
+    body = body.replace(/%26/g, '&amp;');
+    body = unescape(body);
+    if ($('#' + id).length > 0) {
+       $('#' + id + ' p').append('<br/>');
+       $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
+       $('#' + id + ' p').append(body);
+    } else {
+       $('body').append('<div class="chat" id="' + id + '"><p><span class="sender">' + jid + ': </span>' + body + '</p></div>');
+    }
+
+    return true;
+}
+
 function genCaps() {
     var S = '';
     S += 'client/web//' + appName + '<'
@@ -207,7 +259,6 @@ function _cbPEP(e) {
 function onConnect(status) {
     if (status == Strophe.Status.CONNECTING) {
        log('Strophe is connecting.');
-
     } else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        showConnect();
@@ -216,12 +267,16 @@ function onConnect(status) {
     } else if (status == Strophe.Status.DISCONNECTED) {
        log('Strophe is disconnected.');
        showConnect();
-
+    } else if (status == Strophe.Status.AUTHFAIL) {
+       log('Authentication failed.');
+       connection.disconnect();
+       showConnect();
     } else if (status == Strophe.Status.CONNECTED) {
        log('Strophe is connected.');
        getOwnInfo();
        getRoster();
        connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
+       connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
     }
 }
 
@@ -231,9 +286,10 @@ function showConnect() {
     var button = $('#connect').get(0); 
 
     $('#log').empty();
-    $('#ownInfo').empty();
+    $('#ownInfo').hide();
     $('#vCard_container').empty();
     $('#friends').empty();
+    $('.chat').remove();
     $('label').show();
     jid.show();
     pass.show();