summaryrefslogtreecommitdiff
path: root/protocols/Skype/src/skype_account.cpp
blob: 218639d919b460ca87356d1c92858430abff1f0f (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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "skype.h"

wchar_t *CSkypeProto::LogoutReasons[] =
{
	LPGENW("")															/* ---							*/,
	LPGENW("LOGOUT_CALLED")												/* LOGOUT_CALLED				*/,
	LPGENW("HTTPS proxy authentication failed")							/* HTTPS_PROXY_AUTH_FAILED		*/,
	LPGENW("SOCKS proxy authentication failed")							/* SOCKS_PROXY_AUTH_FAILED		*/,
	LPGENW("P2P connection failed")										/* P2P_CONNECT_FAILED			*/,
	LPGENW("SERVER_CONNECT_FAILED")										/* SERVER_CONNECT_FAILED		*/,
	LPGENW("Server is overloaded")										/* SERVER_OVERLOADED			*/,
	LPGENW("SkypeKit database already in use")							/* DB_IN_USE					*/,
	LPGENW("Invalid Skype name")										/* INVALID_SKYPENAME			*/,
	LPGENW("Invalid email")												/* INVALID_EMAIL				*/,
	LPGENW("Unacceptable password")										/* UNACCEPTABLE_PASSWORD		*/,
	LPGENW("Skype name is taken")										/* SKYPENAME_TAKEN				*/,
	LPGENW("REJECTED_AS_UNDERAGE")										/* REJECTED_AS_UNDERAGE			*/,
	LPGENW("NO_SUCH_IDENTITY")											/* NO_SUCH_IDENTITY				*/,
	LPGENW("Incorrect password")										/* INCORRECT_PASSWORD			*/,
	LPGENW("Too many login attempts")									/* TOO_MANY_LOGIN_ATTEMPTS		*/,
	LPGENW("Password has changed")										/* PASSWORD_HAS_CHANGED			*/,
	LPGENW("PERIODIC_UIC_UPDATE_FAILED")								/* PERIODIC_UIC_UPDATE_FAILED	*/,
	LPGENW("DB_DISK_FULL")												/* DB_DISK_FULL					*/,
	LPGENW("DB_IO_ERROR")												/* DB_IO_ERROR					*/,
	LPGENW("SkypeKit database is corrupt")								/* DB_CORRUPT					*/,
	LPGENW("DB_FAILURE")												/* DB_FAILURE					*/,
	LPGENW("Invalid application ID")									/* INVALID_APP_ID				*/,
	LPGENW("APP_ID_FAILURE")											/* APP_ID_FAILURE				*/,
	LPGENW("Version is unsupported")									/* UNSUPPORTED_VERSION			*/,
	LPGENW("Account blocked")											/* ATO_BLOCKED					*/,
	LPGENW("Logout from another instance")								/* REMOTE_LOGOUT				*/,
	LPGENW("ACCESS_TOKEN_RENEWAL_FAILED")								/* ACCESS_TOKEN_RENEWAL_FAILED 	*/
};

wchar_t *CSkypeProto::PasswordChangeReasons[] =
{
	LPGENW("Password successfully changed")								/* PWD_OK						*/,
	LPGENW("Password changing")											/* PWD_CHANGING					*/,
	LPGENW("Old password was incorrect")								/* PWD_INVALID_OLD_PASSWORD		*/,
	LPGENW("Failed to verify password. No connection to server")		/* PWD_SERVER_CONNECT_FAILED	*/,
	LPGENW("Password was set but server didn't like it much")			/* PWD_OK_BUT_CHANGE_SUGGESTED	*/,
	LPGENW("New password was exactly the same as old one")				/* PWD_MUST_DIFFER_FROM_OLD		*/,
	LPGENW("The new password was unacceptable")							/* PWD_INVALID_NEW_PWD			*/,
	LPGENW("Account was currently not logged in")						/* PWD_MUST_LOG_IN_TO_CHANGE	*/,
};

bool CSkypeProto::IsOnline()
{
	return this->m_iStatus > ID_STATUS_OFFLINE;
}

bool CSkypeProto::PrepareLogin()
{
	this->login = ::db_get_wsa(NULL, this->m_szModuleName, SKYPE_SETTINGS_SID);
	if ( !this->login || !::wcslen(this->login))
	{
		this->m_iStatus = ID_STATUS_OFFLINE;
		this->SendBroadcast(ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGINERR_BADUSERID);
		
		wchar_t message[512];
		::mir_sntprintf(message, SIZEOF(message), ::TranslateT("You have not entered a Skype name.\nConfigure this in Options->Network->%s and try again."), this->m_tszUserName);
		this->ShowNotification(message);
		return false;
	}

	return true;
}

bool CSkypeProto::PreparePassword()
{
	if ( !this->rememberPassword)
	{
		if (this->password)
		{
			::mir_free(this->password);
			this->password = NULL;
		}
		this->password = ::db_get_sa(NULL, this->m_szModuleName, SKYPE_SETTINGS_PASSWORD);
		if ( !this->password || !::strlen(this->password))
		{
			if (this->password)
			{
				::mir_free(this->password);
				this->password = NULL;
			}
			PasswordRequestBoxParam param(this->login);
			if ( !this->RequestPassword(param))
			{
				this->SetStatus(ID_STATUS_OFFLINE);
				return false;
			}
			else
			{
				this->password = ::mir_strdup(param.password);
				this->rememberPassword = param.rememberPassword;
			}
		}
		else
			::CallService(MS_DB_CRYPT_DECODESTRING, ::strlen(this->password), (LPARAM)this->password);
	}

	return true;
}

bool CSkypeProto::LogIn()
{
	if (this->IsOnline() || !this->PrepareLogin())
		return false;

	if (this->GetAccount((char*)_T2A(this->login), this->account))
	{
		if ( !this->PreparePassword())
			return false;

		this->account.fetch();

		this->InitProxy();

		this->SetAccountSettings();	

		this->Log(L"Login in an account");
		this->account->LoginWithPassword(this->password, false, false);
	}

	return true;
}

void CSkypeProto::LogOut()
{
	if	(this->IsOnline() || this->m_iStatus == ID_STATUS_CONNECTING)
	{
		this->account->SetAvailability(Contact::OFFLINE);
		this->Log(L"Logout from account");
		this->account->Logout(true);
	}
}

void CSkypeProto::SetAccountSettings()
{
	int port = this->getWord("Port", rand() % 10000 + 10000);
	this->Log(L"Setting port number to %d", port);
	this->SetInt(SETUPKEY_PORT, port);

	bool useAlternativePorts = this->getByte("UseAlternativePorts", 1) > 0;
	if (useAlternativePorts)
		this->Log(L"Setting listening of alternative ports (80, 443)");
	this->SetInt(SETUPKEY_DISABLE_PORT80, (int)!useAlternativePorts);

	// Create default group for new contacts
	DBVARIANT dbv = {0};
	if ( !getTString(SKYPE_SETTINGS_DEF_GROUP, &dbv) && lstrlen(dbv.ptszVal) > 0)
	{
		this->Log(L"Setting default group for new contacts");
		::Clist_CreateGroup(0, dbv.ptszVal);
		::db_free(&dbv);
	}
}

void CSkypeProto::InitProxy()
{
	if (this->hNetLibUser)
	{
		NETLIBUSERSETTINGS nlus = { sizeof(NETLIBUSERSETTINGS) };
		::CallService(MS_NETLIB_GETUSERSETTINGS, (WPARAM)this->hNetLibUser, (LPARAM)&nlus);

		if (nlus.useProxy)
		{
			char address[MAX_PATH];
			::mir_snprintf(address, MAX_PATH, "%s:%d", nlus.szProxyServer, nlus.wProxyPort);

			switch (nlus.proxyType)
			{
			case PROXYTYPE_HTTP:
			case PROXYTYPE_HTTPS:
				this->Log(L"Setting https user proxy config");
				this->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 1);
				this->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 0);
				this->SetStr(SETUPKEY_HTTPS_PROXY_ADDR, address);
				if (nlus.useProxyAuth)
				{
					this->SetStr(SETUPKEY_HTTPS_PROXY_USER, nlus.szProxyAuthUser);
					ptrA encodedPass(::mir_base64_encode((BYTE*)nlus.szProxyAuthPassword, ::lstrlenA(nlus.szProxyAuthPassword)));
					this->SetStr(SETUPKEY_HTTPS_PROXY_PWD,	(char*)encodedPass);
				}
				break;

			case PROXYTYPE_SOCKS4:
			case PROXYTYPE_SOCKS5:
				this->Log(L"Setting socks user proxy config");
				this->SetInt(SETUPKEY_HTTPS_PROXY_ENABLE, 0);
				this->SetInt(SETUPKEY_SOCKS_PROXY_ENABLE, 1);
				this->SetStr(SETUPKEY_SOCKS_PROXY_ADDR, address);
				if (nlus.useProxyAuth)
				{
					this->SetStr(SETUPKEY_SOCKS_PROXY_USER, nlus.szProxyAuthUser);
					ptrA encodedPass(::mir_base64_encode((BYTE*)nlus.szProxyAuthPassword, ::lstrlenA(nlus.szProxyAuthPassword)));
					this->SetStr(SETUPKEY_SOCKS_PROXY_PWD, (char*)encodedPass);
				}
				break;

			default:
				this->Log(L"Setting automatic proxy detection");
				this->Delete(SETUPKEY_HTTPS_PROXY_ENABLE);
				this->Delete(SETUPKEY_HTTPS_PROXY_ADDR);
				this->Delete(SETUPKEY_HTTPS_PROXY_USER);
				this->Delete(SETUPKEY_HTTPS_PROXY_PWD);
				this->Delete(SETUPKEY_SOCKS_PROXY_ENABLE);
				this->Delete(SETUPKEY_SOCKS_PROXY_ADDR);
				this->Delete(SETUPKEY_SOCKS_PROXY_USER);
				this->Delete(SETUPKEY_SOCKS_PROXY_PWD);
				break;
			}
		}
	}
}

