X-Git-Url: http://cgit.babelmonkeys.de/?p=socialXMPP.git;a=blobdiff_plain;f=scripts%2Fbasic.js;h=aecaa7eeed3e062a490b4d5d5f2be1ccee3c815a;hp=e0d2c38c8dd25ff26b4df61c39aa091a4a707fd1;hb=b12b35d39a99293eadee739b291f64433f5da34e;hpb=fea35da5e0bcc2b48a76a9131ff40aee79b60136
diff --git a/scripts/basic.js b/scripts/basic.js
index e0d2c38..aecaa7e 100644
--- a/scripts/basic.js
+++ b/scripts/basic.js
@@ -1,9 +1,18 @@
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 localJID = null;
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) {
var entry = $('
').append(document.createTextNode(msg));
$('#log').append(entry);
@@ -18,46 +27,154 @@ function rawOutput(data) {
}
function jid2id(jid) {
+ jid = Strophe.getBareJidFromJid(jid);
return jid.split('@').join('-').split('.').join('-');
}
+function populateVCard(e, jid) {
+ var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
+ for (var i=0; i
'+nick+'');
- $('#'+jid2id(jid)).click(function() {
- getAvatar(jid);
+ roster[jid2id(jid)] = new Buddy(nick, jid);
+ $('#friends').append('');
+ $('#' + jid2id(jid) + ' img').click(function() {
+ getVCard(jid);
});
+ $('#' + jid2id(jid) + ' a').click(function() {
+ var id = jid2id(jid) + 'Chat';
+ if ($('#' + id).length <= 0) {
+ createBubble(jid);
+ }
+ $('#' + id).show();
+ });
+
}
function _cbRoster(e) {
var query = e.getElementsByTagName('query')[0];
var entries = query.getElementsByTagName('item');
for (var item=0; item');
+ $('#' + id + ' p').append('' + localJID + ': ');
+ $('#' + id + ' p').append(form.text.value);
+ form.text.value = '';
+ }
+
+ return false;
+}
+
+function createBubble(jid) {
+ var id = jid2id(jid) + 'Chat';
+ $('body').append('');
+ $('#' + id).append('')
+ $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
+ $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
+}
+
+
+function _cbMessage(msg) {
+ if ($(msg).attr('type') != 'chat')
+ return true;
+ var jid = $(msg).attr('from');
+ var id = jid2id(jid) + 'Chat';
+ var body = $(msg).find('body:first').text();
+ body = escape(body);
+ body = body.replace(/%0A/g, '
');
+ body = body.replace(/%3C/g, '<');
+ body = body.replace(/%3E/g, '>');
+ body = body.replace(/%26/g, '&');
+ body = unescape(body);
+ if ($('#' + id).length <= 0) {
+ createBubble(jid);
+ }
+ $('#' + id + ' p').append('
');
+ $('#' + id + ' p').append('' + jid + ': ');
+ $('#' + id + ' p').append(body);
+ $('#' + id).show();
+
+ 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('
Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '');
+ }
+ } 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();
@@ -84,10 +304,17 @@ function onConnect(status) {
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
showConnect();
-
+ } else if (status == Strophe.Status.AUTHFAIL) {
+ log('Authentication failed');
+ if (connection) {
+ connection.disconnect();
+ }
} 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');
}
}
@@ -97,7 +324,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();
@@ -122,19 +352,19 @@ $(document).ready(function () {
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
- $("#log_toggle").bind('click', function () {
+ $("#log_toggle").click(function () {
$("#log").toggle();
- }
- );
+ });
$('#cred').bind('submit', function () {
var button = $('#connect').get(0);
var jid = $('#jid');
var pass = $('#pass');
+ localJID = jid.get(0).value;
if (button.value == 'connect') {
showDisconnect();
- connection.connect(jid.get(0).value,
+ connection.connect(localJID,
pass.get(0).value,
onConnect);
} else {
@@ -144,3 +374,40 @@ $(document).ready(function () {
return false;
});
});
+
+// Element moving
+var dragElement = null;
+var mouseX = 0;
+var mouseY = 0;
+var offX = 0;
+var offY = 0;
+
+function startDrag(element) {
+ dragElement = element;
+ offX = mouseX - dragElement.offsetLeft;
+ offY = mouseY - dragElement.offsetTop;
+}
+
+function doDrag(eve) {
+ mouseX = eve.pageX;
+ mouseY = eve.pageY;
+
+ if (dragElement) {
+ dragElement.style.left = (mouseX - offX) + 'px';
+ dragElement.style.top = (mouseY - offY) + 'px';
+ }
+}
+
+function stopDrag(eve) {
+ dragElement = null;
+}
+
+onunload = function() {
+ if (connection) {
+ connection.disconnect();
+ }
+}
+
+onmousemove = doDrag;
+onmouseup = stopDrag;
+