summaryrefslogtreecommitdiffstats
path: root/remoting/client/client_util.cc
blob: ddaee424708ff1a7c6924be360a4a0c242872db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/client/client_util.h"

#include <iostream>

#include "base/logging.h"

namespace remoting {

// Get host JID from command line arguments, or stdin if not specified.
bool GetLoginInfo(int argc, char** argv,
                  std::string* host_jid,
                  std::string* username,
                  std::string* auth_token) {
  bool found_host_jid = false;
  bool found_jid = false;
  bool found_auth_token = false;

  for (int i = 1; i < argc; i++) {
    std::string arg = argv[i];
    if (arg == "--host_jid") {
      if (++i >= argc) {
        LOG(WARNING) << "Expected Host JID to follow --host_jid option"
                     << std::endl;
      } else {
        found_host_jid = true;
        *host_jid = argv[i];
      }
    } else if (arg == "--jid") {
      if (++i >= argc) {
        LOG(WARNING) << "Expected JID to follow --jid option" << std::endl;
      } else {
        found_jid = true;
        *username = argv[i];
      }
    } else if (arg == "--token") {
      if (++i >= argc) {
        LOG(WARNING) << "Expected Auth token to follow --token option"
                     << std::endl;
      } else {
        found_auth_token = true;
        *auth_token = argv[i];
      }
    } else {
      LOG(WARNING) << "Unrecognized option: " << arg << std::endl;
    }
  }

  if (!found_host_jid) {
    std::cout << "Host JID: ";
    std::cin >> *host_jid;
    std::cin.ignore();  // Consume the leftover '\n'
  }

  // Validate the chromoting host JID.
  if (host_jid->find("/chromoting") == std::string::npos) {
    std::cerr << "Error: Expected Host JID in format: <jid>/chromoting<id>"
              << std::endl;
    return false;
  }

  if (!found_jid) {
    // Get username (JID).
    // Extract default JID from host_jid.
    std::string default_username;
    size_t jid_end = host_jid->find('/');
    if (jid_end != std::string::npos) {
      default_username = host_jid->substr(0, jid_end);
    }
    std::cout << "JID [" << default_username << "]: ";
    getline(std::cin, *username);
    if (username->length() == 0) {
      username->swap(default_username);
    }
    if (username->length() == 0) {
      std::cerr << "Error: Expected valid JID username" << std::endl;
      return false;
    }
  }

  if (!found_auth_token) {
    // Get authentication token.
    std::cout << "Auth token: ";
    getline(std::cin, *auth_token);
    std::cout << std::endl;
  }

  return true;
}

}  // namespace remoting