summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/colbase_words.cpp
blob: 9d3b4667054a8d913d514f9353a9127f09d27b92 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "stdafx.h"
#include "colbase_words.h"

/*
 * ColBaseWords
 */

void ColBaseWords::addWord(WordMap* pWords, const ext::string& word, bool bOutgoing) const
{
	// filter words
	if (m_bFilterWords)
	{
		upto_each_(i, m_ActiveWordFilter.size())
		{
			const Filter* pFilter = m_ActiveWordFilter[i];

			switch (pFilter->getMode())
			{
				case Settings::fwmWordsMatching:
					{
						citer_each_(WordSet, j, pFilter->getWords())
						{
							if (word == *j)
							{
								return;
							}
						}
					}
					break;

				case Settings::fwmWordsContaining:
					{
						citer_each_(WordSet, j, pFilter->getWords())
						{
							if (word.find(*j) != ext::string::npos)
							{
								return;
							}
						}
					}
					break;

				case Settings::fwmWordsStartingWith:
					{
						citer_each_(WordSet, j, pFilter->getWords())
						{
							if (word.length() >= j->length() && word.substr(0, j->length()) == *j)
							{
								return;
							}
						}
					}
					break;

				case Settings::fwmWordsEndingWith:
					{
						citer_each_(WordSet, j, pFilter->getWords())
						{
							if (word.length() >= j->length() && word.substr(word.length() - j->length(), j->length()) == *j)
							{
								return;
							}
						}
					}
					break;
			}
		}
	}

	// insert, if we didn't get filtered
	WordMap::iterator i = pWords->find(word);

	if (i != pWords->end())
	{
		(bOutgoing ? i->second.out : i->second.in)++;
	}
	else
	{
		pWords->insert(std::make_pair(word, bOutgoing ? InOut(0, 1) : InOut(1, 0)));
	}
}

void ColBaseWords::parseMsg(WordMap* pWords, const ext::string& msg, bool bOutgoing) const
{
	// filter messages
	if (m_bFilterMessages) {
		ext::string msgLC = utils::toLowerCase(msg);

		upto_each_(i, m_ActiveMessageFilter.size())
		{
			const Filter* pFilter = m_ActiveMessageFilter[i];

			switch (pFilter->getMode()) {
			case Settings::fwmMessagesMatching:
				citer_each_(WordSet, j, pFilter->getWords())
				{
					if (msgLC == *j) {
						return;
					}
				}
				break;

			case Settings::fwmMessagesContaining:
				citer_each_(WordSet, j, pFilter->getWords())
				{
					if (msgLC.find(*j) != ext::string::npos) {
						return;
					}
				}
				break;

			case Settings::fwmMessagesStartingWith:
				citer_each_(WordSet, j, pFilter->getWords())
				{
					if (msgLC.length() >= j->length() && msgLC.substr(0, j->length()) == *j) {
						return;
					}
				}
				break;

			case Settings::fwmMessagesEndingWith:
				citer_each_(WordSet, j, pFilter->getWords())
				{
					if (msgLC.length() >= j->length() && msgLC.substr(msgLC.length() - j->length(), j->length()) == *j) {
						return;
					}
				}
				break;
			}
		}
	}

	// start parsing into words if not already filtered
	size_t firstChar = 0;
	size_t nextSpace;

	while (firstChar < msg.length() && getCharMapper()->mapChar(msg[firstChar]) == ' ')
		++firstChar;

	while (firstChar < msg.length()) {
		nextSpace = firstChar + 1;

		while (nextSpace < msg.length() && getCharMapper()->mapChar(msg[nextSpace]) != ' ')
			++nextSpace;

		int wordLen = nextSpace - firstChar;

		if (wordLen >= m_nMinLength && (m_nMaxLength == 0 || wordLen <= m_nMaxLength)) {
			ext::string word(wordLen, '_');

			upto_each_(i, wordLen)
			{
				word[i] = getCharMapper()->mapChar(msg[firstChar + i]);
			}

			addWord(pWords, word, bOutgoing);
		}

		firstChar = nextSpace + 1;

		while (firstChar < msg.length() && getCharMapper()->mapChar(msg[firstChar]) == ' ')
			++firstChar;
	}
}

ColBaseWords::ColBaseWords() : m_nSource(2), m_nMinLength(1), m_nMaxLength(0), m_bFilterLinks(true),
	m_hSource(nullptr), m_hMinLength(nullptr), m_hMaxLength(nullptr), m_hFilterLinks(nullptr),
	m_bFilterMessages(false), m_bFilterWords(false)
{
}

void ColBaseWords::impl_copyConfig(const Column* pSource)
{
	const ColBaseWords& src = *reinterpret_cast<const ColBaseWords*>(pSource);

	m_nSource      = src.m_nSource;
	m_nMinLength   = src.m_nMinLength;
	m_nMaxLength   = src.m_nMaxLength;
	m_bFilterLinks = src.m_bFilterLinks;
	m_FilterWords  = src.m_FilterWords;
}

void ColBaseWords::impl_configRead(const SettingsTree& settings)
{
	m_nSource      = settings.readIntRanged(con::KeySource, 2, 0, 2);
	m_nMinLength   = settings.readIntRanged(con::KeyMinLength, 1, 1, 1000);
	m_nMaxLength   = settings.readIntRanged(con::KeyMaxLength, 0, 0, 1000);
	m_bFilterLinks = settings.readBool(con::KeyFilterLinks, true);

	// filter words
	m_FilterWords.clear();

	int nCount = settings.readInt(con::KeyFilterWords, 0);

	upto_each_(i, nCount)
	{
		m_FilterWords.insert(settings.readStr((con::KeyFilterWords + utils::intToString(i)).c_str(), L""));
	}
}

