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 $('#vCard').click(function () {
65 $('#vCard_container').slideUp("normal");
68 $('#vCard_container').slideDown("normal");
71 function _cbVCard(e) {
72 var jid = e.getAttribute('from');
73 if (roster[jid2id(jid)].vCard == "") {
74 roster[jid2id(jid)].vCard = e;
76 if ($('#vCard_container').is(':visible')) {
77 $('#vCard_container').hide();
79 $('#vCard_container').empty();
80 $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
85 function _cbOwnVCard(e) {
86 $('#ownInfo').empty();
87 $('#ownInfo').load('vCard.html #vCard', function() {
88 $('#ownInfo').find('div').each(function() {
89 $(this).attr('id', 'own' + $(this).attr('id'));
91 var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
92 for (var i=0; i<easy_cases.length; i++) {
93 var text = e.getElementsByTagName(easy_cases[i])[0];
95 text = Strophe.getText(text);
96 $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
99 var avatar = e.getElementsByTagName('PHOTO')[0];
101 var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
102 var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
104 $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
106 $(e).find('TEL:has(HOME)').each(function() {
107 $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
109 $(e).find('TEL:has(WORK)').each(function() {
110 $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
112 $(e).find('EMAIL:has(HOME)').each(function() {
113 $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
115 $(e).find('EMAIL:has(WORK)').each(function() {
116 $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
118 $('#ownInfo').show();
124 function getVCard(jid) {
125 var id = 'getvCard'+jid2id(jid);
126 if (roster[jid2id(jid)].vCard == "") {
127 var vCardiq = $iq({'to':jid,
130 ).c('vCard', {'xmlns':NS_VCARD});
131 connection.addHandler(_cbVCard, null, 'iq', 'result', id);
132 connection.send(vCardiq.tree());
134 _cbVCard(roster[jid2id(jid)].vCard);
138 function getOwnInfo() {
139 var id = 'getvCard'+jid2id(connection.jid);
140 var vCardiq = $iq({'to': Strophe.getBareJidFromJid(connection.jid),
143 ).c('vCard', {'xmlns':NS_VCARD});
144 connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
145 connection.send(vCardiq.tree());
148 function addFriend(jid, nick) {
149 roster[jid2id(jid)] = new Buddy(nick, jid);
150 $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
151 $('#' + jid2id(jid) + ' img').click(function() {
154 $('#' + jid2id(jid) + ' a').click(function() {
155 var id = jid2id(jid) + 'Chat';
156 if ($('#' + id).length <= 0) {
164 function _cbRoster(e) {
165 var query = e.getElementsByTagName('query')[0];
166 var entries = query.getElementsByTagName('item');
167 for (var item=0; item<entries.length; item++) {
168 nick = entries[item].getAttribute('name');
170 nick = entries[item].getAttribute('jid').split('@')[0];
172 addFriend(entries[item].getAttribute('jid'), nick);
174 connection.addHandler(_cbPEP, NS_PEP, 'message');
175 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();
176 connection.send(initialPresence.tree());
178 $('#friends').show();
183 function getRoster() {
184 var id = connection.getUniqueId('roster');
186 var rosteriq = $iq({'id':id,
188 ).c('query', {'xmlns':Strophe.NS.ROSTER});
190 connection.addHandler(_cbRoster, null, 'iq', 'result', id);
191 connection.send(rosteriq.tree());
194 function _cbDisco(e) {
195 var id = e.getAttribute('id');
196 var jid = e.getAttribute('from');
198 var response = $iq({id: id, type: 'result', to: jid});
199 var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
200 query.c('identity', {category: 'client', type: 'web', name: appName}).up();
201 for (var i = 0; i < features.length; i++) {
202 query.c('feature', {'var': features[i]}).up();
204 connection.send(response.tree());
209 function sendMessage(form, to) {
210 if (form.text.value) {
211 var id = jid2id(to) + 'Chat';
212 var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
213 connection.send(message.tree());
214 if ($('#' + id + ' p *').length > 0) {
215 $('#' + id + ' p').append('<br/>');
217 $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
218 $('#' + id + ' p').append(form.text.value);
219 form.text.value = '';
225 function createBubble(jid) {
226 var id = jid2id(jid) + 'Chat';
227 $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
228 '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
229 $('#' + 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>')
230 $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
231 $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
235 function _cbMessage(msg) {
236 if ($(msg).attr('type') != 'chat')
238 var jid = $(msg).attr('from');
239 var id = jid2id(jid) + 'Chat';
240 var body = $(msg).find('body:first').text();
242 body = body.replace(/%0A/g, '<br/>');
243 body = body.replace(/%3C/g, '<');
244 body = body.replace(/%3E/g, '>');
245 body = body.replace(/%26/g, '&');
246 body = unescape(body);
247 if ($('#' + id).length <= 0) {
250 if ($('#' + id + ' p *').length > 0) {
251 $('#' + id + ' p').append('<br/>');
253 $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
254 $('#' + id + ' p').append(body);
262 S += 'client/web//' + appName + '<'
264 for (var i = 0; i < features.length; i++) {
265 S += features[i] + '<';
271 var from = e.getAttribute('from');
272 if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
273 return true; // Drop own PEP events
275 var items = e.getElementsByTagName('items')[0];
277 if (items.getAttribute('node') == NS_TUNE) {
278 var tune = new Tune();
279 if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
280 tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
281 tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
282 tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
283 tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
284 tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
285 tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
286 tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
287 if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
288 $('#'+jid2id(from)+' .tune').empty();
289 $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
291 $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
294 if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
295 $('#'+jid2id(from)+' .tune').empty();
298 roster[jid2id(from)].tune = tune;
303 function onConnect(status) {
304 if (status == Strophe.Status.CONNECTING) {
305 log('Strophe is connecting.');
306 } else if (status == Strophe.Status.CONNFAIL) {
307 log('Strophe failed to connect.');
309 } else if (status == Strophe.Status.DISCONNECTING) {
310 log('Strophe is disconnecting.');
311 } else if (status == Strophe.Status.DISCONNECTED) {
312 log('Strophe is disconnected.');
314 } else if (status == Strophe.Status.AUTHFAIL) {
315 log('Authentication failed');
317 connection.disconnect();
319 } else if (status == Strophe.Status.CONNECTED) {
320 log('Strophe is connected.');
323 connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
324 connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
328 function showConnect() {
330 var pass = $('#pass');
331 var button = $('#connect').get(0);
334 $('#ownInfo').hide();
335 $('#vCard_container').empty();
336 $('#friends').hide();
341 button.value = 'connect';
345 function showDisconnect() {
347 var pass = $('#pass');
348 var button = $('#connect').get(0);
350 button.value = 'disconnect';
357 $(document).ready(function () {
358 connection = new Strophe.Connection(BOSH_SERVICE);
359 connection.rawInput = rawInput;
360 connection.rawOutput = rawOutput;
362 $("#log_toggle").click(function () {
366 $('#cred').bind('submit', function () {
367 var button = $('#connect').get(0);
369 var pass = $('#pass');
370 localJID = jid.get(0).value;
372 if (button.value == 'connect') {
374 connection.connect(localJID,
378 connection.disconnect();
385 var dragElement = null;
391 function startDrag(element) {
392 dragElement = element;
393 offX = mouseX - dragElement.offsetLeft;
394 offY = mouseY - dragElement.offsetTop;
397 function doDrag(eve) {
402 dragElement.style.left = (mouseX - offX) + 'px';
403 dragElement.style.top = (mouseY - offY) + 'px';
407 function stopDrag(eve) {
411 onunload = function() {
413 connection.disconnect();
417 onmousemove = doDrag;
418 onmouseup = stopDrag;