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