summaryrefslogtreecommitdiff
path: root/protocols/IcqOscarJ/src/icq_rates.cpp
blob: 138c8900a2d7370a6988aa027c80d0acda13f455 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// ---------------------------------------------------------------------------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-2014 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:
//
//  Rate Management stuff
// -----------------------------------------------------------------------------

#include "icqoscar.h"

//
// Rate Level 1 Management
/////////////////////////////

rates::rates(CIcqProto *ppro, BYTE *pBuffer, size_t wLen)
{
	nGroups = 0;
	memset(&groups, 0, MAX_RATES_GROUP_COUNT * sizeof(rates_group));
	this->ppro = ppro;

	// Parse Rate Data Block
	WORD wCount;
	unpackWord(&pBuffer, &wCount);
	wLen -= 2;

	if (wCount > MAX_RATES_GROUP_COUNT) {
		// just sanity check
		ppro->debugLogA("Rates: Error: Data packet contains too many rate groups!");
		wCount = MAX_RATES_GROUP_COUNT;
	}

	nGroups = wCount;
	// Parse Group details
	int i;
	for (i = 0; i < wCount; i++) {
		rates_group *pGroup = &groups[i];

		if (wLen >= 35) {
			pBuffer += 2; // Group ID
			unpackDWord(&pBuffer, &pGroup->dwWindowSize);
			unpackDWord(&pBuffer, &pGroup->dwClearLevel);
			unpackDWord(&pBuffer, &pGroup->dwAlertLevel);
			unpackDWord(&pBuffer, &pGroup->dwLimitLevel);
			pBuffer += 8;
			unpackDWord(&pBuffer, &pGroup->dwMaxLevel);
			pBuffer += 5;
			wLen -= 35;
		}
		else { // packet broken, put some basic defaults
			pGroup->dwWindowSize = 10;
			pGroup->dwMaxLevel = 5000;
		}
		pGroup->rCurrentLevel = pGroup->dwMaxLevel;
	}
	// Parse Group associated pairs
	for (i = 0; i < wCount; i++) {
		rates_group *pGroup = &groups[i];

		if (wLen < 4)
			break;

		pBuffer += 2; // Group ID
		WORD wNum;
		unpackWord(&pBuffer, &wNum);
		wLen -= 4;
		if (wLen < (size_t)wNum * 4)
			break;

		pGroup->nPairs = wNum;
		pGroup->pPairs = (WORD*)SAFE_MALLOC(wNum * 4);
		for (size_t n = 0; n < (size_t)wNum * 2; n++) {
			WORD wItem;
			unpackWord(&pBuffer, &wItem);
			pGroup->pPairs[n] = wItem;
		}

		wLen -= wNum * 4;
	}
}

rates::~rates()
{
	for (int i = 0; i < nGroups; i++)
		SAFE_FREE((void**)&groups[i].pPairs);

	nGroups = 0;
}

WORD rates::getGroupFromSNAC(WORD wFamily, WORD wCommand)
{
	if (this) {
		for (int i = 0; i < nGroups; i++) {
			rates_group* pGroup = &groups[i];

			for (int j = 0; j < 2 * pGroup->nPairs; j += 2) {
				if (pGroup->pPairs[j] == wFamily && pGroup->pPairs[j + 1] == wCommand) { // we found the group
					return (WORD)(i + 1);
				}
			}
		}
		// Legacy ICQ server
	}

	return 0; // Failure
}

WORD rates::getGroupFromPacket(icq_packet *pPacket)
{
	if (this) {
		if (pPacket->nChannel == ICQ_DATA_CHAN && pPacket->wLen >= 0x10) {
			WORD wFamily, wCommand;
			BYTE *pBuf = pPacket->pData + 6;

			unpackWord(&pBuf, &wFamily);
			unpackWord(&pBuf, &wCommand);

			return getGroupFromSNAC(wFamily, wCommand);
		}
	}
	return 0;
}

rates_group* rates::getGroup(WORD wGroup)
{
	if (this && wGroup && wGroup <= nGroups)
		return &groups[wGroup - 1];

	return NULL;
}

int rates::getNextRateLevel(WORD wGroup)
{
	rates_group *pGroup = getGroup(wGroup);

	if (pGroup) {
		int nLevel = pGroup->rCurrentLevel*(pGroup->dwWindowSize - 1) / pGroup->dwWindowSize + (GetTickCount() - pGroup->tCurrentLevel) / pGroup->dwWindowSize;

		return nLevel < (int)pGroup->dwMaxLevel ? nLevel : pGroup->dwMaxLevel;
	}
	return -1; // Failure
}

