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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
Copyright (c) 2015 Miranda NG project (http://miranda-ng.org)
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 version 2
of the License.
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, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
UINT_PTR CSkypeProto::m_timer;
void CSkypeProto::Login()
{
// login
m_iStatus = ID_STATUS_CONNECTING;
requestQueue->Start();
int tokenExpires(getDword("TokenExpiresIn", 0));
ptrA login(getStringA(SKYPE_SETTINGS_ID));
HistorySynced = isTerminated = false;
if ((tokenExpires - 1800) > time(NULL))
OnLoginSuccess();
else
{
if (strstr(login, "@"))
SendRequest(new LoginMSRequest(), &CSkypeProto::OnMSLoginFirst);
else
SendRequest(new LoginOAuthRequest(login, ptrA(getStringA(SKYPE_SETTINGS_PASSWORD))), &CSkypeProto::OnLoginOAuth);
}
}
void CSkypeProto::OnLoginOAuth(const NETLIBHTTPREQUEST *response)
{
if (response == NULL || response->pData == NULL)
{
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
JSONNode json = JSONNode::parse(response->pData);
if (!json)
{
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
if (response->resultCode != 200)
{
if (!json["status"].isnull())
{
const JSONNode &status = json["status"];
if (!status["code"].isnull())
{
switch(status["code"].as_int())
{
case 40120:
{
ShowNotification(_T("Skype"), TranslateT("Authentication failed. Bad username or password."), NULL, 1);
break;
}
case 40121:
{
ShowNotification(_T("Skype"), TranslateT("Too many failed authentication attempts with given username or IP."), NULL, 1);
break;
}
default:
{
ShowNotification(_T("Skype"), !status["text"].isnull() ? status["text"].as_mstring().GetBuffer() : TranslateT("Authentication failed. Unknown error."), NULL, 1);
}
}
}
}
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
if (!json["skypetoken"] || !json["expiresIn"])
{
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
std::string token = json["skypetoken"].as_string();
setString("TokenSecret", token.c_str());
int expiresIn = json["expiresIn"].as_int();
setDword("TokenExpiresIn", time(NULL) + expiresIn);
OnLoginSuccess();
}
void CSkypeProto::OnLoginSuccess()
{
isTerminated = false;
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_SUCCESS, NULL, 0);
m_szSelfSkypeName = getStringA(SKYPE_SETTINGS_ID);
m_szTokenSecret = getStringA("TokenSecret");
m_szServer = getStringA("Server");
if (m_szServer == NULL)
m_szServer = mir_strdup(SKYPE_ENDPOINTS_HOST);
SendRequest(new CreateEndpointRequest(m_szTokenSecret, m_szServer), &CSkypeProto::OnEndpointCreated);
PushRequest(new GetProfileRequest(m_szTokenSecret), &CSkypeProto::LoadProfile);
if (!m_timer)
SkypeSetTimer(this);
}
void CSkypeProto::OnEndpointCreated(const NETLIBHTTPREQUEST *response)
{
m_iStatus++;
if (response == NULL)
{
debugLogA(__FUNCTION__ ": failed to get create endpoint");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
for (int i = 0; i < response->headersCount; i++)
{
if (!mir_strcmpi(response->headers[i].szName, "Set-RegistrationToken"))
{
CMStringA szValue = response->headers[i].szValue, szCookieName, szCookieVal;
int iStart = 0;
while (true)
{
CMStringA szToken = szValue.Tokenize(";", iStart).Trim();
if (iStart == -1)
break;
int iStart2 = 0;
szCookieName = szToken.Tokenize("=", iStart2);
szCookieVal = szToken.Mid(iStart2);
setString(szCookieName, szCookieVal);
}
}
else if (!mir_strcmpi(response->headers[i].szName, "Location"))
{
CMStringA szValue = response->headers[i].szValue;
m_szServer = GetServerFromUrl(szValue).Detach();
setString("Server", m_szServer);
}
}
if (m_iStatus++ > SKYPE_MAX_CONNECT_RETRIES)
{
debugLogA(__FUNCTION__ ": failed to get create endpoint");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
if (response->resultCode != 201)
{
if (response->resultCode == 401)
{
delSetting("TokenExpiresIn");
SendRequest(new LoginOAuthRequest(m_szSelfSkypeName, ptrA(getStringA(SKYPE_SETTINGS_PASSWORD))), &CSkypeProto::OnLoginOAuth);
return;
}
else //it should be rewritten
{
SendRequest(new CreateEndpointRequest(m_szTokenSecret, m_szServer), &CSkypeProto::OnEndpointCreated);
return;
}
}
m_szRegToken = getStringA("registrationToken");
m_szEndpointId = getStringA("endpointId");
SendRequest(new CreateSubscriptionsRequest(m_szRegToken, m_szServer), &CSkypeProto::OnSubscriptionsCreated);
SendRequest(new CreateTrouterRequest(), &CSkypeProto::OnCreateTrouter);
}
void CSkypeProto::OnSubscriptionsCreated(const NETLIBHTTPREQUEST *response)
{
m_iStatus++;
if (response == NULL)
{
debugLogA(__FUNCTION__ ": failed to create subscription");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
SendPresence(true);
}
void CSkypeProto::SendPresence(bool isLogin)
{
ptrA epname;
ptrT place(getTStringA("Place"));
if (!getBool("UseHostName", false) && place && *place)
epname = mir_utf8encodeT(place);
else
{
TCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = SIZEOF(compName);
GetComputerName(compName, &size);
epname = mir_utf8encodeT(compName);
}
if (isLogin)
SendRequest(new SendCapabilitiesRequest(m_szRegToken, m_szEndpointId, epname, m_szServer), &CSkypeProto::OnCapabilitiesSended);
else
PushRequest(new SendCapabilitiesRequest(m_szRegToken, m_szEndpointId, epname, m_szServer));
}
void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response)
{
SendRequest(new SetStatusRequest(m_szRegToken, MirandaToSkypeStatus(m_iDesiredStatus), m_szServer), &CSkypeProto::OnStatusChanged);
LIST<char> skypenames(1);
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
{
if (!isChatRoom(hContact))
skypenames.insert(getStringA(hContact, SKYPE_SETTINGS_ID));
}
SendRequest(new CreateContactsSubscriptionRequest(m_szRegToken, skypenames, m_szServer));
for (int i = 0; i < skypenames.getCount(); i++)
mir_free(skypenames[i]);
skypenames.destroy();
m_hPollingThread = ForkThreadEx(&CSkypeProto::PollingThread, 0, NULL);
PushRequest(new GetAvatarRequest(ptrA(getStringA("AvatarUrl"))), &CSkypeProto::OnReceiveAvatar, NULL);
PushRequest(new GetContactListRequest(m_szTokenSecret, m_szSelfSkypeName, NULL), &CSkypeProto::LoadContactList);
SendRequest(new LoadChatsRequest(m_szRegToken, m_szServer), &CSkypeProto::OnLoadChats);
if (getBool("AutoSync", true))
PushRequest(new SyncHistoryFirstRequest(m_szRegToken, 100, m_szServer), &CSkypeProto::OnSyncHistory);
if (response == NULL || response->pData == NULL)
return;
JSONNode root = JSONNode::parse(response->pData);
if (root)
setString("SelfEndpointName", SelfUrlToName(root["selfLink"].as_string().c_str()));
}
void CSkypeProto::OnStatusChanged(const NETLIBHTTPREQUEST *response)
{
if (response == NULL)
{
debugLogA(__FUNCTION__ ": failed to change status");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
JSONNode json = JSONNode::parse(response->pData);
if (!json)
{
debugLogA(__FUNCTION__ ": failed to change status");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
const JSONNode &nStatus = json["status"];
if (!nStatus) {
debugLogA(__FUNCTION__ ": result contains no valid status to switch to");
return;
}
int iNewStatus = SkypeToMirandaStatus(nStatus.as_string().c_str());
if (iNewStatus == ID_STATUS_OFFLINE)
{
debugLogA(__FUNCTION__ ": failed to change status");
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN);
SetStatus(ID_STATUS_OFFLINE);
return;
}
int oldStatus = m_iStatus;
m_iStatus = m_iDesiredStatus = iNewStatus;
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
}
|