summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/HistoryStats/message.cpp
blob: b72f92bfda39503979ee1481c12ada1c5f35a767 (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
#include "_globals.h"
#include "message.h"

#include <algorithm>

/*
 * Message
 */

void Message::makeRawAvailable()
{
	do
	{
#if defined(MU_WIDE)
		if (m_Available & PtrIsNonT)
		{
			m_Raw = utils::fromA(ext::a::string(reinterpret_cast<const mu_ansi*>(m_RawSource), m_nLength));
			m_Available |= Raw;

			break;
		}
#endif // MU_WIDE

		if (m_Available & PtrIsUTF8)
		{
			m_Raw = utils::fromUTF8(reinterpret_cast<const mu_ansi*>(m_RawSource));
			m_Available |= Raw;

			break;
		}

		m_Raw.assign(reinterpret_cast<const mu_text*>(m_RawSource), m_nLength);
		m_Available |= Raw;
	} while(false);

	if (m_bStripRawRTF)
	{
		stripRawRTF();
	}

	if (m_bStripBBCodes)
	{
		stripBBCodes();
	}
}

void Message::stripRawRTF()
{
	if (m_Raw.substr(0, 6) == muT("{\\rtf1"))
	{
		m_Raw = RTFFilter::filter(m_Raw);
	}
}

void Message::stripBBCodes()
{
	static const mu_text* szSimpleBBCodes[][2] = {
		{ muT("[b]"), muT("[/b]") },
		{ muT("[u]"), muT("[/u]") },
		{ muT("[i]"), muT("[/i]") },
		{ muT("[s]"), muT("[/s]") },
	};

	static const mu_text* szParamBBCodes[][2] = {
		{ muT("[url=")  , muT("[/url]")   },
		{ muT("[color="), muT("[/color]") },
	};

	// convert raw string to lower case
	ext::string strRawLC = utils::toLowerCase(m_Raw);

	// remove simple BBcodes
	array_each_(i, szSimpleBBCodes)
	{
		const mu_text* szOpenTag = szSimpleBBCodes[i][0];
		const mu_text* szCloseTag = szSimpleBBCodes[i][1];

		int lenOpen = ext::strfunc::len(szOpenTag);
		int lenClose = ext::strfunc::len(szCloseTag);
		
		ext::string::size_type posOpen = 0;
		ext::string::size_type posClose = 0;

		while (true)
		{
			if ((posOpen = strRawLC.find(szOpenTag, posOpen)) == ext::string::npos)
			{
				break;
			}

			if ((posClose = strRawLC.find(szCloseTag, posOpen + lenOpen)) == ext::string::npos)
			{
				break;
			}

			strRawLC.erase(posOpen, lenOpen);
			strRawLC.erase(posClose - lenOpen, lenClose);

			// fix real string
			m_Raw.erase(posOpen, lenOpen);
			m_Raw.erase(posClose - lenOpen, lenClose);
		}
	}

	// remove BBcodes with parameters
	array_each_(i, szParamBBCodes)
	{
		const mu_text* szOpenTag = szParamBBCodes[i][0];
		const mu_text* szCloseTag = szParamBBCodes[i][1];
		
		int lenOpen = ext::strfunc::len(szOpenTag);
		int lenClose = ext::strfunc::len(szCloseTag);
		
		ext::string::size_type posOpen = 0;
		ext::string::size_type posOpen2 = 0;
		ext::string::size_type posClose = 0;

		while (true)
		{
			if ((posOpen = strRawLC.find(szOpenTag, posOpen)) == ext::string::npos)
			{
				break;
			}

			if ((posOpen2 = strRawLC.find(muC(']'), posOpen + lenOpen)) == ext::string::npos)
			{
				break;
			}

			if ((posClose = strRawLC.find(szCloseTag, posOpen2 + 1)) == ext::string::npos)
			{
				break;
			}

			strRawLC.erase(posOpen, posOpen2 - posOpen + 1);
			strRawLC.erase(posClose - posOpen2 + posOpen - 1, lenClose);

			// fix real string
			m_Raw.erase(posOpen, posOpen2 - posOpen + 1);
			m_Raw.erase(posClose - posOpen2 + posOpen - 1, lenClose);
		}
	}
}

void Message::filterLinks()
{
	static const mu_text* szSpaces = muT(" \r\r\n");
	static const mu_text* szPrefixes = muT("([{<:\"'");
	static const mu_text* szSuffixes = muT(".,:;!?)]}>\"'");
	static const mu_text* szValidProtocol = muT("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	static const mu_text* szValidHost = muT(".-abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");

	// init with raw text
	m_WithoutLinks = getRaw();

	ext::string& msg = m_WithoutLinks;
	ext::string::size_type pos = -1;

	// detect: protocol://[user[:password]@]host[/path]
	while (true)
	{
		if ((pos = msg.find(muT("://"), pos + 1)) == ext::string::npos)
		{
			break;
		}

		// find start of URL
		ext::string::size_type pos_proto = msg.find_last_not_of(szValidProtocol, pos - 1);

		(pos_proto == ext::string::npos) ? pos_proto = 0 : ++pos_proto;

		if (pos_proto < pos)
		{
			// find end of URL
			ext::string::size_type pos_last = msg.find_first_of(szSpaces, pos + 3);

			(pos_last == ext::string::npos) ? pos_last = msg.length() - 1 : --pos_last;

			// filter suffixes (punctuation, parentheses, ...)
			if (ext::strfunc::chr(szSuffixes, msg[pos_last]))
			{
				--pos_last;
			}

			// find slash: for host name validation
			ext::string::size_type pos_slash = msg.find(muC('/'), pos + 3);

			if (pos_slash == ext::string::npos || pos_slash > pos_last)
			{
				pos_slash = pos_last + 1;
			}

			// find at: for host name validation
			ext::string::size_type pos_at = msg.find(muC('@'), pos + 3);

			if (pos_at == ext::string::npos || pos_at > pos_slash)
			{
				pos_at = pos + 2;
			}

			// check for valid host (x.x)
			if (pos_slash - pos_at > 3)
			{
				ext::string::size_type pos_invalid = msg.find_first_not_of(szValidHost, pos_at + 1);

				if (pos_invalid == ext::string::npos || pos_invalid >= pos_slash)
				{
					if (std::count(msg.begin() + pos_at + 1, msg.begin() + pos_slash, muC('.')) >= 1)
					{
						ext::string link = msg.substr(pos_proto, pos_last - pos_proto + 1);

						// remove extracted link from message text
						msg.erase(pos_proto, link.length());
						pos = pos_last - link.length();

						// TODO: put link in list
					}
				}
			}
		}
	}
	
	// detect: www.host[/path]
	pos = -1;

	while (true)
	{
		if ((pos = msg.find(muT("www."), pos + 1)) == ext::string::npos)
		{
			break;
		}

		// find end of URL
		ext::string::size_type pos_last = msg.find_first_of(szSpaces, pos + 4);

		(pos_last == ext::string::npos) ? pos_last = msg.length() - 1 : --pos_last;

		// filter suffixes (punctuation, parentheses, ...)
		if (ext::strfunc::chr(szSuffixes, msg[pos_last]))
		{
			--pos_last;
		}

		// find slash: for host name validation
		ext::string::size_type pos_slash = msg.find(muC('/'), pos + 4);

		if (pos_slash == ext::string::npos || pos_slash > pos_last)
		{
			pos_slash = pos_last + 1;
		}

		// find at: for host name validation
		ext::string::size_type pos_at = pos + 3;

		// check for valid host (x.x)
		if (pos_slash - pos_at > 3)
		{
			ext::string::size_type pos_invalid = msg.find_first_not_of(szValidHost, pos_at + 1);

			if (pos_invalid == ext::string::npos || pos_invalid >= pos_slash)
			{
				if (std::count(msg.begin() + pos_at + 1, msg.begin() + pos_slash, muC('.')) >= 1)
				{
					ext::string link = muT("http://") + msg.substr(pos, pos_last - pos + 1);

					// remove extracted link from message text
					msg.erase(pos, link.length() - 7);
					pos = pos_last - (link.length() - 7);

					// TODO: put link in list
				}
			}
		}
	}

	// detect: user@host
	pos = -1;

	while (true)
	{
		if ((pos = msg.find(muC('@'), pos + 1)) == ext::string::npos)
		{
			break;
		}

		if (pos > 0 && pos < msg.length() - 1)
		{
			// find end of address
			ext::string::size_type pos_last = msg.find_first_not_of(szValidHost, pos + 1);

			(pos_last == ext::string::npos) ? pos_last = msg.length() - 1 : --pos_last;

			// filter suffixes (punctuation, parentheses, ...)
			if (ext::strfunc::chr(szSuffixes, msg[pos_last]))
			{
				--pos_last;
			}

			// find start of address
			ext::string::size_type pos_first = msg.find_last_of(szSpaces, pos - 1);

			(pos_first == ext::string::npos) ? pos_first = 0 : ++pos_first;

			// filter prefixes (punctuation, parentheses, ...)
			if (ext::strfunc::chr(szPrefixes, msg[pos_first]))
			{
				++pos_first;
			}

			// check for valid host (x.x)
			if (pos_first < pos && pos_last - pos >= 3)
			{
				if (std::count(msg.begin() + pos + 1, msg.begin() + pos_last + 1, muC('.')) >= 1)
				{
					ext::string link = msg.substr(pos_first, pos_last - pos_first + 1);

					// remove extracted link from message text
					msg.erase(pos_first, link.length());
					pos = pos_last - (link.length());

					// prepend "mailto:" if missing
					if (link.substr(0, 7) != muT("mailto:"))
					{
						link.insert(0, muT("mailto:"));
					}

					// TODO: put link in list
				}
			}
		}
	}

	m_Available |= WithoutLinks;
}