int rates::getDelayToLimitLevel(WORD wGroup, int nLevel)
{
	rates_group *pGroup = getGroup(wGroup);

	if (pGroup)
		return (getLimitLevel(wGroup, nLevel) - pGroup->rCurrentLevel)*pGroup->dwWindowSize + pGroup->rCurrentLevel;

	return 0; // Failure
}

void rates::packetSent(icq_packet *pPacket)
{
	if (this) {
		WORD wGroup = getGroupFromPacket(pPacket);

		if (wGroup)
			updateLevel(wGroup, getNextRateLevel(wGroup));
	}
}

void rates::updateLevel(WORD wGroup, int nLevel)
{
	rates_group *pGroup = getGroup(wGroup);

	if (pGroup) {
		pGroup->rCurrentLevel = nLevel;
		pGroup->tCurrentLevel = GetTickCount();

		ppro->debugLogA("Rates: New level %d for #%d", nLevel, wGroup);
	}
}

int rates::getLimitLevel(WORD wGroup, int nLevel)
{
	rates_group *pGroup = getGroup(wGroup);

	if (pGroup) {
		switch (nLevel) {
		case RML_CLEAR:
			return pGroup->dwClearLevel;

		case RML_ALERT:
			return pGroup->dwAlertLevel;

		case RML_LIMIT:
			return pGroup->dwLimitLevel;

		case RML_IDLE_10:
			return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10);

		case RML_IDLE_30:
			return pGroup->dwClearLevel + (3 * (pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10);

		case RML_IDLE_50:
			return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel) / 2);

		case RML_IDLE_70:
			return pGroup->dwClearLevel + (7 * (pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10);
		}
	}
	return 9999; // some high number - without rates we allow anything
}

void rates::initAckPacket(icq_packet *pPacket)
{
	serverPacketInit(pPacket, 10 + nGroups * sizeof(WORD));
	packFNACHeader(pPacket, ICQ_SERVICE_FAMILY, ICQ_CLIENT_RATE_ACK);
	for (WORD wGroup = 1; wGroup <= nGroups; wGroup++)
		packWord(pPacket, wGroup);
}

//
// Rate Level 2 Management
/////////////////////////////

rates_queue_item::rates_queue_item(CIcqProto *ppro, WORD wGroup) : bCreated(FALSE), dwUin(0), szUid(NULL)
{
	this->ppro = ppro;
	this->wGroup = wGroup;
}

rates_queue_item::~rates_queue_item()
{
	if (bCreated) {
		SAFE_FREE(&szUid);
		bCreated = FALSE;
	}
}

BOOL rates_queue_item::isEqual(rates_queue_item *pItem)
{
	// the same event (equal address of _vftable) for the same contact
	return (pItem->hContact == this->hContact) && (*(void**)pItem == *(void**)this);
}

rates_queue_item* rates_queue_item::copyItem(rates_queue_item *pDest)
{
	if (!pDest)
		pDest = new rates_queue_item(ppro, wGroup);

	pDest->hContact = hContact;
	pDest->dwUin = dwUin;
	pDest->szUid = dwUin ? null_strdup(szUid) : NULL;
	pDest->bCreated = TRUE;

	return pDest;
}

void rates_queue_item::execute()
{
	ppro->debugLogA("Rates: Error executing abstract event.");
}

BOOL rates_queue_item::isOverRate(int nLevel)
{
	mir_cslock l(ppro->m_ratesMutex);

	if (ppro->m_rates)
		return ppro->m_rates->getNextRateLevel(wGroup) < ppro->m_rates->getLimitLevel(wGroup, nLevel);

	return FALSE;
}

rates_queue::rates_queue(CIcqProto *_ppro, const char *_descr, int nLimitLevel, int nWaitLevel, int nDuplicates) :
	lstPending(1)
{
	ppro = _ppro;
	szDescr = _descr;
	limitLevel = nLimitLevel;
	waitLevel = nWaitLevel;
	duplicates = nDuplicates;
}

rates_queue::~rates_queue()
{
	cleanup();
}

// links to functions that are under Rate Control
struct rate_delay_args
{
	int nDelay;
	rates_queue *queue;
	IcqRateFunc delaycode;
};

