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
|
/*
WinPopup Protocol plugin for Miranda IM.
Copyright (C) 2004-2009 Nikolay Raspopov
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.
*/
#include "stdafx.h"
#include "winpopup_proto.h"
#include "search.h"
#include "netbios.h"
search pluginSearch;
// Ôóíêöèÿ ïðîñòûõ ðåãóëÿðíûõ âûðàæåíèé
bool MatchPattern (LPCTSTR String, LPCTSTR Pattern);
// Ïîëó÷åíèå NetBIOS-èìåí õîñòà è ïðîâåðêà èõ ïðè ïîìîùè ïðîñòûõ ðåãóëÿðíûõ âûðàæåíèé
bool MatchPatternNetBIOS (LPCTSTR Host, LPCTSTR Pattern);
search::search()
: m_count( 0 )
, m_event( NULL )
{
}
INT_PTR search::StartSearch(LPCTSTR szId)
{
LOG("Search \"%s\"", szId);
if (m_event)
// Çàïðîñ íà îñòàíîâ ïðåäûäóùåãî ïîèñêà
SetEvent (m_event);
else
m_event = CreateEvent (NULL, TRUE, FALSE, NULL);
// Íà÷àëî íîâîãî ïîèñêà
if ( BasicSearchData* data = new BasicSearchData )
{
data->me = this;
HANDLE cookie = data->cookie = GenerateCookie();
data->id = szId;
data->id.MakeUpper();
data->root = NULL;
mir_forkthread( BasicSearchThread, data );
return (INT_PTR)cookie;
}
return 0;
}
void search::AskForDestroy()
{
if ( m_event )
SetEvent( m_event );
}
void search::Destroy()
{
if ( m_event )
{
while( m_count )
{
SetEvent( m_event );
Sleep( 250 );
}
CloseHandle( m_event );
m_event = NULL;
}
}
bool MatchPattern (LPCTSTR String, LPCTSTR Pattern)
{
TCHAR c, p, l;
for (;;)
{
switch ( p = *Pattern++ )
{
case 0:
// end of pattern
return *String ? false : true; // if end of string TRUE
case _T('*'):
// match zero or more char
while (*String)
if (MatchPattern (String++, Pattern))
return true;
return MatchPattern (String, Pattern);
case _T('?'):
// match any one char
if (*String++ == 0)
return false; // not end of string
break;
case _T('['):
// match char set
if ((c = *String++) == 0)
return false; // syntax
l = 0;
if (*Pattern == _T('!'))
{ // match a char if NOT in set []
++Pattern;
while ((p = *Pattern++)!= _T('\0'))
{
if (p == _T(']')) // if end of char set, then
break; // no match found
if (p == _T('-'))
{ // check a range of chars?
p = *Pattern;
// get high limit of range
if (p == 0 || p == _T(']'))
return false; // syntax
if (c >= l && c <= p)
return false; // if in range
}
l = p;
// if char matches this element
if (c == p)
return false;
}
}
else
{ // match if char is in set []
while ((p = *Pattern++) != _T('\0'))
{
if (p == _T(']')) // if end of char set, then
return false; // no match found
if (p == _T('-')) { // check a range of chars?
p = *Pattern;
// get high limit of range
if (p == 0 || p == _T(']'))
return false; // syntax
if (c >= l && c <= p)
break; // if in range, move on
}
l = p;
// if char matches this element
if (c == p)
break; // move on
}
while (p && p != _T(']')) // got a match in char set
p = *Pattern++; // skip to end of set
}
break;
case _T('#'):
c = *String++;
if (c < _T('0') || c > _T('9'))
return false; // not a digit
break;
default:
// check for exact char
c = *String++;
if (c != p)
return false; // not a match
break;
}
}
}
bool MatchPatternNetBIOS (LPCTSTR Host, LPCTSTR Pattern)
{
netbios_name_list names;
if ( pluginNetBIOS.GetNames( names, Host, false ) )
{
POSITION pos = names.GetHeadPosition ();
CString n;
while ( pos )
{
netbios_name& name = names.GetNext (pos);
if ( name.GetType() == 3 )
{
CA2T sName( name.original );
if ( MatchPattern( (LPCTSTR)sName, Pattern ) )
return true;
}
}
}
return false;
}
void search::BasicSearchJob(const BasicSearchData* data)
{
if (WaitForSingleObject (m_event, 0) != WAIT_TIMEOUT)
return;
HANDLE hEnum = NULL;
DWORD res = WNetOpenEnum (RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER, data->root, &hEnum);
if (res == NO_ERROR)
{
for (;;)
{
if (WaitForSingleObject (m_event, 0) != WAIT_TIMEOUT)
return;
DWORD cCount = 1;
DWORD BufferSize = 4096;
char* Buffer = (char*)mir_alloc( BufferSize );
if ( ! Buffer )
break;
res = WNetEnumResource( hEnum, &cCount, Buffer, &BufferSize );
if ( res == NO_ERROR )
{
if (WaitForSingleObject (m_event, 0) != WAIT_TIMEOUT)
return;
LPNETRESOURCE lpnr = (LPNETRESOURCE)Buffer;
if ( lpnr->dwDisplayType == RESOURCEDISPLAYTYPE_SERVER )
{
// Ñâåðêà íàçâàíèÿ êîíòàêòà
// èìÿ êîìïüþòåðà, êîììåíòàðèé, èìÿ ðàáî÷åé ãðóïïû
CharUpper (lpnr->lpRemoteName);
CString comment;
if (lpnr->lpComment)
{
comment = lpnr->lpComment;
CharUpper (lpnr->lpComment);
}
if (MatchPattern (lpnr->lpRemoteName + 2, data->id) ||
(lpnr->lpComment && MatchPattern (lpnr->lpComment, data->id)) ||
(data->root && MatchPattern (data->root->lpRemoteName, data->id)) ||
MatchPatternNetBIOS (lpnr->lpRemoteName + 2, data->id))
{
// Äîáàâëåíèå êîíòàêòà
PROTOSEARCHRESULT psr = {};
psr.cbSize = sizeof( PROTOSEARCHRESULT );
CT2A RemoteNameA( lpnr->lpRemoteName );
psr.nick = RemoteNameA + 2;
CT2A commentA( comment );
psr.firstName = commentA;
CT2A RootNameA( data->root ? data->root->lpRemoteName : _T("") );
psr.lastName = RootNameA;
psr.email = "";
ProtoBroadcastAck (modname, NULL, ACKTYPE_SEARCH, ACKRESULT_DATA,
data->cookie, (LPARAM) &psr);
}
}
else
{
if ( ( lpnr->dwUsage & 0xffff ) == RESOURCEUSAGE_CONTAINER )
{
if ( BasicSearchData* data1 = new BasicSearchData )
{
data1->me = data->me;
data1->cookie = data->cookie;
data1->id = data->id;
data1->root = lpnr;
Buffer = NULL;
res = (DWORD)InterlockedIncrement (&m_count);
mir_forkthread( BasicSearchThread, data1 );
}
}
}
mir_free( Buffer );
}
else
{
mir_free( Buffer );
break;
}
}
WNetCloseEnum (hEnum);
}
}
void search::BasicSearchThread(LPVOID param)
{
if ( BasicSearchData* data = (BasicSearchData*)param )
{
data->me->BasicSearch( data );
if ( data->root ) mir_free( data->root );
delete data;
}
}
void search::BasicSearch(const BasicSearchData* data)
{
// Ïîâòîðíûé çàïóñê?
if ( data->root == NULL )
{
while( m_count )
{
if ( ! pluginInstalled )
return;
// Óæå åñòü ïîèñê â ïðîöåññå, îæèäàíèå îñòàíîâà
Sleep (100);
}
InterlockedIncrement( &m_count );
ResetEvent( m_event );
}
// Ñ ïëàãèíîì âñ¸ â ïîðÿäêå?
if ( pluginInstalled )
{
BasicSearchJob( data );
LONG res = InterlockedDecrement( &m_count );
_ASSERTE( res >= 0 );
if ( res == 0 )
{
// Ïîèñê çàâåðø¸í øòàòíî?
if ( WaitForSingleObject( m_event, 0 ) != WAIT_OBJECT_0 )
{
ProtoBroadcastAck (modname, NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, data->cookie, 0);
}
else
{
LOG("Search aborted by another search");
}
}
}
}
|