]> git.babelmonkeys.de Git - xmppchat.git/blobdiff - js/main.js
Switch to strophejs and jquery
[xmppchat.git] / js / main.js
diff --git a/js/main.js b/js/main.js
new file mode 100644 (file)
index 0000000..857dc25
--- /dev/null
@@ -0,0 +1,220 @@
+var BOSH_LOCATION = '/http-bind/';
+var room = 'guests@conference.babelmonkeys.de';
+var jid = 'muckl@babelmonkeys.de'
+var password = 'ooje0OjuJeekaek6';
+var connection = null;
+var nickname;
+
+function doLogin(aForm) {
+       if (!aForm.nickname.value)
+               return false;
+       try {
+               connection = new Strophe.Connection(BOSH_LOCATION);
+               connection.connect(jid, password, onConnect);
+
+               nickname = aForm.nickname.value;
+       } catch (e) {
+               alert(e);
+       } finally {
+               return false;   
+       }
+}
+
+function onConnect(status) {
+       if (status == Strophe.Status.CONNFAIL) {
+               handleError('Failed to connect');
+       } else if (status == Strophe.Status.DISCONNECTED) {
+               handleDisconnected();
+       } else if (status == Strophe.Status.CONNECTED) {
+               // Add handlers connection.addHandler(callback, namespace, stanza_name, type, id, from)
+               connection.addHandler(handleMessage, null, 'message', null, null, null);
+               connection.addHandler(handlePresence, null, 'presence', null, null, null);
+               connection.addHandler(handleIQ, null, 'iq', null, null, null);
+
+               connection.addHandler(handleIqVersion, Strophe.NS.VERSION, 'iq', null, null, null);
+               connection.addHandler(handleIqVersion, 'urn:xmpp:time', 'iq', null, null, null);
+
+               connection.send($pres().tree());
+               connection.send($pres({to: room + '/' + nickname}).tree());
+
+               // Make things (in)visible
+               $('#login').hide();
+               $('#chat').show();
+               $('#roster').show();
+               $('#entry').show();
+       }
+}
+
+function handleError(error) {
+       alert("An error occured:" + error);
+       handleDisconnected();
+}
+
+function handleDisconnected() {
+       // Make things (in)visible
+       $('#login').show();
+       $('#chat').hide();
+       $('#roster').hide();
+       $('#entry').hide();
+}
+
+function addBubble(nick) {
+       id = nick + 'Bubble';
+       if (!document.getElementById(id)) {
+               var div = '';
+               div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)" style="display: none">';
+               div += '<a href="#" onclick="' +"document.getElementById('" + id + "').style.display='none'" + '">Close</a>';
+               div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
+               div += '<form id="' + id + 'Form" class="bubbleForm" onsubmit="return sendChatMessage(this,' + "'" + nick + "');" + '" action="#">';
+               div += '<input type="text" name="text" id="' + id + 'Text" class="bubbleForm"/>';
+               div += '</form>';
+               div += '</div>';
+               $('body').append(div);
+       }
+       $('#'+id).show('slow');
+}
+
+function handleMessage(msg) {
+       var html = '';
+       var sender = Strophe.getResourceFromJid(msg.getAttribute('from'));
+       var type = msg.getAttribute('type');
+       var body = Strophe.getText(msg.getElementsByTagName('body')[0]);
+
+       html += '<div class="msg">';
+       if (sender) {
+               html += '<span class="sender">';
+               html += sender;
+               html += ':</span> ';
+               html += body + '</div>';
+       } else {
+               html += '<span class="server">';
+               html += body + '</span></div>';
+       }
+
+       if (type == 'chat') {
+               addBubble(sender);
+               $('#' + id + 'Chat').append(html);
+               document.getElementById(id + 'Chat').lastChild.scrollIntoView();
+       } else {
+               $('#chat').append(html);
+               document.getElementById('chat').lastChild.scrollIntoView();
+       }
+
+       return true;
+}
+
+function handlePresence(presence) {
+       if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room)
+               return true
+       roster_list = document.getElementById('roster_list');
+       nick = Strophe.getResourceFromJid(presence.getAttribute('from'));
+       type = presence.getAttribute('type');
+       if (type == 'unavailable') {
+               element = document.getElementById(nick);
+               roster_list.removeChild(element);
+       } else {
+               roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
+       }
+
+       return true;
+}
+
+function handleIQ(iq) {
+       var to = iq.getAttribute('to');
+       var from = iq.getAttribute('from');
+       var type = iq.getAttribute('type');
+       var content = iq.childNodes;
+
+       var reply = $iq({to: from, from: to, type: 'error'}).cnode(content).up().c('error', {type: 'cancel'}).c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'});
+
+       connection.send(reply.tree());
+
+       return true;
+}
+
+function handleIqVersion(iq) {
+       var to = iq.getAttribute('to');
+       var from = iq.getAttribute('from');
+
+       var reply = $iq({type: 'result', to: from, from: to}).c('query', {xmlns: Strophe.NS.VERSION}).c('name').t('XMPPChat').up().c('version').t('preAny').up().c('os').t(navigator.userAgent);
+
+       connection.send(reply.tree());
+
+       return true;
+}
+
+function handleIqTime(iq) {
+       var now = new Date();
+       var to = iq.getAttribute('to');
+       var from = iq.getAttribute('from');
+
+       var reply = $iq({type: 'result', from: to, to: from}).c('time', {xmlns: 'urn:xmpp:time'}).c('utc').t(now.getUTCFullYear() + '-' + now.getUTCMonth() + '-' + now.getUTCDate() + 'T' + now.getUTCHours() + ':' + now.getUTCMinutes() + ':' + now.getUTCSeconds + '.' + now.getUTCMilliseconds() + 'Z').up().c('tzo').t(now.getTimezoneOffset()/60 + ':' + now.getTimezoneOffset()%60);
+
+       connection.send(reply.tree());
+
+       return true;
+}
+
+function sendMessage(aForm) {
+       if (aForm.text.value) {
+               message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
+               connection.send(message.tree());
+               aForm.text.value = '';
+       }
+       return false;
+}
+
+function sendChatMessage(aForm, to) {
+       if (aForm.text.value) {
+               body = aForm.text.value
+               message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
+               connection.send(message.tree());
+               aForm.text.value = '';
+               var html = '';
+               html += '<div class="msg">';
+               html += '<span class="sender">';
+               html += nickname;
+               html += ':</span> ';
+               html += body + '</div>';
+               document.getElementById(to + 'BubbleChat').innerHTML += html;
+               document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
+
+       }
+       return false;
+}
+
+var dragElement = null;
+var mouseX = 0;
+var mouseY = 0;
+var offX = 0;
+var offY = 0;
+
+function startDrag(element) {
+       dragElement = element;
+       offX = mouseX - dragElement.offsetLeft;
+       offY = mouseY - dragElement.offsetTop;
+}
+
+function doDrag(eve) {
+       mouseX = eve.pageX;
+       mouseY = eve.pageY;
+
+       if (dragElement) {
+               dragElement.style.left = (mouseX - offX) + 'px';
+               dragElement.style.top = (mouseY - offY) + 'px';
+       }
+}
+
+function stopDrag(eve) {
+       dragElement = null;
+}
+
+onunload = function() {
+       if (connection) {
+               connection.disconnect();
+       }
+}
+
+onmousemove = doDrag;
+
+onmouseup = stopDrag;