]> git.babelmonkeys.de Git - socialXMPP.git/blob - scripts/basic.js
Show tune in vCard
[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     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>')
68     }
69
70     $('#vCard').click(function () {
71         $('#box-overlay').hide();
72         $('#vCard_container').slideUp("normal");
73         roster[jid2id(jid)].visible = false;
74       });
75
76     $('#box-overlay').show();
77     $('#vCard_container').slideDown("normal");
78     roster[jid2id(jid)].visible = true;
79 }
80
81 function _cbVCard(e) {
82     var jid = e.getAttribute('from');
83     if (roster[jid2id(jid)].vCard == "") {
84         roster[jid2id(jid)].vCard = e;
85     }
86     if ($('#vCard_container').is(':visible')) {
87         $('#vCard_container').hide();
88     }
89     $('#vCard_container').empty();
90     $('#vCard_container').load('vCard.html #vCard', function() {populateVCard(e, jid);});
91
92     return false;
93 }
94
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];
101         if (text) {
102             text = Strophe.getText(text);
103             $('#own'+easy_cases[i]).append(Strophe.xmlTextNode(text));
104         }
105     }
106     var avatar = e.getElementsByTagName('PHOTO')[0];
107     if (avatar) {
108         var mime = Strophe.getText(avatar.getElementsByTagName('TYPE')[0]);
109         var binval = Strophe.getText(avatar.getElementsByTagName('BINVAL')[0]);
110
111         $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
112     }
113     $(e).find('TEL:has(HOME)').each(function() {
114         $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
115     });
116     $(e).find('TEL:has(WORK)').each(function() {
117         $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
118     });
119     $(e).find('EMAIL:has(HOME)').each(function() {
120         $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
121     });
122     $(e).find('EMAIL:has(WORK)').each(function() {
123         $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
124     });
125     $('#ownInfo').show();
126     });
127
128     return false;
129 }
130
131 function getVCard(jid) {
132     var id = connection.getUniqueId('vCardGet');
133     if (roster[jid2id(jid)].vCard == "") {
134         var vCardiq = $iq({'to':jid,
135                         'id':id,
136                         'type':'get'}
137                 ).c('vCard', {'xmlns':NS_VCARD});
138         connection.addHandler(_cbVCard, null, 'iq', 'result', id);
139         connection.send(vCardiq.tree());
140     } else {
141         _cbVCard(roster[jid2id(jid)].vCard);
142     }
143 }
144
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());
151 }
152
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() {
157         getVCard(jid);
158     });
159     $('#' + jid2id(jid) + ' a').click(function() {
160         var id = jid2id(jid) + 'Chat';
161         if ($('#' + id).length <= 0) {
162             createBubble(jid);
163         }
164         $('#' + id).show();
165     });
166
167 }
168
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');
174         if (!nick) {
175             nick = entries[item].getAttribute('jid').split('@')[0];
176         }
177         addFriend(entries[item].getAttribute('jid'), nick);
178     }
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());
182
183     $('#friends').show();
184
185     return false;
186 }
187
188 function getRoster() {
189     var id = connection.getUniqueId('roster');
190
191     var rosteriq = $iq({'id':id,
192                         'type':'get'}
193         ).c('query', {'xmlns':Strophe.NS.ROSTER});
194
195     connection.addHandler(_cbRoster, null, 'iq', 'result', id);
196     connection.send(rosteriq.tree());
197 }
198
199 function _cbDisco(e) {
200     var id = e.getAttribute('id');
201     var jid = e.getAttribute('from');
202
203     if (jid) {
204         var response = $iq({id: id, type: 'result', to: jid});
205     } else {
206         var response = $iq({id: id, type: 'result'});
207     }
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();
212     }
213     connection.send(response.tree());
214
215     return true;
216 }
217
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/>');
225         }
226         $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
227         $('#' + id + ' p').append(form.text.value);
228         form.text.value = '';
229     }
230
231     return false;
232 }
233
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);
241 }
242
243
244 function _cbMessage(msg) {
245     if ($(msg).attr('type') != 'chat')
246         return true;
247     var jid = $(msg).attr('from');
248     var id = jid2id(jid) + 'Chat';
249     var body = $(msg).find('body:first').text();
250     body = escape(body);
251     body = body.replace(/%0A/g, '<br/>');
252     body = body.replace(/%3C/g, '&lt;');
253     body = body.replace(/%3E/g, '&gt;');
254     body = body.replace(/%26/g, '&amp;');
255     body = unescape(body);
256     if ($('#' + id).length <= 0) {
257         createBubble(jid);
258     }
259     if ($('#' + id + ' p *').length > 0) {
260         $('#' + id + ' p').append('<br/>');
261     }
262     $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
263     $('#' + id + ' p').append(body);
264     $('#' + id).show();
265
266     return true;
267 }
268
269 function genCaps() {
270     var S = '';
271     S += 'client/web//' + appName + '<'
272     features.sort();
273     for (var i = 0; i < features.length; i++) {
274         S += features[i] + '<';
275     }
276     return b64_sha1(S);
277 }
278
279 function _cbPEP(e) {
280     var from = e.getAttribute('from');
281     if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
282         return true; // Drop own PEP events
283     }
284     var items = e.getElementsByTagName('items')[0];
285     // Handle Tune
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) {
298                     $('#tune').empty();
299                     $('#tune').append('<span class="vCardName">Tune: </span>');
300                     $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title +
301                                 ' by ' + tune.artist + ' from ' + tune.source));
302                 } else {
303                     $('#vCard ul').append('<li id="tune"><span class="vCardName">Tune: </span>Listening to '+ tune.title +
304                                 ' by ' + tune.artist + ' from ' + tune.source + '</li>')
305                 }
306             }
307         } else {
308             $('#tune').remove();
309         }
310         roster[jid2id(from)].tune = tune;
311     }
312     return true;
313 }
314
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.');
320         showConnect();
321     } else if (status == Strophe.Status.DISCONNECTING) {
322         log('Strophe is disconnecting.');
323     } else if (status == Strophe.Status.DISCONNECTED) {
324         log('Strophe is disconnected.');
325         showConnect();
326     } else if (status == Strophe.Status.AUTHFAIL) {
327         log('Authentication failed');
328         if (connection) {
329             connection.disconnect();
330         }
331     } else if (status == Strophe.Status.CONNECTED) {
332         log('Strophe is connected.');
333         getOwnInfo();
334         getRoster();
335         connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
336         connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
337     }
338 }
339
340 function showConnect() {
341     var jid = $('#jid');
342     var pass = $('#pass');
343     var button = $('#connect').get(0);  
344
345     $('#log').empty();
346     $('#ownInfo').empty();
347     $('#ownInfo').hide();
348     $('#vCard_container').empty();
349     $('#friends').empty();
350     $('#friends').hide();
351     $('.chat').remove();
352     $('label').show();
353     jid.show();
354     pass.show();
355     button.value = 'connect';
356     return false;
357 }
358
359 function showDisconnect() {
360     var jid = $('#jid');
361     var pass = $('#pass');
362     var button = $('#connect').get(0);  
363
364     button.value = 'disconnect';
365     pass.hide();
366     jid.hide();
367     $('label').hide();
368     return false;
369 }
370
371 $(document).ready(function () {
372     connection = new Strophe.Connection(BOSH_SERVICE);
373     connection.rawInput = rawInput;
374     connection.rawOutput = rawOutput;
375
376     $("#log_toggle").click(function () {
377         $("#log").toggle();     
378       });
379
380     $('#cred').bind('submit', function () {
381         var button = $('#connect').get(0);
382         var jid = $('#jid');
383         var pass = $('#pass');  
384         localJID = jid.get(0).value;
385         
386         if (button.value == 'connect') {
387             showDisconnect();
388             connection.connect(localJID,
389                                pass.get(0).value,
390                                onConnect);
391         } else {
392             connection.disconnect();
393         }
394         return false;
395     });
396 });
397
398 // Element moving
399 var dragElement = null;
400 var mouseX = 0;
401 var mouseY = 0;
402 var offX = 0;
403 var offY = 0;
404
405 function startDrag(element) {
406     dragElement = element;
407     offX = mouseX - dragElement.offsetLeft;
408     offY = mouseY - dragElement.offsetTop;
409 }
410
411 function doDrag(eve) {
412     mouseX = eve.pageX;
413     mouseY = eve.pageY;
414
415     if (dragElement) {
416         dragElement.style.left = (mouseX - offX) + 'px';
417         dragElement.style.top = (mouseY - offY) + 'px';
418     }
419 }
420
421 function stopDrag(eve) {
422     dragElement = null;
423 }
424
425 onunload = function() {
426     if (connection) {
427         connection.disconnect();
428     }
429 }
430
431 onmousemove = doDrag;
432 onmouseup = stopDrag;
433