1 var BOSH_LOCATION = '/http-bind/';
2 var room = 'guests@conference.babelmonkeys.de';
3 var jid = 'muckl@babelmonkeys.de'
4 var password = 'ooje0OjuJeekaek6';
8 function doLogin(aForm) {
9 if (!aForm.nickname.value)
12 connection = new Strophe.Connection(BOSH_LOCATION);
13 connection.connect(jid, password, onConnect);
15 nickname = aForm.nickname.value;
23 function onConnect(status) {
24 if (status == Strophe.Status.CONNFAIL) {
25 handleError('Failed to connect');
26 } else if (status == Strophe.Status.DISCONNECTED) {
28 } else if (status == Strophe.Status.CONNECTED) {
29 // Add handlers connection.addHandler(callback, namespace, stanza_name, type, id, from)
30 connection.addHandler(handleMessage, null, 'message', null, null, null);
31 connection.addHandler(handlePresence, null, 'presence', null, null, null);
32 connection.addHandler(handleIQ, null, 'iq', null, null, null);
34 connection.addHandler(handleIqVersion, Strophe.NS.VERSION, 'iq', null, null, null);
35 connection.addHandler(handleIqVersion, 'urn:xmpp:time', 'iq', null, null, null);
37 connection.send($pres().tree());
38 connection.send($pres({to: room + '/' + nickname}).tree());
40 // Make things (in)visible
48 function handleError(error) {
49 alert("An error occured:" + error);
53 function handleDisconnected() {
54 // Make things (in)visible
61 function addBubble(nick) {
63 if (!document.getElementById(id)) {
65 div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)" style="display: none">';
66 div += '<a href="#" onclick="' +"$('#" + id + "').hide('slow')" + '">Close</a>';
67 div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
68 div += '<form id="' + id + 'Form" class="bubbleForm" onsubmit="return sendChatMessage(this,' + "'" + nick + "');" + '" action="#">';
69 div += '<input type="text" name="text" id="' + id + 'Text" class="bubbleForm"/>';
72 $('body').append(div);
74 $('#'+id).show('slow');
77 function handleMessage(msg) {
79 var sender = Strophe.getResourceFromJid(msg.getAttribute('from'));
80 var type = msg.getAttribute('type');
81 var body = Strophe.getText(msg.getElementsByTagName('body')[0]);
83 html += '<div class="msg">';
85 html += '<span class="sender">';
88 html += body + '</div>';
90 html += '<span class="server">';
91 html += body + '</span></div>';
96 $('#' + id + 'Chat').append(html);
97 document.getElementById(id + 'Chat').lastChild.scrollIntoView();
99 $('#chat').append(html);
100 document.getElementById('chat').lastChild.scrollIntoView();
106 function handlePresence(presence) {
107 if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room)
109 roster_list = document.getElementById('roster_list');
110 nick = Strophe.getResourceFromJid(presence.getAttribute('from'));
111 type = presence.getAttribute('type');
112 if (type == 'unavailable') {
113 element = document.getElementById(nick);
114 roster_list.removeChild(element);
115 $('#chat').append('<div class="msg"><span class="server">' + nick + ' left the groupchat</span></div>');
117 roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
118 $('#chat').append('<div class="msg"><span class="server">' + nick + ' joined the groupchat</span></div>');
124 function handleIQ(iq) {
125 var to = iq.getAttribute('to');
126 var from = iq.getAttribute('from');
127 var type = iq.getAttribute('type');
129 //FIXME: Clients SHOULD send the content of the original stanza back for analysis
131 var reply = $iq({to: from, from: to, type: 'error'}).c('error', {type: 'cancel'}).c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'});
133 connection.send(reply.tree());
138 function handleIqVersion(iq) {
139 var to = iq.getAttribute('to');
140 var from = iq.getAttribute('from');
142 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);
144 connection.send(reply.tree());
149 function handleIqTime(iq) {
150 var now = new Date();
151 var to = iq.getAttribute('to');
152 var from = iq.getAttribute('from');
154 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);
156 connection.send(reply.tree());
161 function sendMessage(aForm) {
162 if (aForm.text.value) {
163 message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
164 connection.send(message.tree());
165 aForm.text.value = '';
170 function sendChatMessage(aForm, to) {
171 if (aForm.text.value) {
172 body = aForm.text.value
173 message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
174 connection.send(message.tree());
175 aForm.text.value = '';
177 html += '<div class="msg">';
178 html += '<span class="sender">';
181 html += body + '</div>';
182 document.getElementById(to + 'BubbleChat').innerHTML += html;
183 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
189 var dragElement = null;
195 function startDrag(element) {
196 dragElement = element;
197 offX = mouseX - dragElement.offsetLeft;
198 offY = mouseY - dragElement.offsetTop;
201 function doDrag(eve) {
206 dragElement.style.left = (mouseX - offX) + 'px';
207 dragElement.style.top = (mouseY - offY) + 'px';
211 function stopDrag(eve) {
215 onunload = function() {
217 connection.disconnect();
221 onmousemove = doDrag;
223 onmouseup = stopDrag;