summaryrefslogtreecommitdiff
path: root/protocols/IcqOscarJ/src/cookies.cpp
blob: 4e0c8bada0345ea55cf24afacffecc8307562b00 (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
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
// 
// Copyright © 2000-2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2010 Joe Kucera
// Copyright © 2012-2018 Miranda NG team
// 
// 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; either version 2
// of the License, or (at your option) any later version.
// 
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// -----------------------------------------------------------------------------
//  DESCRIPTION:
//
//  Handles packet & message cookies
// -----------------------------------------------------------------------------

#include "stdafx.h"

void CIcqProto::RemoveExpiredCookies()
{
	time_t tNow = time(nullptr);

	for (int i = cookies.getCount() - 1; i >= 0; i--)
		if ((cookies[i].dwTime + COOKIE_TIMEOUT) < tNow)
			cookies.remove(i);
}

// Generate and allocate cookie
DWORD CIcqProto::AllocateCookie(BYTE bType, WORD wIdent, MCONTACT hContact, void *pvExtra)
{
	mir_cslock l(cookieMutex);

	DWORD dwThisSeq = wCookieSeq++;
	dwThisSeq &= 0x7FFF;
	dwThisSeq |= wIdent << 0x10;

	icq_cookie_info *p = new icq_cookie_info();
	p->bType = bType;
	p->dwCookie = dwThisSeq;
	p->hContact = hContact;
	p->pvExtra = pvExtra;
	p->dwTime = time(nullptr);
	cookies.insert(p);

	return dwThisSeq;
}

DWORD CIcqProto::GenerateCookie(WORD wIdent)
{
	mir_cslock l(cookieMutex);

	DWORD dwThisSeq = wCookieSeq++;
	dwThisSeq &= 0x7FFF;
	dwThisSeq |= wIdent << 0x10;
	return dwThisSeq;
}

int CIcqProto::GetCookieType(DWORD dwCookie)
{
	mir_cslock l(cookieMutex);

	int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
	if (i != -1)
		i = cookies[i].bType;

	return i;
}

int CIcqProto::FindCookie(DWORD dwCookie, MCONTACT *phContact, void **ppvExtra)
{
	mir_cslock l(cookieMutex);

	int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
	if (i != -1) {
		if (phContact)
			*phContact = cookies[i].hContact;
		if (ppvExtra)
			*ppvExtra = cookies[i].pvExtra;

		// Cookie found
		return 1;
	}

	return 0;
}

int CIcqProto::FindCookieByData(void *pvExtra, DWORD *pdwCookie, MCONTACT *phContact)
{
	mir_cslock l(cookieMutex);

	for (int i = 0; i < cookies.getCount(); i++) {
		icq_cookie_info &cookie = cookies[i];
		if (pvExtra == cookie.pvExtra) {
			if (phContact)
				*phContact = cookie.hContact;
			if (pdwCookie)
				*pdwCookie = cookie.dwCookie;

			// Cookie found
			return 1;
		}
	}

	return 0;
}

int CIcqProto::FindCookieByType(BYTE bType, DWORD *pdwCookie, MCONTACT *phContact, void **ppvExtra)
{
	mir_cslock l(cookieMutex);

	for (int i = 0; i < cookies.getCount(); i++) {
		icq_cookie_info &cookie = cookies[i];

		if (bType == cookie.bType) {
			if (pdwCookie)
				*pdwCookie = cookie.dwCookie;
			if (phContact)
				*phContact = cookie.hContact;
			if (ppvExtra)
				*ppvExtra = cookie.pvExtra;

			// Cookie found
			return 1;
		}
	}

	return 0;
}

int CIcqProto::FindMessageCookie(DWORD dwMsgID1, DWORD dwMsgID2, DWORD *pdwCookie, MCONTACT *phContact, cookie_message_data **ppvExtra)
{
	mir_cslock l(cookieMutex);

	for (int i = 0; i < cookies.getCount(); i++) {
		icq_cookie_info &cookie = cookies[i];
		if (cookie.bType == CKT_MESSAGE || cookie.bType == CKT_FILE || cookie.bType == CKT_REVERSEDIRECT) {
			// message cookie found
			cookie_message_data *pCookie = (cookie_message_data*)cookie.pvExtra;

			if (pCookie->dwMsgID1 == dwMsgID1 && pCookie->dwMsgID2 == dwMsgID2) {
				if (phContact)
					*phContact = cookie.hContact;
				if (pdwCookie)
					*pdwCookie = cookie.dwCookie;
				if (ppvExtra)
					*ppvExtra = pCookie;

				// Cookie found
				return 1;
			}
		}
	}

	return 0;
}

void CIcqProto::FreeCookie(DWORD dwCookie)
{
	mir_cslock l(cookieMutex);

	int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
	if (i != -1) // Cookie found, remove from list
		cookies.remove(i);

	RemoveExpiredCookies();
}

void CIcqProto::FreeCookieByData(BYTE bType, void *pvExtra)
{
	mir_cslock l(cookieMutex);

	for (int i = 0; i < cookies.getCount(); i++) {
		icq_cookie_info &cookie = cookies[i];
		if (bType == cookie.bType && pvExtra == cookie.pvExtra) {
			// Cookie found, remove from list
			cookies.remove(i);
			break;
		}
	}

	RemoveExpiredCookies();
}

void CIcqProto::ReleaseCookie(DWORD dwCookie)
{
	mir_cslock l(cookieMutex);

	int i = cookies.getIndex(( icq_cookie_info* )&dwCookie );
	if (i != -1) { // Cookie found, remove from list
		SAFE_FREE((void**)&cookies[i].pvExtra);
		cookies.remove(i);
	}

	RemoveExpiredCookies();
}

void CIcqProto::InitMessageCookie(cookie_message_data *pCookie)
{
	DWORD dwMsgID1;
	DWORD dwMsgID2;

	do {
		// ensure that message ids are unique
		dwMsgID1 = time(nullptr);
		dwMsgID2 = RandRange(0, 0x0FFFF);
	}
		while (FindMessageCookie(dwMsgID1, dwMsgID2, nullptr, nullptr, nullptr));

	if (pCookie) {
		pCookie->dwMsgID1 = dwMsgID1;
		pCookie->dwMsgID2 = dwMsgID2;
	}
}

cookie_message_data* CIcqProto::CreateMessageCookie(WORD bMsgType, BYTE bAckType)
{
	cookie_message_data *pCookie = (cookie_message_data*)SAFE_MALLOC(sizeof(cookie_message_data));
	if (pCookie) {
		pCookie->bMessageType = bMsgType;
		pCookie->nAckType = bAckType;

		InitMessageCookie(pCookie);
	}
	return pCookie;
}

cookie_message_data* CIcqProto::CreateMessageCookieData(BYTE bMsgType, MCONTACT hContact, DWORD dwUin, int bUseSrvRelay)
{
	BYTE bAckType;
	WORD wStatus = getContactStatus(hContact);

	if (!getByte(hContact, "SlowSend", getByte("SlowSend", DEFAULT_SLOWSEND)) || (!dwUin && wStatus == ID_STATUS_OFFLINE))
		bAckType = ACKTYPE_NONE;
	else if (bUseSrvRelay)
		bAckType = ACKTYPE_CLIENT;
	else
		bAckType = ACKTYPE_SERVER;

	cookie_message_data* pCookieData = CreateMessageCookie(bMsgType, bAckType);

	// set flag for offline messages - to allow proper error handling
	if (wStatus == ID_STATUS_OFFLINE || wStatus == ID_STATUS_INVISIBLE)
		pCookieData->isOffline = TRUE;

	return pCookieData;
}