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();
-}
+Strophe.addNamespace('TIME', "urn:xmpp:time");
function handleDisconnected() {
// Make things (in)visible
$('#entry').hide();
}
+function handleError(error) {
+ alert("An error occured:" + error);
+ handleDisconnected();
+}
+
function addBubble(nick) {
+ var id, div;
id = nick + 'Bubble';
if (!document.getElementById(id)) {
- var div = '';
+ div = '';
div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)" style="display: none">';
div += '<a href="#" onclick="' +"$('#" + id + "').hide('slow')" + '">Close</a>';
div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
}
function handleMessage(msg) {
- var html = '';
- var sender = Strophe.getResourceFromJid(msg.getAttribute('from'));
+ var html, sender, type, body, subject;
+ html = '';
+ sender = Strophe.getResourceFromJid(msg.getAttribute('from'));
if (sender) {
sender = Strophe.xmlescape(sender);
} else {
sender = false;
}
- var type = msg.getAttribute('type');
- var body = msg.getElementsByTagName('body')[0];
+ type = msg.getAttribute('type');
+ body = msg.getElementsByTagName('body')[0];
if (body) {
body = Strophe.xmlescape(Strophe.getText(body));
} else {
body = false;
}
- var subject = msg.getElementsByTagName('subject')[0];
+ subject = msg.getElementsByTagName('subject')[0];
if (subject) {
subject = Strophe.xmlescape(Strophe.getText(subject));
} else {
html += '<div class="msg">';
if (body) {
if (sender) {
- if (body.search(/^\/me/) == 0) {
+ if (body.search(/^\/me/) === 0) {
body = body.replace(/^\/me/, sender);
html += '<span class="sender">';
html += body;
if (type == 'chat') {
addBubble(sender);
- $('#' + id + 'Chat').append(html);
- document.getElementById(id + 'Chat').lastChild.scrollIntoView();
+ $('#' + sender + 'BubbleChat').append(html);
+ document.getElementById(sender + 'BubbleChat').lastChild.scrollIntoView();
} else {
$('#chat').append(html);
document.getElementById('chat').lastChild.scrollIntoView();
}
function handlePresence(presence) {
- if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room)
- return true
+ var roster_list, nick, type, element;
+ if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room) {
+ return true;
+ }
roster_list = document.getElementById('roster_list');
- nick = Strophe.getResourceFromJid(presence.getAttribute('from'));
+ nick = Strophe.xmlescape(Strophe.getResourceFromJid(presence.getAttribute('from')));
type = presence.getAttribute('type');
if (type == 'unavailable') {
element = document.getElementById(nick);
}
function handleIQ(iq) {
- var to = iq.getAttribute('to');
- var from = iq.getAttribute('from');
- var type = iq.getAttribute('type');
+ var to, from, type, id, reply;
+ to = iq.getAttribute('to');
+ from = iq.getAttribute('from');
+ type = iq.getAttribute('type');
+ id = iq.getAttribute('id');
//FIXME: Clients SHOULD send the content of the original stanza back for analysis
-
- 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'});
-
+ reply = $iq({to: from, from: to, id: id, type: 'error'}).c('error', {type: 'cancel'}).c('feature-not-implemented', {xmlns: Strophe.NS.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);
+ var to, from, id, reply;
+ to = iq.getAttribute('to');
+ from = iq.getAttribute('from');
+ id = iq.getAttribute('id');
+ reply = $iq({type: 'result', to: from, from: to, id: id}).c('query', {xmlns: Strophe.NS.VERSION}).c('name').t('XMPPChat').up().c('version').t('git').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);
+ var now, to, from, id, year, month, day, hours, minutes, seconds, offsetHour, offsetMin, reply;
+ now = new Date();
+ to = iq.getAttribute('to');
+ from = iq.getAttribute('from');
+ id = iq.getAttribute('id');
+
+ year = now.getUTCFullYear();
+ month = now.getUTCMonth() + 1;
+ month = (month < 10) ? '0' + month : month;
+ day = now.getUTCDate();
+ day = (day < 10) ? '0' + day : day;
+ hours = now.getUTCHours();
+ hours = (hours < 10) ? '0' + hours : hours;
+ minutes = now.getUTCMinutes();
+ minutes = (minutes < 10) ? '0' + minutes : minutes;
+ seconds = now.getUTCSeconds();
+ seconds = (seconds < 10) ? '0' + seconds : seconds;
+ offsetMin = now.getTimezoneOffset() * (-1);
+ offsetHour = offsetMin / 60;
+ offsetHour = (offsetHour < 10) ? '0' + offsetHour : offsetHour;
+ offsetMin = offsetMin % 60;
+ offsetMin = (offsetMin < 0) ? (-1)*offsetMin : offsetMin;
+ offsetMin = (offsetMin < 10) ? '0' + offsetMin : offsetMin;
+
+ reply = $iq({type: 'result', from: to, to: from, id: id}).c('time', {xmlns: Strophe.NS.TIME}).c('utc').t(year + '-' + month + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds + 'Z').up().c('tzo').t( ((offsetHour >= 0) ? '+':'') + offsetHour + ':' + offsetMin);
connection.send(reply.tree());
}
function sendMessage(aForm) {
+ var message;
if (aForm.text.value) {
message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
connection.send(message.tree());
}
function sendChatMessage(aForm, to) {
+ var body, message, html;
if (aForm.text.value) {
- body = 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 += '<div class="msg">';
html += '<span class="sender">';
html += nickname;
html += ':</span> ';
- html += body + '</div>';
+ html += Strophe.xmlescape(body) + '</div>';
document.getElementById(to + 'BubbleChat').innerHTML += html;
document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
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, options)
+ connection.addHandler(handleMessage, null, 'message');
+ connection.addHandler(handlePresence, null, 'presence');
+ connection.addHandler(handleIQ, null, 'iq');
+
+ connection.addHandler(handleIqVersion, Strophe.NS.VERSION, 'iq');
+ connection.addHandler(handleIqTime, Strophe.NS.TIME, 'iq');
+
+ connection.send($pres().tree());
+ connection.send($pres({to: room + '/' + nickname}).tree());
+
+ // Make things (in)visible
+ $('#login').hide();
+ $('#chat').show();
+ $('#roster').show();
+ $('#entry').show();
+ }
+}
+
+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 doDisconnect(aForm) {
+ if (connection) {
+ connection.send($pres({to: room, type: 'unavailable'}).tree());
+ connection.flush();
+ connection.disconnect();
+ connection = null;
+ }
+ return false;
+}
+
var dragElement = null;
var mouseX = 0;
var mouseY = 0;
dragElement = null;
}
-onunload = function() {
- if (connection) {
- connection.disconnect();
- }
-}
+window.onunload = window.onbeforeunload = doDisconnect;
-onmousemove = doDrag;
+window.onmousemove = doDrag;
-onmouseup = stopDrag;
+window.onmouseup = stopDrag;