+function _cbDisco(e) {
+ var id = e.getAttribute('id');
+ var jid = e.getAttribute('from');
+
+ var response = $iq({id: id, type: 'result', to: jid});
+ var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO})
+ query.c('identity', {category: 'client', type: 'web', name: appName}).up();
+ for (var i = 0; i < features.length; i++) {
+ query.c('feature', {'var': features[i]}).up();
+ }
+ connection.send(response.tree());
+
+ return true;
+}
+
+function sendMessage(form, to) {
+ if (form.text.value) {
+ var id = jid2id(to) + 'Chat';
+ var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
+ connection.send(message.tree());
+ if ($('#' + id + ' p *').length > 0) {
+ $('#' + id + ' p').append('<br/>');
+ }
+ $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
+ $('#' + id + ' p').append(form.text.value);
+ form.text.value = '';
+ }
+
+ return false;
+}
+
+function createBubble(jid) {
+ var id = jid2id(jid) + 'Chat';
+ $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
+ '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
+ $('#' + 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>')
+ $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
+ $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
+}
+
+
+function _cbMessage(msg) {
+ if ($(msg).attr('type') != 'chat')
+ return true;
+ var jid = $(msg).attr('from');
+ var id = jid2id(jid) + 'Chat';
+ var body = $(msg).find('body:first').text();
+ body = escape(body);
+ body = body.replace(/%0A/g, '<br/>');
+ body = body.replace(/%3C/g, '<');
+ body = body.replace(/%3E/g, '>');
+ body = body.replace(/%26/g, '&');
+ body = unescape(body);
+ if ($('#' + id).length <= 0) {
+ createBubble(jid);
+ }
+ if ($('#' + id + ' p *').length > 0) {
+ $('#' + id + ' p').append('<br/>');
+ }
+ $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
+ $('#' + id + ' p').append(body);
+ $('#' + id).show();
+
+ return true;
+}
+
+function genCaps() {
+ var S = '';
+ S += 'client/web//' + appName + '<'
+ features.sort();
+ for (var i = 0; i < features.length; i++) {
+ S += features[i] + '<';
+ }
+ return b64_sha1(S);
+}
+
+function _cbPEP(e) {
+ var from = e.getAttribute('from');
+ if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) {
+ return true; // Drop own PEP events
+ }
+ var items = e.getElementsByTagName('items')[0];
+ // Handle Tune
+ if (items.getAttribute('node') == NS_TUNE) {
+ var tune = new Tune();
+ if (items.getElementsByTagName('tune')[0].childNodes.length > 0) {
+ tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]);
+ tune.length= Strophe.getText(items.getElementsByTagName('length')[0]);
+ tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]);
+ tune.source= Strophe.getText(items.getElementsByTagName('source')[0]);
+ tune.title= Strophe.getText(items.getElementsByTagName('title')[0]);
+ tune.track= Strophe.getText(items.getElementsByTagName('track')[0]);
+ tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]);
+ if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
+ $('#'+jid2id(from)+' .tune').empty();
+ $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source));
+ } else {
+ $('#'+jid2id(from)).append('<br /><span class="tune">Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + '</span>');
+ }
+ } else {
+ if ( $('#'+jid2id(from)+' .tune').length > 0 ) {
+ $('#'+jid2id(from)+' .tune').empty();
+ }
+ }
+ roster[jid2id(from)].tune = tune;
+ }
+ return true;
+}
+