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
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (c) 2012-18 Miranda NG team (https://miranda-ng.org),
Copyright (c) 2000-08 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 "stdafx.h"
#include "modern_awaymsg.h"
#include "modern_sync.h"
#define AMASKPERIOD 3000
static mir_cs amCS;
static LIST<void> amItems(10, PtrKeySortT);
static HANDLE hamProcessEvent = nullptr;
static DWORD amRequestTick = 0;
/*
* Add contact handle to requests queue
*/
static int amAddHandleToChain(MCONTACT hContact)
{
mir_cslockfull lck(amCS);
if (amItems.find((HANDLE)hContact) != nullptr)
return 0;
amItems.insert((HANDLE)hContact);
lck.unlock();
SetEvent(hamProcessEvent);
return 1;
}
/*
* Gets handle from queue for request
*/
static MCONTACT amGetCurrentChain()
{
mir_cslock lck(amCS);
if (amItems.getCount() == 0)
return 0;
MCONTACT res = (DWORD_PTR)amItems[0];
amItems.remove(0);
return res;
}
/*
* Tread sub to ask protocol to retrieve away message
*/
static void amThreadProc(void *)
{
Netlib_Logf(nullptr, "amThreadProc thread start");
while (!MirandaExiting()) {
MCONTACT hContact = amGetCurrentChain();
while (hContact) {
DWORD time = GetTickCount();
if ((time - amRequestTick) < AMASKPERIOD) {
SleepEx(AMASKPERIOD - (time - amRequestTick) + 10, TRUE);
if (MirandaExiting())
goto LBL_Exit;
}
ClcCacheEntry *pdnce = pcli->pfnGetCacheEntry(hContact);
HANDLE ACK = nullptr;
if (pdnce->ApparentMode != ID_STATUS_OFFLINE) //don't ask if contact is always invisible (should be done with protocol)
ACK = (HANDLE)ProtoChainSend(hContact, PSS_GETAWAYMSG, 0, 0);
if (!ACK) {
ACKDATA ack;
ack.hContact = hContact;
ack.type = ACKTYPE_AWAYMSG;
ack.result = ACKRESULT_FAILED;
ack.szModule = pdnce->szProto;
ClcDoProtoAck(&ack);
}
amRequestTick = time;
hContact = amGetCurrentChain();
if (hContact) {
DWORD i = 0;
do {
i++;
SleepEx(50, TRUE);
} while (i < AMASKPERIOD / 50 && !MirandaExiting());
}
else break;
if (MirandaExiting())
goto LBL_Exit;
}
WaitForSingleObjectEx(hamProcessEvent, INFINITE, TRUE);
ResetEvent(hamProcessEvent);
if (MirandaExiting())
break;
}
LBL_Exit:
g_hAwayMsgThread = nullptr;
Netlib_Logf(nullptr, "amThreadProc thread end");
}
BOOL amWakeThread()
{
if (hamProcessEvent && g_hAwayMsgThread) {
SetEvent(hamProcessEvent);
return TRUE;
}
return FALSE;
}
/*
* Sub to be called outside on status changing to retrieve away message
*/
void amRequestAwayMsg(MCONTACT hContact)
{
if (!g_CluiData.bInternalAwayMsgDiscovery || !hContact)
return;
//Do not re-ask for chat rooms
char *szProto = GetContactProto(hContact);
if (szProto != nullptr && !db_get_b(hContact, szProto, "ChatRoom", 0))
amAddHandleToChain(hContact);
}
void InitAwayMsgModule()
{
hamProcessEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
g_hAwayMsgThread = mir_forkthread(amThreadProc, nullptr);
}
void UninitAwayMsgModule()
{
SetEvent(hamProcessEvent);
while (g_hAwayMsgThread)
SleepEx(50, TRUE);
CloseHandle(hamProcessEvent);
}
|