void __cdecl CIcqProto::rateDelayThread(rate_delay_args *pArgs)
{
	SleepEx(pArgs->nDelay, TRUE);
	(pArgs->queue->*pArgs->delaycode)();
	SAFE_FREE((void**)&pArgs);
}

void rates_queue::initDelay(int nDelay, IcqRateFunc delaycode)
{
	ppro->debugLogA("Rates: Delay %dms", nDelay);

	rate_delay_args *pArgs = (rate_delay_args*)SAFE_MALLOC(sizeof(rate_delay_args)); // This will be freed in the new thread
	pArgs->queue = this;
	pArgs->nDelay = nDelay;
	pArgs->delaycode = delaycode;

	ppro->ForkThread((CIcqProto::MyThreadFunc)&CIcqProto::rateDelayThread, pArgs);
}

void rates_queue::cleanup()
{
	mir_cslock l(csLists);
	for (int i = 0; i < lstPending.getCount(); i++)
		delete lstPending[i];
	lstPending.destroy();
}

void rates_queue::processQueue()
{
	if (!ppro->icqOnline()) {
		cleanup();
		return;
	}

	// take from queue, execute
	mir_cslockfull l(csLists);
	if (lstPending.getCount() == 0)
		return;

	rates_queue_item *item = lstPending[0];
	{
		mir_cslockfull rlck(ppro->m_ratesMutex);
		if (item->isOverRate(limitLevel)) { // the rate is higher, keep sleeping
			int nDelay = ppro->m_rates->getDelayToLimitLevel(item->wGroup, ppro->m_rates->getLimitLevel(item->wGroup, waitLevel));

			l.unlock();
			rlck.unlock();
			if (nDelay < 10) nDelay = 10;
			initDelay(nDelay, &rates_queue::processQueue);
			return;
		}
	}

	lstPending.remove(int(0));
	bool bSetupTimer = lstPending.getCount() != 0;

	l.unlock();

	if (ppro->icqOnline()) {
		ppro->debugLogA("Rates: Resuming %s.", szDescr);
		item->execute();
	}
	else ppro->debugLogA("Rates: Discarding %s.", szDescr);

	if (bSetupTimer) { // in queue remained some items, setup timer
		int nDelay;
		{
			mir_cslockfull rlck(ppro->m_ratesMutex);
			nDelay = ppro->m_rates->getDelayToLimitLevel(item->wGroup, waitLevel);
		}

		if (nDelay < 10) nDelay = 10;
		initDelay(nDelay, &rates_queue::processQueue);
	}
	delete item;
}

void rates_queue::putItem(rates_queue_item *pItem, int nMinDelay)
{
	if (!ppro->icqOnline())
		return;

	ppro->debugLogA("Rates: Delaying %s.", szDescr);
	{
		mir_cslock l(csLists);
		if (lstPending.getCount()) {
			for (int i = 0; i < lstPending.getCount(); i++) {
				if (lstPending[i]->isEqual(pItem)) {
					if (duplicates == 1) // keep existing, ignore new
						return;

					if (duplicates == -1) { // discard existing, append new item
						delete lstPending[i];
						lstPending.remove(i);
					}
				}
			}
		}

		lstPending.insert(pItem->copyItem());
	}

	if (lstPending.getCount() != 1)
		return;
	
	// queue was empty setup timer
	int nDelay;
	{	mir_cslock rlck(ppro->m_ratesMutex);
		nDelay = ppro->m_rates->getDelayToLimitLevel(pItem->wGroup, waitLevel);
	}

	if (nDelay < 10) nDelay = 10;
	if (nDelay < nMinDelay) nDelay = nMinDelay;
	initDelay(nDelay, &rates_queue::processQueue);
}

int CIcqProto::handleRateItem(rates_queue_item *item, int nQueueType, int nMinDelay, BOOL bAllowDelay)
{
	rates_queue *pQueue = NULL;
	{
		mir_cslock rlck(m_ratesMutex);
		switch (nQueueType) {
		case RQT_REQUEST:
			pQueue = m_ratesQueue_Request;
			break;
		case RQT_RESPONSE:
			pQueue = m_ratesQueue_Response;
			break;
		}

		if (pQueue) {
			if (bAllowDelay && (item->isOverRate(pQueue->waitLevel) || nMinDelay)) {
				// limit reached or min delay configured, add to queue
				pQueue->putItem(item, nMinDelay);
				return 1;
			}
		}
	}

	item->execute();
	return 0;
}