From 9e7fd66da0e612601f38b6dbe370163f9526e155 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Sun, 1 Jun 2008 16:41:51 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + index.html | 37 ++++++++++++ main.js | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 78 ++++++++++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 main.js create mode 100644 style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d054878 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +jsjac* diff --git a/index.html b/index.html new file mode 100644 index 0000000..3069d5f --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + +XMPP Chat + + + + + + + +
+
+ Nickname: + + +
+
+ +
+ +
+ +
+ +
+
+ + +
+
+ + + diff --git a/main.js b/main.js new file mode 100644 index 0000000..f1f878a --- /dev/null +++ b/main.js @@ -0,0 +1,161 @@ +function doLogin(aForm) { + room = 'guests@conference.babelmonkeys.de'; + if (!aForm.nickname.value) + return false; + try { + oArgs = new Object(); + oArgs.httpbase = '/http-bind/'; + oArgs.timerval = 2000; + con = new JSJaCHttpBindingConnection(oArgs) + + nickname = aForm.nickname.value; + setupHandlers(con); + + oArgs = new Object(); + oArgs.domain = 'babelmonkeys.de'; + oArgs.username = 'muckl'; + oArgs.resource = randomString(); + oArgs.pass = 'ooje0OjuJeekaek6'; + con.connect(oArgs); + } catch (e) { + alert(e); + } finally { + return false; + } +} + +function setupHandlers(con) { + con.registerHandler('message', handleMessage); + con.registerHandler('presence', handlePresence); + con.registerHandler('iq', handleIQ); + con.registerHandler('onconnect', handleConnected); + con.registerHandler('onerror', handleError); + // con.registerHandler('status_changed', handleStatusChanged); + con.registerHandler('ondisconnect', handleDisconnected); + + con.registerIQGet('query', NS_VERSION, handleIqVersion); + con.registerIQGet('query', NS_TIME, handleIqTime); +} + +function handleConnected() { + con.send(new JSJaCPresence()); + GCpresence = new JSJaCPresence(); + GCpresence.setTo(room + '/' + nickname); + con.send(GCpresence); + + // Make things (in)visible + document.getElementById('login').style.display = 'none'; + document.getElementById('chat').style.display = 'block'; + document.getElementById('roster').style.display = 'block'; + document.getElementById('entry').style.display = 'block'; +} + +function handleError(e) { + alert("An error occured:" + + "\nCode: " + e.getAttribute('code') + + "\nType: " + e.getAttribute('type') + + "\nCondition: " + e.firstChild.nodeName); + // Make things (in)visible + document.getElementById('login').style.display = 'block'; + document.getElementById('chat').style.display = 'none'; + document.getElementById('roster').style.display = 'none'; + document.getElementById('entry').style.display = 'none'; + + + if (con.connected()) + con.disconnect(); +} + +function handleDisconnected() { + // Make things (in)visible + document.getElementById('login').style.display = 'block'; + document.getElementById('chat').style.display = 'none'; + document.getElementById('roster').style.display = 'none'; + document.getElementById('entry').style.display = 'none'; +} + +function handleMessage(aJSJaCPacket) { + var html = ''; + html += '
'; + if (aJSJaCPacket.getFromJID().getResource()) { + html += ''; + html += aJSJaCPacket.getFromJID().getResource(); + html += ': '; + html += aJSJaCPacket.getBody().htmlEnc() + '
'; + } else { + html += ''; + html += aJSJaCPacket.getBody().htmlEnc() + ''; + } + + document.getElementById('chat').innerHTML += html; + document.getElementById('chat').lastChild.scrollIntoView(); +} + +function handlePresence(aJSJaCPacket) { + if (aJSJaCPacket.getFromJID().toString().split('/')[0] != room) + return + roster_list = document.getElementById('roster_list'); + nick = aJSJaCPacket.getFromJID().getResource(); + if (aJSJaCPacket.getType() == 'unavailable') { + element = document.getElementById(nick); + roster_list.removeChild(element); + } else { + roster_list.innerHTML += '
  • ' + nick + '
  • '; + } +} + +function handleIQ(iq) { + con.send(iq.errorReply(ERR_FEATURE_NOT_IMPLEMENTED)); +} + +function handleIqVersion(iq) { + con.send(iq.reply([ + iq.buildNode('name', 'jsjacChatClient'), + iq.buildNode('version', JSJaC.Version), + iq.buildNode('os', navigator.userAgent) + ])); + return true; +} + +function handleIqTime(iq) { + var now = new Date(); + con.send(iq.reply([iq.buildNode('display', + now.toLocaleString()), + iq.buildNode('utc', + now.jabberDate()), + iq.buildNode('tz', + now.toLocaleString().substring(now.toLocaleString().lastIndexOf(' ')+1)) + ])); + return true; +} + +function sendMessage(aForm) { + if (aForm.text.value) { + message = new JSJaCMessage(); + message.setBody(aForm.text.value); + message.setType('groupchat'); + message.setTo(room); + con.send(message); + aForm.text.value = ''; + } + return false; +} + +function randomString() { + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; + var string_length = 20; + var randomstring = ''; + for (var i=0; i