1 var NS_VCARD = 'vcard-temp';
2 var NS_CAPS= 'http://jabber.org/protocol/caps';
3 var NS_PEP = 'http://jabber.org/protocol/pubsub#event';
4 var NS_TUNE = 'http://jabber.org/protocol/tune';
5 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
11 var features = new Array(NS_CAPS, NS_TUNE+'+notify', Strophe.NS.DISCO_INFO);
12 var appName = 'socialXMPP';
14 var roster = new Array();
17 var entry = $('<div></div>').append(document.createTextNode(msg));
18 $('#log').append(entry);
21 function rawInput(data) {
25 function rawOutput(data) {
29 function jid2id(jid) {
30 jid = Strophe.getBareJidFromJid(jid);
31 return jid.split('@').join('-').split('.').join('-');
34 function populateVCard(e, jid) {
35 var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
36 for (var i=0; i<easy_cases.length; i++) {
37 var text = e.getElementsByTagName(easy_cases[i])[0];
39 text = Strophe.getText(text);
40 $('#'+easy_cases[i]).append(Strophe.xmlTextNode(text));
43 var avatar = e.getElementsByTagName('PHOTO')[0];
45 var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
46 var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
48 $("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
49 $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
51 $(e).find('TEL:has(HOME)').each(function() {
52 $('#TELHOME').append(' ');
53 $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
55 $(e).find('TEL:has(WORK)').each(function() {
56 $('#TELWORK').append(' ');
57 $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
59 $(e).find('EMAIL:has(HOME)').each(function() {
60 $('#EMAILHOME').append(' ');
61 $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
63 $(e).find('EMAIL:has(WORK)').each(function() {
64 $('#EMAILWORK').append(' ');
65 $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
68 if (!roster[jid2id(jid)].tune.isEmpty()) {
69 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+
70 roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist +
71 ' from ' + roster[jid2id(jid)].tune.source + '</li>')
74 $('#vCard').click(function () {
75 $('#vCard_container').slideUp("normal", function() {
76 $('#box-overlay').hide();
78 roster[jid2id(jid)].visible = false;
81 $('#box-overlay').show();
82 $('#vCard_container').slideDown("normal");
83 roster[jid2id(jid)].visible = true;
86 function _cbVCard(e) {
87 var jid = e.getAttribute('from');
88 if (roster[jid2id(jid)].vCard == "") {
89 roster[jid2id(jid)].vCard = e;
91 if ($('#vCard_container').is(':visible')) {
92 $('#vCard_container').hide();
94 $('#vCard_container').empty();
95 $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
100 function _cbOwnVCard(e) {
101 $('#ownInfo').empty();
102 $('#ownInfo').load('vCard.html #ownvCard', function() {
103 var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
104 for (var i=0; i<easy_cases.length; i++) {
105 var text = e.getElementsByTagName(easy_cases[i])[0];
107 text = Strophe.getText(text);
108 $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
111 var avatar = e.getElementsByTagName('PHOTO')[0];
113 var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
114 var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
116 $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
118 $(e).find('TEL:has(HOME)').each(function() {
119 $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
121 $(e).find('TEL:has(WORK)').each(function() {
122 $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
124 $(e).find('EMAIL:has(HOME)').each(function() {
125 $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
127 $(e).find('EMAIL:has(WORK)').each(function() {
128 $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
130 $('#ownInfo').show();
136 function getVCard(jid) {
137 var id = connection.getUniqueId('vCardGet');
138 if (roster[jid2id(jid)].vCard == "") {
139 var vCardiq = $iq({'to':jid,
142 ).c('vCard', {'xmlns':NS_VCARD});
143 connection.addHandler(_cbVCard, null, 'iq', 'result', id);
144 connection.send(vCardiq.tree());
146 _cbVCard(roster[jid2id(jid)].vCard);
150 function getOwnInfo() {
151 var id = connection.getUniqueId('vCardGet');
152 var vCardiq = $iq({'id': id, 'type': 'get'}
153 ).c('vCard', {'xmlns':NS_VCARD});
154 connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
155 connection.send(vCardiq.tree());
158 function addFriend(jid, nick) {
159 roster[jid2id(jid)] = new Buddy(nick, jid);
160 $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
161 $('#' + jid2id(jid) + ' img').click(function() {
164 $('#' + jid2id(jid) + ' a').click(function() {
165 var id = jid2id(jid) + 'Chat';
166 if ($('#' + id).length <= 0) {
174 function _cbRoster(e) {
175 var query = e.getElementsByTagName('query')[0];
176 var entries = query.getElementsByTagName('item');
177 for (var item=0; item<entries.length; item++) {
178 nick = entries[item].getAttribute('name');
180 nick = entries[item].getAttribute('jid').split('@')[0];
182 addFriend(entries[item].getAttribute('jid'), nick);
184 connection.addHandler(_cbPEP, NS_PEP, 'message');
185 var initialPresence = $pres().c('show').t('online').up().c('status').t('Hy, I am an socialXMPP instance').up().c('priority').t('1').up().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
186 connection.send(initialPresence.tree());
188 $('#friends').show();
193 function getRoster() {
194 var id = connection.getUniqueId('roster');
196 var rosteriq = $iq({'id':id,
198 ).c('query', {'xmlns':Strophe.NS.ROSTER});
200 connection.addHandler(_cbRoster, null, 'iq', 'result', id);
201 connection.send(rosteriq.tree());
204 function _cbDisco(e) {
205 var id = e.getAttribute('id');
206 var jid = e.getAttribute('from');
209 var response = $iq({id: id, type: 'result', to: jid});
211 var response = $iq({id: id, type: 'result'});
213 var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
214 query.c('identity', {category: 'client', type: 'web', name: appName}).up();
215 for (var i = 0; i < features.length; i++) {
216 query.c('feature', {'var': features[i]}).up();
218 connection.send(response.tree());
223 function sendMessage(form, to) {
224 if (form.text.value) {
225 var id = jid2id(to) + 'Chat';
226 var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
227 connection.send(message.tree());
228 if ($('#' + id + ' p *').length > 0) {
229 $('#' + id + ' p').append('<br/>');
231 $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
232 $('#' + id + ' p').append(form.text.value);
233 form.text.value = '';
239 function createBubble(jid) {
240 var id = jid2id(jid) + 'Chat';
241 $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
242 '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
243 $('#' + id).append('<form id="' + id + 'Form" class="chatForm" onsubmit="return sendMessage(this, \'' + jid + '\');" action="#"><input type="text" name="text" id="' + id + 'Text" class="chatForm" /></form>')
244 $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
245 $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
249 function _cbMessage(msg) {
250 if ($(msg).attr('type') != 'chat')
252 var jid = $(msg).attr('from');
253 var id = jid2id(jid) + 'Chat';
254 var body = $(msg).find('body:first').text();
256 body = body.replace(/%0A/g, '<br/>');
257 body = body.replace(/%3C/g, '<');
258 body = body.replace(/%3E/g, '>');
259 body = body.replace(/%26/g, '&');
260 body = unescape(body);
261 if ($('#' + id).length <= 0) {
264 if ($('#' + id + ' p *').length > 0) {
265 $('#' + id + ' p').append('<br/>');
267 $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
268 $('#' + id + ' p').append(body);
276 S += 'client/web//' + appName + '<'
278 for (var i = 0; i < features.length; i++) {
279 S += features[i] + '<';
284 function handleTune(jid, tuneXML) {
285 var tune = new Tune();
286 if (tuneXML.childNodes.length > 0) {
287 tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]);
288 tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]);
289 tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]);
290 tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]);
291 tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]);
292 tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]);
293 tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]);
294 if (roster[jid2id(jid)].visible == true) {
295 if ( $('#tune').length > 0) {
297 $('#tune').append('<span class="vCardName">Tune: </span>');
298 $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
299 ' by ' + tune.artist + ' from ' + tune.source));
301 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
302 ' by ' + tune.artist + ' from ' + tune.source + '</li>')
308 roster[jid2id(jid)].tune = tune;
312 var from = e.getAttribute('from');
313 if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
314 return true; // Drop own PEP events
316 var items = e.getElementsByTagName('items')[0];
318 if (items.getAttribute('node') == NS_TUNE) {
319 handleTune(from, items.getElementsByTagName('tune')[0]);
324 function onConnect(status) {
325 if (status == Strophe.Status.CONNECTING) {
326 log('Strophe is connecting.');
327 } else if (status == Strophe.Status.CONNFAIL) {
328 log('Strophe failed to connect.');
330 } else if (status == Strophe.Status.DISCONNECTING) {
331 log('Strophe is disconnecting.');
332 } else if (status == Strophe.Status.DISCONNECTED) {
333 log('Strophe is disconnected.');
335 } else if (status == Strophe.Status.AUTHFAIL) {
336 log('Authentication failed');
338 connection.disconnect();
340 } else if (status == Strophe.Status.CONNECTED) {
341 log('Strophe is connected.');
344 connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
345 connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
349 function showConnect() {
351 var pass = $('#pass');
352 var button = $('#connect').get(0);
355 $('#ownInfo').empty();
356 $('#ownInfo').hide();
357 $('#vCard_container').empty();
358 $('#friends').empty();
359 $('#friends').hide();
364 button.value = 'connect';
368 function showDisconnect() {
370 var pass = $('#pass');
371 var button = $('#connect').get(0);
373 button.value = 'disconnect';
380 $(document).ready(function () {
381 connection = new Strophe.Connection(BOSH_SERVICE);
382 connection.rawInput = rawInput;
383 connection.rawOutput = rawOutput;
385 $("#log_toggle").click(function () {
389 $('#cred').bind('submit', function () {
390 var button = $('#connect').get(0);
392 var pass = $('#pass');
393 localJID = jid.get(0).value;
395 if (button.value == 'connect') {
397 connection.connect(localJID,
401 connection.disconnect();
408 var dragElement = null;
414 function startDrag(element) {
415 dragElement = element;
416 offX = mouseX - dragElement.offsetLeft;
417 offY = mouseY - dragElement.offsetTop;
420 function doDrag(eve) {
425 dragElement.style.left = (mouseX - offX) + 'px';
426 dragElement.style.top = (mouseY - offY) + 'px';
430 function stopDrag(eve) {
434 onunload = function() {
436 connection.disconnect();
440 onmousemove = doDrag;
441 onmouseup = stopDrag;