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
|
/*
New Away System - plugin for Miranda IM
Copyright (C) 2005-2007 Chervov Dmitry
Copyright (C) 2004-2005 Iksaif Entertainment
Copyright (C) 2002-2003 Goblineye Entertainment
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 "Common.h"
#include "Properties.h"
// stupid compiler.. kept returning me an "INTERNAL COMPILER ERROR" on almost every line of MsgEventAdded function with Release mode and unicode enabled >:( even disabling optimization through #pragma optimize didn't help.. The only acceptable solution I found is to move this function to a separate file and to disable optimization for the whole file in the project properties.
static struct
{
int Status, DisableReplyCtlID, DontShowDialogCtlID;
} StatusModeList[] = {
ID_STATUS_ONLINE, IDC_REPLYDLG_DISABLE_ONL, IDC_MOREOPTDLG_DONTPOPDLG_ONL,
ID_STATUS_AWAY, IDC_REPLYDLG_DISABLE_AWAY, IDC_MOREOPTDLG_DONTPOPDLG_AWAY,
ID_STATUS_NA, IDC_REPLYDLG_DISABLE_NA, IDC_MOREOPTDLG_DONTPOPDLG_NA,
ID_STATUS_OCCUPIED, IDC_REPLYDLG_DISABLE_OCC, IDC_MOREOPTDLG_DONTPOPDLG_OCC,
ID_STATUS_DND, IDC_REPLYDLG_DISABLE_DND, IDC_MOREOPTDLG_DONTPOPDLG_DND,
ID_STATUS_FREECHAT, IDC_REPLYDLG_DISABLE_FFC, IDC_MOREOPTDLG_DONTPOPDLG_FFC,
ID_STATUS_INVISIBLE, IDC_REPLYDLG_DISABLE_INV, IDC_MOREOPTDLG_DONTPOPDLG_INV,
ID_STATUS_ONTHEPHONE, IDC_REPLYDLG_DISABLE_OTP, IDC_MOREOPTDLG_DONTPOPDLG_OTP,
ID_STATUS_OUTTOLUNCH, IDC_REPLYDLG_DISABLE_OTL, IDC_MOREOPTDLG_DONTPOPDLG_OTL
};
class CAutoreplyData
{
public:
CAutoreplyData(HANDLE hContact, TCString Reply): hContact(hContact), Reply(Reply) {}
HANDLE hContact;
TCString Reply;
};
void __cdecl AutoreplyDelayThread(void *_ad)
{ // _ad must be allocated using "new CAutoreplyData()"
CAutoreplyData *ad = (CAutoreplyData*)_ad;
_ASSERT(ad && ad->hContact && ad->Reply.GetLen());
char *szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ad->hContact, 0);
if (!szProto)
{
_ASSERT(0);
return;
}
int ReplyLen = (ad->Reply.GetLen() + 1) * (sizeof(char) + sizeof(WCHAR));
PBYTE pBuf = (PBYTE)malloc(ReplyLen);
memcpy(pBuf, TCHAR2ANSI(ad->Reply), ad->Reply.GetLen() + 1);
memcpy(pBuf + ad->Reply.GetLen() + 1, ad->Reply, (ad->Reply.GetLen() + 1) * sizeof(WCHAR));
CallContactService(ad->hContact, ServiceExists(CString(szProto) + PSS_MESSAGE "W") ? (PSS_MESSAGE "W") : PSS_MESSAGE, PREF_UNICODE, (LPARAM)pBuf);
if (g_AutoreplyOptPage.GetDBValueCopy(IDC_REPLYDLG_LOGREPLY))
{ // store in the history
DBEVENTINFO dbeo = {0};
dbeo.cbSize = sizeof(dbeo);
dbeo.eventType = EVENTTYPE_MESSAGE;
dbeo.flags = DBEF_SENT;
dbeo.szModule = szProto;
dbeo.timestamp = time(NULL);
dbeo.cbBlob = ReplyLen;
dbeo.pBlob = pBuf;
SleepEx(1000, true); // delay before sending the reply, as we need it to be later than the message we're replying to (without this delay, srmm puts the messages in a wrong order)
CallService(MS_DB_EVENT_ADD, (WPARAM)ad->hContact, (LPARAM)&dbeo);
}
free(pBuf);
/*
char *utf8Reply = mir_utf8encodeT(ad->Reply); // todo: use this instead of the code above, when 0.7 will be released
if (g_AutoreplyOptPage.GetDBValueCopy(IDC_REPLYDLG_LOGREPLY))
{ // store in the history
DBEVENTINFO dbeo = {0};
dbeo.cbSize = sizeof(dbeo);
dbeo.eventType = EVENTTYPE_MESSAGE;
dbeo.flags = DBEF_SENT | DBEF_UTF;
dbeo.szModule = szProto;
dbeo.timestamp = time(NULL);
dbeo.cbBlob = strlen(utf8Reply);
dbeo.pBlob = utf8Reply;
CallService(MS_DB_EVENT_ADD, (WPARAM)ad->hContact, (LPARAM)&dbeo);
}
CallContactService(ad->hContact, ServiceExists(CString(szProto) + PSS_MESSAGE "W") ? (PSS_MESSAGE "W") : PSS_MESSAGE, PREF_UTF, (LPARAM)utf8Reply);
mir_free(utf8Reply);
*/
delete ad;
}
int IsSRMsgWindowOpen(HANDLE hContact, int DefaultRetVal)
{
if (ServiceExists(MS_MSG_GETWINDOWDATA))
{
MessageWindowData mwd = {0};
mwd.cbSize = sizeof(mwd);
MessageWindowInputData mwid = {0};
mwid.cbSize = sizeof(mwid);
mwid.uFlags = MSG_WINDOW_UFLAG_MSG_BOTH;
mwid.hContact = hContact;
return !CallService(MS_MSG_GETWINDOWDATA, (WPARAM)&mwid, (LPARAM)&mwd) && mwd.hwndWindow;
} else
{
return DefaultRetVal;
}
}
#define MAX_REPLY_TIMEDIFF 5 // maximum "age" of an event to remain unfiltered; in seconds
#define MSGWNDOPEN_UNDEFINED (-1)
class CMetacontactEvent
{
public:
CMetacontactEvent(HANDLE hMetaContact, DWORD timestamp, int bMsgWindowIsOpen): hMetaContact(hMetaContact), timestamp(timestamp), bMsgWindowIsOpen(bMsgWindowIsOpen) {};
HANDLE hMetaContact;
DWORD timestamp;
int bMsgWindowIsOpen;
};
TMyArray<CMetacontactEvent> MetacontactEvents;
int MsgEventAdded(WPARAM wParam, LPARAM lParam)
{
HANDLE hContact = (HANDLE)wParam;
DBEVENTINFO *dbei = (DBEVENTINFO*)lParam;
if (!hContact)
{
return 0;
}
if (dbei->flags & DBEF_SENT || (dbei->eventType != EVENTTYPE_MESSAGE && dbei->eventType != EVENTTYPE_URL && dbei->eventType != EVENTTYPE_FILE))
{
return 0;
}
if (time(NULL) - dbei->timestamp > MAX_REPLY_TIMEDIFF)
{ // don't reply to offline messages
return 0;
}
char *szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if (!szProto)
{
return 0;
}
DWORD Flags1 = CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0);
if (!(Flags1 & PF1_IMSEND))
{ // don't reply to protocols that don't support outgoing messages
return 0;
}
int bMsgWindowIsOpen = MSGWNDOPEN_UNDEFINED;
if (dbei->flags & DBEF_READ)
{
HANDLE hMetaContact;
if (ServiceExists(MS_MC_GETMETACONTACT) && (hMetaContact = (HANDLE)CallService(MS_MC_GETMETACONTACT, (WPARAM)hContact, 0))) // if it's a subcontact of a metacontact
{ // ugly workaround for metacontacts, part II
// remove outdated events first
DWORD CurTime = time(NULL);
int I;
for (I = MetacontactEvents.GetSize() - 1; I >= 0; I--)
{
if (CurTime - MetacontactEvents[I].timestamp > MAX_REPLY_TIMEDIFF)
{
MetacontactEvents.RemoveElem(I);
}
}
// we compare only event timestamps, and do not look at the message itself. it's unlikely that there'll be two events from a contact at the same second, so it's a trade-off between speed and reliability
for (I = MetacontactEvents.GetSize() - 1; I >= 0; I--)
{
if (MetacontactEvents[I].timestamp == dbei->timestamp && MetacontactEvents[I].hMetaContact == hMetaContact)
{
bMsgWindowIsOpen = MetacontactEvents[I].bMsgWindowIsOpen;
break;
}
}
if (I < 0)
{
_ASSERT(0);
return 0;
}
} else
{
return 0;
}
}
if (ServiceExists(MS_MC_GETPROTOCOLNAME) && !lstrcmpA(szProto, (char*)CallService(MS_MC_GETPROTOCOLNAME, 0, 0)))
{ // ugly workaround for metacontacts, part I; store all metacontacts' events to a temporary array, so we'll be able to get the 'source' protocol when subcontact event happens later. we need the protocol to get its status and per-status settings properly
// remove outdated events first
DWORD CurTime = time(NULL);
int I;
for (I = MetacontactEvents.GetSize() - 1; I >= 0; I--)
{
if (CurTime - MetacontactEvents[I].timestamp > MAX_REPLY_TIMEDIFF)
{
MetacontactEvents.RemoveElem(I);
}
}
// add the new event and wait for a subcontact's event
MetacontactEvents.AddElem(CMetacontactEvent(hContact, dbei->timestamp, IsSRMsgWindowOpen(hContact, false)));
return 0;
}
unsigned int iMode = CallProtoService(szProto, PS_GETSTATUS, 0, 0);
int I;
for (I = lengthof(StatusModeList) - 1; I >= 0; I--)
{
if (iMode == StatusModeList[I].Status)
{
break;
}
}
if (I < 0)
{
return 0;
}
COptPage AutoreplyOptData(g_AutoreplyOptPage);
AutoreplyOptData.DBToMem();
if (dbei->eventType == EVENTTYPE_MESSAGE)
{
DBWriteContactSettingWord(hContact, MOD_NAME, DB_MESSAGECOUNT, DBGetContactSettingWord(hContact, MOD_NAME, DB_MESSAGECOUNT, 0) + 1); // increment message counter
}
if (AutoreplyOptData.GetValue(StatusModeList[I].DisableReplyCtlID))
{
return 0;
}
HANDLE hContactForSettings = hContact; // used to take into account not-on-list contacts when getting contact settings, but at the same time allows to get correct contact info for contacts that are in the DB
if (hContactForSettings != INVALID_HANDLE_VALUE && DBGetContactSettingByte(hContactForSettings, "CList", "NotOnList", 0))
{
hContactForSettings = INVALID_HANDLE_VALUE; // INVALID_HANDLE_VALUE means the contact is not-on-list
}
if (!CContactSettings(iMode, hContactForSettings).Autoreply.IncludingParents(szProto) || CContactSettings(iMode, hContactForSettings).Ignore)
{
return 0;
}
if (AutoreplyOptData.GetValue(IDC_REPLYDLG_DONTREPLYINVISIBLE))
{
WORD ApparentMode = DBGetContactSettingWord(hContact, szProto, "ApparentMode", 0);
if ((iMode == ID_STATUS_INVISIBLE && (!(Flags1 & PF1_INVISLIST) || ApparentMode != ID_STATUS_ONLINE)) ||
(Flags1 & PF1_VISLIST && ApparentMode == ID_STATUS_OFFLINE))
{
return 0;
}
}
if (AutoreplyOptData.GetValue(IDC_REPLYDLG_ONLYCLOSEDDLGREPLY))
{
if (bMsgWindowIsOpen && bMsgWindowIsOpen != MSGWNDOPEN_UNDEFINED)
{
return 0;
}
// we never get here for a metacontact; we did check for metacontact's window earlier, and here we need to check only for subcontact's window
if (IsSRMsgWindowOpen(hContact, false))
{
return 0;
}
}
if (AutoreplyOptData.GetValue(IDC_REPLYDLG_ONLYIDLEREPLY) && !g_bIsIdle)
{
return 0;
}
int UIN = 0;
if (IsAnICQProto(szProto))
{
UIN = DBGetContactSettingDword(hContact, szProto, "UIN", 0);
}
int SendCount = AutoreplyOptData.GetValue(IDC_REPLYDLG_SENDCOUNT);
if ((AutoreplyOptData.GetValue(IDC_REPLYDLG_DONTSENDTOICQ) && UIN) || // an icq contact
(SendCount != -1 && DBGetContactSettingByte(hContact, MOD_NAME, DB_SENDCOUNT, 0) >= SendCount))
{
return 0;
}
if ((dbei->eventType == EVENTTYPE_MESSAGE && !AutoreplyOptData.GetValue(IDC_REPLYDLG_EVENTMSG)) || (dbei->eventType == EVENTTYPE_URL && !AutoreplyOptData.GetValue(IDC_REPLYDLG_EVENTURL)) || (dbei->eventType == EVENTTYPE_FILE && !AutoreplyOptData.GetValue(IDC_REPLYDLG_EVENTFILE)))
{
return 0;
}
DBWriteContactSettingByte(hContact, MOD_NAME, DB_SENDCOUNT, DBGetContactSettingByte(hContact, MOD_NAME, DB_SENDCOUNT, 0) + 1);
GetDynamicStatMsg(hContact); // it updates VarParseData.Message needed for %extratext% in the format
TCString Reply(*(TCString*)AutoreplyOptData.GetValue(IDC_REPLYDLG_PREFIX));
if (Reply != NULL && ServiceExists(MS_VARS_FORMATSTRING) && !g_SetAwayMsgPage.GetDBValueCopy(IDS_SAWAYMSG_DISABLEVARIABLES))
{
FORMATINFO fi = {0};
fi.cbSize = sizeof(FORMATINFO);
fi.tszFormat = Reply;
fi.hContact = hContact;
fi.flags = FIF_TCHAR;
fi.tszExtraText = VarParseData.Message;
TCHAR *szResult = (TCHAR *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
if (szResult != NULL)
{
Reply = szResult;
mir_free(szResult);
}
}
if (Reply.GetLen())
{
CAutoreplyData *ad = new CAutoreplyData(hContact, Reply);
mir_forkthread(AutoreplyDelayThread, ad);
}
return 0;
}
|