X-Git-Url: https://cgit.babelmonkeys.de/?a=blobdiff_plain;f=js%2Fmain.js;h=8d1d47638cbe6c27acdf14ad16c11fcc3004c77a;hb=refs%2Fheads%2Fmaster;hp=857dc258418421771bf40b6d4582fdb4c608e962;hpb=0f774d8b5c4049745e6f8560e80f744d50eda3ee;p=xmppchat.git
diff --git a/js/main.js b/js/main.js
index 857dc25..8d1d476 100644
--- a/js/main.js
+++ b/js/main.js
@@ -1,54 +1,7 @@
-var BOSH_LOCATION = '/http-bind/';
-var room = 'guests@conference.babelmonkeys.de';
-var jid = 'muckl@babelmonkeys.de'
-var password = 'ooje0OjuJeekaek6';
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
@@ -58,12 +11,18 @@ function handleDisconnected() {
$('#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 += '
Close';
+ div += '
Close';
div += '
';
div += '
';
+ }
+ } else {
+ html += '';
+ html += body + '';
+ }
+ } else if (subject) {
html += '';
- html += body + '';
+ html += "The subject is: " + subject + '';
+ } else {
+ return true;
}
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();
@@ -104,51 +94,77 @@ function handleMessage(msg) {
}
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);
roster_list.removeChild(element);
+ $('#chat').append('' + nick + ' left the groupchat
');
} else {
roster_list.innerHTML += '' + nick + '';
+ $('#chat').append('' + nick + ' joined the groupchat
');
}
return true;
}
function handleIQ(iq) {
- var to = iq.getAttribute('to');
- var from = iq.getAttribute('from');
- var type = iq.getAttribute('type');
- var content = iq.childNodes;
-
- 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'});
-
+ 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
+ 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());
@@ -156,6 +172,7 @@ function handleIqTime(iq) {
}
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());
@@ -165,17 +182,18 @@ function sendMessage(aForm) {
}
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 += '';
html += '';
html += nickname;
html += ': ';
- html += body + '
';
+ html += Strophe.xmlescape(body) + '';
document.getElementById(to + 'BubbleChat').innerHTML += html;
document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
@@ -183,6 +201,57 @@ function sendChatMessage(aForm, to) {
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;
@@ -209,12 +278,8 @@ function stopDrag(eve) {
dragElement = null;
}
-onunload = function() {
- if (connection) {
- connection.disconnect();
- }
-}
+window.onunload = window.onbeforeunload = doDisconnect;
-onmousemove = doDrag;
+window.onmousemove = doDrag;
-onmouseup = stopDrag;
+window.onmouseup = stopDrag;