void ColBaseWords::impl_configWrite(SettingsTree& settings) const
{
	settings.writeInt (con::KeySource, m_nSource);
	settings.writeInt (con::KeyMinLength, m_nMinLength);
	settings.writeInt (con::KeyMaxLength, m_nMaxLength);
	settings.writeBool(con::KeyFilterLinks, m_bFilterLinks);

	// filter words
	settings.writeInt(con::KeyFilterWords, m_FilterWords.size());

	int nFilterNr = 0;

	citer_each_(ColFilterSet, i, m_FilterWords)
	{
		settings.writeStr((con::KeyFilterWords + utils::intToString(nFilterNr++)).c_str(), i->c_str());
	}
}

void ColBaseWords::impl_configToUI(OptionsCtrl& Opt, OptionsCtrl::Item hGroup)
{
	OptionsCtrl::Group hTemp;

	hTemp = Opt.insertGroup (hGroup, TranslateT("Extract words from"));
	m_hSource = Opt.insertRadio (hTemp, nullptr, TranslateT("Incoming messages"));
	             Opt.insertRadio (hTemp, m_hSource, TranslateT("Outgoing messages"));
	              Opt.insertRadio (hTemp, m_hSource, TranslateT("All messages"));
	m_hMinLength = Opt.insertEdit(hGroup, TranslateT("Ignore words shorter than (chars)"), L"", OptionsCtrl::OCF_NUMBER);
	m_hMaxLength = Opt.insertEdit(hGroup, TranslateT("Ignore words longer than (chars, 0=no limit)"), L"", OptionsCtrl::OCF_NUMBER);
	m_hFilterLinks = Opt.insertCheck(hGroup, TranslateT("Filter URLs/e-mail addresses"));
	                 Opt.insertButton(hGroup, TranslateT("Filter words/messages"), TranslateT("Define..."), 0, Settings::biFilterWords);

	Opt.setRadioChecked(m_hSource     , m_nSource     );
	Opt.setEditNumber  (m_hMinLength  , m_nMinLength  );
	Opt.setEditNumber  (m_hMaxLength  , m_nMaxLength  );
	Opt.checkItem      (m_hFilterLinks, m_bFilterLinks);
}

void ColBaseWords::impl_configFromUI(OptionsCtrl& Opt)
{
	m_nSource = Opt.getRadioChecked(m_hSource);
	m_nMinLength = Opt.getEditNumber(m_hMinLength);
	m_nMaxLength = Opt.getEditNumber(m_hMaxLength);
	m_bFilterLinks = Opt.isItemChecked(m_hFilterLinks);

	// ensure constraints
	utils::ensureRange(m_nMinLength, 1, 1000, 1);
	utils::ensureRange(m_nMaxLength, 0, 1000, 0);
}

ext::string ColBaseWords::impl_contactDataGetUID() const
{
	ext::string strUID = ext::str(ext::format(L"words-|-|-|-|")
											% m_nSource
											% m_nMinLength
											% m_nMaxLength
											% (m_bFilterLinks ? 1 : 0));

	citer_each_(ColFilterSet, i, m_FilterWords)
	{
		strUID += L"-";
		strUID += *i;
	}

	return strUID;
}

void ColBaseWords::impl_contactDataBeginAcquire()
{
	m_bFilterMessages = false;
	m_bFilterWords = false;
	m_ActiveMessageFilter.clear();
	m_ActiveWordFilter.clear();

	citer_each_(ColFilterSet, i, m_FilterWords)
	{
		const Filter* pFilter = getSettings()->getFilter(*i);

		if (pFilter && !pFilter->getWords().empty()) {
			switch (pFilter->getMode()) {
			case Settings::fwmMessagesMatching:
			case Settings::fwmMessagesContaining:
			case Settings::fwmMessagesStartingWith:
			case Settings::fwmMessagesEndingWith:
				m_ActiveMessageFilter.push_back(pFilter);
				m_bFilterMessages = true;
				break;

			case Settings::fwmWordsMatching:
			case Settings::fwmWordsContaining:
			case Settings::fwmWordsStartingWith:
			case Settings::fwmWordsEndingWith:
				m_ActiveWordFilter.push_back(pFilter);
				m_bFilterWords = true;
				break;
			}
		}
	}
}

void ColBaseWords::impl_contactDataPrepare(Contact& contact) const
{
	WordMap* pData = new WordMap;
	contact.setSlot(contactDataSlotGet(), pData);
}

void ColBaseWords::impl_contactDataFree(Contact& contact) const
{
	WordMap* pData = reinterpret_cast<WordMap*>(contact.getSlot(contactDataSlotGet()));
	if (pData) {
		delete pData;
		contact.setSlot(contactDataSlotGet(), nullptr);
	}
}

void ColBaseWords::impl_contactDataAcquireMessage(Contact& contact, Message& msg)
{
	if (m_nSource == 2 || m_nSource == 1 && msg.isOutgoing() || m_nSource == 0 && !msg.isOutgoing()) {
		WordMap* pData = reinterpret_cast<WordMap*>(contact.getSlot(contactDataSlotGet()));
		parseMsg(pData, m_bFilterLinks ? msg.getWithoutLinks() : msg.getRaw(), msg.isOutgoing());
	}
}

void ColBaseWords::impl_contactDataMerge(Contact& contact, const Contact& include) const
{
	WordMap* pData = reinterpret_cast<WordMap*>(contact.getSlot(contactDataSlotGet()));
	const WordMap* pIncData = reinterpret_cast<const WordMap*>(include.getSlot(contactDataSlotGet()));

	citer_each_(WordMap, j, *pIncData)
	{
		if (pData->find(j->first) != pData->end())
			(*pData)[j->first] += j->second;
		else
			pData->insert(*j);
	}
}