summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/MraMPop.cpp
blob: 0c2c529f3109efea920d1fe77a13c44ec2b46150 (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
#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, "");
			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, const CMStringA &szUrl)
{
	if (!hMPopSessionQueue || szUrl.IsEmpty())
		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)+szUrl.GetLength()+sizeof(size_t)));
	if (!pmpsqi)
		return GetLastError();

	pmpsqi->dwUrlSize = szUrl.GetLength();
	pmpsqi->lpszUrl = (LPSTR)(pmpsqi+1);
	memmove(pmpsqi->lpszUrl, szUrl, szUrl.GetLength());
	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, const CMStringA &lpszUrl, CMStringA &szEmail)
{
	if (!hMPopSessionQueue || lpszUrl.IsEmpty() || szEmail.IsEmpty())
		return ERROR_INVALID_HANDLE;

	szEmail.MakeLower();

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

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

DWORD CMraProto::MraMPopSessionQueueStart(HANDLE hMPopSessionQueue)
{
	if (!hMPopSessionQueue)
		return ERROR_INVALID_HANDLE;

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

	if (pmpsqMPopSessionQueue->bKeyValid == TRUE)
	if ( FifoMTItemPop(pmpsqMPopSessionQueue, NULL, (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, pmpsqMPopSessionQueue->lpszMPOPKey, pmpsqi->lpszUrl);
			CallService(MS_UTILS_OPENURL, TRUE, (LPARAM)szUrl.c_str());
			debugLogA("Opening URL: %s\n", szUrl);
		}
		mir_free(pmpsqi);
	}
	return NO_ERROR;
}

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

	MRA_MPOP_SESSION_QUEUE *pmpsqMPopSessionQueue = (MRA_MPOP_SESSION_QUEUE*)hMPopSessionQueue;
	if (pmpsqMPopSessionQueue->dwMPOPKeySize < 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();
		memmove(pmpsqMPopSessionQueue->lpszMPOPKey, szKey, szKey.GetLength());
		(*(pmpsqMPopSessionQueue->lpszMPOPKey + szKey.GetLength())) = 0;
		return NO_ERROR;
	}

	pmpsqMPopSessionQueue->bKeyValid = FALSE;
	pmpsqMPopSessionQueue->lpszMPOPKey = NULL;
	pmpsqMPopSessionQueue->dwMPOPKeySize = 0;
	return GetLastError();
}