void CSkypeProto::OnLoggedIn()
{
	if ( !this->rememberPassword)
	{
		::mir_free(this->password);
		this->password = NULL;
	}
	else
	{
		::CallService(MS_DB_CRYPT_ENCODESTRING, ::strlen(this->password), (LPARAM)this->password);
	}

	this->SetServerStatus(this->m_iDesiredStatus);

	this->LoadOwnInfo(this);
	this->LoadChatList(this);
	this->LoadContactList(this);
	this->LoadAuthWaitList(this);
	
	fetch(this->transferList);
}

void CSkypeProto::SetServerStatus(int iNewStatus)
{
	if (!this->account)
		return;

	// change status
	if (m_iStatus == iNewStatus)
		return;

	int oldStatus = m_iStatus;
	m_iStatus = iNewStatus;

	CContact::AVAILABILITY availability = CSkypeProto::MirandaToSkypeStatus(iNewStatus);
	if (availability != CContact::UNKNOWN)
	{
		this->Log(L"Setting status to %d", iNewStatus);
		this->account->SetAvailability(availability);
	}

	this->SendBroadcast(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, this->m_iStatus);
}

void CSkypeProto::OnCblUpdated()
{
	// reload our CL after skype CL fully synced
	this->LoadContactList(NULL);
}

