2 var BOSH_SERVICE = '/http-bind/';
4 Strophe.addNamespace('VCARD', 'vcard-temp');
5 Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
6 Strophe.addNamespace('PEP', 'http://jabber.org/protocol/pubsub#event');
7 Strophe.addNamespace('TUNE', 'http://jabber.org/protocol/tune');
10 var connection = null;
13 var features = [Strophe.NS.CAPS, Strophe.NS.TUNE + '+notify', Strophe.NS.DISCO_INFO];
14 var appName = 'socialXMPP';
19 var entry = $('<div></div>').append(document.createTextNode(msg));
20 $('#log').append(entry);
23 function rawInput(data) {
27 function rawOutput(data) {
31 function jid2id(jid) {
32 jid = Strophe.getBareJidFromJid(jid);
33 return jid.split('@').join('-').split('.').join('-');
38 S += 'client/web//' + appName + '<';
40 for (i = 0; i < features.length; i++) {
41 S += features[i] + '<';
46 function populateVCard(e, jid) {
47 var easy_cases = ['FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'], i, text, avatar, mime, binval;
48 for (i = 0; i < easy_cases.length; i++) {
49 text = e.getElementsByTagName(easy_cases[i])[0];
51 text = Strophe.getText(text);
52 $('#' + easy_cases[i]).append(Strophe.xmlTextNode(text));
55 avatar = e.getElementsByTagName('PHOTO')[0];
57 mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
58 binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
60 $("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
61 $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
63 $(e).find('TEL:has(HOME)').each(function() {
64 $('#TELHOME').append(' ');
65 $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
67 $(e).find('TEL:has(WORK)').each(function() {
68 $('#TELWORK').append(' ');
69 $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
71 $(e).find('EMAIL:has(HOME)').each(function() {
72 $('#EMAILHOME').append(' ');
73 $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
75 $(e).find('EMAIL:has(WORK)').each(function() {
76 $('#EMAILWORK').append(' ');
77 $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
80 if (!roster[jid2id(jid)].tune.isEmpty()) {
81 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+
82 roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist +
83 ' from ' + roster[jid2id(jid)].tune.source + '</li>');
86 $('#vCard').click(function () {
87 $('#vCard_container').slideUp("normal", function() {
88 $('#box-overlay').hide();
90 roster[jid2id(jid)].visible = false;
93 $('#box-overlay').show();
94 $('#vCard_container').slideDown("normal");
95 roster[jid2id(jid)].visible = true;
98 function _cbVCard(e) {
99 var jid = e.getAttribute('from');
100 if (roster[jid2id(jid)].vCard === "") {
101 roster[jid2id(jid)].vCard = e;
103 if ($('#vCard_container').is(':visible')) {
104 $('#vCard_container').hide();
106 $('#vCard_container').empty();
107 $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
112 function _cbOwnVCard(e) {
113 var easy_cases = ['FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'], i, text, avatar, mime, binval;
114 $('#ownInfo').empty();
115 $('#ownInfo').load('vCard.html #ownvCard', function() {
116 for (i = 0; i < easy_cases.length; i++) {
117 text = e.getElementsByTagName(easy_cases[i])[0];
119 text = Strophe.getText(text);
120 $('#own' + easy_cases[i]).append(Strophe.xmlTextNode(text));
123 avatar = e.getElementsByTagName('PHOTO')[0];
125 mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
126 binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
128 $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
130 $(e).find('TEL:has(HOME)').each(function() {
131 $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
133 $(e).find('TEL:has(WORK)').each(function() {
134 $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
136 $(e).find('EMAIL:has(HOME)').each(function() {
137 $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
139 $(e).find('EMAIL:has(WORK)').each(function() {
140 $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
142 $('#ownInfo').show();
148 function getVCard(jid) {
149 var id = connection.getUniqueId('vCardGet'), vCardiq;
150 if (roster[jid2id(jid)].vCard === "") {
151 vCardiq = $iq({'to':jid,
154 ).c('vCard', {'xmlns': Strophe.NS.VCARD});
155 connection.addHandler(_cbVCard, null, 'iq', 'result', id);
156 connection.send(vCardiq.tree());
158 _cbVCard(roster[jid2id(jid)].vCard);
162 function getOwnInfo() {
163 var id = connection.getUniqueId('vCardGet'),
164 vCardiq = $iq({'id': id, 'type': 'get'}
165 ).c('vCard', {'xmlns': Strophe.NS.VCARD});
166 connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
167 connection.send(vCardiq.tree());
170 function createBubble(jid) {
171 var id = jid2id(jid) + 'Chat';
172 $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
173 '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
174 $('#' + 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>');
175 if ($('#' + jid2id(jid)).length > 0) {
176 $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
177 $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
181 function addFriend(jid, nick) {
182 roster[jid2id(jid)] = new Buddy(nick, jid);
183 $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
184 $('#' + jid2id(jid) + ' img').click(function() {
187 $('#' + jid2id(jid) + ' a').click(function() {
188 var id = jid2id(jid) + 'Chat';
189 if ($('#' + id).length <= 0) {
197 function handleTune(jid, tuneXML) {
198 var tune = new Tune();
199 if (tuneXML.childNodes.length > 0) {
200 tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]);
201 tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]);
202 tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]);
203 tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]);
204 tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]);
205 tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]);
206 tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]);
207 if (roster[jid2id(jid)].visible === true) {
208 if ( $('#tune').length > 0) {
210 $('#tune').append('<span class="vCardName">Tune: </span>');
211 $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
212 ' by ' + tune.artist + ' from ' + tune.source));
214 $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
215 ' by ' + tune.artist + ' from ' + tune.source + '</li>');
221 roster[jid2id(jid)].tune = tune;
225 var from = e.getAttribute('from'), items;
226 if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
227 return true; // Drop own PEP events
229 items = e.getElementsByTagName('items')[0];
231 if (items.getAttribute('node') == Strophe.NS.TUNE) {
232 handleTune(from, items.getElementsByTagName('tune')[0]);
237 function _cbRoster(e) {
238 var query = e.getElementsByTagName('query')[0],
239 entries = query.getElementsByTagName('item'), item, nick, initialPresence;
240 for (item = 0; item < entries.length; item++) {
241 nick = entries[item].getAttribute('name');
243 nick = entries[item].getAttribute('jid').split('@')[0];
245 addFriend(entries[item].getAttribute('jid'), nick);
247 connection.addHandler(_cbPEP, Strophe.NS.PEP, 'message');
248 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: Strophe.NS.CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
249 connection.send(initialPresence.tree());
251 $('#friends').css('display', 'table');
256 function getRoster() {
258 id = connection.getUniqueId('roster');
260 rosteriq = $iq({'id':id,
262 ).c('query', {'xmlns':Strophe.NS.ROSTER});
264 connection.addHandler(_cbRoster, null, 'iq', 'result', id);
265 connection.send(rosteriq.tree());
268 function _cbDisco(e) {
269 var i, id, jid, response, query, response;
270 id = e.getAttribute('id');
271 jid = e.getAttribute('from');
274 response = $iq({id: id, type: 'result', to: jid});
276 response = $iq({id: id, type: 'result'});
278 query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO});
279 query.c('identity', {category: 'client', type: 'web', name: appName}).up();
280 for (i = 0; i < features.length; i++) {
281 query.c('feature', {'var': features[i]}).up();
283 connection.send(response.tree());
288 function sendMessage(form, to) {
290 if (form.text.value) {
291 id = jid2id(to) + 'Chat';
292 message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
293 connection.send(message.tree());
294 if ($('#' + id + ' p *').length > 0) {
295 $('#' + id + ' p').append('<br/>');
297 $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
298 $('#' + id + ' p').append(form.text.value);
299 form.text.value = '';
305 function _cbMessage(msg) {
307 if ($(msg).attr('type') != 'chat') {
310 jid = $(msg).attr('from');
311 id = jid2id(jid) + 'Chat';
312 body = $(msg).find('body:first').text();
314 body = body.replace(/%0A/g, '<br/>');
315 body = body.replace(/%3C/g, '<');
316 body = body.replace(/%3E/g, '>');
317 body = body.replace(/%26/g, '&');
318 body = unescape(body);
319 if ($('#' + id).length <= 0) {
322 if ($('#' + id + ' p *').length > 0) {
323 $('#' + id + ' p').append('<br/>');
325 $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
326 $('#' + id + ' p').append(body);
332 function showConnect() {
333 var jid, pass, button;
336 button = $('#connect').get(0);
339 $('#ownInfo').empty();
340 $('#ownInfo').hide();
341 $('#vCard_container').empty();
342 $('#friends').empty();
343 $('#friends').hide();
348 button.value = 'connect';
352 function onConnect(status) {
353 if (status == Strophe.Status.CONNECTING) {
354 log('Strophe is connecting.');
355 } else if (status == Strophe.Status.CONNFAIL) {
356 log('Strophe failed to connect.');
358 } else if (status == Strophe.Status.DISCONNECTING) {
359 log('Strophe is disconnecting.');
360 } else if (status == Strophe.Status.DISCONNECTED) {
361 log('Strophe is disconnected.');
363 } else if (status == Strophe.Status.AUTHFAIL) {
364 log('Authentication failed');
366 connection.disconnect();
368 } else if (status == Strophe.Status.CONNECTED) {
369 log('Strophe is connected.');
372 connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
373 connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
377 function showDisconnect() {
378 var jid, pass, button;
381 button = $('#connect').get(0);
383 button.value = 'disconnect';
390 $(document).ready(function () {
391 var button, jid, pass;
393 $('#log_container').show();
395 connection = new Strophe.Connection(BOSH_SERVICE);
396 connection.rawInput = rawInput;
397 connection.rawOutput = rawOutput;
399 $("#log_toggle").click(function () {
403 $('#cred').bind('submit', function () {
404 button = $('#connect').get(0);
407 localJID = jid.get(0).value;
409 if (button.value == 'connect') {
411 connection.connect(localJID,
415 connection.disconnect();
422 var dragElement = null;
428 function startDrag(element) {
429 dragElement = element;
430 offX = mouseX - dragElement.offsetLeft;
431 offY = mouseY - dragElement.offsetTop;
434 function doDrag(eve) {
439 dragElement.style.left = (mouseX - offX) + 'px';
440 dragElement.style.top = (mouseY - offY) + 'px';
444 function stopDrag(eve) {
448 onunload = function() {
450 connection.disconnect();
454 onmousemove = doDrag;
455 onmouseup = stopDrag;