summaryrefslogtreecommitdiff
path: root/plugins/StopSpamMod/src/options.cpp
blob: b7f8fc1274e04f9ec65e6376e87d162c6f7b3ef0 (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
/* Copyright (C) Miklashevsky Roman, sss, elzor
*
* 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"

wchar_t const *pluginDescription = LPGENW("No more spam! Robots can't go! Only human beings invited!\r\n\r\nThis plugin works pretty simple:\r\nWhile messages from users on your contact list go as there is no any anti-spam software, messages from unknown users are not delivered to you. But also they are not ignored, this plugin replies with a simple question, and if user gives the right answer, plugin adds him to your contact list so that he can contact you.");

const wchar_t *defQuestion = LPGENW("Spammers made me to install small anti-spam system you are now speaking with. Please reply \"nospam\" without quotes and spaces if you want to contact me.");
const wchar_t *defCongrats = LPGENW("Congratulations! You just passed human/robot test. Now you can write me a message.");
const wchar_t *defAuthReply = LPGENW("StopSpam: send a message and reply to an anti-spam bot question.");

class COptMainDlg : public CDlgBase
{
public:
	COptMainDlg() : CDlgBase(g_plugin, IDD_MAIN),
		chk_INFTALKPROT(this, ID_INFTALKPROT), chk_ADDPERMANENT(this, ID_ADDPERMANENT), chk_HANDLEAUTHREQ(this, ID_HANDLEAUTHREQ),
		chk_HIDECONTACTS(this, ID_HIDECONTACTS), chk_IGNORESPAMMERS(this, ID_IGNORESPAMMERS), chk_LOGSPAMTOFILE(this, ID_LOGSPAMTOFILE),
		ctrl_DESCRIPTION(this, ID_DESCRIPTION), edit_MAXQUESTCOUNT(this, ID_MAXQUESTCOUNT)
	{
	}

	bool OnInitDialog() override
	{
		ctrl_DESCRIPTION.SetText(TranslateW(pluginDescription));
		edit_MAXQUESTCOUNT.SetInt(gbMaxQuestCount);
		chk_INFTALKPROT.SetState(gbInfTalkProtection);
		chk_ADDPERMANENT.SetState(gbAddPermanent);
		chk_HANDLEAUTHREQ.SetState(gbHandleAuthReq);
		chk_HIDECONTACTS.SetState(gbHideContacts);
		chk_IGNORESPAMMERS.SetState(gbIgnoreContacts);
		chk_LOGSPAMTOFILE.SetState(gbLogToFile);
		return true;
	}

	bool OnApply() override
	{
		g_plugin.setDword("maxQuestCount", gbMaxQuestCount = edit_MAXQUESTCOUNT.GetInt());
		g_plugin.setByte("infTalkProtection", gbInfTalkProtection = chk_INFTALKPROT.GetState());
		g_plugin.setByte("addPermanent", gbAddPermanent = chk_ADDPERMANENT.GetState());
		g_plugin.setByte("handleAuthReq", gbHandleAuthReq = chk_HANDLEAUTHREQ.GetState());
		g_plugin.setByte("HideContacts", gbHideContacts = chk_HIDECONTACTS.GetState());
		g_plugin.setByte("IgnoreContacts", gbIgnoreContacts = chk_IGNORESPAMMERS.GetState());
		g_plugin.setByte("LogSpamToFile", gbLogToFile = chk_LOGSPAMTOFILE.GetState());
		return true;
	}

private:
	CCtrlCheck chk_INFTALKPROT, chk_ADDPERMANENT, chk_HANDLEAUTHREQ, chk_HIDECONTACTS, chk_IGNORESPAMMERS, chk_LOGSPAMTOFILE;
	CCtrlData ctrl_DESCRIPTION;
	CCtrlSpin edit_MAXQUESTCOUNT;
};

class COptMessagesDlg : public CDlgBase
{
public:
	COptMessagesDlg() : CDlgBase(g_plugin, IDD_MESSAGES),
		edit_QUESTION(this, ID_QUESTION), edit_ANSWER(this, ID_ANSWER), edit_CONGRATULATION(this, ID_CONGRATULATION), edit_AUTHREPL(this, ID_AUTHREPL),
		btn_RESTOREDEFAULTS(this, ID_RESTOREDEFAULTS), btn_VARS(this, IDC_VARS)
	{
		btn_RESTOREDEFAULTS.OnClick = Callback(this, &COptMessagesDlg::onClick_RESTOREDEFAULTS);
		btn_VARS.OnClick = Callback(this, &COptMessagesDlg::onClick_VARS);

	}

	bool OnInitDialog() override
	{
		edit_QUESTION.SetText(gbQuestion.c_str());
		edit_ANSWER.SetText(gbAnswer.c_str());
		edit_CONGRATULATION.SetText(gbCongratulation.c_str());
		edit_AUTHREPL.SetText(gbAuthRepl.c_str());
		if (gbMathExpression)
			edit_ANSWER.Disable();
		variables_skin_helpbutton(m_hwnd, IDC_VARS);
		gbVarsServiceExist ? btn_VARS.Enable() : btn_VARS.Disable();
		return true;
	}

	bool OnApply() override
	{
		g_plugin.setWString("question", edit_QUESTION.GetText());
		gbQuestion = DBGetContactSettingStringPAN(0, MODULENAME, "question", TranslateW(defQuestion));
		g_plugin.setWString("answer", edit_ANSWER.GetText());
		gbAnswer = DBGetContactSettingStringPAN(0, MODULENAME, "answer", L"nospam");
		g_plugin.setWString("authrepl", edit_AUTHREPL.GetText());
		gbAuthRepl = DBGetContactSettingStringPAN(0, MODULENAME, "authrepl", TranslateW(defAuthReply));
		g_plugin.setWString("congratulation", edit_CONGRATULATION.GetText());
		gbCongratulation = DBGetContactSettingStringPAN(0, MODULENAME, "congratulation", TranslateW(defCongrats));
		return true;
	}

	void onClick_RESTOREDEFAULTS(CCtrlButton*)
	{
		edit_QUESTION.SetText(TranslateW(defQuestion));
		edit_ANSWER.SetText(L"nospam");
		edit_AUTHREPL.SetText(TranslateW(defAuthReply));
		edit_CONGRATULATION.SetText(TranslateW(defCongrats));
	}
	void onClick_VARS(CCtrlButton*)
	{
		variables_showhelp(m_hwnd, WM_COMMAND, VHF_FULLDLG | VHF_SETLASTSUBJECT, nullptr, nullptr);
	}
private:
	CCtrlEdit edit_QUESTION, edit_ANSWER, edit_CONGRATULATION, edit_AUTHREPL;
	CCtrlButton btn_RESTOREDEFAULTS, btn_VARS;

};

class COptProtoDlg : public CDlgBase
{
public:
	COptProtoDlg() : CDlgBase(g_plugin, IDD_PROTO),
		list_USEDPROTO(this, ID_USEDPROTO), list_ALLPROTO(this, ID_ALLPROTO),
		btn_ADD(this, ID_ADD), btn_REMOVE(this, ID_REMOVE), btn_ADDALL(this, ID_ADDALL), btn_REMOVEALL(this, ID_REMOVEALL)
	{
		btn_ADD.OnClick = Callback(this, &COptProtoDlg::onClick_ADD);
		btn_REMOVE.OnClick = Callback(this, &COptProtoDlg::onClick_REMOVE);
		btn_ADDALL.OnClick = Callback(this, &COptProtoDlg::onClick_ADDALL);
		btn_REMOVEALL.OnClick = Callback(this, &COptProtoDlg::onClick_REMOVEALL);
	}
	
	bool OnInitDialog() override
	{
		for (auto &pa : Accounts()) {
			wchar_t *str = mir_utf8decodeW(pa->szModuleName);
			ProtoInList(pa->szModuleName) ? list_USEDPROTO.AddString(str) : list_ALLPROTO.AddString(str);
			mir_free(str);
		}
		return true;
	}
	
	bool OnApply() override
	{
		
		LRESULT count = list_USEDPROTO.GetCount();
		std::ostringstream out;
		for (int i = 0; i < count; ++i) {
			char *str = mir_utf8encodeW(list_USEDPROTO.GetItemText(i));
			out << str << "\r\n";
			mir_free(str);
		}
		g_plugin.setString("protoList", out.str().c_str());
		return true;
	}

	void onClick_ADD(CCtrlButton*)
	{
		list_USEDPROTO.AddString(list_ALLPROTO.GetItemText(list_ALLPROTO.GetCurSel()));
		list_ALLPROTO.DeleteString(list_ALLPROTO.GetCurSel());
		this->NotifyChange();
	}
	void onClick_REMOVE(CCtrlButton*)
	{
		list_ALLPROTO.AddString(list_USEDPROTO.GetItemText(list_USEDPROTO.GetCurSel()));
		list_USEDPROTO.DeleteString(list_USEDPROTO.GetCurSel());
		this->NotifyChange();
	}
	void onClick_ADDALL(CCtrlButton*)
	{
		for (;;) {
			int count = list_ALLPROTO.GetCount();
			if (!count || LB_ERR == count)
				break;
			list_USEDPROTO.AddString(list_ALLPROTO.GetItemText(0));
			list_ALLPROTO.DeleteString(0);
		}
		this->NotifyChange();
	}
	void onClick_REMOVEALL(CCtrlButton*)
	{
		for (;;) {
			int count = list_USEDPROTO.GetCount();
			if (!count || LB_ERR == count)
				break;
			list_ALLPROTO.AddString(list_USEDPROTO.GetItemText(0));
			list_USEDPROTO.DeleteString(0);
		}
		this->NotifyChange();
	}
private:
	CCtrlListBox list_USEDPROTO, list_ALLPROTO;
	CCtrlButton btn_ADD, btn_REMOVE, btn_ADDALL, btn_REMOVEALL;

};

class COptAdvancedDlg : public CDlgBase
{
public:
	COptAdvancedDlg() : CDlgBase(g_plugin, IDD_ADVANCED),
		chk_INVIS_DISABLE(this, IDC_INVIS_DISABLE), chk_CASE_INSENSITIVE(this, IDC_CASE_INSENSITIVE), chk_SPECIALGROUP(this, ID_SPECIALGROUP), chk_EXCLUDE(this, ID_EXCLUDE),
		chk_REMOVE_TMP(this, ID_REMOVE_TMP), chk_REMOVE_TMP_ALL(this, ID_REMOVE_TMP_ALL), chk_IGNOREURL(this, ID_IGNOREURL), chk_AUTOAUTH(this, IDC_AUTOAUTH), chk_ADDTOSRVLST(this, IDC_ADDTOSRVLST),
		chk_REQAUTH(this, IDC_REQAUTH), chk_REGEX(this, IDC_REGEX), chk_HISTORY_LOG(this, IDC_HISTORY_LOG), chk_MATH_QUESTION(this, IDC_MATH_QUESTION),
		edit_SPECIALGROUPNAME(this, ID_SPECIALGROUPNAME), edit_AUTOADDGROUP(this, IDC_AUTOADDGROUP),
		btn_MATH_DETAILS(this, IDC_MATH_DETAILS)
	{
		btn_MATH_DETAILS.OnClick = Callback(this, &COptAdvancedDlg::onClick_MATH_DETAILS);
	}
	bool OnInitDialog() override
	{
		chk_INVIS_DISABLE.SetState(gbInvisDisable);
		chk_CASE_INSENSITIVE.SetState(gbCaseInsensitive);
		edit_SPECIALGROUPNAME.SetText(gbSpammersGroup.c_str());
		chk_SPECIALGROUP.SetState(gbSpecialGroup);
		chk_EXCLUDE.SetState(gbExclude);
		chk_REMOVE_TMP.SetState(gbDelExcluded);
		chk_REMOVE_TMP_ALL.SetState(gbDelAllTempory);
		chk_IGNOREURL.SetState(gbIgnoreURL);
		chk_AUTOAUTH.SetState(gbAutoAuth);
		chk_ADDTOSRVLST.SetState(gbAutoAddToServerList);
		chk_REQAUTH.SetState(gbAutoReqAuth);
		chk_REGEX.SetState(gbRegexMatch);
		chk_HISTORY_LOG.SetState(gbHistoryLog);
		chk_MATH_QUESTION.SetState(gbMathExpression);
		edit_AUTOADDGROUP.SetText(gbAutoAuthGroup.c_str());
		return true;
	}

	bool OnApply() override
	{
		g_plugin.setByte("CaseInsensitive", gbCaseInsensitive = chk_CASE_INSENSITIVE.GetState());
		g_plugin.setByte("DisableInInvis", gbInvisDisable = chk_INVIS_DISABLE.GetState());
		{
			static wstring NewGroupName = edit_SPECIALGROUPNAME.GetText(), CurrentGroupName;
			CurrentGroupName = gbSpammersGroup = DBGetContactSettingStringPAN(0, MODULENAME, "SpammersGroup", L"0");
			if (mir_wstrcmp(CurrentGroupName.c_str(), NewGroupName.c_str()) != 0) {
				bool GroupExist = Clist_GroupExists(NewGroupName.c_str()) != NULL;
				g_plugin.setWString("SpammersGroup", NewGroupName.c_str());
				gbSpammersGroup = DBGetContactSettingStringPAN(0, MODULENAME, "SpammersGroup", L"Spammers");
				if (!GroupExist && gbSpecialGroup)
					Clist_GroupCreate(0, gbSpammersGroup.c_str());
			}
		}
		g_plugin.setByte("SpecialGroup", gbSpecialGroup = chk_SPECIALGROUP.GetState());
		g_plugin.setByte("ExcludeContacts", gbExclude = chk_EXCLUDE.GetState());
		g_plugin.setByte("DelExcluded", gbDelExcluded = chk_REMOVE_TMP.GetState());
		g_plugin.setByte("DelAllTempory", gbDelAllTempory = chk_REMOVE_TMP_ALL.GetState());
		g_plugin.setByte("IgnoreURL", gbIgnoreURL = chk_IGNOREURL.GetState());

		g_plugin.setByte("AutoAuth", gbAutoAuth = chk_AUTOAUTH.GetState());
		g_plugin.setByte("AutoAddToServerList", gbAutoAddToServerList = chk_ADDTOSRVLST.GetState());
		g_plugin.setByte("AutoReqAuth", gbAutoReqAuth = chk_REQAUTH.GetState());
		g_plugin.setByte("RegexMatch", gbRegexMatch = chk_REGEX.GetState());
		g_plugin.setByte("HistoryLog", gbHistoryLog = chk_HISTORY_LOG.GetState());
		g_plugin.setByte("MathExpression", gbMathExpression = chk_MATH_QUESTION.GetState());
		{
			static wstring NewAGroupName = edit_AUTOADDGROUP.GetText(), CurrentAGroupName;
			CurrentAGroupName = gbAutoAuthGroup = DBGetContactSettingStringPAN(0, MODULENAME, "AutoAuthGroup", L"0");
			if (mir_wstrcmp(CurrentAGroupName.c_str(), NewAGroupName.c_str()) != 0) {
				bool GroupExist = Clist_GroupExists(NewAGroupName.c_str()) != NULL;
				g_plugin.setWString("AutoAuthGroup", NewAGroupName.c_str());
				gbAutoAuthGroup = DBGetContactSettingStringPAN(0, MODULENAME, "AutoAuthGroup", L"Not Spammers");
				if (!GroupExist && gbAutoAddToServerList)
					Clist_GroupCreate(0, gbAutoAuthGroup.c_str());
			}
		}
		return true;
	}

	void onClick_MATH_DETAILS(CCtrlButton*)
	{
		MessageBox(m_hwnd, TranslateT("If math expression is turned on, you can use following expression in message text:\nXX+XX-X/X*X\neach X will be replaced by one random number and answer will be expression result.\nMessage must contain only one expression without spaces."), TranslateT("Info"), MB_OK);
	}

private:
	CCtrlCheck chk_INVIS_DISABLE, chk_CASE_INSENSITIVE, chk_SPECIALGROUP, chk_EXCLUDE, chk_REMOVE_TMP, chk_REMOVE_TMP_ALL, chk_IGNOREURL, chk_AUTOAUTH, chk_ADDTOSRVLST, chk_REQAUTH, chk_REGEX, chk_HISTORY_LOG, chk_MATH_QUESTION;
	CCtrlEdit edit_SPECIALGROUPNAME, edit_AUTOADDGROUP;
	CCtrlButton btn_MATH_DETAILS;
};

int OnOptInit(WPARAM w, LPARAM l)
{
	UNREFERENCED_PARAMETER(l);

	OPTIONSDIALOGPAGE odp = {};
	odp.szGroup.w = LPGENW("Message sessions");
	odp.szTitle.w = LPGENW("StopSpam");
	odp.position = -1;
	odp.flags = ODPF_UNICODE;

	odp.szTab.w = LPGENW("General");
	odp.pDialog = new COptMainDlg();
	g_plugin.addOptions(w, &odp);


	odp.szTab.w = LPGENW("Messages");
	odp.pDialog = new COptMessagesDlg();
	g_plugin.addOptions(w, &odp);

	odp.szTab.w = LPGENW("Accounts");
	odp.pDialog = new COptProtoDlg();
	g_plugin.addOptions(w, &odp);

	odp.szTab.w = LPGENW("Advanced");
	odp.pDialog = new COptAdvancedDlg();
	g_plugin.addOptions(w, &odp);
	return 0;
}