summaryrefslogtreecommitdiff
path: root/protocols/MRA/MraMPop.cpp
blob: 55bc9dda4eb7b6ce606a968d59eb7ab4deeb51e9 (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
#include "Mra.h"
#include "MraMPop.h"

//	MPOP_SESSION
struct MRA_MPOP_SESSION_QUEUE : public FIFO_MT
{
	BOOL    bKeyValid;
	LPSTR   lpszMPOPKey;
	size_t  dwMPOPKeySize;
};

struct MRA_MPOP_SESSION_QUEUE_ITEM : public FIFO_MT_ITEM
{
	LPSTR        lpszUrl;
	size_t       dwUrlSize;
};

void MraMPopSessionQueueClear(HANDLE hMPopSessionQueue);

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

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)mir_calloc(sizeof(MRA_MPOP_SESSION_QUEUE));
	if (!pmpsqMPopSessionQueue)
		return GetLastError();
		
	pmpsqMPopSessionQueue->bKeyValid = FALSE;
	pmpsqMPopSessionQueue->lpszMPOPKey = NULL;
	pmpsqMPopSessionQueue->dwMPOPKeySize = 0;
	FifoMTInitialize(pmpsqMPopSessionQueue, 0);
	*phMPopSessionQueue = (HANDLE)pmpsqMPopSessionQueue;
	return NO_ERROR;
}

