]> cgit.babelmonkeys.de Git - socialXMPP.git/blobdiff - scripts/basic.js
Handle Authentication and update to latest Stropejs version
[socialXMPP.git] / scripts / basic.js
index 1246bfbcff0de95e3aff7a0b368a1eb046b0feb4..a4b4a186717282899f116943ca279dd01739a530 100644 (file)
@@ -1,9 +1,15 @@
 var NS_VCARD = 'vcard-temp';
+var NS_CAPS= 'http://jabber.org/protocol/caps';
+var NS_PEP = 'http://jabber.org/protocol/pubsub#event';
+var NS_TUNE = 'http://jabber.org/protocol/tune';
 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
 
 var connection   = null;
 var show_log     = true;
 
+var features = new Array(NS_CAPS, NS_TUNE+'+notify', Strophe.NS.DISCO_INFO);
+var appName = 'socialXMPP';
+
 var roster = new Array();
 
 function log(msg) {
@@ -20,6 +26,7 @@ function rawOutput(data) {
 }
 
 function jid2id(jid) {
+    jid = Strophe.getBareJidFromJid(jid);
     return jid.split('@').join('-').split('.').join('-');
 }
 
@@ -29,7 +36,7 @@ function populateVCard(e, jid) {
        var text = e.getElementsByTagName(easy_cases[i])[0];
        if (text) {
            text = Strophe.getText(text);
-           $('#'+easy_cases[i]).append(document.createTextNode(text));
+           $('#'+easy_cases[i]).append(Strophe.xmlTextNode(text));
        }
     }
     var avatar = e.getElementsByTagName('PHOTO')[0];
@@ -40,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");
 }
 
@@ -57,6 +76,45 @@ function _cbVCard(e) {
     return false;
 }
 
+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];
+       if (text) {
+           text = Strophe.getText(text);
+           $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
+       }
+    }
+    var avatar = e.getElementsByTagName('PHOTO')[0];
+    if (avatar) {
+       var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
+       var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
+
+       $("#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;
+}
+
 function getVCard(jid) {
     var id = 'getvCard'+jid2id(jid);
     if (roster[jid2id(jid)].vCard == "") {
@@ -69,7 +127,16 @@ function getVCard(jid) {
     } else {
        _cbVCard(roster[jid2id(jid)].vCard);
     }
+}
 
+function getOwnInfo() {
+    var id = 'getvCard'+jid2id(connection.jid);
+    var vCardiq = $iq({'to': Strophe.getBareJidFromJid(connection.jid),
+               'id': id,
+               'type': 'get'}
+       ).c('vCard', {'xmlns':NS_VCARD});
+    connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
+    connection.send(vCardiq.tree());
 }
 
 function addFriend(jid, nick) {
@@ -90,6 +157,10 @@ 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();
+    connection.send(initialPresence.tree());
+
     return false;
 }
 
@@ -104,10 +175,88 @@ function getRoster() {
     connection.send(rosteriq.tree());
 }
 
+function _cbDisco(e) {
+    var id = e.getAttribute('id');
+    var jid = e.getAttribute('from');
+
+    var response = $iq({id: id, type: 'result', to: jid});
+    var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
+    query.c('identity', {category: 'client', type: 'web', name: appName}).up();
+    for (var i = 0; i < features.length; i++) {
+       query.c('feature', {'var': features[i]}).up();
+    }
+    connection.send(response.tree());
+
+    return true;
+}
+
+function _cbMessage(msg) {
+    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 + '<'
+    features.sort();
+    for (var i = 0; i < features.length; i++) {
+       S += features[i] + '<';
+    }
+    return b64_sha1(S);
+}
+
+function _cbPEP(e) {
+    var from = e.getAttribute('from');
+    if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
+       return true; // Drop own PEP events
+    }
+    var items = e.getElementsByTagName('items')[0];
+    // Handle Tune
+    if (items.getAttribute('node') == NS_TUNE) {
+       var tune = new Tune();
+       if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
+           tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
+           tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
+           tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
+           tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
+           tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
+           tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
+           tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
+           if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
+               $('#'+jid2id(from)+' .tune').empty();
+               $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
+           } else {
+               $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
+           }
+       } else {
+           if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
+               $('#'+jid2id(from)+' .tune').empty();
+           }
+       }
+       roster[jid2id(from)].tune = tune;
+    }
+    return true;
+}
+
 function onConnect(status) {
     if (status == Strophe.Status.CONNECTING) {
        log('Strophe is connecting.');
-
     } else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        showConnect();
@@ -116,10 +265,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');
     }
 }
 
@@ -129,8 +284,10 @@ function showConnect() {
     var button = $('#connect').get(0); 
 
     $('#log').empty();
+    $('#ownInfo').hide();
     $('#vCard_container').empty();
     $('#friends').empty();
+    $('.chat').remove();
     $('label').show();
     jid.show();
     pass.show();