From 7aae0482a1c2a984b634a9eec9aabcfe4efec369 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 10 Jun 2019 21:22:16 +0200 Subject: Update dependencies, and fix stdin input with new node --- client.js | 121 ++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 79 insertions(+), 42 deletions(-) (limited to 'client.js') diff --git a/client.js b/client.js index 5313b0a..efd721c 100755 --- a/client.js +++ b/client.js @@ -4,7 +4,7 @@ var fs=require("fs"), path=require("path"), https=require("https"), crypto=require("crypto"), - kbd=require("kbd"), + read=require("read"), toClipboard=require("to-clipboard"), notifier=require("node-notifier"); @@ -274,53 +274,90 @@ function registerUser(userid,password){ req.end(password); } +var stdinFD = null; +if (!process.stdin.isTTY) { + stdinFD = fs.openSync("/dev/stdin", "r"); +} + +function readStdinLine(options, callback) { + console.log("Entering read with " + JSON.stringify(options)); + if (stdinFD) { + var input = "", buf = Buffer.alloc(1), ch; + while (true) { + if (fs.readSync(stdinFD, buf, 0, 1, null) < 1) { + console.log("Error reading from stdin"); + process.exit(1); + } + ch = buf.toString() + if (ch == "\n") break; + input += ch; + } + callback(input); + } else { + read(options, function(err, res) { + console.log("Returned from read with " + JSON.stringify(options)); + if (err) { + console.log("Error reading from stdin: " + err); + process.exit(1); + } + callback(res); + }); + } +} + +function readUserPass(callback) { + readStdinLine({prompt: "Username? "}, function(res) { + userid = res; + + readStdinLine({prompt: "Password? ", silent: true}, function(passinput) { + var hasher = crypto.createHash("sha256"); + hasher.update(passinput); + password = hasher.digest("hex"); + + callback(); + }); + }); +} -process.stdout.write("Username? "); -userid=kbd.getLineSync().replace(/[^a-zA-Z0-9_-]/g,""); -process.stdout.write("Password? "); -(function(){ - var hasher=crypto.createHash("sha256"); - kbd.setEcho(false); - var passinput=kbd.getLineSync(); - hasher.update(passinput); - kbd.setEcho(true); - password=hasher.digest("hex"); -})(); -console.log("\nChecking existence..."); -userExists(userid,function(exists){ - if(exists){ - checkLogin(userid,password,function(ok){ - if(ok)console.log("User login ok."); +readUserPass(function() { + console.log("\nChecking existence..."); + + userExists(userid,function(exists){ + if(exists){ + checkLogin(userid,password,function(ok){ + if(ok)console.log("User login ok."); + else { + console.log("Username or password incorrect!"); + process.exit(); + } + }); + return; + } + readStdinLine({prompt: "That username doesn't seem to exist. Register it? [y/N] "}, function(response) { + response = response[0]; + if(response=="y"||response=="Y")registerUser(userid,password); else { - console.log("Username or password incorrect!"); + console.log("Not registered. Exiting."); process.exit(); } }); - return; - } - process.stdout.write("That username doesn't seem to exist. Register it? [y/N] "); - var response=kbd.getLineSync()[0]; - if(response=="y"||response=="Y")registerUser(userid,password); - else { - console.log("Not registered. Exiting."); - process.exit(); - } -}); + }); -var timeout=null; -var watcher=fs.watch(WATCHDIR,{persistent:true,recursive:false},function(ev,fname){ - //console.log("change in directory "+WATCHDIR+" (fname "+fname+")"); - if(timeout)return; - timeout=setTimeout(function(){ - var newstate=collectDirState(WATCHDIR); - var changes=collectChanges(WATCHDIR,newstate).map(function(o){o.name=o.name.replace(/^\.\//,"");return o;}); - currentState=newstate; - if(changes.length!=0)handleChanges(changes); - timeout=null; - //console.log(currentState); - },500); + var timeout=null; + var watcher=fs.watch(WATCHDIR,{persistent:true,recursive:false},function(ev,fname){ + //console.log("change in directory "+WATCHDIR+" (fname "+fname+")"); + if(timeout)return; + timeout=setTimeout(function(){ + var newstate=collectDirState(WATCHDIR); + var changes=collectChanges(WATCHDIR,newstate).map(function(o){o.name=o.name.replace(/^\.\//,"");return o;}); + currentState=newstate; + if(changes.length!=0)handleChanges(changes); + timeout=null; + //console.log(currentState); + },500); + }); + currentState=collectDirState(WATCHDIR); + console.log("-- (Client ready.)"); }); -currentState=collectDirState(WATCHDIR); -console.log("-- (Client ready.)"); -- cgit v1.2.3-54-g00ecf