From b3913ac1235f9bf4a7edf810b07e9bc80609b13f Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 6 Jul 2016 08:08:20 +0200 Subject: [PATCH] Moved login code to it's own method; using System.console() (if possible) to let the user enter his password without it being echoed to the console. --- .../CommandLineController.java | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index 294a70d..9217c7c 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -98,26 +98,7 @@ public class CommandLineController { if (options.cmd_login) { - System.out.println("Please enter your phone number in international format."); - System.out.println("Example: +4917077651234"); - System.out.print("> "); - String phone = new Scanner(System.in).nextLine(); - user.sendCodeToPhoneNumber(phone); - - System.out.println("Telegram sent you a code. Please enter it here."); - System.out.print("> "); - String code = new Scanner(System.in).nextLine(); - user.verifyCode(code); - - if (user.isPasswordNeeded()) { - System.out.println("We also need your account password."); - System.out.print("> "); - String pw = new Scanner(System.in).nextLine(); - user.verifyPassword(pw); - } - storage.setPrefix("+" + user.getUser().getPhone()); - - System.out.println("Please run this tool with '--account +" + user.getUser().getPhone() + " to use this account."); + cmd_login(); System.exit(0); } @@ -137,6 +118,43 @@ public class CommandLineController { System.out.println("If this program doesn't exit by itself, please press Ctrl-C now."); } + private void cmd_login() throws RpcErrorException, IOException { + System.out.println("Please enter your phone number in international format."); + System.out.println("Example: +4917077651234"); + String phone = getLine(); + user.sendCodeToPhoneNumber(phone); + + System.out.println("Telegram sent you a code. Please enter it here."); + String code = getLine(); + user.verifyCode(code); + + if (user.isPasswordNeeded()) { + System.out.println("We also need your account password."); + String pw = getPassword(); + user.verifyPassword(pw); + } + storage.setPrefix("+" + user.getUser().getPhone()); + + System.out.println("Please run this tool with '--account +" + user.getUser().getPhone() + " to use this account."); + } + + private String getLine() { + if (System.console()!=null) { + return System.console().readLine("> "); + } else { + System.out.print("> "); + return new Scanner(System.in).nextLine(); + } + } + + private String getPassword() { + if (System.console()!=null) { + return String.valueOf(System.console().readPassword("> ")); + } else { + return getLine(); + } + } + private void show_help() { System.out.println("Valid options are:"); System.out.println(" -h, --help Shows this help.");