d99kris / nchat

Terminal-based Telegram / WhatsApp client for Linux and macOS
MIT License
647 stars 45 forks source link

support password with space chars #2

Closed vilkoz closed 5 years ago

vilkoz commented 5 years ago

Currently program gets only part of password before the space characters. Fixed with patch provided.

d99kris commented 5 years ago

Hi @vilkoz - thanks for contributing. I noticed that the patch adds a getline call, but does not remove the previous std::cin >> assignment, so it feels like it wouldn't work correctly, did you test it? I don't have a password set up for my account though, so I haven't tested.

Anyway, I agree getline is much preferable to std::cin >>, but then all input needs to be read using getline, as getline and std::cin >> handles line-breaks differently.

I would propose something like this:

diff --git a/src/telegram.cpp b/src/telegram.cpp
index 7a7744a..9f8ce9f 100644
--- a/src/telegram.cpp
+++ b/src/telegram.cpp
@@ -444,13 +444,13 @@ void Telegram::OnAuthStateUpdate()
         if (!wait_code.is_registered_)
         {
           std::cout << "Enter your first name: ";
-          std::cin >> first_name;
+          std::getline(std::cin, first_name);
           std::cout << "Enter your last name: ";
-          std::cin >> last_name;
+          std::getline(std::cin, last_name);
         }
         std::cout << "Enter authentication code: ";
         std::string code;
-        std::cin >> code;
+        std::getline(std::cin, code);
         SendQuery(td::td_api::make_object<td::td_api::checkAuthenticationCode>(code, first_name,
                                                                                last_name),
                   CreateAuthQueryHandler());
@@ -467,7 +467,7 @@ void Telegram::OnAuthStateUpdate()
       {
         std::cout << "Enter authentication password: ";
         std::string password;
-        std::cin >> password;
+        std::getline(std::cin, password);
         SendQuery(td::td_api::make_object<td::td_api::checkAuthenticationPassword>(password),
                   CreateAuthQueryHandler());
       }
@@ -483,7 +483,7 @@ void Telegram::OnAuthStateUpdate()
       {
         std::cout << "Enter phone number: ";
         std::string phone_number;
-        std::cin >> phone_number;
+        std::getline(std::cin, phone_number);
         SendQuery(td::td_api::make_object<td::td_api::setAuthenticationPhoneNumber>(phone_number,
                                                                                     false,
                                                                                     false),

Feel free to update the pull request accordingly, or otherwise I'll push the change myself.

vilkoz commented 5 years ago

Hello @d99kris, that kind of error may happen when I push at 2 am ) I've applied std::getline to all user inputs in Telegram::OnAuthStateUpdate method as you proposed, and force-pushed them.

d99kris commented 5 years ago

Thanks @vilkoz, but for some reason your new changes does not seem to reflect here in the pull-request, see for example the "Files changed" tab above. I'm not very familiar with Git, but possibly it's an issue with force push to pull request https://stackoverflow.com/questions/50132325/github-advises-force-pushing-can-corrupt-your-pull-request-why

An alternative is perhaps to create a new pull request with all the changes.