summaryrefslogtreecommitdiff
path: root/protocols/JabberG/jabber_rc.h
blob: a83db38ab4acf1173e0520529ba7b3cebb1a8c52 (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
/*

Jabber Protocol Plugin for Miranda IM
Copyright ( C ) 2002-04  Santithorn Bunchua
Copyright ( C ) 2005-11  George Hazan
Copyright ( C ) 2007     Maxim Mluhov

XEP-0146 support for Miranda IM

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.

Revision       : $Revision: 13452 $
Last change on : $Date: 2011-03-17 21:12:56 +0200 (Чт, 17 мар 2011) $
Last change by : $Author: george.hazan $

*/

#ifndef _JABBER_RC_H_
#define _JABBER_RC_H_

class CJabberAdhocSession;

#define JABBER_ADHOC_HANDLER_STATUS_EXECUTING            1
#define JABBER_ADHOC_HANDLER_STATUS_COMPLETED            2
#define JABBER_ADHOC_HANDLER_STATUS_CANCEL               3
#define JABBER_ADHOC_HANDLER_STATUS_REMOVE_SESSION       4
typedef int ( CJabberProto::*JABBER_ADHOC_HANDLER )( HXML iqNode, CJabberIqInfo* pInfo, CJabberAdhocSession* pSession );

// 5 minutes to fill out form :)
#define JABBER_ADHOC_SESSION_EXPIRE_TIME                 300000

class CJabberAdhocSession
{
protected:
	CMString m_szSessionId;
	CJabberAdhocSession* m_pNext;
	DWORD m_dwStartTime;

	void* m_pUserData;
	BOOL m_bAutofreeUserData;

	DWORD m_dwStage;
public:
	CJabberProto* ppro;
	CJabberAdhocSession( CJabberProto* global );
	~CJabberAdhocSession()
	{
		if ( m_bAutofreeUserData && m_pUserData )
			mir_free( m_pUserData );
		if ( m_pNext )
			delete m_pNext;
	}
	CJabberAdhocSession* GetNext()
	{
		return m_pNext;
	}
	CJabberAdhocSession* SetNext( CJabberAdhocSession *pNext )
	{
		CJabberAdhocSession *pRetVal = m_pNext;
		m_pNext = pNext;
		return pRetVal;
	}
	DWORD GetSessionStartTime()
	{
		return m_dwStartTime;
	}
	LPCTSTR GetSessionId()
	{
		return m_szSessionId;
	}
	BOOL SetUserData( void* pUserData, BOOL bAutofree = FALSE )
	{
		if ( m_bAutofreeUserData && m_pUserData )
			mir_free( m_pUserData );
		m_pUserData = pUserData;
		m_bAutofreeUserData = bAutofree;
		return TRUE;
	}
	DWORD SetStage( DWORD dwStage )
	{
		DWORD dwRetVal = m_dwStage;
		m_dwStage = dwStage;
		return dwRetVal;
	}
	DWORD GetStage()
	{
		return m_dwStage;
	}
};

class CJabberAdhocNode;
class CJabberAdhocNode
{
protected:
	TCHAR* m_szJid;
	TCHAR* m_szNode;
	TCHAR* m_szName;
	CJabberAdhocNode* m_pNext;
	JABBER_ADHOC_HANDLER m_pHandler;
	CJabberProto* m_pProto;
public:
	CJabberAdhocNode( CJabberProto* pProto, TCHAR* szJid, TCHAR* szNode, TCHAR* szName, JABBER_ADHOC_HANDLER pHandler )
	{
		ZeroMemory( this, sizeof( CJabberAdhocNode ));
		replaceStr( m_szJid, szJid );
		replaceStr( m_szNode, szNode );
		replaceStr( m_szName, szName );
		m_pHandler = pHandler;
		m_pProto = pProto;
	}
	~CJabberAdhocNode()
	{
		if ( m_szJid) 
			mir_free( m_szJid );
		if ( m_szNode )
			mir_free( m_szNode );
		if ( m_szName )
			mir_free( m_szName );
		if ( m_pNext )
			delete m_pNext;
	}
	CJabberAdhocNode* GetNext()
	{
		return m_pNext;
	}
	CJabberAdhocNode* SetNext( CJabberAdhocNode *pNext )
	{
		CJabberAdhocNode *pRetVal = m_pNext;
		m_pNext = pNext;
		return pRetVal;
	}
	TCHAR* GetJid()
	{
		return m_szJid;
	}
	TCHAR* GetNode()
	{
		return m_szNode;
	}
	TCHAR* GetName()
	{
		return m_szName;
	}
	BOOL CallHandler( HXML iqNode, CJabberIqInfo* pInfo, CJabberAdhocSession* pSession )
	{
		if ( m_pHandler == NULL )
			return FALSE;
		return (m_pProto->*m_pHandler)( iqNode, pInfo, pSession );
	}
};

class CJabberAdhocManager
{
protected:
	CJabberProto* m_pProto;
	CJabberAdhocNode* m_pNodes;
	CJabberAdhocSession* m_pSessions;
	CRITICAL_SECTION m_cs;

	CJabberAdhocSession* FindSession( const TCHAR* szSession )
	{
		CJabberAdhocSession* pSession = m_pSessions;
		while ( pSession ) {
			if ( !_tcscmp( pSession->GetSessionId(), szSession ))
				return pSession;
			pSession = pSession->GetNext();
		}
		return NULL;
	}

	CJabberAdhocSession* AddNewSession()
	{
		CJabberAdhocSession* pSession = new CJabberAdhocSession( m_pProto );
		if ( !pSession )
			return NULL;

		pSession->SetNext( m_pSessions );
		m_pSessions = pSession;

		return pSession;
	}

	CJabberAdhocNode* FindNode( const TCHAR* szNode )
	{
		CJabberAdhocNode* pNode = m_pNodes;
		while ( pNode ) {
			if ( !_tcscmp( pNode->GetNode(), szNode ))
				return pNode;
			pNode = pNode->GetNext();
		}
		return NULL;
	}

	BOOL RemoveSession( CJabberAdhocSession* pSession )
	{
		if ( !m_pSessions )
			return FALSE;

		if ( pSession == m_pSessions ) {
			m_pSessions = m_pSessions->GetNext();
			pSession->SetNext( NULL );
			delete pSession;
			return TRUE;
		}

		CJabberAdhocSession* pTmp = m_pSessions;
		while ( pTmp->GetNext()) {
			if ( pTmp->GetNext() == pSession ) {
				pTmp->SetNext( pSession->GetNext());
				pSession->SetNext( NULL );
				delete pSession;
				return TRUE;
			}
			pTmp = pTmp->GetNext();
		}
		return FALSE;
	}

	BOOL _ExpireSession( DWORD dwExpireTime )
	{
		if ( !m_pSessions )
			return FALSE;

		CJabberAdhocSession* pSession = m_pSessions;
		if ( pSession->GetSessionStartTime() < dwExpireTime ) {
			m_pSessions = pSession->GetNext();
			pSession->SetNext( NULL );
			delete pSession;
			return TRUE;
		}

		while ( pSession->GetNext()) {
			if ( pSession->GetNext()->GetSessionStartTime() < dwExpireTime ) {
				CJabberAdhocSession* pRetVal = pSession->GetNext();
				pSession->SetNext( pSession->GetNext()->GetNext());
				pRetVal->SetNext( NULL );
				delete pRetVal;
				return TRUE;
			}
			pSession = pSession->GetNext();
		}
		return FALSE;
	}

public:
	CJabberAdhocManager( CJabberProto* pProto )
	{
		ZeroMemory( this, sizeof( CJabberAdhocManager ));
		m_pProto = pProto;
		InitializeCriticalSection( &m_cs );
	}
	~CJabberAdhocManager()
	{
		if ( m_pNodes )
			delete m_pNodes;
		if ( m_pSessions )
			delete m_pSessions;
		DeleteCriticalSection( &m_cs );
	}
	void Lock()
	{
		EnterCriticalSection( &m_cs );
	}
	void Unlock()
	{
		LeaveCriticalSection( &m_cs );
	}
	BOOL FillDefaultNodes();
	BOOL AddNode( TCHAR* szJid, TCHAR* szNode, TCHAR* szName, JABBER_ADHOC_HANDLER pHandler )
	{
		CJabberAdhocNode* pNode = new CJabberAdhocNode( m_pProto, szJid, szNode, szName, pHandler );
		if ( !pNode )
			return FALSE;

		Lock();
		if ( !m_pNodes )
			m_pNodes = pNode;
		else {
			CJabberAdhocNode* pTmp = m_pNodes;
			while ( pTmp->GetNext())
				pTmp = pTmp->GetNext();
			pTmp->SetNext( pNode );
		}
		Unlock();

		return TRUE;
	}
	CJabberAdhocNode* GetFirstNode()
	{
		return m_pNodes;
	}
	BOOL HandleItemsRequest( HXML iqNode, CJabberIqInfo* pInfo, const TCHAR* szNode );
	BOOL HandleInfoRequest( HXML iqNode, CJabberIqInfo* pInfo, const TCHAR* szNode );
	BOOL HandleCommandRequest( HXML iqNode, CJabberIqInfo* pInfo, const TCHAR* szNode );

	BOOL ExpireSessions()
	{
		Lock();
		DWORD dwExpireTime = GetTickCount() - JABBER_ADHOC_SESSION_EXPIRE_TIME;
		while ( _ExpireSession( dwExpireTime ));
		Unlock();
		return TRUE;
	}
};

#endif //_JABBER_RC_H_