]> cgit.babelmonkeys.de Git - socialXMPP.git/blob - scripts/basic.js
Basic message receiving support
[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(Strophe.xmlTextNode(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('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     var jid = $(msg).attr('from');
195     var id = jid2id($(msg).attr('from')) + 'Chat';
196     var body = $(msg).find('body:first').text();
197     body = escape(body);
198     body = body.replace(/%0A/g, '<br/>');
199     body = body.replace(/%3C/g, '&lt;');
200     body = body.replace(/%3E/g, '&gt;');
201     body = body.replace(/%26/g, '&amp;');
202     body = unescape(body);
203     if ($('#' + id).length > 0) {
204         $('#' + id + ' p').append('<br/>');
205         $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
206         $('#' + id + ' p').append(body);
207     } else {
208         $('body').append('<div class="chat" id="' + id + '"><p><span class="sender">' + jid + ': </span>' + body + '</p></div>');
209     }
210
211     return true;
212 }
213
214 function genCaps() {
215     var S = '';
216     S += 'client/web//' + appName + '<'
217     features.sort();
218     for (var i = 0; i < features.length; i++) {
219         S += features[i] + '<';
220     }
221     return b64_sha1(S);
222 }
223
224 function _cbPEP(e) {
225     var from = e.getAttribute('from');
226     if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
227         return true; // Drop own PEP events
228     }
229     var items = e.getElementsByTagName('items')[0];
230     // Handle Tune
231     if (items.getAttribute('node') == NS_TUNE) {
232         var tune = new Tune();
233         if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
234             tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
235             tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
236             tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
237             tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
238             tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
239             tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
240             tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
241             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
242                 $('#'+jid2id(from)+' .tune').empty();
243                 $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
244             } else {
245                 $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
246             }
247         } else {
248             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
249                 $('#'+jid2id(from)+' .tune').empty();
250             }
251         }
252         roster[jid2id(from)].tune = tune;
253     }
254     return true;
255 }
256
257 function onConnect(status) {
258     if (status == Strophe.Status.CONNECTING) {
259         log('Strophe is connecting.');
260
261     } else if (status == Strophe.Status.CONNFAIL) {
262         log('Strophe failed to connect.');
263         showConnect();
264     } else if (status == Strophe.Status.DISCONNECTING) {
265         log('Strophe is disconnecting.');
266     } else if (status == Strophe.Status.DISCONNECTED) {
267         log('Strophe is disconnected.');
268         showConnect();
269
270     } else if (status == Strophe.Status.CONNECTED) {
271         log('Strophe is connected.');
272         getOwnInfo();
273         getRoster();
274         connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
275         connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
276     }
277 }
278
279 function showConnect() {
280     var jid = $('#jid');
281     var pass = $('#pass');
282     var button = $('#connect').get(0);  
283
284     $('#log').empty();
285     $('#ownInfo').hide();
286     $('#vCard_container').empty();
287     $('#friends').empty();
288     $('.chat').remove();
289     $('label').show();
290     jid.show();
291     pass.show();
292     button.value = 'connect';
293     return false;
294 }
295
296 function showDisconnect() {
297     var jid = $('#jid');
298     var pass = $('#pass');
299     var button = $('#connect').get(0);  
300
301     button.value = 'disconnect';
302     pass.hide();
303     jid.hide();
304     $('label').hide();
305     return false;
306 }
307
308 $(document).ready(function () {
309     connection = new Strophe.Connection(BOSH_SERVICE);
310     connection.rawInput = rawInput;
311     connection.rawOutput = rawOutput;
312
313     $("#log_toggle").click(function () {
314         $("#log").toggle();     
315       });
316       
317     $('#vCard_container').click(function () {
318         $('#vCard_container').slideUp("normal");
319       });
320
321     $('#cred').bind('submit', function () {
322         var button = $('#connect').get(0);
323         var jid = $('#jid');
324         var pass = $('#pass');  
325         
326         if (button.value == 'connect') {
327             showDisconnect();
328             connection.connect(jid.get(0).value,
329                                pass.get(0).value,
330                                onConnect);
331         } else {
332             connection.disconnect();
333             showConnect();
334         }
335         return false;
336     });
337 });