summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/WinPopup/src/scanner.cpp
blob: 5074e97a5f172355388dfba18e3d9744e85cf6dc (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
/*

WinPopup Protocol plugin for Miranda IM.

Copyright (C) 2004-2011 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"

contact_scanner	pluginScanner;

contact_scanner::contact_scanner () :
	m_ScannerTerm (NULL),
	m_Scanner (NULL)
{
}

contact_scanner::~contact_scanner ()
{
	Destroy ();
}

bool contact_scanner::Create ()
{
	// Çàïóñê ñêàíåðà êîíòàêòîâ
	if (m_ScannerTerm)
		ResetEvent (m_ScannerTerm);
	else
		m_ScannerTerm = CreateEvent (NULL, TRUE, FALSE, NULL);

	if ( ! m_Scanner )
		m_Scanner = (HANDLE)mir_forkthread( ScannerThread, this );

	return ( m_Scanner != NULL );
}

void contact_scanner::AskForDestroy()
{
	if (m_ScannerTerm)
		SetEvent (m_ScannerTerm);
}

void contact_scanner::Destroy ()
{
	AskForDestroy();

	if (m_Scanner)
	{
		if (WaitForSingleObject (m_Scanner, ALMOST_INFINITE) == WAIT_TIMEOUT)
		{
			LOG("Terminate scanner!");
			TerminateThread (m_Scanner, 0);
		}
		m_Scanner = NULL;
	}

	if (m_ScannerTerm) {
		CloseHandle (m_ScannerTerm);
		m_ScannerTerm = NULL;
	}
}

contact_scanner::operator bool () const
{
	return ( m_Scanner != NULL );
}

// First, Next, ... Next, NULL, First, Next...
HANDLE contact_scanner::GetNextScannableContact()
{
	static HANDLE hContact = NULL;
	if (!hContact)
		hContact = (HANDLE) CallService (MS_DB_CONTACT_FINDFIRST, 0, 0);

	// Öèêëè÷åñêèé ïåðåáîð êîíòàêòîâ
	while( hContact )
	{
		// Ïðîâåðêà íà ñîâïàäåíèå ïðîòîêîëà êîíòàêòà
		if ( IsMyContact( hContact ) &&
			!DBGetContactSettingByte (hContact, "CList", "NotOnList", 0) &&
			!DBGetContactSettingByte (hContact, "CList", "Hidden", 0) )
		{
			// Âû÷èñëåíèå ñêîëüêî ñåêóíä ïðîøëî ñî âðåìåíè ïîñëåäíåãî
			// îáíîâëåíèÿ ñòàòóñà êîíòàêòà. Íóæíà ïðîâåðêà?
			DWORD elapsed = GetElapsed (hContact, "LastSeen");
			if ( elapsed >= MIN_PING_INTERVAL )
				break;
		}
		hContact = (HANDLE) CallService (MS_DB_CONTACT_FINDNEXT, (WPARAM) hContact, 0);
	}
	return hContact;
}

int contact_scanner::ScanContact(HANDLE  hContact)
{
	if ( ! pluginInstalled )
		return ID_STATUS_OFFLINE;

#ifdef CHAT_ENABLED
	if ( IsChatRoom( hContact ) )
	{
		CString sSession = GetChatSession( hContact );
		if ( pluginChatEnabled && ! sSession.IsEmpty() )
		{
			CAtlList< CString > lst;
			EnumWorkgroups( lst );
			for ( POSITION pos = lst.GetHeadPosition(); pos; )
			{
				if ( lst.GetNext( pos ) == sSession )
					return ID_STATUS_ONLINE;
			}
		}
		return ID_STATUS_OFFLINE;	
	}
	else
#endif // CHAT_ENABLED
	{
		int status = ID_STATUS_OFFLINE;

		// Ïîëó÷åíèå ñòàòóñà "Always Online"
		if ( DBGetContactSettingByte( hContact, modname, "AlwaysOnline", FALSE ) )
			status = ID_STATUS_ONLINE;

		// Ïîëó÷åíèå èìåíè êîíòàêòà
		CString sNick = GetNick( hContact );
		if ( ! sNick.IsEmpty() )
		{
			if ( IsGroup( hContact ) )
			{
				// Èìÿ "ÂÑÅ" âåãäà îíëàéí
				if ( sNick == _T("*") )
					return ID_STATUS_ONLINE;

				// Ïåðå÷èñëåíèå ãðóïï è ñëè÷åíèå ñ íàøåé
				CAtlList< CString > lst;
				EnumWorkgroups( lst );
				for ( POSITION pos = lst.GetHeadPosition(); pos; )
				{
					if ( lst.GetNext( pos ).CompareNoCase( sNick ) == 0 )
						return ID_STATUS_ONLINE;
				}
			}
			else if ( IsLegacyOnline( NULL ) || IsLegacyOnline( hContact ) )
			{
				// Ñèíõðîííûé îïðîñ õîñòà íà íàëè÷èå NetBIOS-èìåíè "Nick <00> U"
				netbios_name nname( sNick, 0x00, false );
				UCHAR foo;
				if ( pluginNetBIOS.FindNameLana( nname, foo ) )
				{
					status = ID_STATUS_ONLINE;

					// Àñèíõðîííûé îïðîñ êîíòàêòà "Nick <00> U"
					pluginNetBIOS.AskStatus( nname );
				}
			}
			else
			{
				// Ñèíõðîííûé îïðîñ õîñòà íà íàëè÷èå NetBIOS-èìåíè "Nick <03> U"
				netbios_name nname( sNick, 0x03, false );
				UCHAR foo;
				if ( pluginNetBIOS.FindNameLana( nname, foo ) )
				{
					status = ID_STATUS_ONLINE;

					// Àñèíõðîííûé îïðîñ êîíòàêòà "Nick <03> U"
					pluginNetBIOS.AskStatus( nname );
				}
			}
		}
		return status;
	}
}

void contact_scanner::Scanner ()
{
	while (WaitForSingleObject (m_ScannerTerm, 1000) == WAIT_TIMEOUT)
	{
		// Âûáîðêà ñëåäóþùåãî êàíäèäàòà íà ïðîâåðêó
		if ( HANDLE hContact = GetNextScannableContact() )
		{
			// Ïðîâåðêà êîíòàêòà
			SetContactStatus( hContact, ScanContact( hContact ), true );
		}
	}
}

void contact_scanner::ScannerThread (LPVOID lpParameter)
{
	if ( contact_scanner* pScanner = (contact_scanner*)lpParameter )
	{
		pScanner->Scanner();
		pScanner->m_Scanner = NULL;
	}
}