void MraMPopSessionQueueClear(HANDLE hMPopSessionQueue)
{
	if (hMPopSessionQueue) {
		MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
		pmpsqMPopSessionQueue->bKeyValid = FALSE;
		mir_free(pmpsqMPopSessionQueue->lpszMPOPKey);
		pmpsqMPopSessionQueue->dwMPOPKeySize = 0;

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

void CMraProto::MraMPopSessionQueueFlush(HANDLE hMPopSessionQueue)
{
	if (hMPopSessionQueue) {
		MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
		while( FifoMTGetCount(pmpsqMPopSessionQueue)) {
			MraMPopSessionQueueSetNewMPopKey(hMPopSessionQueue, NULL, 0);
			MraMPopSessionQueueStart(hMPopSessionQueue);
		}
	}
}

void MraMPopSessionQueueDestroy(HANDLE hMPopSessionQueue)
{
	if (hMPopSessionQueue) {
		MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
		MraMPopSessionQueueClear(hMPopSessionQueue);
		FifoMTDestroy(pmpsqMPopSessionQueue);
		mir_free(pmpsqMPopSessionQueue);
	}
}

DWORD CMraProto::MraMPopSessionQueueAddUrl(HANDLE hMPopSessionQueue, LPSTR lpszUrl, size_t dwUrlSize)
{
	if (!hMPopSessionQueue || !lpszUrl || !dwUrlSize)
		return ERROR_INVALID_HANDLE;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
	MRA_MPOP_SESSION_QUEUE_ITEM *pmpsqi = (MRA_MPOP_SESSION_QUEUE_ITEM*)mir_calloc((sizeof(MRA_MPOP_SESSION_QUEUE_ITEM)+dwUrlSize+sizeof(size_t)));
	if (!pmpsqi)
		return GetLastError();

	pmpsqi->dwUrlSize = dwUrlSize;
	pmpsqi->lpszUrl = (LPSTR)(pmpsqi+1);
	memmove(pmpsqi->lpszUrl, lpszUrl, dwUrlSize);
	FifoMTItemPush(pmpsqMPopSessionQueue, pmpsqi, (LPVOID)pmpsqi);
	if (pmpsqMPopSessionQueue->bKeyValid)
		return MraMPopSessionQueueStart(hMPopSessionQueue);

	if (m_bLoggedIn)
		MraSendCMD(MRIM_CS_GET_MPOP_SESSION, NULL, 0);
	else
		MraMPopSessionQueueFlush(hMPopSessionQueue);

	return NO_ERROR;
}

DWORD CMraProto::MraMPopSessionQueueAddUrlAndEMail(HANDLE hMPopSessionQueue, LPSTR lpszUrl, size_t dwUrlSize, LPSTR lpszEMail, size_t dwEMailSize)
{
	if (!hMPopSessionQueue || !lpszUrl || !dwUrlSize || !lpszEMail || !dwEMailSize)
		return ERROR_INVALID_HANDLE;

	char szUrl[BUFF_SIZE_URL], szEMail[MAX_EMAIL_LEN];
	LPSTR lpszUser, lpszDomain;

	BuffToLowerCase(szEMail, lpszEMail, dwEMailSize);
	lpszDomain = (LPSTR)MemoryFindByte(0, szEMail, dwEMailSize, '@');
	if (lpszDomain)
	if (lpszUser = (LPSTR)MemoryFindByte((lpszDomain-szEMail), szEMail, dwEMailSize, '.')) {
		*lpszUser = 0;
		lpszUser = szEMail;
		*lpszDomain = 0;
		lpszDomain++;

		dwUrlSize = mir_snprintf(szUrl, SIZEOF(szUrl), "%s/%s/%s", lpszUrl, lpszDomain, lpszUser);
		return MraMPopSessionQueueAddUrl(hMPopSessionQueue, szUrl, dwUrlSize);
	}
	return ERROR_INVALID_DATA;
}

DWORD CMraProto::MraMPopSessionQueueStart(HANDLE hMPopSessionQueue)
{
	DWORD dwRetErrorCode;

	if (!hMPopSessionQueue)
		return ERROR_INVALID_HANDLE;

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

	dwRetErrorCode = NO_ERROR;
	if (pmpsqMPopSessionQueue->bKeyValid == TRUE)
	if ( FifoMTItemPop(pmpsqMPopSessionQueue, NULL, (LPVOID*)&pmpsqi) == NO_ERROR) {
		char szUrl[BUFF_SIZE_URL], szEMail[MAX_EMAIL_LEN];
		size_t dwUrlSize, dwEMailSize;

		if (mraGetStaticStringA(NULL, "e-mail", szEMail, SIZEOF(szEMail), &dwEMailSize)) {
			pmpsqMPopSessionQueue->bKeyValid = FALSE;
			BuffToLowerCase(szEMail, szEMail, dwEMailSize);
			dwUrlSize = mir_snprintf(szUrl, SIZEOF(szUrl), MRA_MPOP_AUTH_URL, szEMail, pmpsqMPopSessionQueue->lpszMPOPKey, pmpsqi->lpszUrl);

			CallService(MS_UTILS_OPENURL, TRUE, (LPARAM)szUrl);
			DebugPrint(L"Opening URL: ");DebugPrintCRLFA(szUrl);
		}
		mir_free(pmpsqi);
	}
	return NO_ERROR;
}

DWORD MraMPopSessionQueueSetNewMPopKey(HANDLE hMPopSessionQueue, LPSTR lpszKey, size_t dwKeySize)
{
	if (!hMPopSessionQueue)
		return ERROR_INVALID_HANDLE;

	if (lpszKey == NULL) dwKeySize = 0;

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
	if (pmpsqMPopSessionQueue->dwMPOPKeySize<dwKeySize || dwKeySize == 0) {
		mir_free(pmpsqMPopSessionQueue->lpszMPOPKey);
		pmpsqMPopSessionQueue->lpszMPOPKey = (LPSTR)mir_calloc(dwKeySize+sizeof(size_t));
	}

	if (pmpsqMPopSessionQueue->lpszMPOPKey) {
		pmpsqMPopSessionQueue->bKeyValid = TRUE;
		pmpsqMPopSessionQueue->dwMPOPKeySize = dwKeySize;
		memmove(pmpsqMPopSessionQueue->lpszMPOPKey, lpszKey, dwKeySize);
		(*(pmpsqMPopSessionQueue->lpszMPOPKey+dwKeySize)) = 0;

		DebugPrint(L"New MPOP session key: ");DebugPrintCRLFA(pmpsqMPopSessionQueue->lpszMPOPKey);
		return NO_ERROR;
	}
	
	pmpsqMPopSessionQueue->bKeyValid = FALSE;
	pmpsqMPopSessionQueue->lpszMPOPKey = NULL;
	pmpsqMPopSessionQueue->dwMPOPKeySize = 0;
	return GetLastError();
}