1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

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.

This commit is contained in:
Fabian Schlenz 2016-07-06 08:08:20 +02:00
parent 2cb1d717b3
commit b3913ac123

View File

@ -98,26 +98,7 @@ public class CommandLineController {
if (options.cmd_login) { if (options.cmd_login) {
System.out.println("Please enter your phone number in international format."); cmd_login();
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.");
System.exit(0); 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."); 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() { private void show_help() {
System.out.println("Valid options are:"); System.out.println("Valid options are:");
System.out.println(" -h, --help Shows this help."); System.out.println(" -h, --help Shows this help.");