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
|
// ---------------------------------------------------------------------------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
//
// 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 "icqoscar.h"
#define INVALID_COOKIE_INDEX -1
void CIcqProto::RemoveExpiredCookies()
{
time_t tNow = time(NULL);
for (int i = cookies.getCount() - 1; i >= 0; i--) {
icq_cookie_info *cookie = cookies[i];
if ((cookie->dwTime + COOKIE_TIMEOUT) < tNow) {
cookies.remove(i);
SAFE_FREE((void**)&cookie);
}
}
}
// Generate and allocate cookie
DWORD CIcqProto::AllocateCookie(BYTE bType, WORD wIdent, MCONTACT hContact, void *pvExtra)
{
icq_lock l(cookieMutex);
DWORD dwThisSeq = wCookieSeq++;
dwThisSeq &= 0x7FFF;
dwThisSeq |= wIdent << 0x10;
icq_cookie_info* p = (icq_cookie_info*)SAFE_MALLOC(sizeof(icq_cookie_info));
if (p) {
p->bType = bType;
p->dwCookie = dwThisSeq;
p->hContact = hContact;
p->pvExtra = pvExtra;
p->dwTime = time(NULL);
cookies.insert(p);
}
return dwThisSeq;
}
DWORD CIcqProto::GenerateCookie(WORD wIdent)
{
icq_lock l(cookieMutex);
DWORD dwThisSeq = wCookieSeq++;
dwThisSeq &= 0x7FFF;
dwThisSeq |= wIdent << 0x10;
return dwThisSeq;
}
int CIcqProto::GetCookieType(DWORD dwCookie)
{
icq_lock l(cookieMutex);
int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
if (i != INVALID_COOKIE_INDEX)
i = cookies[i]->bType;
return i;
}
int CIcqProto::FindCookie(DWORD dwCookie, MCONTACT *phContact, void **ppvExtra)
{
icq_lock l(cookieMutex);
int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
if (i != INVALID_COOKIE_INDEX) {
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)
{
icq_lock l(cookieMutex);
for (int i = 0; i < cookies.getCount(); i++) {
if (pvExtra == cookies[i]->pvExtra) {
if (phContact)
*phContact = cookies[i]->hContact;
if (pdwCookie)
*pdwCookie = cookies[i]->dwCookie;
// Cookie found
return 1;
}
}
return 0;
}
int CIcqProto::FindCookieByType(BYTE bType, DWORD *pdwCookie, MCONTACT *phContact, void** ppvExtra)
{
icq_lock l(cookieMutex);
for (int i = 0; i < cookies.getCount(); i++) {
if (bType == cookies[i]->bType) {
if (pdwCookie)
*pdwCookie = cookies[i]->dwCookie;
if (phContact)
*phContact = cookies[i]->hContact;
if (ppvExtra)
*ppvExtra = cookies[i]->pvExtra;
// Cookie found
return 1;
}
}
return 0;
}
int CIcqProto::FindMessageCookie(DWORD dwMsgID1, DWORD dwMsgID2, DWORD *pdwCookie, MCONTACT *phContact, cookie_message_data **ppvExtra)
{
icq_lock l(cookieMutex);
for (int i = 0; i < cookies.getCount(); i++) {
if (cookies[i]->bType == CKT_MESSAGE || cookies[i]->bType == CKT_FILE || cookies[i]->bType == CKT_REVERSEDIRECT) {
// message cookie found
cookie_message_data *pCookie = (cookie_message_data*)cookies[i]->pvExtra;
if (pCookie->dwMsgID1 == dwMsgID1 && pCookie->dwMsgID2 == dwMsgID2) {
if (phContact)
*phContact = cookies[i]->hContact;
if (pdwCookie)
*pdwCookie = cookies[i]->dwCookie;
if (ppvExtra)
*ppvExtra = pCookie;
// Cookie found
return 1;
}
}
}
return 0;
}
void CIcqProto::FreeCookie(DWORD dwCookie)
{
icq_lock l(cookieMutex);
int i = cookies.getIndex((icq_cookie_info*)&dwCookie);
if (i != INVALID_COOKIE_INDEX) {
// Cookie found, remove from list
icq_cookie_info *cookie = cookies[i];
cookies.remove(i);
SAFE_FREE((void**)&cookie);
}
RemoveExpiredCookies();
}
void CIcqProto::FreeCookieByData(BYTE bType, void *pvExtra)
{
icq_lock 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);
SAFE_FREE((void**)&cookie);
break;
}
}
RemoveExpiredCookies();
}
void CIcqProto::ReleaseCookie(DWORD dwCookie)
{
icq_lock l(cookieMutex);
int i = cookies.getIndex(( icq_cookie_info* )&dwCookie );
if (i != INVALID_COOKIE_INDEX) {
// Cookie found, remove from list
icq_cookie_info *cookie = cookies[i];
cookies.remove(i);
SAFE_FREE((void**)&cookie->pvExtra);
SAFE_FREE((void**)&cookie);
}
RemoveExpiredCookies();
}
void CIcqProto::InitMessageCookie(cookie_message_data *pCookie)
{
DWORD dwMsgID1;
DWORD dwMsgID2;
do {
// ensure that message ids are unique
dwMsgID1 = time(NULL);
dwMsgID2 = RandRange(0, 0x0FFFF);
}
while (FindMessageCookie(dwMsgID1, dwMsgID2, NULL, NULL, NULL));
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;
}
|