From 3a0015c3578083514987b254b2b6fac34a2eb237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bartelme=C3=9F?= Date: Sun, 8 Jul 2012 11:51:04 +0200 Subject: [PATCH 01/17] remove unused _opt in favor of code size/readability --- src/static/js/Changeset.js | 245 ++++++++++++++----------------------- 1 file changed, 89 insertions(+), 156 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 738ee1bab..cfea43625 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -27,8 +27,6 @@ var AttributePool = require("./AttributePool"); -var _opt = null; - /** * ==================== General Util Functions ======================= */ @@ -127,22 +125,13 @@ exports.opIterator = function (opsStr, optStartIndex) { function nextRegexMatch() { prevIndex = curIndex; var result; - if (_opt) { - result = _opt.nextOpInString(opsStr, curIndex); - if (result) { - if (result.opcode() == '?') { - exports.error("Hit error opcode in op stream"); - } - curIndex = result.lastIndex(); - } - } else { - regex.lastIndex = curIndex; - result = regex.exec(opsStr); - curIndex = regex.lastIndex; - if (result[0] == '?') { - exports.error("Hit error opcode in op stream"); - } + regex.lastIndex = curIndex; + result = regex.exec(opsStr); + curIndex = regex.lastIndex; + if (result[0] == '?') { + exports.error("Hit error opcode in op stream"); } + return result; } var regexResult = nextRegexMatch(); @@ -150,13 +139,7 @@ exports.opIterator = function (opsStr, optStartIndex) { function next(optObj) { var op = (optObj || obj); - if (_opt && regexResult) { - op.attribs = regexResult.attribs(); - op.lines = regexResult.lines(); - op.chars = regexResult.chars(); - op.opcode = regexResult.opcode(); - regexResult = nextRegexMatch(); - } else if ((!_opt) && regexResult[0]) { + if (regexResult[0]) { op.attribs = regexResult[1]; op.lines = exports.parseNum(regexResult[2] || 0); op.opcode = regexResult[3]; @@ -169,7 +152,7 @@ exports.opIterator = function (opsStr, optStartIndex) { } function hasNext() { - return !!(_opt ? regexResult : regexResult[0]); + return !!(regexResult[0]); } function lastIndex() { @@ -414,159 +397,109 @@ exports.smartOpAssembler = function () { }; }; -if (_opt) { - exports.mergingOpAssembler = function () { - var assem = _opt.mergingOpAssembler(); - function append(op) { - assem.append(op.opcode, op.chars, op.lines, op.attribs); - } +exports.mergingOpAssembler = function () { + // This assembler can be used in production; it efficiently + // merges consecutive operations that are mergeable, ignores + // no-ops, and drops final pure "keeps". It does not re-order + // operations. + var assem = exports.opAssembler(); + var bufOp = exports.newOp(); - function toString() { - return assem.toString(); - } + // If we get, for example, insertions [xxx\n,yyy], those don't merge, + // but if we get [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n]. + // This variable stores the length of yyy and any other newline-less + // ops immediately after it. + var bufOpAdditionalCharsAfterNewline = 0; - function clear() { - assem.clear(); - } - - function endDocument() { - assem.endDocument(); - } - - return { - append: append, - toString: toString, - clear: clear, - endDocument: endDocument - }; - }; -} else { - exports.mergingOpAssembler = function () { - // This assembler can be used in production; it efficiently - // merges consecutive operations that are mergeable, ignores - // no-ops, and drops final pure "keeps". It does not re-order - // operations. - var assem = exports.opAssembler(); - var bufOp = exports.newOp(); - - // If we get, for example, insertions [xxx\n,yyy], those don't merge, - // but if we get [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n]. - // This variable stores the length of yyy and any other newline-less - // ops immediately after it. - var bufOpAdditionalCharsAfterNewline = 0; - - function flush(isEndDocument) { - if (bufOp.opcode) { - if (isEndDocument && bufOp.opcode == '=' && !bufOp.attribs) { - // final merged keep, leave it implicit - } else { + function flush(isEndDocument) { + if (bufOp.opcode) { + if (isEndDocument && bufOp.opcode == '=' && !bufOp.attribs) { + // final merged keep, leave it implicit + } else { + assem.append(bufOp); + if (bufOpAdditionalCharsAfterNewline) { + bufOp.chars = bufOpAdditionalCharsAfterNewline; + bufOp.lines = 0; assem.append(bufOp); - if (bufOpAdditionalCharsAfterNewline) { - bufOp.chars = bufOpAdditionalCharsAfterNewline; - bufOp.lines = 0; - assem.append(bufOp); - bufOpAdditionalCharsAfterNewline = 0; - } + bufOpAdditionalCharsAfterNewline = 0; } - bufOp.opcode = ''; } + bufOp.opcode = ''; } + } - function append(op) { - if (op.chars > 0) { - if (bufOp.opcode == op.opcode && bufOp.attribs == op.attribs) { - if (op.lines > 0) { - // bufOp and additional chars are all mergeable into a multi-line op - bufOp.chars += bufOpAdditionalCharsAfterNewline + op.chars; - bufOp.lines += op.lines; - bufOpAdditionalCharsAfterNewline = 0; - } else if (bufOp.lines == 0) { - // both bufOp and op are in-line - bufOp.chars += op.chars; - } else { - // append in-line text to multi-line bufOp - bufOpAdditionalCharsAfterNewline += op.chars; - } + function append(op) { + if (op.chars > 0) { + if (bufOp.opcode == op.opcode && bufOp.attribs == op.attribs) { + if (op.lines > 0) { + // bufOp and additional chars are all mergeable into a multi-line op + bufOp.chars += bufOpAdditionalCharsAfterNewline + op.chars; + bufOp.lines += op.lines; + bufOpAdditionalCharsAfterNewline = 0; + } else if (bufOp.lines == 0) { + // both bufOp and op are in-line + bufOp.chars += op.chars; } else { - flush(); - exports.copyOp(op, bufOp); + // append in-line text to multi-line bufOp + bufOpAdditionalCharsAfterNewline += op.chars; } + } else { + flush(); + exports.copyOp(op, bufOp); } } + } - function endDocument() { - flush(true); - } + function endDocument() { + flush(true); + } - function toString() { - flush(); - return assem.toString(); - } + function toString() { + flush(); + return assem.toString(); + } - function clear() { - assem.clear(); - exports.clearOp(bufOp); - } - return { - append: append, - toString: toString, - clear: clear, - endDocument: endDocument - }; + function clear() { + assem.clear(); + exports.clearOp(bufOp); + } + return { + append: append, + toString: toString, + clear: clear, + endDocument: endDocument }; -} +}; -if (_opt) { - exports.opAssembler = function () { - var assem = _opt.opAssembler(); - // this function allows op to be mutated later (doesn't keep a ref) - function append(op) { - assem.append(op.opcode, op.chars, op.lines, op.attribs); + +exports.opAssembler = function () { + var pieces = []; + // this function allows op to be mutated later (doesn't keep a ref) + + function append(op) { + pieces.push(op.attribs); + if (op.lines) { + pieces.push('|', exports.numToString(op.lines)); } + pieces.push(op.opcode); + pieces.push(exports.numToString(op.chars)); + } - function toString() { - return assem.toString(); - } + function toString() { + return pieces.join(''); + } - function clear() { - assem.clear(); - } - return { - append: append, - toString: toString, - clear: clear - }; + function clear() { + pieces.length = 0; + } + return { + append: append, + toString: toString, + clear: clear }; -} else { - exports.opAssembler = function () { - var pieces = []; - // this function allows op to be mutated later (doesn't keep a ref) - - function append(op) { - pieces.push(op.attribs); - if (op.lines) { - pieces.push('|', exports.numToString(op.lines)); - } - pieces.push(op.opcode); - pieces.push(exports.numToString(op.chars)); - } - - function toString() { - return pieces.join(''); - } - - function clear() { - pieces.length = 0; - } - return { - append: append, - toString: toString, - clear: clear - }; - }; -} +}; /** * A custom made String Iterator From 4c8f69b7c55f1a8ba8e8d338c2cee6bac0cc85c4 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Sun, 8 Jul 2012 18:59:46 +0200 Subject: [PATCH 02/17] Use v8 to parse settings.json --- src/node/utils/Settings.js | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index e60446df4..aeeb9015e 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -24,6 +24,7 @@ var os = require("os"); var path = require('path'); var argv = require('./Cli').argv; var npm = require("npm/lib/npm.js"); +var vm = require('vm'); /* Root path of the installation */ exports.root = path.normalize(path.join(npm.dir, "..")); @@ -45,6 +46,7 @@ exports.dbType = "dirty"; * This setting is passed with dbType to ueberDB to set up the database */ exports.dbSettings = { "filename" : path.join(exports.root, "dirty.db") }; + /** * The default Text of a new pad */ @@ -102,33 +104,25 @@ exports.abiwordAvailable = function() // Discover where the settings file lives var settingsFilename = argv.settings || "settings.json"; -if (settingsFilename.charAt(0) != '/') { - settingsFilename = path.normalize(path.join(root, settingsFilename)); -} +settingsFilename = path.resolve(path.join(root, settingsFilename)); -var settingsStr +var settingsStr; try{ //read the settings sync - settingsStr = fs.readFileSync(settingsFilename).toString(); + settingsStr = fs.readFileSync(settingsFilename); } catch(e){ console.warn('No settings file found. Using defaults.'); - settingsStr = '{}'; } - -//remove all comments -settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); -//try to parse the settings +// try to parse the settings var settings; -try -{ - settings = JSON.parse(settingsStr); -} -catch(e) -{ - console.error("There is a syntax error in your settings.json file"); - console.error(e.message); - process.exit(1); +try { + if(settingsStr) { + settings = vm.runInContext('exports = '+settingsStr, vm.createContext(), "settings.json"); + } +}catch(e){ + console.warn('There was an error processing your settings.json file. Using defaults.'); + console.warn(e.message); } //loop trough the settings From 975171a86b1c3eae8bebb91e1b87f6efcf990ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bartelme=C3=9F?= Date: Sun, 8 Jul 2012 21:06:19 +0200 Subject: [PATCH 03/17] Make handleMessage async --- src/node/handler/PadMessageHandler.js | 34 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index a0aef6648..d02e65377 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -159,11 +159,7 @@ exports.handleDisconnect = function(client) */ exports.handleMessage = function(client, message) { - _.map(hooks.callAll( "handleMessage", { client: client, message: message }), function ( newmessage ) { - if ( newmessage || newmessage === null ) { - message = newmessage; - } - }); + if(message == null) { messageLogger.warn("Message is null!"); @@ -175,6 +171,23 @@ exports.handleMessage = function(client, message) return; } + var handleMessageHook = function(callback){ + var dropMessage = false; + + // Call handleMessage hook. If a plugin returns null, the message will be dropped. Note that for all messages + // handleMessage will be called, even if the client is not authorized + hooks.aCallAll("handleMessage", { client: client, message: message }, function ( messages ) { + _.each(messages, function(newMessage){ + if ( newmessage === null ) { + dropMessage = true; + } + }); + + // If no plugins explicitly told us to drop the message, its ok to proceed + if(!dropMessage){ callback() }; + }); + } + var finalHandler = function () { //Check what type of message we get and delegate to the other methodes if(message.type == "CLIENT_READY") { @@ -203,11 +216,18 @@ exports.handleMessage = function(client, message) } }; - if (message && message.padId) { + if (message) { async.series([ + handleMessageHook, //check permissions function(callback) { + + if(!message.padId){ + // If the message has a padId we assume the client is already known to the server and needs no re-authorization + callback(); + return; + } // Note: message.sessionID is an entirely different kind of // session from the sessions we use here! Beware! FIXME: Call // our "sessions" "connections". @@ -231,8 +251,6 @@ exports.handleMessage = function(client, message) }, finalHandler ]); - } else { - finalHandler(); } } From 885844667887411c8fc2822ab1914b7277bcb4ca Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 10 Jul 2012 21:38:14 +0200 Subject: [PATCH 04/17] Exit on error. --- src/node/utils/Settings.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index aeeb9015e..68e58eb22 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -111,7 +111,8 @@ try{ //read the settings sync settingsStr = fs.readFileSync(settingsFilename); } catch(e){ - console.warn('No settings file found. Using defaults.'); + console.error('No settings file found.'); + process.exit(1); } // try to parse the settings @@ -121,8 +122,8 @@ try { settings = vm.runInContext('exports = '+settingsStr, vm.createContext(), "settings.json"); } }catch(e){ - console.warn('There was an error processing your settings.json file. Using defaults.'); - console.warn(e.message); + console.error('There was an error processing your settings.json file: '+e.message); + process.exit(1); } //loop trough the settings @@ -142,8 +143,7 @@ for(var i in settings) //this setting is unkown, output a warning and throw it away else { - console.warn("Unkown Setting: '" + i + "'"); - console.warn("This setting doesn't exist or it was removed"); + console.warn("Unkown Setting: '" + i + "'. This setting doesn't exist or it was removed"); } } From 87f26334d1259f6422c3a2b7d9325b611c72316b Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 10 Jul 2012 21:55:35 +0200 Subject: [PATCH 05/17] Fix typo. --- src/node/utils/Settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 68e58eb22..205b675f2 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -143,7 +143,7 @@ for(var i in settings) //this setting is unkown, output a warning and throw it away else { - console.warn("Unkown Setting: '" + i + "'. This setting doesn't exist or it was removed"); + console.warn("Unknown Setting: '" + i + "'. This setting doesn't exist or it was removed"); } } From f09dd0f3fba8630d221dba3a652dad0a229aa509 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 11 Jul 2012 15:34:33 +0200 Subject: [PATCH 06/17] Put toString() back in. --- src/node/utils/Settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 205b675f2..1b7c3f46c 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -109,7 +109,7 @@ settingsFilename = path.resolve(path.join(root, settingsFilename)); var settingsStr; try{ //read the settings sync - settingsStr = fs.readFileSync(settingsFilename); + settingsStr = fs.readFileSync(settingsFilename).toString(); } catch(e){ console.error('No settings file found.'); process.exit(1); From dc09323d8f6ee5a65f301bb677d9e18a54672322 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 11 Jul 2012 15:36:41 +0200 Subject: [PATCH 07/17] Don't exit if no settings file was found. --- src/node/utils/Settings.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 1b7c3f46c..dd34ac5ee 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -111,8 +111,7 @@ try{ //read the settings sync settingsStr = fs.readFileSync(settingsFilename).toString(); } catch(e){ - console.error('No settings file found.'); - process.exit(1); + console.warn('No settings file found. Continuing using defaults!'); } // try to parse the settings From 6726ea66320e3075451027d5362d35d77bebaf5d Mon Sep 17 00:00:00 2001 From: John McLear Date: Wed, 11 Jul 2012 17:42:59 +0100 Subject: [PATCH 08/17] option to stop autoscroll --- src/static/js/chat.js | 7 +++++-- src/templates/pad.html | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/static/js/chat.js b/src/static/js/chat.js index 47b0ae3ca..b2a7d2737 100644 --- a/src/static/js/chat.js +++ b/src/static/js/chat.js @@ -62,8 +62,11 @@ var chat = (function() }, scrollDown: function() { - if($('#chatbox').css("display") != "none") - $('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, "slow"); + if($('#options-scrollchat').is(':checked')){ + if($('#chatbox').css("display") != "none"){ + $('#chattext').animate({scrollTop: $('#chattext')[0].scrollHeight}, "slow"); + } + } }, send: function() { diff --git a/src/templates/pad.html b/src/templates/pad.html index 02af9b107..a25133472 100644 --- a/src/templates/pad.html +++ b/src/templates/pad.html @@ -168,6 +168,10 @@

+

+ + +

Font type:

-

- - -

Font type: