OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/client/client_util.h" | 5 #include "remoting/client/client_util.h" |
6 | 6 |
7 #include <iostream> | 7 #include <iostream> |
8 | 8 |
9 static void SetConsoleEcho(bool on) { | |
10 #ifdef WIN32 | |
11 HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); | |
12 if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL)) | |
13 return; | |
14 | |
15 DWORD mode; | |
16 if (!GetConsoleMode(hIn, &mode)) | |
17 return; | |
18 | |
19 if (on) { | |
20 mode = mode | ENABLE_ECHO_INPUT; | |
21 } else { | |
22 mode = mode & ~ENABLE_ECHO_INPUT; | |
23 } | |
24 | |
25 SetConsoleMode(hIn, mode); | |
26 #else | |
27 if (on) | |
28 system("stty echo"); | |
29 else | |
30 system("stty -echo"); | |
31 #endif | |
32 } | |
33 | |
34 namespace remoting { | 9 namespace remoting { |
35 | 10 |
36 // Get host JID from command line arguments, or stdin if not specified. | 11 // Get host JID from command line arguments, or stdin if not specified. |
37 bool GetLoginInfo(std::string& host_jid, | 12 bool GetLoginInfo(std::string& host_jid, |
38 std::string& username, | 13 std::string& username, |
39 std::string& password) { | 14 std::string& auth_token) { |
40 std::cout << "Host JID: "; | 15 std::cout << "Host JID: "; |
41 std::cin >> host_jid; | 16 std::cin >> host_jid; |
42 std::cin.ignore(); // Consume the leftover '\n' | 17 std::cin.ignore(); // Consume the leftover '\n' |
43 | 18 |
44 if (host_jid.find("/chromoting") == std::string::npos) { | 19 if (host_jid.find("/chromoting") == std::string::npos) { |
45 std::cerr << "Error: Expected Host JID in format: <jid>/chromoting<id>" | 20 std::cerr << "Error: Expected Host JID in format: <jid>/chromoting<id>" |
46 << std::endl; | 21 << std::endl; |
47 return false; | 22 return false; |
48 } | 23 } |
49 | 24 |
50 // Get username (JID). | 25 // Get username (JID). |
51 // Extract default JID from host_jid. | 26 // Extract default JID from host_jid. |
52 std::string default_username; | 27 std::string default_username; |
53 size_t jid_end = host_jid.find('/'); | 28 size_t jid_end = host_jid.find('/'); |
54 if (jid_end != std::string::npos) { | 29 if (jid_end != std::string::npos) { |
55 default_username = host_jid.substr(0, jid_end); | 30 default_username = host_jid.substr(0, jid_end); |
56 } | 31 } |
57 std::cout << "JID [" << default_username << "]: "; | 32 std::cout << "JID [" << default_username << "]: "; |
58 getline(std::cin, username); | 33 getline(std::cin, username); |
59 if (username.length() == 0) { | 34 if (username.length() == 0) { |
60 username = default_username; | 35 username = default_username; |
61 } | 36 } |
62 if (username.length() == 0) { | 37 if (username.length() == 0) { |
63 std::cerr << "Error: Expected valid JID username" << std::endl; | 38 std::cerr << "Error: Expected valid JID username" << std::endl; |
64 return 1; | 39 return 1; |
65 } | 40 } |
66 | 41 |
67 // Get password (with console echo turned off). | 42 // Get authenication token (with console echo turned off). |
68 SetConsoleEcho(false); | 43 std::cout << "Auth token: "; |
69 std::cout << "Password: "; | 44 getline(std::cin, auth_token); |
70 getline(std::cin, password); | |
71 SetConsoleEcho(true); | |
72 std::cout << std::endl; | 45 std::cout << std::endl; |
73 return true; | 46 return true; |
74 } | 47 } |
75 | 48 |
76 } // namespace remoting | 49 } // namespace remoting |
OLD | NEW |