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="' +"document.getElementById('" + id + "').style.display='none'" + '">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');
126 var content = iq.childNodes;
128 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'});
130 connection.send(reply.tree());
135 function handleIqVersion(iq) {
136 var to = iq.getAttribute('to');
137 var from = iq.getAttribute('from');
139 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);
141 connection.send(reply.tree());
146 function handleIqTime(iq) {
147 var now = new Date();
148 var to = iq.getAttribute('to');
149 var from = iq.getAttribute('from');
151 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);
153 connection.send(reply.tree());
158 function sendMessage(aForm) {
159 if (aForm.text.value) {
160 message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
161 connection.send(message.tree());
162 aForm.text.value = '';
167 function sendChatMessage(aForm, to) {
168 if (aForm.text.value) {
169 body = aForm.text.value
170 message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
171 connection.send(message.tree());
172 aForm.text.value = '';
174 html += '<div class="msg">';
175 html += '<span class="sender">';
178 html += body + '</div>';
179 document.getElementById(to + 'BubbleChat').innerHTML += html;
180 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
186 var dragElement = null;
192 function startDrag(element) {
193 dragElement = element;
194 offX = mouseX - dragElement.offsetLeft;
195 offY = mouseY - dragElement.offsetTop;
198 function doDrag(eve) {
203 dragElement.style.left = (mouseX - offX) + 'px';
204 dragElement.style.top = (mouseY - offY) + 'px';
208 function stopDrag(eve) {
212 onunload = function() {
214 connection.disconnect();
218 onmousemove = doDrag;
220 onmouseup = stopDrag;