From 8c4afe807502d8fc5abb0155a14505363dbf4cfd Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 17 Mar 2010 19:53:04 +0100 Subject: [PATCH] Cleanup and dependency updates * Listen to jslint * New Strophe.js version * Provide sha1.js (Strophe.js doesn't provide it any longer) --- index.html | 1 + scripts/basic.js | 283 ++++++++++++++++++++++++---------------------- scripts/sha1.js | 207 +++++++++++++++++++++++++++++++++ scripts/strophejs | 2 +- 4 files changed, 354 insertions(+), 139 deletions(-) create mode 100644 scripts/sha1.js diff --git a/index.html b/index.html index b35cc75..a6f35cf 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ + diff --git a/scripts/basic.js b/scripts/basic.js index 7b57e52..c1ba6cb 100644 --- a/scripts/basic.js +++ b/scripts/basic.js @@ -1,19 +1,19 @@ var DEBUG = false; var BOSH_SERVICE = '/http-bind/'; -var NS_VCARD = 'vcard-temp'; -var NS_CAPS= 'http://jabber.org/protocol/caps'; -var NS_PEP = 'http://jabber.org/protocol/pubsub#event'; -var NS_TUNE = 'http://jabber.org/protocol/tune'; +Strophe.addNamespace('VCARD', 'vcard-temp'); +Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps'); +Strophe.addNamespace('PEP', 'http://jabber.org/protocol/pubsub#event'); +Strophe.addNamespace('TUNE', 'http://jabber.org/protocol/tune'); var localJID = null; -var connection = null; -var show_log = true; +var connection = null; +var show_log = true; -var features = new Array(NS_CAPS, NS_TUNE+'+notify', Strophe.NS.DISCO_INFO); +var features = [Strophe.NS.CAPS, Strophe.NS.TUNE + '+notify', Strophe.NS.DISCO_INFO]; var appName = 'socialXMPP'; -var roster = new Array(); +var roster = []; function log(msg) { var entry = $('
').append(document.createTextNode(msg)); @@ -33,19 +33,29 @@ function jid2id(jid) { return jid.split('@').join('-').split('.').join('-'); } +function genCaps() { + var S = '', i; + S += 'client/web//' + appName + '<'; + features.sort(); + for (i = 0; i < features.length; i++) { + S += features[i] + '<'; + } + return b64_sha1(S); +} + function populateVCard(e, jid) { - var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'); - for (var i=0; iTune: Listening to '+ roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist + - ' from ' + roster[jid2id(jid)].tune.source + '') + ' from ' + roster[jid2id(jid)].tune.source + ''); } $('#vCard').click(function () { @@ -87,7 +97,7 @@ function populateVCard(e, jid) { function _cbVCard(e) { var jid = e.getAttribute('from'); - if (roster[jid2id(jid)].vCard == "") { + if (roster[jid2id(jid)].vCard === "") { roster[jid2id(jid)].vCard = e; } if ($('#vCard_container').is(':visible')) { @@ -100,20 +110,20 @@ function _cbVCard(e) { } function _cbOwnVCard(e) { + var easy_cases = ['FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'], i, text, avatar, mime, binval; $('#ownInfo').empty(); $('#ownInfo').load('vCard.html #ownvCard', function() { - var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'); - for (var i=0; iClose' + + ' ' + jid + '

'); + $('#' + id).append('
'); + $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40); + $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40); +} + function addFriend(jid, nick) { roster[jid2id(jid)] = new Buddy(nick, jid); $('#friends').append(''); $('#' + jid2id(jid) + ' img').click(function() { - getVCard(jid); + getVCard(jid); }); $('#' + jid2id(jid) + ' a').click(function() { var id = jid2id(jid) + 'Chat'; @@ -173,18 +192,58 @@ function addFriend(jid, nick) { } +function handleTune(jid, tuneXML) { + var tune = new Tune(); + if (tuneXML.childNodes.length > 0) { + tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]); + tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]); + tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]); + tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]); + tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]); + tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]); + tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]); + if (roster[jid2id(jid)].visible === true) { + if ( $('#tune').length > 0) { + $('#tune').empty(); + $('#tune').append('Tune: '); + $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + + ' by ' + tune.artist + ' from ' + tune.source)); + } else { + $('#vCard ul').append('
  • Tune: Listening to '+ tune.title + + ' by ' + tune.artist + ' from ' + tune.source + '
  • '); + } + } + } else { + $('#tune').remove(); + } + roster[jid2id(jid)].tune = tune; +} + +function _cbPEP(e) { + var from = e.getAttribute('from'), items; + if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) { + return true; // Drop own PEP events + } + items = e.getElementsByTagName('items')[0]; + // Handle Tune + if (items.getAttribute('node') == Strophe.NS.TUNE) { + handleTune(from, items.getElementsByTagName('tune')[0]); + } + return true; +} + function _cbRoster(e) { - var query = e.getElementsByTagName('query')[0]; - var entries = query.getElementsByTagName('item'); - for (var item=0; item 0) { $('#' + id + ' p').append('
    '); @@ -238,22 +300,14 @@ function sendMessage(form, to) { return false; } -function createBubble(jid) { - var id = jid2id(jid) + 'Chat'; - $('body').append('
    Close' + - ' ' + jid + '

    '); - $('#' + id).append('
    ') - $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40); - $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40); -} - - function _cbMessage(msg) { - if ($(msg).attr('type') != 'chat') + var id, jid, body; + if ($(msg).attr('type') != 'chat') { return true; - var jid = $(msg).attr('from'); - var id = jid2id(jid) + 'Chat'; - var body = $(msg).find('body:first').text(); + } + jid = $(msg).attr('from'); + id = jid2id(jid) + 'Chat'; + body = $(msg).find('body:first').text(); body = escape(body); body = body.replace(/%0A/g, '
    '); body = body.replace(/%3C/g, '<'); @@ -273,54 +327,24 @@ function _cbMessage(msg) { return true; } -function genCaps() { - var S = ''; - S += 'client/web//' + appName + '<' - features.sort(); - for (var i = 0; i < features.length; i++) { - S += features[i] + '<'; - } - return b64_sha1(S); -} - -function handleTune(jid, tuneXML) { - var tune = new Tune(); - if (tuneXML.childNodes.length > 0) { - tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]); - tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]); - tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]); - tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]); - tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]); - tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]); - tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]); - if (roster[jid2id(jid)].visible == true) { - if ( $('#tune').length > 0) { - $('#tune').empty(); - $('#tune').append('Tune: '); - $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + - ' by ' + tune.artist + ' from ' + tune.source)); - } else { - $('#vCard ul').append('
  • Tune: Listening to '+ tune.title + - ' by ' + tune.artist + ' from ' + tune.source + '
  • ') - } - } - } else { - $('#tune').remove(); - } - roster[jid2id(jid)].tune = tune; -} +function showConnect() { + var jid, pass, button; + jid = $('#jid'); + pass = $('#pass'); + button = $('#connect').get(0); -function _cbPEP(e) { - var from = e.getAttribute('from'); - if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) { - return true; // Drop own PEP events - } - var items = e.getElementsByTagName('items')[0]; - // Handle Tune - if (items.getAttribute('node') == NS_TUNE) { - handleTune(from, items.getElementsByTagName('tune')[0]); - } - return true; + $('#log').empty(); + $('#ownInfo').empty(); + $('#ownInfo').hide(); + $('#vCard_container').empty(); + $('#friends').empty(); + $('#friends').hide(); + $('.chat').remove(); + $('label').show(); + jid.show(); + pass.show(); + button.value = 'connect'; + return false; } function onConnect(status) { @@ -348,29 +372,11 @@ function onConnect(status) { } } -function showConnect() { - var jid = $('#jid'); - var pass = $('#pass'); - var button = $('#connect').get(0); - - $('#log').empty(); - $('#ownInfo').empty(); - $('#ownInfo').hide(); - $('#vCard_container').empty(); - $('#friends').empty(); - $('#friends').hide(); - $('.chat').remove(); - $('label').show(); - jid.show(); - pass.show(); - button.value = 'connect'; - return false; -} - function showDisconnect() { - var jid = $('#jid'); - var pass = $('#pass'); - var button = $('#connect').get(0); + var jid, pass, button; + jid = $('#jid'); + pass = $('#pass'); + button = $('#connect').get(0); button.value = 'disconnect'; pass.hide(); @@ -380,6 +386,7 @@ function showDisconnect() { } $(document).ready(function () { + var button, jid, pass; if (DEBUG) { $('#log_container').show(); } @@ -392,13 +399,13 @@ $(document).ready(function () { }); $('#cred').bind('submit', function () { - var button = $('#connect').get(0); - var jid = $('#jid'); - var pass = $('#pass'); + button = $('#connect').get(0); + jid = $('#jid'); + pass = $('#pass'); localJID = jid.get(0).value; if (button.value == 'connect') { - showDisconnect(); + showDisconnect(); connection.connect(localJID, pass.get(0).value, onConnect); @@ -436,12 +443,12 @@ function stopDrag(eve) { dragElement = null; } -onunload = function() { +var onunload = function() { if (connection) { connection.disconnect(); } -} +}; -onmousemove = doDrag; -onmouseup = stopDrag; +var onmousemove = doDrag; +var onmouseup = stopDrag; diff --git a/scripts/sha1.js b/scripts/sha1.js new file mode 100644 index 0000000..db3bf05 --- /dev/null +++ b/scripts/sha1.js @@ -0,0 +1,207 @@ +/* + * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined + * in FIPS PUB 180-1 + * Version 2.1a Copyright Paul Johnston 2000 - 2002. + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * Distributed under the BSD License + * See http://pajhome.org.uk/crypt/md5 for details. + */ + +/* + * Configurable variables. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + */ +var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ +var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ +var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ + +/* + * These are the functions you'll usually want to call + * They take string arguments and return either hex or base-64 encoded strings + */ +function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));} +function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));} +function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));} +function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));} +function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));} +function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));} + +/* + * Perform a simple self-test to see if the VM is working + */ +function sha1_vm_test() +{ + return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"; +} + +/* + * Calculate the SHA-1 of an array of big-endian words, and a bit length + */ +function core_sha1(x, len) +{ + /* append padding */ + x[len >> 5] |= 0x80 << (24 - len % 32); + x[((len + 64 >> 9) << 4) + 15] = len; + + var w = new Array(80); + var a = 1732584193; + var b = -271733879; + var c = -1732584194; + var d = 271733878; + var e = -1009589776; + + var i, j, t, olda, oldb, oldc, oldd, olde; + for (i = 0; i < x.length; i += 16) + { + olda = a; + oldb = b; + oldc = c; + oldd = d; + olde = e; + + for (j = 0; j < 80; j++) + { + if (j < 16) { w[j] = x[i + j]; } + else { w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); } + t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), + safe_add(safe_add(e, w[j]), sha1_kt(j))); + e = d; + d = c; + c = rol(b, 30); + b = a; + a = t; + } + + a = safe_add(a, olda); + b = safe_add(b, oldb); + c = safe_add(c, oldc); + d = safe_add(d, oldd); + e = safe_add(e, olde); + } + return [a, b, c, d, e]; +} + +/* + * Perform the appropriate triplet combination function for the current + * iteration + */ +function sha1_ft(t, b, c, d) +{ + if (t < 20) { return (b & c) | ((~b) & d); } + if (t < 40) { return b ^ c ^ d; } + if (t < 60) { return (b & c) | (b & d) | (c & d); } + return b ^ c ^ d; +} + +/* + * Determine the appropriate additive constant for the current iteration + */ +function sha1_kt(t) +{ + return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : + (t < 60) ? -1894007588 : -899497514; +} + +/* + * Calculate the HMAC-SHA1 of a key and some data + */ +function core_hmac_sha1(key, data) +{ + var bkey = str2binb(key); + if (bkey.length > 16) { bkey = core_sha1(bkey, key.length * chrsz); } + + var ipad = new Array(16), opad = new Array(16); + for (var i = 0; i < 16; i++) + { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + + var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); + return core_sha1(opad.concat(hash), 512 + 160); +} + +/* + * Add integers, wrapping at 2^32. This uses 16-bit operations internally + * to work around bugs in some JS interpreters. + */ +function safe_add(x, y) +{ + var lsw = (x & 0xFFFF) + (y & 0xFFFF); + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); +} + +/* + * Bitwise rotate a 32-bit number to the left. + */ +function rol(num, cnt) +{ + return (num << cnt) | (num >>> (32 - cnt)); +} + +/* + * Convert an 8-bit or 16-bit string to an array of big-endian words + * In 8-bit function, characters >255 have their hi-byte silently ignored. + */ +function str2binb(str) +{ + var bin = []; + var mask = (1 << chrsz) - 1; + for (var i = 0; i < str.length * chrsz; i += chrsz) + { + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32); + } + return bin; +} + +/* + * Convert an array of big-endian words to a string + */ +function binb2str(bin) +{ + var str = ""; + var mask = (1 << chrsz) - 1; + for (var i = 0; i < bin.length * 32; i += chrsz) + { + str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask); + } + return str; +} + +/* + * Convert an array of big-endian words to a hex string. + */ +function binb2hex(binarray) +{ + var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; + var str = ""; + for (var i = 0; i < binarray.length * 4; i++) + { + str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); + } + return str; +} + +/* + * Convert an array of big-endian words to a base-64 string + */ +function binb2b64(binarray) +{ + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + var str = ""; + var triplet, j; + for (var i = 0; i < binarray.length * 4; i += 3) + { + triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) | + (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) | + ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); + for (j = 0; j < 4; j++) + { + if (i * 8 + j * 6 > binarray.length * 32) { str += b64pad; } + else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); } + } + } + return str; +} diff --git a/scripts/strophejs b/scripts/strophejs index edd32cc..6d7b886 160000 --- a/scripts/strophejs +++ b/scripts/strophejs @@ -1 +1 @@ -Subproject commit edd32ccb8b787f982d19b4df37ca697c1e8824a5 +Subproject commit 6d7b8863f0f36f73c0b65686956437d26bd93c01 -- 2.39.2