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(Strophe.xmlTextNode($(this).find('NUMBER').text()));
54 $(e).find('TEL:has(WORK)').each(function() {
55 $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
57 $(e).find('EMAIL:has(HOME)').each(function() {
58 $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
60 $(e).find('EMAIL:has(WORK)').each(function() {
61 $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
64 if (!roster[jid2id(jid)].tune.isEmpty()) {
65 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+
66 roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist +
67 ' from ' + roster[jid2id(jid)].tune.source + '</li>')
70 $('#vCard').click(function () {
71 $('#box-overlay').hide();
72 $('#vCard_container').slideUp("normal");
73 roster[jid2id(jid)].visible = false;
76 $('#box-overlay').show();
77 $('#vCard_container').slideDown("normal");
78 roster[jid2id(jid)].visible = true;
81 function _cbVCard(e) {
82 var jid = e.getAttribute('from');
83 if (roster[jid2id(jid)].vCard == "") {
84 roster[jid2id(jid)].vCard = e;
86 if ($('#vCard_container').is(':visible')) {
87 $('#vCard_container').hide();
89 $('#vCard_container').empty();
90 $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
95 function _cbOwnVCard(e) {
96 $('#ownInfo').empty();
97 $('#ownInfo').load('vCard.html #ownvCard', function() {
98 var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
99 for (var i=0; i<easy_cases.length; i++) {
100 var text = e.getElementsByTagName(easy_cases[i])[0];
102 text = Strophe.getText(text);
103 $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
106 var avatar = e.getElementsByTagName('PHOTO')[0];
108 var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
109 var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
111 $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
113 $(e).find('TEL:has(HOME)').each(function() {
114 $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
116 $(e).find('TEL:has(WORK)').each(function() {
117 $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
119 $(e).find('EMAIL:has(HOME)').each(function() {
120 $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
122 $(e).find('EMAIL:has(WORK)').each(function() {
123 $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
125 $('#ownInfo').show();
131 function getVCard(jid) {
132 var id = connection.getUniqueId('vCardGet');
133 if (roster[jid2id(jid)].vCard == "") {
134 var vCardiq = $iq({'to':jid,
137 ).c('vCard', {'xmlns':NS_VCARD});
138 connection.addHandler(_cbVCard, null, 'iq', 'result', id);
139 connection.send(vCardiq.tree());
141 _cbVCard(roster[jid2id(jid)].vCard);
145 function getOwnInfo() {
146 var id = connection.getUniqueId('vCardGet');
147 var vCardiq = $iq({'id': id, 'type': 'get'}
148 ).c('vCard', {'xmlns':NS_VCARD});
149 connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
150 connection.send(vCardiq.tree());
153 function addFriend(jid, nick) {
154 roster[jid2id(jid)] = new Buddy(nick, jid);
155 $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
156 $('#' + jid2id(jid) + ' img').click(function() {
159 $('#' + jid2id(jid) + ' a').click(function() {
160 var id = jid2id(jid) + 'Chat';
161 if ($('#' + id).length <= 0) {
169 function _cbRoster(e) {
170 var query = e.getElementsByTagName('query')[0];
171 var entries = query.getElementsByTagName('item');
172 for (var item=0; item<entries.length; item++) {
173 nick = entries[item].getAttribute('name');
175 nick = entries[item].getAttribute('jid').split('@')[0];
177 addFriend(entries[item].getAttribute('jid'), nick);
179 connection.addHandler(_cbPEP, NS_PEP, 'message');
180 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();
181 connection.send(initialPresence.tree());
183 $('#friends').show();
188 function getRoster() {
189 var id = connection.getUniqueId('roster');
191 var rosteriq = $iq({'id':id,
193 ).c('query', {'xmlns':Strophe.NS.ROSTER});
195 connection.addHandler(_cbRoster, null, 'iq', 'result', id);
196 connection.send(rosteriq.tree());
199 function _cbDisco(e) {
200 var id = e.getAttribute('id');
201 var jid = e.getAttribute('from');
204 var response = $iq({id: id, type: 'result', to: jid});
206 var response = $iq({id: id, type: 'result'});
208 var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
209 query.c('identity', {category: 'client', type: 'web', name: appName}).up();
210 for (var i = 0; i < features.length; i++) {
211 query.c('feature', {'var': features[i]}).up();
213 connection.send(response.tree());
218 function sendMessage(form, to) {
219 if (form.text.value) {
220 var id = jid2id(to) + 'Chat';
221 var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
222 connection.send(message.tree());
223 if ($('#' + id + ' p *').length > 0) {
224 $('#' + id + ' p').append('<br/>');
226 $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
227 $('#' + id + ' p').append(form.text.value);
228 form.text.value = '';
234 function createBubble(jid) {
235 var id = jid2id(jid) + 'Chat';
236 $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
237 '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
238 $('#' + 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>')
239 $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
240 $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
244 function _cbMessage(msg) {
245 if ($(msg).attr('type') != 'chat')
247 var jid = $(msg).attr('from');
248 var id = jid2id(jid) + 'Chat';
249 var body = $(msg).find('body:first').text();
251 body = body.replace(/%0A/g, '<br/>');
252 body = body.replace(/%3C/g, '<');
253 body = body.replace(/%3E/g, '>');
254 body = body.replace(/%26/g, '&');
255 body = unescape(body);
256 if ($('#' + id).length <= 0) {
259 if ($('#' + id + ' p *').length > 0) {
260 $('#' + id + ' p').append('<br/>');
262 $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
263 $('#' + id + ' p').append(body);
271 S += 'client/web//' + appName + '<'
273 for (var i = 0; i < features.length; i++) {
274 S += features[i] + '<';
280 var from = e.getAttribute('from');
281 if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
282 return true; // Drop own PEP events
284 var items = e.getElementsByTagName('items')[0];
286 if (items.getAttribute('node') == NS_TUNE) {
287 var tune = new Tune();
288 if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
289 tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
290 tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
291 tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
292 tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
293 tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
294 tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
295 tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
296 if (roster[jid2id(from)].visible == true) {
297 if ( $('#tune').length > 0) {
299 $('#tune').append('<span class="vCardName">Tune: </span>');
300 $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
301 ' by ' + tune.artist + ' from ' + tune.source));
303 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
304 ' by ' + tune.artist + ' from ' + tune.source + '</li>')
310 roster[jid2id(from)].tune = tune;
315 function onConnect(status) {
316 if (status == Strophe.Status.CONNECTING) {
317 log('Strophe is connecting.');
318 } else if (status == Strophe.Status.CONNFAIL) {
319 log('Strophe failed to connect.');
321 } else if (status == Strophe.Status.DISCONNECTING) {
322 log('Strophe is disconnecting.');
323 } else if (status == Strophe.Status.DISCONNECTED) {
324 log('Strophe is disconnected.');
326 } else if (status == Strophe.Status.AUTHFAIL) {
327 log('Authentication failed');
329 connection.disconnect();
331 } else if (status == Strophe.Status.CONNECTED) {
332 log('Strophe is connected.');
335 connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
336 connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
340 function showConnect() {
342 var pass = $('#pass');
343 var button = $('#connect').get(0);
346 $('#ownInfo').empty();
347 $('#ownInfo').hide();
348 $('#vCard_container').empty();
349 $('#friends').empty();
350 $('#friends').hide();
355 button.value = 'connect';
359 function showDisconnect() {
361 var pass = $('#pass');
362 var button = $('#connect').get(0);
364 button.value = 'disconnect';
371 $(document).ready(function () {
372 connection = new Strophe.Connection(BOSH_SERVICE);
373 connection.rawInput = rawInput;
374 connection.rawOutput = rawOutput;
376 $("#log_toggle").click(function () {
380 $('#cred').bind('submit', function () {
381 var button = $('#connect').get(0);
383 var pass = $('#pass');
384 localJID = jid.get(0).value;
386 if (button.value == 'connect') {
388 connection.connect(localJID,
392 connection.disconnect();
399 var dragElement = null;
405 function startDrag(element) {
406 dragElement = element;
407 offX = mouseX - dragElement.offsetLeft;
408 offY = mouseY - dragElement.offsetTop;
411 function doDrag(eve) {
416 dragElement.style.left = (mouseX - offX) + 'px';
417 dragElement.style.top = (mouseY - offY) + 'px';
421 function stopDrag(eve) {
425 onunload = function() {
427 connection.disconnect();
431 onmousemove = doDrag;
432 onmouseup = stopDrag;