summaryrefslogtreecommitdiff
path: root/icqj_mod/icq_rates.c
blob: fa84c4c177f077a98ca8818b58fc4f125a03684b (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
// ---------------------------------------------------------------------------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,2003,2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004,2005,2006 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// -----------------------------------------------------------------------------
//
// File name      : $Source: /cvsroot/miranda/miranda/protocols/IcqOscarJ/icq_rates.c,v $
// Revision       : $Revision: 2874 $
// Last change on : $Date: 2006-05-16 23:38:00 +0200 (út, 16 V 2006) $
// Last change by : $Author: ghazan $
//
// DESCRIPTION:
//
//  Rate Management stuff
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"

// links to functions that are under Rate Control
extern DWORD sendXStatusDetailsRequest(HANDLE hContact, int bForced);

static int rGroupXtrazRequest = 4500; // represents higher limits for ICQ Rate Group #3
static int tGroupXtrazRequest;
static int rGroupMsgResponse = 6000;  //  dtto #1
static int tGroupMsgResponse;

static CRITICAL_SECTION ratesMutex;  // we need to be thread safe

static rate_record **pendingList1; // rate queue for xtraz requests
static int pendingListSize1;

static rate_record **pendingList2; // rate queue for msg responses
static int pendingListSize2;


  
typedef struct rate_delay_args_s
{
  int nDelay;
  void (*delaycode)();
} rate_delay_args;

void __cdecl rateDelayThread(rate_delay_args* pArgs)
{
  SleepEx(pArgs->nDelay, TRUE);
  pArgs->delaycode();

  SAFE_FREE(&pArgs);
  return;
}



void InitDelay(int nDelay, void (*delaycode)())
{
  rate_delay_args* pArgs;

//#ifdef _DEBUG
  NetLog_Server("Delay %dms", nDelay);
//#endif

  pArgs = (rate_delay_args*)SAFE_MALLOC(sizeof(rate_delay_args)); // This will be freed in the new thread

  pArgs->nDelay = nDelay;
  pArgs->delaycode = delaycode;

  forkthread(rateDelayThread, 0, pArgs);
}



static void RatesTimer1()
{
  rate_record *item;
  int nLev;

  if (!pendingList1) return;

  EnterCriticalSection(&ratesMutex);
  // take from queue, execute
  item = pendingList1[0];
  if (pendingListSize1 > 1)
  { // we need to keep order
    memmove(&pendingList1[0], &pendingList1[1], (pendingListSize1 - 1)*sizeof(rate_record*));
  }
  else
    SAFE_FREE((void**)&pendingList1);
  pendingListSize1--;
  nLev = rGroupXtrazRequest*19/20 + (GetTickCount() - tGroupXtrazRequest)/20;
  rGroupXtrazRequest = nLev < 4500 ? nLev : 4500;
  tGroupXtrazRequest = GetTickCount();
  if (pendingListSize1 && icqOnline)
  { // in queue remains some items, setup timer
    int nDelay = (3800 - rGroupXtrazRequest)*20;

    if (nDelay < 10) nDelay = 10;
    InitDelay(nDelay, RatesTimer1);
  }
  if (!icqOnline)
  {
    int i;

    for (i=0; i<pendingListSize1; i++) SAFE_FREE(&pendingList1[i]);
    SAFE_FREE((void**)&pendingList1);
    pendingListSize1 = 0;
  }
  LeaveCriticalSection(&ratesMutex);

  if (icqOnline)
  {
    NetLog_Server("Rates: Resuming Xtraz request.");
    if (item->bType = RIT_XSTATUS_REQUEST)
      sendXStatusDetailsRequest(item->hContact, FALSE);
  }
  else
    NetLog_Server("Rates: Discarding request.");
  SAFE_FREE(&item);
}



static void putItemToQueue1(rate_record *item, int nLev)
{
  int nDelay = (3800 - nLev)*20;

  if (!icqOnline) return;

  NetLog_Server("Rates: Delaying operation.");

  if (pendingListSize1)
  { // something already in queue, check duplicity
    rate_record *tmp;
    int i;

    for (i = 0; i < pendingListSize1; i++)
    { // TODO: make this more universal - for more xtraz msg types
      if (pendingList1[i]->hContact == item->hContact)
        return; // request xstatus from same contact, do it only once
    }
    pendingListSize1++;
    pendingList1 = (rate_record**)realloc(pendingList1, pendingListSize1*sizeof(rate_record*));
    tmp = (rate_record*)SAFE_MALLOC(sizeof(rate_record));
    memcpy(tmp, item, sizeof(rate_record));
    pendingList1[pendingListSize1 - 1] = tmp;
  }
  else
  {
    rate_record *tmp;

    pendingListSize1++;
    pendingList1 = (rate_record**)SAFE_MALLOC(sizeof(rate_record*));
    tmp = (rate_record*)SAFE_MALLOC(sizeof(rate_record));
    memcpy(tmp, item, sizeof(rate_record));
    pendingList1[0] = tmp;

    if (nDelay < 10) nDelay = 10;
    if (nDelay < item->nMinDelay) nDelay = item->nMinDelay;
    InitDelay(nDelay, RatesTimer1);
  }
}



static void RatesTimer2()
{
  rate_record *item;
  int nLev;

  if (!pendingList2) return;

  EnterCriticalSection(&ratesMutex);
  // take from queue, execute
  item = pendingList2[0];
  if (pendingListSize2 > 1)
  { // we need to keep order
    memmove(&pendingList2[0], &pendingList2[1], (pendingListSize2 - 1)*sizeof(rate_record*));
  }
  else
    SAFE_FREE((void**)&pendingList2);
  pendingListSize2--;
  nLev = rGroupMsgResponse*79/80 + (GetTickCount() - tGroupMsgResponse)/80;
  rGroupMsgResponse = nLev < 6000 ? nLev : 6000;
  tGroupMsgResponse = GetTickCount();
  if (pendingListSize2 && icqOnline)
  { // in queue remains some items, setup timer
    int nDelay = (4500 - rGroupMsgResponse)*80;

    if (nDelay < 10) nDelay = 10;
    InitDelay(nDelay, RatesTimer2);
  }
  if (!icqOnline)
  {
    int i;

    for (i=0; i<pendingListSize2; i++) SAFE_FREE(&pendingList2[i]);
    SAFE_FREE((void**)&pendingList2);
    pendingListSize2 = 0;
  }
  LeaveCriticalSection(&ratesMutex);

  if (icqOnline)
  {
    NetLog_Server("Rates: Resuming message response.");
    if (item->bType == RIT_AWAYMSG_RESPONSE)
    {
      icq_sendAwayMsgReplyServ(item->dwUin, item->dwMid1, item->dwMid2, item->wCookie, item->wVersion, item->msgType, MirandaStatusToAwayMsg(AwayMsgTypeToStatus(item->msgType)));
    }
    else if (item->bType == RIT_XSTATUS_RESPONSE)
    {
      SendXtrazNotifyResponse(item->dwUin, item->dwMid1, item->dwMid2, item->wCookie, item->szData, strlennull(item->szData), item->bThruDC);
    }
  }
  else
    NetLog_Server("Rates: Discarding response.");
  SAFE_FREE(&item->szData);
  SAFE_FREE(&item);
}



static void putItemToQueue2(rate_record *item, int nLev)
{
  int nDelay = (4500 - nLev)*80;
  rate_record *tmp;

  if (!icqOnline) return;

  NetLog_Server("Rates: Delaying operation.");

  pendingListSize2++;
  pendingList2 = (rate_record**)realloc(pendingList2, pendingListSize2*sizeof(rate_record*));
  tmp = (rate_record*)SAFE_MALLOC(sizeof(rate_record));
  memcpy(tmp, item, sizeof(rate_record));
  tmp->szData = null_strdup(item->szData);
  pendingList2[pendingListSize2 - 1] = tmp;

  if (pendingListSize2 == 1)
  { // queue was empty setup timer
    if (nDelay < 10) nDelay = 10;
    InitDelay(nDelay, RatesTimer2);
  }
}



int handleRateItem(rate_record *item, BOOL bAllowDelay)
{
  int tNow = GetTickCount();

  EnterCriticalSection(&ratesMutex);

  if (item->rate_group == 0x101)
  { // xtraz request
    int nLev = rGroupXtrazRequest*19/20 + (tNow - tGroupXtrazRequest)/20;

    if ((nLev < 3800 || item->nMinDelay) && bAllowDelay)
    { // limit reached or min delay configured, add to queue
      putItemToQueue1(item, nLev);
      LeaveCriticalSection(&ratesMutex);
      return 1;
    }
    else
    {
      rGroupXtrazRequest = nLev < 4500 ? nLev : 4500;
      tGroupXtrazRequest = tNow;
    }
  }
  else if (item->rate_group == 0x102)
  { // msg response
    int nLev = rGroupMsgResponse*79/80 + (tNow - tGroupMsgResponse)/80;

    if (nLev < 4500)
    { // limit reached or min delay configured, add to queue
      putItemToQueue2(item, nLev);
      LeaveCriticalSection(&ratesMutex);
      return 1;
    }
    else
    {
      rGroupMsgResponse = nLev < 6000 ? nLev : 6000;
      tGroupMsgResponse = tNow;
    }
  }
  LeaveCriticalSection(&ratesMutex);

  return 0;
}



void InitRates()
{
  InitializeCriticalSection(&ratesMutex);

  pendingListSize1 = 0;
  pendingList1 = NULL;
  pendingListSize2 = 0;
  pendingList2 = NULL;

  tGroupXtrazRequest = tGroupMsgResponse = GetTickCount();
}



void UninitRates()
{
  SAFE_FREE((void**)&pendingList1);
  SAFE_FREE((void**)&pendingList2);
  DeleteCriticalSection(&ratesMutex);
}