summaryrefslogtreecommitdiff
path: root/plugins/Clist_modern/src/modern_awaymsg.cpp
blob: 34dc4ad51cd4a65f3041e4a0c5dd2bf1a28ff0b4 (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2008 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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.

*/

/*
*	Author Artem Shpynov aka FYR
*   Copyright 2000-2008 Artem Shpynov
*/

//////////////////////////////////////////////////////////////////////////
// Module to Request Away Messages

#include "hdr/modern_commonheaders.h"
#include "hdr/modern_awaymsg.h"
#include "newpluginapi.h"
#include "hdr/modern_sync.h"

#define AMASKPERIOD 3000
#define amlock EnterCriticalSection(&amLockChain)
#define amunlock LeaveCriticalSection(&amLockChain)

typedef struct _tag_amChainItem {
	HANDLE hContact;
	_tag_amChainItem *Next;
} AMCHAINITEM;

static AMCHAINITEM *	amFirstChainItem = NULL;
static AMCHAINITEM *	amLastChainItem  = NULL;
static CRITICAL_SECTION	amLockChain;
static HANDLE			hamProcessEvent	 = NULL;
static DWORD			amRequestTick	 = 0;


static int		amAddHandleToChain(HANDLE hContact);
static HANDLE	amGetCurrentChain();
static int		amThreadProc(HWND hwnd);

/*
*  Add contact handle to requests queue
*/
static int amAddHandleToChain(HANDLE hContact)
{
	AMCHAINITEM * workChain;
	amlock;
	{
		//check that handle is present
		AMCHAINITEM * wChain;
		wChain = amFirstChainItem;
		if (wChain)
			do {
				if (wChain->hContact == hContact)
				{
					amunlock;
					return 0;
				}
			} while(wChain = (AMCHAINITEM *)wChain->Next);
	}
	if ( !amFirstChainItem)  
	{
		amFirstChainItem = (AMCHAINITEM*)malloc(sizeof(AMCHAINITEM));
		workChain = amFirstChainItem;
	}
	else 
	{
		amLastChainItem->Next = (AMCHAINITEM*)malloc(sizeof(AMCHAINITEM));
		workChain = (AMCHAINITEM *)amLastChainItem->Next;
	}
	amLastChainItem = workChain;
	workChain->Next = NULL;
	workChain->hContact = hContact;
	amunlock;
	SetEvent(hamProcessEvent);
	return 1;
}


/*
*	Gets handle from queue for request
*/
static HANDLE amGetCurrentChain()
{
	AMCHAINITEM * workChain;
	HANDLE res = NULL;
	amlock;
	if (amFirstChainItem)
	{
		res = amFirstChainItem->hContact;
		workChain = amFirstChainItem->Next;
		free(amFirstChainItem);
		amFirstChainItem = (AMCHAINITEM *)workChain;
	}
	amunlock;
	return res;
}

/*
*	Tread sub to ask protocol to retrieve away message
*/
static int amThreadProc(HWND hwnd)
{
	DWORD time;
	HANDLE hContact;
	HANDLE ACK = 0;
	ClcCacheEntry dnce;
	memset( &dnce, 0, sizeof(dnce));

	while (!MirandaExiting())
	{
		hContact = amGetCurrentChain(); 
		while (hContact)
		{ 
			time = GetTickCount();
			if ((time-amRequestTick) < AMASKPERIOD)
			{
				SleepEx(AMASKPERIOD-(time-amRequestTick)+10, TRUE);
				if (MirandaExiting())
				{
					g_dwAwayMsgThreadID = 0;
					return 0; 
				}
			}
			CListSettings_FreeCacheItemData(&dnce);
			dnce.hContact = (HANDLE)hContact;
			Sync(CLUI_SyncGetPDNCE, (WPARAM) 0, (LPARAM)&dnce);            
			if (dnce.ApparentMode != ID_STATUS_OFFLINE) //don't ask if contact is always invisible (should be done with protocol)
				ACK = (HANDLE)CallContactService(hContact,PSS_GETAWAYMSG, 0, 0);		
			if ( !ACK)
			{
				ACKDATA ack;
				ack.hContact = hContact;
				ack.type = ACKTYPE_AWAYMSG;
				ack.result = ACKRESULT_FAILED;
				if (dnce.m_cache_cszProto)
					ack.szModule = dnce.m_cache_cszProto;
				else
					ack.szModule = NULL;
				ClcDoProtoAck(hContact, &ack);
			}
			CListSettings_FreeCacheItemData(&dnce);
			amRequestTick = time;
			hContact = amGetCurrentChain();
			if (hContact) 
			{
				DWORD i=0;
				do 
				{
					i++;
					SleepEx(50, TRUE);
				} while (i < AMASKPERIOD/50 && !MirandaExiting());
			}
			else break;
			if (MirandaExiting()) 
			{	
				g_dwAwayMsgThreadID = 0;
				return 0;			
			}
		}
		WaitForSingleObjectEx(hamProcessEvent, INFINITE, TRUE);
		ResetEvent(hamProcessEvent);
		if (MirandaExiting()) 
		{
			g_dwAwayMsgThreadID = 0;
			return 0;
		}
	}
	g_dwAwayMsgThreadID = 0;
	return 1;
}

BOOL amWakeThread()
{
	if (hamProcessEvent && g_dwAwayMsgThreadID)
	{
		SetEvent(hamProcessEvent);

		return TRUE;
	}

	return FALSE;
}

/*
*	Sub to be called outside on status changing to retrieve away message
*/
void amRequestAwayMsg(HANDLE hContact)
{
	char *szProto;
	if ( !g_CluiData.bInternalAwayMsgDiscovery || !hContact) 
		return;
	//Do not re-ask for chat rooms   
	szProto = GetContactProto(hContact);
	if (szProto == NULL || db_get_b(hContact, szProto, "ChatRoom", 0) != 0) 	
		return;
	amAddHandleToChain(hContact);        
}

void InitAwayMsgModule()
{
	InitializeCriticalSection(&amLockChain);
	hamProcessEvent = CreateEvent(NULL,FALSE,FALSE,NULL);   
	g_dwAwayMsgThreadID = (DWORD)mir_forkthread((pThreadFunc)amThreadProc,0);
}

void UninitAwayMsgModule()
{
	SetEvent(hamProcessEvent);
	CloseHandle(hamProcessEvent);
	amlock;
	while (amGetCurrentChain());
	amunlock;
	DeleteCriticalSection(&amLockChain);
}