henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2013, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | // |
| 29 | // A simple text chat application, largely copied from examples/call. |
| 30 | // |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | #include "talk/base/logging.h" |
| 35 | #include "talk/base/ssladapter.h" |
| 36 | |
| 37 | #ifdef OSX |
| 38 | #include "talk/base/maccocoasocketserver.h" |
| 39 | #elif defined(WIN32) |
| 40 | #include "talk/base/win32socketserver.h" |
| 41 | #else |
| 42 | #include "talk/base/physicalsocketserver.h" |
| 43 | #endif |
| 44 | |
| 45 | #include "talk/xmpp/constants.h" |
| 46 | #include "talk/xmpp/xmppauth.h" |
| 47 | #include "talk/xmpp/xmppclientsettings.h" |
| 48 | #include "talk/xmpp/xmpppump.h" |
| 49 | #include "talk/xmpp/xmppsocket.h" |
| 50 | |
| 51 | #include "talk/examples/chat/chatapp.h" |
| 52 | #include "talk/examples/chat/consoletask.h" |
| 53 | |
| 54 | static const int kDefaultPort = 5222; |
| 55 | |
| 56 | int main(int argc, char* argv[]) { |
| 57 | // TODO(pmclean): Remove duplication of code with examples/call. |
| 58 | // Set up debugging. |
| 59 | bool debug = true; |
| 60 | if (debug) { |
| 61 | talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE); |
| 62 | } |
| 63 | |
| 64 | // Set up the crypto subsystem. |
| 65 | talk_base::InitializeSSL(); |
| 66 | |
| 67 | // Parse username and password, if present. |
| 68 | buzz::Jid jid; |
| 69 | std::string username; |
| 70 | talk_base::InsecureCryptStringImpl pass; |
| 71 | if (argc > 1) { |
| 72 | username = argv[1]; |
| 73 | if (argc > 2) { |
| 74 | pass.password() = argv[2]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // ... else prompt for them |
| 79 | if (username.empty()) { |
| 80 | printf("JID: "); |
| 81 | std::cin >> username; |
| 82 | } |
| 83 | if (username.find('@') == std::string::npos) { |
| 84 | username.append("@localhost"); |
| 85 | } |
| 86 | |
| 87 | jid = buzz::Jid(username); |
| 88 | if (!jid.IsValid() || jid.node() == "") { |
| 89 | printf("Invalid JID. JIDs should be in the form user@domain\n"); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | if (pass.password().empty()) { |
| 94 | buzz::ConsoleTask::SetEcho(false); |
| 95 | printf("Password: "); |
| 96 | std::cin >> pass.password(); |
| 97 | buzz::ConsoleTask::SetEcho(true); |
| 98 | printf("\n"); |
| 99 | } |
| 100 | |
| 101 | // OTP (this can be skipped) |
| 102 | std::string otp_token; |
| 103 | printf("OTP: "); |
| 104 | fflush(stdin); |
| 105 | std::getline(std::cin, otp_token); |
| 106 | |
| 107 | // Setup the connection settings. |
| 108 | buzz::XmppClientSettings xcs; |
| 109 | xcs.set_user(jid.node()); |
| 110 | xcs.set_resource("chat"); |
| 111 | xcs.set_host(jid.domain()); |
| 112 | bool allow_plain = false; |
| 113 | xcs.set_allow_plain(allow_plain); |
| 114 | xcs.set_use_tls(buzz::TLS_REQUIRED); |
| 115 | xcs.set_pass(talk_base::CryptString(pass)); |
| 116 | if (!otp_token.empty() && *otp_token.c_str() != '\n') { |
| 117 | xcs.set_auth_token(buzz::AUTH_MECHANISM_OAUTH2, otp_token); |
| 118 | } |
| 119 | |
| 120 | // Build the server spec |
| 121 | std::string host; |
| 122 | int port; |
| 123 | |
| 124 | std::string server = "talk.google.com"; |
| 125 | int colon = server.find(':'); |
| 126 | if (colon == -1) { |
| 127 | host = server; |
| 128 | port = kDefaultPort; |
| 129 | } else { |
| 130 | host = server.substr(0, colon); |
| 131 | port = atoi(server.substr(colon + 1).c_str()); |
| 132 | } |
| 133 | xcs.set_server(talk_base::SocketAddress(host, port)); |
| 134 | |
| 135 | talk_base::Thread* main_thread = talk_base::Thread::Current(); |
| 136 | #if WIN32 |
| 137 | // Need to pump messages on our main thread on Windows. |
| 138 | talk_base::Win32Thread w32_thread; |
| 139 | talk_base::ThreadManager::Instance()->SetCurrentThread(&w32_thread); |
| 140 | #elif defined(OSX) |
| 141 | talk_base::MacCocoaSocketServer ss; |
| 142 | talk_base::SocketServerScope ss_scope(&ss); |
| 143 | #else |
| 144 | talk_base::PhysicalSocketServer ss; |
| 145 | #endif |
| 146 | |
| 147 | buzz::XmppPump* pump = new buzz::XmppPump(); |
| 148 | ChatApp *client = new ChatApp(pump->client(), main_thread); |
| 149 | |
| 150 | // Start pumping messages! |
| 151 | pump->DoLogin(xcs, new buzz::XmppSocket(buzz::TLS_REQUIRED), new XmppAuth()); |
| 152 | |
| 153 | main_thread->Run(); |
| 154 | pump->DoDisconnect(); |
| 155 | |
| 156 | delete client; |
| 157 | |
| 158 | return 0; |
| 159 | } |