summaryrefslogtreecommitdiff
path: root/protocols/WinPopup/src/mailslot.cpp
blob: f632872d368bd10e527170f079025f71edb9c78d (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
/*

WinPopup Protocol plugin for Miranda IM.

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

#define MAX_MESSAGE_SIZE		424 // Ðàçìåð ïàêåòà äàííûõ (áàéò) ïîñûëàåìûõ/ïðèíèìàåìûõ
									// ÷åðåç ìåéëñëîò

mailslot pluginMailslot;			// Ìåéëñëîò äëÿ ïðèåìà ñîîáùåíèé

/*const struct {					// Ñïèñîê êîíôëèêòóþùèõ ïðîöåññîâ
	LPCTSTR name;
} blacklist [] = {
	_T("winpopup"),
	_T("vikpopup"),
	_T("netter"),
	_T("realpopup"),
	NULL
};*/

////////////////////////////////////////////////////////////////////////
// Class mailslot

mailslot::mailslot () :
	m_hMailslot (INVALID_HANDLE_VALUE),
	m_MonitorTerm (NULL),
	m_Monitor (NULL)
{
}

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

bool mailslot::Create (LPCTSTR Name)
{
	CLock oLock( m_cs );

	m_sName = Name;

	bool ret = true;
	if ( ! IsValid() )
	{		
		// Îòêðûòèå ìåéëñëîòà
		CString sAddr;
		sAddr.Format( _T("\\\\.\\mailslot\\%s"), (LPCTSTR)m_sName );

		m_hMailslot = CreateMailslot( sAddr, 0, 2000, NULL );
		if ( ! IsValid() )
		{
			ret = false;
			DWORD err = GetLastError ();
			if (err == ERROR_ALREADY_EXISTS)
				WarningBox (NULL, 0, _T("%s\r\n%s"), T_CREATE_ERROR,
					TranslateT ("Please shutdown any other IM applications and/or Messenger service"));
			else
				WarningBox (NULL, err, T_CREATE_ERROR);
		}
	}
	if (ret)
	{
		if (m_MonitorTerm)
			ResetEvent (m_MonitorTerm);
		else
			m_MonitorTerm = CreateEvent (NULL, TRUE, FALSE, NULL);
		m_Monitor = (HANDLE)mir_forkthread( MonitorThread, this );
	}

	return ret;
}

void mailslot::AskForDestroy()
{
	if (m_MonitorTerm)
		SetEvent (m_MonitorTerm);
}

void mailslot::Destroy ()
{
	CLock oLock( m_cs );
	
	// Çàïðîñ îñòàíîâà ìîíèòîðèíãà ìåéëñëîòà
	AskForDestroy();
	
	// Çàêðûòèå ìåéëñëîòà
	if ( IsValid() )
	{
		CloseHandle (m_hMailslot);
		m_hMailslot = INVALID_HANDLE_VALUE;
	}

	// Îæèäàíèå îñòàíîâà ìîíèòîðèíãà
	if (m_Monitor)
	{
		if (WaitForSingleObject (m_Monitor, ALMOST_INFINITE) == WAIT_TIMEOUT)
		{
			LOG("Terminate mailslot monitor!");
			TerminateThread (m_Monitor, 0);
		}
		m_Monitor = NULL;
	}
	if (m_MonitorTerm)
	{
		CloseHandle (m_MonitorTerm);
		m_MonitorTerm = NULL;
	}
}

bool mailslot::IsValid() const
{
	return ( m_hMailslot != INVALID_HANDLE_VALUE );
}

bool mailslot::SendMailslotMessage(HANDLE hContact, LPCTSTR msg, DWORD& err)
{
	// Ïîëó÷åíèå àäðåñàòà
	CString sTo = GetNick( hContact );
	if ( sTo.IsEmpty() )
	{
		err = ERROR_BAD_NETPATH;
		return false;
	}

	// Ïîëó÷åíèå ñâîåãî èìåíè
	CString sFrom = GetNick( NULL );
	if ( sFrom.IsEmpty() )
	{
		err = ERROR_BAD_NETPATH;
		return false;
	}

	// Íåò ðàçíèöû ãðóïïà ýòî èëè êîìïüþòåð
	// bool bGroup = IsGroup( hContact );

	// Ñáîðêà ïàêåòà ñîîáùåíèÿ: FROM<00>TO<00>MESSAGE<00>
	COemString sOemMessage = msg;
	COemString sOemTo = (LPCTSTR)sTo;
	COemString sOemFrom = (LPCTSTR)sFrom;

	// Ðàçìåð çàãîëîâêà ïàêåòà
	int fixed_size = sOemFrom.GetLength() + 1 + sOemTo.GetLength() + 1 + 1;
	if ( fixed_size >= MAX_MESSAGE_SIZE )
	{
		err = ERROR_BAD_LENGTH;
		return false;
	}

	// Ñîçäàíèå ìåéëñëîòà äëÿ îòïðàâêè ñîîáùåíèÿ
	CString sAddr;
	sAddr.Format( _T("\\\\%s\\mailslot\\%s"), sTo, (LPCTSTR)m_sName );
	HANDLE hFile = CreateFile( sAddr, GENERIC_WRITE, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); 
	if ( hFile == INVALID_HANDLE_VALUE )
	{
		err = GetLastError();
		return false;
	}

	int max_message_size = MAX_MESSAGE_SIZE - fixed_size;
	char buf[ MAX_MESSAGE_SIZE ] = {};
	lstrcpynA( buf, sOemFrom, sOemFrom.GetLength() + 1 );
	lstrcpynA( buf + sOemFrom.GetLength() + 1, sOemTo, sOemTo.GetLength() + 1 );
	do
	{
		int message_size = ( sOemMessage.GetLength() < max_message_size ) ? 
			sOemMessage.GetLength() : max_message_size;
		lstrcpynA( buf + fixed_size - 1, sOemMessage, message_size + 1 );

		// Îòñûëêà ïàêåòà
		DWORD written = 0;
		if ( ! WriteFile( hFile, buf, (DWORD)fixed_size + message_size, &written, NULL ) ||
			( written < (DWORD)fixed_size ) )
		{
			err = GetLastError();
			CloseHandle( hFile );
			return false;
		}
		Sleep( 100 );

		// Óêîðà÷èâàíèå íà îòîñëàííûé ïàêåò
		sOemMessage.CutFromStart( message_size );
	}
	while ( sOemMessage.GetLength() );

	err = ERROR_SUCCESS;
	CloseHandle( hFile );
	return true;
}

bool mailslot::Receive(unsigned char* buf /* OEM */, DWORD size)
{
	// Ðàçáîðêà ñîîáùåíèÿ <FROM><00><TO><00><MESSAGE><00> (ïîñëåäíèé <00> íåîáÿçàòåëåí)
	if (size)
	{
		char* from = (char*) buf;
		char* to = lstrnchr (from, 0, (int)size);
		if (to)
		{
			DWORD from_len = (DWORD)( to - from + 1 );
			if ( from_len < size )
			{
				to++;
				size -= from_len;
				char* msg = lstrnchr (to, 0, (int)size);
				if (msg)
				{
					DWORD to_len = (DWORD)( msg - to + 1 );
					if (to_len < size)
					{
						msg++;
						size -= to_len;
						char* eof = lstrnchr (msg, 0, (int)size);
						DWORD msg_len = eof ? (DWORD)( eof - msg + 1 ) : size;
						if (msg_len == size)
						{
							CAnsiString sFrom (from);
							CAnsiString sTo (to);							
							CAnsiString sMessage (msg);
							ReceiveContactMessage(sFrom, sTo, sMessage, sMessage.GetLength ());
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}

void mailslot::MonitorThread(void* param)
{
	if ( mailslot* pMailslot = (mailslot*)param )
	{
		pMailslot->Monitor();	
		pMailslot->m_Monitor = NULL;
	}
}

void mailslot::Monitor ()
{
	// Îæèäàíèå ïðåðûâàíèÿ 500 ìñ ïðè çàêðûòîì ìàéëñëîòå, èíà÷å 50 ìñ
	while ( WaitForSingleObject( m_MonitorTerm, IsValid() ? 50u : 500u ) == WAIT_TIMEOUT )
	{
		// Ïðîâåðêà âõîäÿùèõ ñîîáùåíèé
		LPSTR buf = NULL;
		for ( DWORD buf_size = MAX_MESSAGE_SIZE; IsValid(); buf_size += 1024 )
		{
			if ( WaitForSingleObject( m_MonitorTerm, 0 ) != WAIT_TIMEOUT )
				break;

			if ( buf ) mir_free( buf );
			buf = (LPSTR)mir_alloc( buf_size );

			DWORD readed = 0;
			DWORD err = ReadFile (m_hMailslot, buf, buf_size,
				&readed, NULL) ? ERROR_SUCCESS : GetLastError ();
			if (err == ERROR_ACCESS_DENIED || err == ERROR_SEM_TIMEOUT)
			{
				// Òàéì-àóò ìåéëñëîòà
				break;
			}
			else if (err == ERROR_SUCCESS)
			{
				// Äàííûå ïðèíÿòû
				if (readed)
					if (!Receive((LPBYTE)buf, readed))
						LOG("Receive error (bad format?)");
				break;
			}
			else if (err == ERROR_INSUFFICIENT_BUFFER)
			{
				// Íåõâàòêà ðàçìåðà áóôåðà
				continue;
			}
			else 
			{
				// Äðóãèå îøèáêè
				// ERROR_HANDLE_EOF - õåíäë ìåéëñëîòà çàêðûò
				LOG("ReadFile form mailslot error: %d", err);
				break;
			}
		}
		if ( buf ) mir_free( buf );
	}
}