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
|
#include "common.h"
void WhatsAppProto::stayConnectedLoop(void*)
{
std::string cc, in, pass;
DBVARIANT dbv = { 0 };
if (!getString(WHATSAPP_KEY_CC, &dbv)) {
cc = dbv.pszVal;
db_free(&dbv);
}
if (cc.empty()) {
NotifyEvent(m_tszUserName, TranslateT("Please enter a country code."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
if (!getString(WHATSAPP_KEY_LOGIN, &dbv)) {
in = dbv.pszVal;
db_free(&dbv);
}
if (in.empty()) {
NotifyEvent(m_tszUserName, TranslateT("Please enter a phone number without country code."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
this->phoneNumber = cc + in;
this->jid = this->phoneNumber + "@s.whatsapp.net";
if (!getString(WHATSAPP_KEY_NICK, &dbv)) {
this->nick = dbv.pszVal;
db_free(&dbv);
}
if (this->nick.empty()) {
NotifyEvent(m_tszUserName, TranslateT("Please enter a nickname."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
if (!getString(WHATSAPP_KEY_PASS, &dbv)) {
pass = dbv.pszVal;
db_free(&dbv);
}
if (pass.empty()) {
NotifyEvent(m_tszUserName, TranslateT("Please enter a password."), NULL, WHATSAPP_EVENT_CLIENT);
return;
}
// -----------------------------
Mutex writerMutex;
bool error = false;
this->conn = NULL;
while (true) {
if (m_pConnection != NULL) {
delete m_pConnection;
m_pConnection = NULL;
}
if (this->conn != NULL) {
delete this->conn;
this->conn = NULL;
}
if (m_iDesiredStatus == ID_STATUS_OFFLINE || error) {
debugLogA("Set status to offline");
SetAllContactStatuses(ID_STATUS_OFFLINE, true);
this->ToggleStatusMenuItems(false);
int prevStatus = m_iStatus;
m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, prevStatus);
break;
}
debugLogA("Connecting...");
try {
unsigned passLen;
ptrA passBin((char*)mir_base64_decode(pass.c_str(), &passLen));
std::string password(passBin, passLen), resource = ACCOUNT_RESOURCE;
int portNumber;
if (getByte(WHATSAPP_KEY_SSL, 0))
portNumber = 443, resource += "-443";
else
portNumber = 5222, resource += "-5222";
this->conn = new WASocketConnection("c.whatsapp.net", portNumber);
m_pConnection = new WAConnection(this->phoneNumber, resource, &this->connMutex, this, this);
m_pConnection->init(&writerMutex, this->conn);
{
WALogin login(m_pConnection, password);
m_Challenge = login.login(m_Challenge);
m_pConnection->setLogin(&login);
}
m_pConnection->nick = this->nick;
m_pConnection->setVerboseId(true);
if (m_iDesiredStatus != ID_STATUS_INVISIBLE)
m_pConnection->sendAvailableForChat();
debugLogA("Set status to online");
m_iStatus = m_iDesiredStatus;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_CONNECTING);
this->ToggleStatusMenuItems(true);
// ProcessBuddyList(0);
// #TODO Move out of try block. Exception is expected on disconnect
while (true) {
this->lastPongTime = time(NULL);
if (!m_pConnection->read())
break;
}
debugLogA("Exit from read-loop");
}
catch (WAException &e) {
debugLogA("Exception: %s", e.what());
error = true;
}
catch (exception &e) {
debugLogA("Exception: %s", e.what());
error = true;
}
catch (...) {
debugLogA("Unknown exception");
error = true;
}
}
debugLogA("Break out from loop");
}
void WhatsAppProto::sentinelLoop(void*)
{
while (WaitForSingleObjectEx(update_loop_lock_, 1000, true) == WAIT_TIMEOUT) {
if (m_iStatus != ID_STATUS_OFFLINE && m_pConnection != NULL && m_iDesiredStatus == m_iStatus) {
// #TODO Quiet after pong or tree read?
int quietInterval = difftime(time(NULL), this->lastPongTime);
if (quietInterval >= MAX_SILENT_INTERVAL) {
try {
debugLogA("send ping");
this->lastPongTime = time(NULL);
m_pConnection->sendPing();
}
catch (exception &e) {
debugLogA("Exception: %s", e.what());
}
}
}
}
ResetEvent(update_loop_lock_);
debugLogA("Exiting sentinel loop");
}
void WhatsAppProto::onPing(const std::string& id)
{
if (this->isOnline()) {
try {
debugLogA("Sending pong with id %s", id.c_str());
m_pConnection->sendPong(id);
}
CODE_BLOCK_CATCH_ALL
}
}
|