4 function doLogin(aForm) {
5 if (!aForm.nickname.value)
8 connection = new Strophe.Connection(BOSH_LOCATION);
9 connection.connect(jid, password, onConnect);
11 nickname = aForm.nickname.value;
19 function onConnect(status) {
20 if (status == Strophe.Status.CONNFAIL) {
21 handleError('Failed to connect');
22 } else if (status == Strophe.Status.DISCONNECTED) {
24 } else if (status == Strophe.Status.CONNECTED) {
25 // Add handlers connection.addHandler(callback, namespace, stanza_name, type, id, from)
26 connection.addHandler(handleMessage, null, 'message', null, null, null);
27 connection.addHandler(handlePresence, null, 'presence', null, null, null);
28 connection.addHandler(handleIQ, null, 'iq', null, null, null);
30 connection.addHandler(handleIqVersion, Strophe.NS.VERSION, 'iq', null, null, null);
31 connection.addHandler(handleIqVersion, 'urn:xmpp:time', 'iq', null, null, null);
33 connection.send($pres().tree());
34 connection.send($pres({to: room + '/' + nickname}).tree());
36 // Make things (in)visible
44 function handleError(error) {
45 alert("An error occured:" + error);
49 function handleDisconnected() {
50 // Make things (in)visible
57 function addBubble(nick) {
59 if (!document.getElementById(id)) {
61 div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)" style="display: none">';
62 div += '<a href="#" onclick="' +"$('#" + id + "').hide('slow')" + '">Close</a>';
63 div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
64 div += '<form id="' + id + 'Form" class="bubbleForm" onsubmit="return sendChatMessage(this,' + "'" + nick + "');" + '" action="#">';
65 div += '<input type="text" name="text" id="' + id + 'Text" class="bubbleForm"/>';
68 $('body').append(div);
70 $('#'+id).show('slow');
73 function handleMessage(msg) {
75 var sender = Strophe.getResourceFromJid(msg.getAttribute('from'));
76 var type = msg.getAttribute('type');
77 var body = Strophe.getText(msg.getElementsByTagName('body')[0]);
79 html += '<div class="msg">';
81 html += '<span class="sender">';
84 html += body + '</div>';
86 html += '<span class="server">';
87 html += body + '</span></div>';
92 $('#' + id + 'Chat').append(html);
93 document.getElementById(id + 'Chat').lastChild.scrollIntoView();
95 $('#chat').append(html);
96 document.getElementById('chat').lastChild.scrollIntoView();
102 function handlePresence(presence) {
103 if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room)
105 roster_list = document.getElementById('roster_list');
106 nick = Strophe.getResourceFromJid(presence.getAttribute('from'));
107 type = presence.getAttribute('type');
108 if (type == 'unavailable') {
109 element = document.getElementById(nick);
110 roster_list.removeChild(element);
111 $('#chat').append('<div class="msg"><span class="server">' + nick + ' left the groupchat</span></div>');
113 roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
114 $('#chat').append('<div class="msg"><span class="server">' + nick + ' joined the groupchat</span></div>');
120 function handleIQ(iq) {
121 var to = iq.getAttribute('to');
122 var from = iq.getAttribute('from');
123 var type = iq.getAttribute('type');
125 //FIXME: Clients SHOULD send the content of the original stanza back for analysis
127 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'});
129 connection.send(reply.tree());
134 function handleIqVersion(iq) {
135 var to = iq.getAttribute('to');
136 var from = iq.getAttribute('from');
138 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);
140 connection.send(reply.tree());
145 function handleIqTime(iq) {
146 var now = new Date();
147 var to = iq.getAttribute('to');
148 var from = iq.getAttribute('from');
150 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);
152 connection.send(reply.tree());
157 function sendMessage(aForm) {
158 if (aForm.text.value) {
159 message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
160 connection.send(message.tree());
161 aForm.text.value = '';
166 function sendChatMessage(aForm, to) {
167 if (aForm.text.value) {
168 body = aForm.text.value
169 message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
170 connection.send(message.tree());
171 aForm.text.value = '';
173 html += '<div class="msg">';
174 html += '<span class="sender">';
177 html += body + '</div>';
178 document.getElementById(to + 'BubbleChat').innerHTML += html;
179 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
185 var dragElement = null;
191 function startDrag(element) {
192 dragElement = element;
193 offX = mouseX - dragElement.offsetLeft;
194 offY = mouseY - dragElement.offsetTop;
197 function doDrag(eve) {
202 dragElement.style.left = (mouseX - offX) + 'px';
203 dragElement.style.top = (mouseY - offY) + 'px';
207 function stopDrag(eve) {
211 onunload = function() {
213 connection.disconnect();
217 onmousemove = doDrag;
219 onmouseup = stopDrag;