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