summaryrefslogtreecommitdiff
path: root/plugins/StopSpamPlus/src/events.cpp
blob: de5a5cfad94b906123e360dcf4143c9b8446e04e (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
#include "stdafx.h"

int OnDbEventAdded(WPARAM, LPARAM lParam)
{
	MEVENT hDbEvent = (MEVENT)lParam;

	DB::EventInfo dbei(hDbEvent);
	if (!dbei)
		return 0;

	// if event is in protocol that is not despammed
	if (g_plugin.ProtoDisabled(dbei.szModule))
		return 0;

	// event is an auth request
	if (!(dbei.flags & DBEF_SENT) && !(dbei.flags & DBEF_READ) && dbei.eventType == EVENTTYPE_AUTHREQUEST) {
		MCONTACT hContact = DbGetAuthEventContact(&dbei);

		// if request is from unknown or not marked Answered contact
		//and if I don't sent message to this contact
		if (!Contact::OnList(hContact) && !g_plugin.getByte(hContact, DB_KEY_ANSWERED) && !g_plugin.getByte(hContact, DB_KEY_HASSENT)) {
			if (!g_plugin.HandleAuthReq) {
				char *buf = mir_utf8encodeW(variables_parse(g_plugin.getReply(), hContact).c_str());
				ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf);
				mir_free(buf);
			}

			g_plugin.setDword(hContact, DB_KEY_HASAUTH, hDbEvent);
			Contact::RemoveFromList(hContact);
			Contact::Hide(hContact);
			if (!g_plugin.HistLog)
				db_event_delete(hDbEvent);
			return 1;
		}
	}
	return 0;
}

int OnDbEventFilterAdd(WPARAM w, LPARAM l)
{
	MCONTACT hContact = (MCONTACT)w;
	DBEVENTINFO *dbei = (DBEVENTINFO *)l;
	if (dbei == nullptr || dbei->szModule == nullptr) //fix potential DEP crash
		return 0;

	// if event is in protocol that is not despammed
	if (g_plugin.ProtoDisabled(dbei->szModule))
		return 0; // ...let the event go its way

	// if event is not a message, or if the message has been read or sent...
	if (dbei->eventType != EVENTTYPE_MESSAGE || (dbei->flags & DBEF_READ) != 0)
		return 0;

	if (dbei->flags & DBEF_SENT) {
		g_plugin.setByte(hContact, DB_KEY_HASSENT, 1);
		return 0;
	}

	// if message is from known or marked Answered contact
	if (g_plugin.getByte(hContact, DB_KEY_ANSWERED) || g_plugin.getByte(hContact, DB_KEY_HASSENT))
		return 0;

	// checking if message from self-added contact
	if (Contact::OnList(hContact))
		return 0;

	// if message is corrupted or empty it cannot be an answer.
	if (!dbei->cbBlob || !dbei->pBlob)
		// reject processing of the event
		return 1;

	tstring message;

	if (dbei->flags & DBEF_UTF) {
		wchar_t *msg_u = mir_utf8decodeW((char *)dbei->pBlob);
		message = msg_u;
		mir_free(msg_u);
	}
	else {
		wchar_t *msg_u = mir_a2u((char *)(dbei->pBlob));
		message = msg_u;
		mir_free(msg_u);
	}

	// if message equal right answer...
	tstring answers = variables_parse(g_plugin.Answer, hContact);
	answers.append(g_plugin.AnswSplitString);
	tstring::size_type pos = 0;
	tstring::size_type prev_pos = 0;
	while ((pos = answers.find(g_plugin.AnswSplitString, pos)) != tstring::npos) {
		// get one of answers and trim witespace chars
		tstring answer = trim(answers.substr(prev_pos, pos - prev_pos));
		// if answer not empty
		if (answer.length() > 0) {
			// if message equal right answer...
			if (g_plugin.AnswNotCaseSens ? !mir_wstrcmpi(message.c_str(), answer.c_str()) : !mir_wstrcmp(message.c_str(), answer.c_str())) {
				// unhide contact
				Contact::Hide(hContact, false);

				// add contact permanently and delete our temporary variables
				if (g_plugin.AddPermanent) {
					Contact::PutOnList(hContact);
					db_delete_module(hContact, MODULENAME);
				}
				else g_plugin.setByte(hContact, DB_KEY_ANSWERED, 1);

				// send congratulation
				char *buf = mir_utf8encodeW(variables_parse(g_plugin.getCongrats(), hContact).c_str());
				ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf);
				mir_free(buf);

				// process the event
				return 1;
			}
		}
		prev_pos = ++pos;
	}

	// if message message does not contain infintite talk protection prefix
	// and question count for this contact is less then maximum
	const wchar_t *pwszPrefix = TranslateT("StopSpam automatic message:\r\n");
	if ((!g_plugin.InfTalkProtection || tstring::npos == message.find(pwszPrefix)) && (!g_plugin.MaxQuestCount || g_plugin.getDword(hContact, DB_KEY_QUESTCOUNT) < g_plugin.MaxQuestCount)) {
		// send question
		tstring q = pwszPrefix + variables_parse(g_plugin.getQuestion(), hContact);

		char *buf = mir_utf8encodeW(q.c_str());
		ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)buf);
		mir_free(buf);

		// increment question count
		uint32_t questCount = g_plugin.getDword(hContact, DB_KEY_QUESTCOUNT);
		g_plugin.setDword(hContact, DB_KEY_QUESTCOUNT, questCount + 1);
	}

	// hide contact from contact list
	Contact::RemoveFromList(hContact);
	Contact::Hide(hContact);

	// mark message as read and allow to insert it into the history
	dbei->flags |= DBEF_READ;
	return 0;
}

int OnShutdown(WPARAM, LPARAM)
{
	for (auto &cc : Contacts()) {
		if (Contact::OnList(cc))
			continue;

		if (MEVENT hDbEvent = g_plugin.getByte(cc, DB_KEY_HASAUTH)) {
			char *szProto = Proto_GetBaseAccountName(cc);
			CallProtoService(szProto, PS_AUTHDENY, hDbEvent, (LPARAM)_T2A(variables_parse(g_plugin.getReply(), cc).c_str()));
		}
	}
	
	return 0;
}