summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/MraMPop.cpp
blob: 397cb0a51b71eb23e777031f024c311f25f46e1c (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
#include "stdafx.h"
#include "MraMPop.h"

//	MPOP_SESSION
struct MRA_MPOP_SESSION_QUEUE : public FIFO_MT
{
	bool    bKeyValid;	/* lpszMPOPKey contain valid key. */
	LPSTR   lpszMPOPKey;	/* Key for web auth on mail.ru services. */
	size_t  dwMPOPKeySize;	/* Key size. */
};

struct MRA_MPOP_SESSION_QUEUE_ITEM : public FIFO_MT_ITEM
{
	LPSTR   lpszUrl;	/* Url to open. */
	size_t  dwUrlSize;
};

DWORD MraMPopSessionQueueInitialize(HANDLE *phMPopSessionQueue)
{
	if (!phMPopSessionQueue)
		return ERROR_INVALID_HANDLE;
	if ((*phMPopSessionQueue))
		return ERROR_ALREADY_INITIALIZED;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = new MRA_MPOP_SESSION_QUEUE();
	if (!pmpsqMPopSessionQueue)
		return GetLastError();

	*phMPopSessionQueue = (HANDLE)pmpsqMPopSessionQueue;
	return NO_ERROR;
}

void MraMPopSessionQueueClear(HANDLE hQueue)
{
	if (!hQueue)
		return;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	pmpsqMPopSessionQueue->bKeyValid = false;
	mir_free(pmpsqMPopSessionQueue->lpszMPOPKey);
	pmpsqMPopSessionQueue->lpszMPOPKey = nullptr;
	pmpsqMPopSessionQueue->dwMPOPKeySize = 0;

	MRA_MPOP_SESSION_QUEUE_ITEM *pmpsqi;
	while ( !FifoMTItemPop(pmpsqMPopSessionQueue, nullptr, (LPVOID*)&pmpsqi))
		mir_free(pmpsqi);
}

void MraMPopSessionQueueDestroy(HANDLE hQueue)
{
	if (!hQueue)
		return;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	MraMPopSessionQueueClear(hQueue);
	delete pmpsqMPopSessionQueue;
}

DWORD CMraProto::MraMPopSessionQueueAddUrl(HANDLE hQueue, const CMStringA &lpszUrl)
{
	if (!hQueue)
		return ERROR_INVALID_HANDLE;
	if (lpszUrl.IsEmpty())
		return ERROR_INVALID_DATA;
	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	MRA_MPOP_SESSION_QUEUE_ITEM *pmpsqi;

	if (!getByte("AutoAuthOnWebServices", MRA_DEFAULT_AUTO_AUTH_ON_WEB_SVCS) || !m_bLoggedIn) { /* Open without web auth. / Not loggedIn. */
		Utils_OpenUrl(lpszUrl);
		return NO_ERROR;
	}
	/* Add to queue. */
	pmpsqi = (MRA_MPOP_SESSION_QUEUE_ITEM*)mir_calloc((sizeof(MRA_MPOP_SESSION_QUEUE_ITEM) + lpszUrl.GetLength() + sizeof(size_t)));
	if (!pmpsqi)
		return GetLastError();

	pmpsqi->dwUrlSize = lpszUrl.GetLength();
	pmpsqi->lpszUrl = (LPSTR)(pmpsqi + 1);
	memcpy(pmpsqi->lpszUrl, lpszUrl, lpszUrl.GetLength());
	FifoMTItemPush(pmpsqMPopSessionQueue, pmpsqi, (LPVOID)pmpsqi);
	MraMPopSessionQueueStart(hQueue);
	return NO_ERROR;
}

DWORD CMraProto::MraMPopSessionQueueAddUrlAndEMail(HANDLE hQueue, const CMStringA &lpszUrl, CMStringA &szEmail)
{
	if (!hQueue)
		return ERROR_INVALID_HANDLE;
	if (lpszUrl.IsEmpty() || szEmail.IsEmpty())
		return ERROR_INVALID_DATA;

	szEmail.MakeLower();

	int iStart = 0;
	CMStringA szUser = szEmail.Tokenize("@", iStart);
	CMStringA szDomain = szEmail.Tokenize("@", iStart);

	CMStringA szUrl;
	szUrl.Format("%s/%s/%s", lpszUrl.c_str(), szDomain.c_str(), szUser.c_str());
	return MraMPopSessionQueueAddUrl(hQueue, szUrl);
}

void CMraProto::MraMPopSessionQueueStart(HANDLE hQueue)
{
	if (!hQueue)
		return;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	MRA_MPOP_SESSION_QUEUE_ITEM *pmpsqi;

	if (!getByte("AutoAuthOnWebServices", MRA_DEFAULT_AUTO_AUTH_ON_WEB_SVCS) || !m_bLoggedIn) { /* Open without web auth. / Not loggedIn. */
		MraMPopSessionQueueFlush(hQueue);
		return;
	}

	while ( FifoMTGetCount(pmpsqMPopSessionQueue)) {
		if (!pmpsqMPopSessionQueue->bKeyValid) { /* We have no key, try to get one. */
			if (0 == MraSendCMD(MRIM_CS_GET_MPOP_SESSION, nullptr, 0))	/* Fail to send. */
				MraMPopSessionQueueFlush(hQueue);
			return;
		}

		if ( FifoMTItemPop(pmpsqMPopSessionQueue, nullptr, (LPVOID*)&pmpsqi) == NO_ERROR) {
			CMStringA szUrl, szEmail;
			if (mraGetStringA(NULL, "e-mail", szEmail)) {
				pmpsqMPopSessionQueue->bKeyValid = false;
				szEmail.MakeLower();
				szUrl.Format(MRA_MPOP_AUTH_URL, szEmail.c_str(), pmpsqMPopSessionQueue->lpszMPOPKey, pmpsqi->lpszUrl);
				Utils_OpenUrl(szUrl);
				debugLogA("Opening URL: %s\n", szUrl.c_str());
			}
			mir_free(pmpsqi);
		}
	}
}

void CMraProto::MraMPopSessionQueueFlush(HANDLE hQueue)
{
	if (!hQueue)
		return;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	MRA_MPOP_SESSION_QUEUE_ITEM *pmpsqi;

	while (FifoMTItemPop(pmpsqMPopSessionQueue, nullptr, (LPVOID*)&pmpsqi) == NO_ERROR) {
		Utils_OpenUrl(pmpsqi->lpszUrl);
		mir_free(pmpsqi);
	}
}

DWORD MraMPopSessionQueueSetNewMPopKey(HANDLE hQueue, const CMStringA &szKey)
{
	if (!hQueue)
		return ERROR_INVALID_HANDLE;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hQueue;
	if (pmpsqMPopSessionQueue->dwMPOPKeySize < (size_t)szKey.GetLength() || szKey.IsEmpty()) {
		mir_free(pmpsqMPopSessionQueue->lpszMPOPKey);
		pmpsqMPopSessionQueue->lpszMPOPKey = (LPSTR)mir_calloc(szKey.GetLength() + sizeof(size_t));
	}

	if (pmpsqMPopSessionQueue->lpszMPOPKey) {
		pmpsqMPopSessionQueue->bKeyValid = true;
		pmpsqMPopSessionQueue->dwMPOPKeySize = szKey.GetLength();
		memcpy(pmpsqMPopSessionQueue->lpszMPOPKey, szKey, szKey.GetLength());
		*(pmpsqMPopSessionQueue->lpszMPOPKey + szKey.GetLength()) = 0;
		return NO_ERROR;
	}

	pmpsqMPopSessionQueue->bKeyValid = false;
	pmpsqMPopSessionQueue->lpszMPOPKey = nullptr;
	pmpsqMPopSessionQueue->dwMPOPKeySize = 0;
	return GetLastError();
}