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
|
// ---------------------------------------------------------------------------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,2007 Joe Kucera
// Copyright 2006,2007 [sss], chaos.persei, [sin], Faith Healer, Theif, nullbie
//
// 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$
// Revision : $Revision: 36 $
// Last change on : $Date: 2007-08-05 03:45:10 +0300 (Вс, 05 авг 2007) $
// Last change by : $Author: sss123next $
//
// DESCRIPTION:
//
// Describe me here please...
//
// -----------------------------------------------------------------------------
#include "icqoscar.h"
#define LISTSIZE 100
static CRITICAL_SECTION listmutex;
static HANDLE hQueueEvent = NULL;
static int nUserCount = 0;
static int bPendingUsers = 0;
static BOOL bEnabled = FALSE;
static BOOL bRunning = FALSE;
static HANDLE hInfoThread = NULL;
static DWORD dwUpdateThreshold;
typedef struct s_userinfo {
DWORD dwUin;
HANDLE hContact;
} userinfo;
static userinfo userList[LISTSIZE];
// Retrieve users' info
unsigned __stdcall icq_InfoUpdateThread(void* arg);
void icq_InitInfoUpdate(void)
{
int i;
// Create wait objects
hQueueEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hQueueEvent)
{
// Init mutexes
InitializeCriticalSection(&listmutex);
// Init list
for (i = 0; i<LISTSIZE; i++)
{
userList[i].dwUin = 0;
userList[i].hContact = NULL;
}
dwUpdateThreshold = ICQGetContactSettingByte(NULL, "InfoUpdate", UPDATE_THRESHOLD)*3600*24;
hInfoThread = ICQCreateThreadEx(icq_InfoUpdateThread, NULL, NULL);
}
bPendingUsers = 0;
}
// Returns TRUE if user was queued
// Returns FALSE if the list was full
BOOL icq_QueueUser(HANDLE hContact)
{
if (nUserCount < LISTSIZE)
{
int i, nChecked = 0, nFirstFree = -1;
BOOL bFound = FALSE;
EnterCriticalSection(&listmutex);
// Check if in list
for (i = 0; (i<LISTSIZE && nChecked<nUserCount); i++)
{
if (userList[i].hContact)
{
nChecked++;
if (userList[i].hContact == hContact)
{
bFound = TRUE;
break;
}
}
else if (nFirstFree == -1)
{
nFirstFree = i;
}
}
if (nFirstFree == -1)
nFirstFree = i;
// Add to list
if (!bFound)
{
DWORD dwUin = ICQGetContactSettingUIN(hContact);
if (dwUin)
{
userList[nFirstFree].dwUin = dwUin;
userList[nFirstFree].hContact = hContact;
nUserCount++;
#ifdef _DEBUG
NetLog_Server("Queued user %u, place %u, count %u", userList[nFirstFree].dwUin, nFirstFree, nUserCount);
#endif
// Notify worker thread
if (hQueueEvent)
SetEvent(hQueueEvent);
}
}
LeaveCriticalSection(&listmutex);
return TRUE;
}
return FALSE;
}
void icq_DequeueUser(DWORD dwUin)
{
if (nUserCount > 0)
{
int i, nChecked = 0;
// Check if in list
EnterCriticalSection(&listmutex);
for (i = 0; (i<LISTSIZE && nChecked<nUserCount); i++)
{
if (userList[i].dwUin)
{
nChecked++;
// Remove from list
if (userList[i].dwUin == dwUin)
{
#ifdef _DEBUG
NetLog_Server("Dequeued user %u", userList[i].dwUin);
#endif
userList[i].dwUin = 0;
userList[i].hContact = NULL;
nUserCount--;
break;
}
}
}
LeaveCriticalSection(&listmutex);
}
}
void icq_RescanInfoUpdate()
{
HANDLE hContact = NULL;
DWORD dwCurrentTime = 0;
BOOL bOldEnable = bEnabled;
bPendingUsers = 0;
/* This is here, cause we do not want to emit large number of reuqest at once,
fill queue, and let thread deal with it */
bEnabled = 0; // freeze thread
// Queue all outdated users
dwCurrentTime = time(NULL);
hContact = ICQFindFirstContact();
while (hContact != NULL)
{
if ((dwCurrentTime - ICQGetContactSettingDword(hContact, "InfoTS", 0)) > dwUpdateThreshold)
{
// Queue user
if (!icq_QueueUser(hContact))
{ // The queue is full, pause queuing contacts
bPendingUsers = 1;
break;
}
}
hContact = ICQFindNextContact(hContact);
}
icq_EnableUserLookup(bOldEnable); // wake up thread
}
void icq_EnableUserLookup(BOOL bEnable)
{
bEnabled = bEnable;
// Notify worker thread
if (bEnabled && hQueueEvent)
SetEvent(hQueueEvent);
}
unsigned __stdcall icq_InfoUpdateThread(void* arg)
{
int i;
DWORD dwWait;
bRunning = TRUE;
while (bRunning)
{
// Wait for a while
ResetEvent(hQueueEvent);
if (!nUserCount && bPendingUsers) // whole queue processed, check if more users needs updating
icq_RescanInfoUpdate();
if (!nUserCount || !bEnabled || !icqOnline)
{
dwWait = WAIT_TIMEOUT;
while (bRunning && dwWait == WAIT_TIMEOUT)
{ // wait for new work or until we should end
dwWait = WaitForSingleObjectEx(hQueueEvent, 10000, TRUE);
}
}
if (!bRunning) break;
switch (dwWait)
{
case WAIT_IO_COMPLETION:
// Possible shutdown in progress
break;
case WAIT_OBJECT_0:
case WAIT_TIMEOUT:
// Time to check for new users
if (!bEnabled) continue; // we can't send requests now
#ifdef _DEBUG
NetLog_Server("Users %u", nUserCount);
#endif
if (nUserCount && icqOnline)
{
EnterCriticalSection(&listmutex);
for (i = 0; i<LISTSIZE; i++)
{
if (userList[i].hContact)
{
// Check TS again, maybe it has been updated while we slept
if ((time(NULL) - ICQGetContactSettingDword(userList[i].hContact, "InfoTS", 0)) > dwUpdateThreshold)
{
WORD wGroup;
EnterCriticalSection(&ratesMutex);
if (!gRates)
{ // we cannot send info request - icq is offline
LeaveCriticalSection(&ratesMutex);
break;
}
wGroup = ratesGroupFromSNAC(gRates, ICQ_EXTENSIONS_FAMILY, ICQ_META_CLI_REQ);
while (ratesNextRateLevel(gRates, wGroup) < ratesGetLimitLevel(gRates, wGroup, RML_IDLE_50))
{ // we are over rate, need to wait before sending
int nDelay = ratesDelayToLevel(gRates, wGroup, ratesGetLimitLevel(gRates, wGroup, RML_IDLE_50) + 200);
LeaveCriticalSection(&ratesMutex);
LeaveCriticalSection(&listmutex);
#ifdef _DEBUG
NetLog_Server("Rates: InfoUpdate delayed %dms", nDelay);
#endif
SleepEx(nDelay, TRUE); // do not keep things locked during sleep
if (!bRunning)
{ // need to end as fast as possible
NetLog_Server("%s thread ended.", "Info-Update");
return 0;
}
EnterCriticalSection(&listmutex);
EnterCriticalSection(&ratesMutex);
if (!gRates) // we lost connection when we slept, go away
break;
}
if (!gRates)
{ // we cannot send info request - icq is offline
LeaveCriticalSection(&ratesMutex);
break;
}
LeaveCriticalSection(&ratesMutex);
if (!userList[i].hContact) continue; // user was dequeued during our sleep
#ifdef _DEBUG
NetLog_Server("Request info for user %u", userList[i].dwUin);
#endif
sendUserInfoAutoRequest(userList[i].hContact, userList[i].dwUin);
// Dequeue user and go back to sleep
userList[i].dwUin = 0;
userList[i].hContact = NULL;
nUserCount--;
break;
}
else
{
#ifdef _DEBUG
NetLog_Server("Dequeued absolete user %u", userList[i].dwUin);
#endif
// Dequeue user and find another one
userList[i].dwUin = 0;
userList[i].hContact = NULL;
nUserCount--;
// continue for loop
}
}
}
LeaveCriticalSection(&listmutex);
}
break;
default:
// Something strange happened. Exit
bRunning = FALSE;
break;
}
}
NetLog_Server("%s thread ended.", "Info-Update");
return 0;
}
// Clean up before exit
void icq_InfoUpdateCleanup(void)
{
bRunning = FALSE;
SetEvent(hQueueEvent); // break queue loop
if (hInfoThread) WaitForSingleObjectEx(hInfoThread, INFINITE, TRUE);
// Uninit mutex
DeleteCriticalSection(&listmutex);
CloseHandle(hQueueEvent);
CloseHandle(hInfoThread);
}
|