void CSkypeProto::OnLoggedOut(CAccount::LOGOUTREASON reason)
{
	this->Log(L"Failed to login: %s", CSkypeProto::LogoutReasons[reason]);

	if (this->m_iStatus == ID_STATUS_CONNECTING)
		this->SendBroadcast(
			ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL,
			CSkypeProto::SkypeToMirandaLoginError(reason));

	this->SetStatus(ID_STATUS_OFFLINE);
	this->ShowNotification(CSkypeProto::LogoutReasons[reason]);

	if (this->rememberPassword && reason == CAccount::INCORRECT_PASSWORD)
	{
		this->rememberPassword = false;
		if (this->password)
		{
			::mir_free(this->password);
			this->password = NULL;
		}
	}
}

void CSkypeProto::OnAccountChanged(int prop)
{
	switch(prop)
	{
	case CAccount::P_STATUS:
		CAccount::STATUS loginStatus;
		this->account->GetPropStatus(loginStatus);

		if (loginStatus == CAccount::LOGGED_IN)
		{
			//this->ForkThread(&CSkypeProto::SignInAsync, 0);
			this->OnLoggedIn();
		}

		if (loginStatus == CAccount::LOGGED_OUT)
		{
			CAccount::LOGOUTREASON reason;
			if (this->account->GetPropLogoutreason(reason))
				if (reason != CAccount::LOGOUT_CALLED)
					this->OnLoggedOut(reason);
		}
		break;

	case CAccount::P_CBLSYNCSTATUS:
		{
			CAccount::CBLSYNCSTATUS status;
			this->account->GetPropCblsyncstatus(status);
			if (status == CAccount::CBL_IN_SYNC)
			{
				this->OnCblUpdated();
			}
		}
		break;

	case CAccount::P_PWDCHANGESTATUS:
		{
			CAccount::PWDCHANGESTATUS status;
			this->account->GetPropPwdchangestatus(status);
			if (status != CAccount::PWD_CHANGING)
			{
				this->Log(L"Failed to chage password: %s", CSkypeProto::PasswordChangeReasons[status]);
				this->ShowNotification(CSkypeProto::PasswordChangeReasons[status]);
			}
		}
		break;

	//case CAccount::P_AVATAR_IMAGE:
	case CAccount::P_AVATAR_TIMESTAMP:
		this->UpdateProfileAvatar(this->account.fetch());
		break;

	//case CAccount::P_MOOD_TEXT:
	case CAccount::P_MOOD_TIMESTAMP:
		this->UpdateProfileStatusMessage(this->account.fetch());
		break;

	case CAccount::P_PROFILE_TIMESTAMP:
		this->UpdateProfile(this->account.fetch());
		break;

/*	case Account::P_AVAILABILITY:
		{
			Contact::AVAILABILITY status;
			this->account->GetPropAvailability(status);
			int mir_status = this->SkypeToMirandaStatus(status);
			if (mir_status != this->m_iStatus && mir_status >= ID_STATUS_OFFLINE)
				this->SetStatus(mir_status);
		}
		break;*/
	}
}