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);
116 roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
122 function handleIQ(iq) {
123 var to = iq.getAttribute('to');
124 var from = iq.getAttribute('from');
125 var type = iq.getAttribute('type');
127 //FIXME: Clients SHOULD send the content of the original stanza back for analysis
129 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'});
131 connection.send(reply.tree());
136 function handleIqVersion(iq) {
137 var to = iq.getAttribute('to');
138 var from = iq.getAttribute('from');
140 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);
142 connection.send(reply.tree());
147 function handleIqTime(iq) {
148 var now = new Date();
149 var to = iq.getAttribute('to');
150 var from = iq.getAttribute('from');
152 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);
154 connection.send(reply.tree());
159 function sendMessage(aForm) {
160 if (aForm.text.value) {
161 message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
162 connection.send(message.tree());
163 aForm.text.value = '';
168 function sendChatMessage(aForm, to) {
169 if (aForm.text.value) {
170 body = aForm.text.value
171 message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
172 connection.send(message.tree());
173 aForm.text.value = '';
175 html += '<div class="msg">';
176 html += '<span class="sender">';
179 html += body + '</div>';
180 document.getElementById(to + 'BubbleChat').innerHTML += html;
181 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
187 var dragElement = null;
193 function startDrag(element) {
194 dragElement = element;
195 offX = mouseX - dragElement.offsetLeft;
196 offY = mouseY - dragElement.offsetTop;
199 function doDrag(eve) {
204 dragElement.style.left = (mouseX - offX) + 'px';
205 dragElement.style.top = (mouseY - offY) + 'px';
209 function stopDrag(eve) {
213 onunload = function() {
215 connection.disconnect();
219 onmousemove = doDrag;
221 onmouseup = stopDrag;