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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Jingle call example
* Copyright 2004--2005, Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <time.h>
#include <iomanip>
#include "talk/base/logging.h"
#include "talk/base/physicalsocketserver.h"
#include "talk/base/ssladapter.h"
#include "talk/xmpp/xmppclientsettings.h"
#include "talk/examples/login/xmppthread.h"
#include "talk/examples/login/xmppauth.h"
#include "talk/examples/call/callclient.h"
#include "talk/examples/call/console.h"
#if defined(_MSC_VER) && (_MSC_VER < 1400)
// The following are necessary to properly link when compiling STL without
// /EHsc, otherwise known as C++ exceptions.
void __cdecl std::_Throw(const std::exception &) {}
std::_Prhand std::_Raise_handler = 0;
#endif
void SetConsoleEcho(bool on) {
#ifdef WIN32
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
return;
DWORD mode;
if (!GetConsoleMode(hIn, &mode))
return;
if (on) {
mode = mode | ENABLE_ECHO_INPUT;
} else {
mode = mode & ~ENABLE_ECHO_INPUT;
}
SetConsoleMode(hIn, mode);
#else
if (on)
system("stty echo");
else
system("stty -echo");
#endif
}
class DebugLog : public sigslot::has_slots<> {
public:
DebugLog() :
debug_input_buf_(NULL), debug_input_len_(0), debug_input_alloc_(0),
debug_output_buf_(NULL), debug_output_len_(0), debug_output_alloc_(0),
censor_password_(false)
{}
char * debug_input_buf_;
int debug_input_len_;
int debug_input_alloc_;
char * debug_output_buf_;
int debug_output_len_;
int debug_output_alloc_;
bool censor_password_;
void Input(const char * data, int len) {
if (debug_input_len_ + len > debug_input_alloc_) {
char * old_buf = debug_input_buf_;
debug_input_alloc_ = 4096;
while (debug_input_alloc_ < debug_input_len_ + len) {
debug_input_alloc_ *= 2;
}
debug_input_buf_ = new char[debug_input_alloc_];
memcpy(debug_input_buf_, old_buf, debug_input_len_);
delete[] old_buf;
}
memcpy(debug_input_buf_ + debug_input_len_, data, len);
debug_input_len_ += len;
DebugPrint(debug_input_buf_, &debug_input_len_, false);
}
void Output(const char * data, int len) {
if (debug_output_len_ + len > debug_output_alloc_) {
char * old_buf = debug_output_buf_;
debug_output_alloc_ = 4096;
while (debug_output_alloc_ < debug_output_len_ + len) {
debug_output_alloc_ *= 2;
}
debug_output_buf_ = new char[debug_output_alloc_];
memcpy(debug_output_buf_, old_buf, debug_output_len_);
delete[] old_buf;
}
memcpy(debug_output_buf_ + debug_output_len_, data, len);
debug_output_len_ += len;
DebugPrint(debug_output_buf_, &debug_output_len_, true);
}
static bool
IsAuthTag(const char * str, size_t len) {
if (str[0] == '<' && str[1] == 'a' &&
str[2] == 'u' &&
str[3] == 't' &&
str[4] == 'h' &&
str[5] <= ' ') {
std::string tag(str, len);
if (tag.find("mechanism") != std::string::npos)
return true;
}
return false;
}
void
DebugPrint(char * buf, int * plen, bool output) {
int len = *plen;
if (len > 0) {
time_t tim = time(NULL);
struct tm * now = localtime(&tim);
char *time_string = asctime(now);
if (time_string) {
size_t time_len = strlen(time_string);
if (time_len > 0) {
time_string[time_len-1] = 0; // trim off terminating \n
}
}
LOG(INFO) << (output ? "SEND >>>>>>>>>>>>>>>>>>>>>>>>>" : "RECV <<<<<<<<<<<<<<<<<<<<<<<<<")
<< " : " << time_string;
bool indent;
int start = 0, nest = 3;
for (int i = 0; i < len; i += 1) {
if (buf[i] == '>') {
if ((i > 0) && (buf[i-1] == '/')) {
indent = false;
} else if ((start + 1 < len) && (buf[start + 1] == '/')) {
indent = false;
nest -= 2;
} else {
indent = true;
}
// Output a tag
LOG(INFO) << std::setw(nest) << " " << std::string(buf + start, i + 1 - start);
if (indent)
nest += 2;
// Note if it's a PLAIN auth tag
if (IsAuthTag(buf + start, i + 1 - start)) {
censor_password_ = true;
}
// incr
start = i + 1;
}
if (buf[i] == '<' && start < i) {
if (censor_password_) {
LOG(INFO) << std::setw(nest) << " " << "## TEXT REMOVED ##";
censor_password_ = false;
}
else {
LOG(INFO) << std::setw(nest) << " " << std::string(buf + start, i - start);
}
start = i;
}
}
len = len - start;
memcpy(buf, buf + start, len);
*plen = len;
}
}
};
static DebugLog debug_log_;
int main(int argc, char **argv) {
// This app has three threads. The main thread will run the XMPP client,
// which will print to the screen in its own thread. A second thread
// will get input from the console, parse it, and pass the appropriate
// message back to the XMPP client's thread. A third thread is used
// by PhoneSessionClient as its worker thread.
bool debug = false;
if (argc > 1 && !strcmp(argv[1], "-d"))
debug = true;
if (debug)
talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE);
talk_base::InitializeSSL();
XmppPump pump;
buzz::Jid jid;
buzz::XmppClientSettings xcs;
talk_base::InsecureCryptStringImpl pass;
std::string username;
std::cout << "JID: ";
std::cin >> username;
jid = buzz::Jid(username);
if (!jid.IsValid() || jid.node() == "") {
printf("Invalid JID. JIDs should be in the form user@domain\n");
return 1;
}
SetConsoleEcho(false);
std::cout << "Password: ";
std::cin >> pass.password();
SetConsoleEcho(true);
std::cout << std::endl;
xcs.set_user(jid.node());
xcs.set_resource("call");
xcs.set_host(jid.domain());
xcs.set_use_tls(true);
xcs.set_pass(talk_base::CryptString(pass));
xcs.set_server(talk_base::SocketAddress("talk.google.com", 5222));
printf("Logging in as %s\n", jid.Str().c_str());
talk_base::PhysicalSocketServer ss;
CallClient *client = new CallClient(pump.client());
talk_base::Thread main_thread(&ss);
talk_base::ThreadManager::SetCurrent(&main_thread);
Console *console = new Console(&main_thread, client);
client->SetConsole(console);
talk_base::Thread *console_thread = new talk_base::Thread(&ss);
console_thread->Start();
console_thread->Post(console, MSG_START);
if (debug) {
pump.client()->SignalLogInput.connect(&debug_log_, &DebugLog::Input);
pump.client()->SignalLogOutput.connect(&debug_log_, &DebugLog::Output);
}
pump.DoLogin(xcs, new XmppSocket(true), NULL);
main_thread.Run();
return 0;
}
|