X-Git-Url: http://git.babelmonkeys.de/?p=xmppchat.git;a=blobdiff_plain;f=js%2Fmain.js;fp=js%2Fmain.js;h=857dc258418421771bf40b6d4582fdb4c608e962;hp=0000000000000000000000000000000000000000;hb=0f774d8b5c4049745e6f8560e80f744d50eda3ee;hpb=7edeada8f83809dd0c748a1c11914e2c93fb6ad0 diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..857dc25 --- /dev/null +++ b/js/main.js @@ -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 += ''; + $('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 += '
'; + if (sender) { + html += ''; + html += sender; + html += ': '; + html += body + '
'; + } else { + html += ''; + html += body + ''; + } + + 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 += '
  • ' + nick + '
  • '; + } + + 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 += '
    '; + html += ''; + html += nickname; + html += ': '; + html += body + '
    '; + 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;