]> git.babelmonkeys.de Git - socialXMPP.git/blob - scripts/basic.js
Change vCard display
[socialXMPP.git] / scripts / basic.js
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/';
6
7 var localJID = null;
8 var connection   = null;
9 var show_log     = true;
10
11 var features = new Array(NS_CAPS, NS_TUNE+'+notify', Strophe.NS.DISCO_INFO);
12 var appName = 'socialXMPP';
13
14 var roster = new Array();
15
16 function log(msg) {
17     var entry = $('<div></div>').append(document.createTextNode(msg));
18     $('#log').append(entry);
19 }
20
21 function rawInput(data) {
22     log('RECV: ' + data);
23 }
24
25 function rawOutput(data) {
26     log('SENT: ' + data);
27 }
28
29 function jid2id(jid) {
30     jid = Strophe.getBareJidFromJid(jid);
31     return jid.split('@').join('-').split('.').join('-');
32 }
33
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];
38         if (text) {
39             text = Strophe.getText(text);
40             $('#'+easy_cases[i]).append(Strophe.xmlTextNode(text));
41         }
42     }
43     var avatar = e.getElementsByTagName('PHOTO')[0];
44     if (avatar) {
45         var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
46         var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
47
48         $("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
49         $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
50     }
51     $(e).find('TEL:has(HOME)').each(function() {
52         $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
53     });
54     $(e).find('TEL:has(WORK)').each(function() {
55         $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
56     });
57     $(e).find('EMAIL:has(HOME)').each(function() {
58         $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
59     });
60     $(e).find('EMAIL:has(WORK)').each(function() {
61         $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
62     });
63
64     $('#vCard').click(function () {
65         $('#box-overlay').hide();
66         $('#vCard_container').slideUp("normal");
67       });
68
69     $('#box-overlay').show();
70     $('#vCard_container').slideDown("normal");
71 }
72
73 function _cbVCard(e) {
74     var jid = e.getAttribute('from');
75     if (roster[jid2id(jid)].vCard == "") {
76         roster[jid2id(jid)].vCard = e;
77     }
78     if ($('#vCard_container').is(':visible')) {
79         $('#vCard_container').hide();
80     }
81     $('#vCard_container').empty();
82     $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
83
84     return false;
85 }
86
87 function _cbOwnVCard(e) {
88     $('#ownInfo').empty();
89     $('#ownInfo').load('vCard.html #ownvCard', function() {
90     var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
91     for (var i=0; i<easy_cases.length; i++) {
92         var text = e.getElementsByTagName(easy_cases[i])[0];
93         if (text) {
94             text = Strophe.getText(text);
95             $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
96         }
97     }
98     var avatar = e.getElementsByTagName('PHOTO')[0];
99     if (avatar) {
100         var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
101         var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
102
103         $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
104     }
105     $(e).find('TEL:has(HOME)').each(function() {
106         $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
107     });
108     $(e).find('TEL:has(WORK)').each(function() {
109         $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
110     });
111     $(e).find('EMAIL:has(HOME)').each(function() {
112         $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
113     });
114     $(e).find('EMAIL:has(WORK)').each(function() {
115         $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
116     });
117     $('#ownInfo').show();
118     });
119
120     return false;
121 }
122
123 function getVCard(jid) {
124     var id = connection.getUniqueId('vCardGet');
125     if (roster[jid2id(jid)].vCard == "") {
126         var vCardiq = $iq({'to':jid,
127                         'id':id,
128                         'type':'get'}
129                 ).c('vCard', {'xmlns':NS_VCARD});
130         connection.addHandler(_cbVCard, null, 'iq', 'result', id);
131         connection.send(vCardiq.tree());
132     } else {
133         _cbVCard(roster[jid2id(jid)].vCard);
134     }
135 }
136
137 function getOwnInfo() {
138     var id = connection.getUniqueId('vCardGet');
139     var vCardiq = $iq({'to': Strophe.getBareJidFromJid(connection.jid),
140                 'id': id,
141                 'type': 'get'}
142         ).c('vCard', {'xmlns':NS_VCARD});
143     connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
144     connection.send(vCardiq.tree());
145 }
146 function addFriend(jid, nick) {
147     roster[jid2id(jid)] = new Buddy(nick, jid);
148     $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
149     $('#' + jid2id(jid) + ' img').click(function() {
150         getVCard(jid);
151     });
152     $('#' + jid2id(jid) + ' a').click(function() {
153         var id = jid2id(jid) + 'Chat';
154         if ($('#' + id).length <= 0) {
155             createBubble(jid);
156         }
157         $('#' + id).show();
158     });
159
160 }
161
162 function _cbRoster(e) {
163     var query = e.getElementsByTagName('query')[0];
164     var entries = query.getElementsByTagName('item');
165     for (var item=0; item<entries.length; item++) {
166         nick = entries[item].getAttribute('name');
167         if (!nick) {
168             nick = entries[item].getAttribute('jid').split('@')[0];
169         }
170         addFriend(entries[item].getAttribute('jid'), nick);
171     }
172     connection.addHandler(_cbPEP, NS_PEP, 'message');
173     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();
174     connection.send(initialPresence.tree());
175
176     $('#friends').show();
177
178     return false;
179 }
180
181 function getRoster() {
182     var id = connection.getUniqueId('roster');
183
184     var rosteriq = $iq({'id':id,
185                         'type':'get'}
186         ).c('query', {'xmlns':Strophe.NS.ROSTER});
187
188     connection.addHandler(_cbRoster, null, 'iq', 'result', id);
189     connection.send(rosteriq.tree());
190 }
191
192 function _cbDisco(e) {
193     var id = e.getAttribute('id');
194     var jid = e.getAttribute('from');
195
196     var response = $iq({id: id, type: 'result', to: jid});
197     var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
198     query.c('identity', {category: 'client', type: 'web', name: appName}).up();
199     for (var i = 0; i < features.length; i++) {
200         query.c('feature', {'var': features[i]}).up();
201     }
202     connection.send(response.tree());
203
204     return true;
205 }
206
207 function sendMessage(form, to) {
208     if (form.text.value) {
209         var id = jid2id(to) + 'Chat';
210         var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
211         connection.send(message.tree());
212         if ($('#' + id + ' p *').length > 0) {
213             $('#' + id + ' p').append('<br/>');
214         }
215         $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
216         $('#' + id + ' p').append(form.text.value);
217         form.text.value = '';
218     }
219
220     return false;
221 }
222
223 function createBubble(jid) {
224     var id = jid2id(jid) + 'Chat';
225     $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
226                     '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
227     $('#' + 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>')
228     $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
229     $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
230 }
231
232
233 function _cbMessage(msg) {
234     if ($(msg).attr('type') != 'chat')
235         return true;
236     var jid = $(msg).attr('from');
237     var id = jid2id(jid) + 'Chat';
238     var body = $(msg).find('body:first').text();
239     body = escape(body);
240     body = body.replace(/%0A/g, '<br/>');
241     body = body.replace(/%3C/g, '&lt;');
242     body = body.replace(/%3E/g, '&gt;');
243     body = body.replace(/%26/g, '&amp;');
244     body = unescape(body);
245     if ($('#' + id).length <= 0) {
246         createBubble(jid);
247     }
248     if ($('#' + id + ' p *').length > 0) {
249         $('#' + id + ' p').append('<br/>');
250     }
251     $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
252     $('#' + id + ' p').append(body);
253     $('#' + id).show();
254
255     return true;
256 }
257
258 function genCaps() {
259     var S = '';
260     S += 'client/web//' + appName + '<'
261     features.sort();
262     for (var i = 0; i < features.length; i++) {
263         S += features[i] + '<';
264     }
265     return b64_sha1(S);
266 }
267
268 function _cbPEP(e) {
269     var from = e.getAttribute('from');
270     if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
271         return true; // Drop own PEP events
272     }
273     var items = e.getElementsByTagName('items')[0];
274     // Handle Tune
275     if (items.getAttribute('node') == NS_TUNE) {
276         var tune = new Tune();
277         if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
278             tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
279             tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
280             tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
281             tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
282             tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
283             tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
284             tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
285             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
286                 $('#'+jid2id(from)+' .tune').empty();
287                 $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
288             } else {
289                 $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
290             }
291         } else {
292             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
293                 $('#'+jid2id(from)+' .tune').empty();
294             }
295         }
296         roster[jid2id(from)].tune = tune;
297     }
298     return true;
299 }
300
301 function onConnect(status) {
302     if (status == Strophe.Status.CONNECTING) {
303         log('Strophe is connecting.');
304     } else if (status == Strophe.Status.CONNFAIL) {
305         log('Strophe failed to connect.');
306         showConnect();
307     } else if (status == Strophe.Status.DISCONNECTING) {
308         log('Strophe is disconnecting.');
309     } else if (status == Strophe.Status.DISCONNECTED) {
310         log('Strophe is disconnected.');
311         showConnect();
312     } else if (status == Strophe.Status.AUTHFAIL) {
313         log('Authentication failed');
314         if (connection) {
315             connection.disconnect();
316         }
317     } else if (status == Strophe.Status.CONNECTED) {
318         log('Strophe is connected.');
319         getOwnInfo();
320         getRoster();
321         connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
322         connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
323     }
324 }
325
326 function showConnect() {
327     var jid = $('#jid');
328     var pass = $('#pass');
329     var button = $('#connect').get(0);  
330
331     $('#log').empty();
332     $('#ownInfo').empty();
333     $('#ownInfo').hide();
334     $('#vCard_container').empty();
335     $('#friends').empty();
336     $('#friends').hide();
337     $('.chat').remove();
338     $('label').show();
339     jid.show();
340     pass.show();
341     button.value = 'connect';
342     return false;
343 }
344
345 function showDisconnect() {
346     var jid = $('#jid');
347     var pass = $('#pass');
348     var button = $('#connect').get(0);  
349
350     button.value = 'disconnect';
351     pass.hide();
352     jid.hide();
353     $('label').hide();
354     return false;
355 }
356
357 $(document).ready(function () {
358     connection = new Strophe.Connection(BOSH_SERVICE);
359     connection.rawInput = rawInput;
360     connection.rawOutput = rawOutput;
361
362     $("#log_toggle").click(function () {
363         $("#log").toggle();     
364       });
365
366     $('#cred').bind('submit', function () {
367         var button = $('#connect').get(0);
368         var jid = $('#jid');
369         var pass = $('#pass');  
370         localJID = jid.get(0).value;
371         
372         if (button.value == 'connect') {
373             showDisconnect();
374             connection.connect(localJID,
375                                pass.get(0).value,
376                                onConnect);
377         } else {
378             connection.disconnect();
379         }
380         return false;
381     });
382 });
383
384 // Element moving
385 var dragElement = null;
386 var mouseX = 0;
387 var mouseY = 0;
388 var offX = 0;
389 var offY = 0;
390
391 function startDrag(element) {
392     dragElement = element;
393     offX = mouseX - dragElement.offsetLeft;
394     offY = mouseY - dragElement.offsetTop;
395 }
396
397 function doDrag(eve) {
398     mouseX = eve.pageX;
399     mouseY = eve.pageY;
400
401     if (dragElement) {
402         dragElement.style.left = (mouseX - offX) + 'px';
403         dragElement.style.top = (mouseY - offY) + 'px';
404     }
405 }
406
407 function stopDrag(eve) {
408     dragElement = null;
409 }
410
411 onunload = function() {
412     if (connection) {
413         connection.disconnect();
414     }
415 }
416
417 onmousemove = doDrag;
418 onmouseup = stopDrag;
419