]> cgit.babelmonkeys.de Git - socialXMPP.git/blob - scripts/basic.js
934308c22fc1fc413305f34911545322d563514f
[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
133 function getOwnInfo() {
134     var id = 'getvCard'+jid2id(connection.jid);
135     var vCardiq = $iq({'to': Strophe.getBareJidFromJid(connection.jid),
136                 'id': id,
137                 'type': 'get'}
138         ).c('vCard', {'xmlns':NS_VCARD});
139     connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
140     connection.send(vCardiq.tree());
141 }
142
143 function addFriend(jid, nick) {
144     roster[jid2id(jid)] = new Buddy(nick, jid);
145     $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><span class="nick">'+nick+'</span></div>');
146     $('#'+jid2id(jid)).click(function() {
147         getVCard(jid);
148     });
149 }
150
151 function _cbRoster(e) {
152     var query = e.getElementsByTagName('query')[0];
153     var entries = query.getElementsByTagName('item');
154     for (var item=0; item<entries.length; item++) {
155         nick = entries[item].getAttribute('name');
156         if (!nick) {
157             nick = entries[item].getAttribute('jid').split('@')[0];
158         }
159         addFriend(entries[item].getAttribute('jid'), nick);
160     }
161     connection.addHandler(_cbPEP, NS_PEP, 'message');
162     var initialPresence = $pres().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
163     connection.send(initialPresence.tree());
164
165     return false;
166 }
167
168 function getRoster() {
169     var id = 'getRoster';
170
171     var rosteriq = $iq({'id':id,
172                         'type':'get'}
173         ).c('query', {'xmlns':Strophe.NS.ROSTER});
174
175     connection.addHandler(_cbRoster, null, 'iq', 'result', id);
176     connection.send(rosteriq.tree());
177 }
178
179 function _cbDisco(e) {
180     var id = e.getAttribute('id');
181     var jid = e.getAttribute('from');
182
183     var response = $iq({id: id, type: 'result', to: jid});
184     var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
185     query.c('identity', {category: 'client', type: 'web', name: appName}).up();
186     for (var i = 0; i < features.length; i++) {
187         query.c('feature', {'var': features[i]}).up();
188     }
189     connection.send(response.tree());
190
191     return true;
192 }
193
194 function genCaps() {
195     var S = '';
196     S += 'client/web//' + appName + '<'
197     features.sort();
198     for (var i = 0; i < features.length; i++) {
199         S += features[i] + '<';
200     }
201     return b64_sha1(S);
202 }
203
204 function _cbPEP(e) {
205     var from = e.getAttribute('from');
206     if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
207         return true; // Drop own PEP events
208     }
209     var items = e.getElementsByTagName('items')[0];
210     // Handle Tune
211     if (items.getAttribute('node') == NS_TUNE) {
212         var tune = new Tune();
213         if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
214             tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
215             tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
216             tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
217             tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
218             tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
219             tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
220             tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
221             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
222                 $('#'+jid2id(from)+' .tune').empty();
223                 $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
224             } else {
225                 $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
226             }
227         } else {
228             if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
229                 $('#'+jid2id(from)+' .tune').empty();
230             }
231         }
232         roster[jid2id(from)].tune = tune;
233     }
234     return true;
235 }
236
237 function onConnect(status) {
238     if (status == Strophe.Status.CONNECTING) {
239         log('Strophe is connecting.');
240
241     } else if (status == Strophe.Status.CONNFAIL) {
242         log('Strophe failed to connect.');
243         showConnect();
244     } else if (status == Strophe.Status.DISCONNECTING) {
245         log('Strophe is disconnecting.');
246     } else if (status == Strophe.Status.DISCONNECTED) {
247         log('Strophe is disconnected.');
248         showConnect();
249
250     } else if (status == Strophe.Status.CONNECTED) {
251         log('Strophe is connected.');
252         getOwnInfo();
253         getRoster();
254         connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
255     }
256 }
257
258 function showConnect() {
259     var jid = $('#jid');
260     var pass = $('#pass');
261     var button = $('#connect').get(0);  
262
263     $('#log').empty();
264     $('#ownInfo').hide();
265     $('#vCard_container').empty();
266     $('#friends').empty();
267     $('label').show();
268     jid.show();
269     pass.show();
270     button.value = 'connect';
271     return false;
272 }
273
274 function showDisconnect() {
275     var jid = $('#jid');
276     var pass = $('#pass');
277     var button = $('#connect').get(0);  
278
279     button.value = 'disconnect';
280     pass.hide();
281     jid.hide();
282     $('label').hide();
283     return false;
284 }
285
286 $(document).ready(function () {
287     connection = new Strophe.Connection(BOSH_SERVICE);
288     connection.rawInput = rawInput;
289     connection.rawOutput = rawOutput;
290
291     $("#log_toggle").click(function () {
292         $("#log").toggle();     
293       });
294       
295     $('#vCard_container').click(function () {
296         $('#vCard_container').slideUp("normal");
297       });
298
299     $('#cred').bind('submit', function () {
300         var button = $('#connect').get(0);
301         var jid = $('#jid');
302         var pass = $('#pass');  
303         
304         if (button.value == 'connect') {
305             showDisconnect();
306             connection.connect(jid.get(0).value,
307                                pass.get(0).value,
308                                onConnect);
309         } else {
310             connection.disconnect();
311             showConnect();
312         }
313         return false;
314     });
315 });