summaryrefslogtreecommitdiffstats
path: root/src/CompositeChat.cpp
blob: d3b7595b702217dee2162e151e96091b38e241de (plain) (blame)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

// CompositeChat.cpp

// Implements the cCompositeChat class used to wrap a chat message with multiple parts (text, url, cmd)

#include "Globals.h"
#include "CompositeChat.h"





#ifdef SELF_TEST

/** A simple self-test that verifies that the composite chat parser is working properly. */
class SelfTest_CompositeChat
{
public:
	SelfTest_CompositeChat(void)
	{
		fprintf(stderr, "cCompositeChat self test...\n");
		TestParser1();
		TestParser2();
		TestParser3();
		TestParser4();
		TestParser5();
		fprintf(stderr, "cCompositeChat self test finished.\n");
	}
	
	void TestParser1(void)
	{
		cCompositeChat Msg;
		Msg.ParseText("Testing @2color codes and http://links parser");
		const cCompositeChat::cParts & Parts = Msg.GetParts();
		assert_test(Parts.size() == 4);
		assert_test(Parts[0]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[1]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[2]->m_PartType == cCompositeChat::ptUrl);
		assert_test(Parts[3]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[0]->m_Style == "");
		assert_test(Parts[1]->m_Style == "@2");
		assert_test(Parts[2]->m_Style == "@2");
		assert_test(Parts[3]->m_Style == "@2");
	}
	
	void TestParser2(void)
	{
		cCompositeChat Msg;
		Msg.ParseText("@3Advanced stuff: @5overriding color codes and http://links.with/@4color-in-them handling");
		const cCompositeChat::cParts & Parts = Msg.GetParts();
		assert_test(Parts.size() == 4);
		assert_test(Parts[0]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[1]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[2]->m_PartType == cCompositeChat::ptUrl);
		assert_test(Parts[3]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[0]->m_Style == "@3");
		assert_test(Parts[1]->m_Style == "@5");
		assert_test(Parts[2]->m_Style == "@5");
		assert_test(Parts[3]->m_Style == "@5");
	}
	
	void TestParser3(void)
	{
		cCompositeChat Msg;
		Msg.ParseText("http://links.starting the text");
		const cCompositeChat::cParts & Parts = Msg.GetParts();
		assert_test(Parts.size() == 2);
		assert_test(Parts[0]->m_PartType == cCompositeChat::ptUrl);
		assert_test(Parts[1]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[0]->m_Style == "");
		assert_test(Parts[1]->m_Style == "");
	}
	
	void TestParser4(void)
	{
		cCompositeChat Msg;
		Msg.ParseText("links finishing the text: http://some.server");
		const cCompositeChat::cParts & Parts = Msg.GetParts();
		assert_test(Parts.size() == 2);
		assert_test(Parts[0]->m_PartType == cCompositeChat::ptText);
		assert_test(Parts[1]->m_PartType == cCompositeChat::ptUrl);
		assert_test(Parts[0]->m_Style == "");
		assert_test(Parts[1]->m_Style == "");
	}
	
	void TestParser5(void)
	{
		cCompositeChat Msg;
		Msg.ParseText("http://only.links");
		const cCompositeChat::cParts & Parts = Msg.GetParts();
		assert_test(Parts.size() == 1);
		assert_test(Parts[0]->m_PartType == cCompositeChat::ptUrl);
		assert_test(Parts[0]->m_Style == "");
	}
	
} gTest;
#endif  // SELF_TEST





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat:

cCompositeChat::cCompositeChat(void) :
	m_MessageType(mtCustom)
{
}





cCompositeChat::cCompositeChat(const AString & a_ParseText, eMessageType a_MessageType) :
	m_MessageType(a_MessageType)
{
	ParseText(a_ParseText);
}





cCompositeChat::~cCompositeChat()
{
	Clear();
}





void cCompositeChat::Clear(void)
{
	for (cParts::iterator itr = m_Parts.begin(), end = m_Parts.end(); itr != end; ++itr)
	{
		delete *itr;
	}  // for itr - m_Parts[]
	m_Parts.clear();
}





void cCompositeChat::AddTextPart(const AString & a_Message, const AString & a_Style)
{
	m_Parts.push_back(new cTextPart(a_Message, a_Style));
}





void cCompositeChat::AddClientTranslatedPart(const AString & a_TranslationID, const AStringVector & a_Parameters, const AString & a_Style)
{
	m_Parts.push_back(new cClientTranslatedPart(a_TranslationID, a_Parameters, a_Style));
}





void cCompositeChat::AddUrlPart(const AString & a_Text, const AString & a_Url, const AString & a_Style)
{
	m_Parts.push_back(new cUrlPart(a_Text, a_Url, a_Style));
}





void cCompositeChat::AddRunCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style)
{
	m_Parts.push_back(new cRunCommandPart(a_Text, a_Command, a_Style));
}





void cCompositeChat::AddSuggestCommandPart(const AString & a_Text, const AString & a_SuggestedCommand, const AString & a_Style)
{
	m_Parts.push_back(new cSuggestCommandPart(a_Text, a_SuggestedCommand, a_Style));
}





void cCompositeChat::AddShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style)
{
	m_Parts.push_back(new cShowAchievementPart(a_PlayerName, a_Achievement, a_Style));
}





void cCompositeChat::ParseText(const AString & a_ParseText)
{
	size_t len = a_ParseText.length();
	size_t first = 0;  // First character of the currently parsed block
	AString CurrentStyle;
	AString CurrentText;
	for (size_t i = 0; i < len; i++)
	{
		switch (a_ParseText[i])
		{
			case '@':
			{
				// Color code
				i++;
				if (i >= len)
				{
					// Not enough following text
					break;
				}
				if (a_ParseText[i] == '@')
				{
					// "@@" escape, just put a "@" into the current text and keep parsing as text
					if (i > first + 1)
					{
						CurrentText.append(a_ParseText.c_str() + first, i - first - 1);
					}
					first = i + 1;
					continue;
				}
				else
				{
					// True color code. Create a part for the CurrentText and start parsing anew:
					if (i >= first)
					{
						CurrentText.append(a_ParseText.c_str() + first, i - first - 1);
						first = i + 1;
					}
					if (!CurrentText.empty())
					{
						m_Parts.push_back(new cTextPart(CurrentText, CurrentStyle));
						CurrentText.clear();
					}
					AddStyle(CurrentStyle, a_ParseText.substr(i - 1, 2));
				}
				break;
			}
			
			case ':':
			{
				const char * LinkPrefixes[] =
				{
					"http",
					"https"
				};
				for (size_t Prefix = 0; Prefix < ARRAYCOUNT(LinkPrefixes); Prefix++)
				{
					size_t PrefixLen = strlen(LinkPrefixes[Prefix]);
					if (
						(i >= first + PrefixLen) &&  // There is enough space in front of the colon for the prefix
						(strncmp(a_ParseText.c_str() + i - PrefixLen, LinkPrefixes[Prefix], PrefixLen) == 0)  // the prefix matches
					)
					{
						// Add everything before this as a text part:
						if (i > first + PrefixLen)
						{
							CurrentText.append(a_ParseText.c_str() + first, i - first - PrefixLen);
							first = i - PrefixLen;
						}
						if (!CurrentText.empty())
						{
							AddTextPart(CurrentText, CurrentStyle);
							CurrentText.clear();
						}
						
						// Go till the last non-whitespace char in the text:
						for (; i < len; i++)
						{
							if (isspace(a_ParseText[i]))
							{
								break;
							}
						}
						AddUrlPart(a_ParseText.substr(first, i - first), a_ParseText.substr(first, i - first), CurrentStyle);
						first = i;
						break;
					}
				}  // for Prefix - LinkPrefix[]
				break;
			}  // case ':'
		}  // switch (a_ParseText[i])
	}  // for i - a_ParseText[]
	if (first < len)
	{
		AddTextPart(a_ParseText.substr(first, len - first), CurrentStyle);
	}
}





void cCompositeChat::SetMessageType(eMessageType a_MessageType)
{
	m_MessageType = a_MessageType;
}





void cCompositeChat::UnderlineUrls(void)
{
	for (cParts::iterator itr = m_Parts.begin(), end = m_Parts.end(); itr != end; ++itr)
	{
		if ((*itr)->m_PartType == ptUrl)
		{
			(*itr)->m_Style.append("u");
		}
	}  // for itr - m_Parts[]
}





AString cCompositeChat::ExtractText(void) const
{
	AString Msg;
	for (cParts::const_iterator itr = m_Parts.begin(), end = m_Parts.end(); itr != end; ++itr)
	{
		switch ((*itr)->m_PartType)
		{
			case ptText:
			case ptClientTranslated:
			case ptRunCommand:
			case ptSuggestCommand:
			{
				Msg.append((*itr)->m_Text);
				break;
			}
			case ptUrl:
			{
				Msg.append(((cUrlPart *)(*itr))->m_Url);
				break;
			}
		}  // switch (PartType)
	}  // for itr - m_Parts[]
	return Msg;
}





cMCLogger::eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType)
{
	switch (a_MessageType)
	{
		case mtCustom:         return cMCLogger::llRegular;
		case mtFailure:        return cMCLogger::llWarning;
		case mtInformation:    return cMCLogger::llInfo;
		case mtSuccess:        return cMCLogger::llRegular;
		case mtWarning:        return cMCLogger::llWarning;
		case mtFatal:          return cMCLogger::llError;
		case mtDeath:          return cMCLogger::llRegular;
		case mtPrivateMessage: return cMCLogger::llRegular;
		case mtJoin:           return cMCLogger::llRegular;
		case mtLeave:          return cMCLogger::llRegular;
	}
	ASSERT(!"Unhandled MessageType");
	return cMCLogger::llError;
}





void cCompositeChat::AddStyle(AString & a_Style, const AString & a_AddStyle)
{
	if (a_AddStyle.empty())
	{
		return;
	}
	if (a_AddStyle[0] == '@')
	{
		size_t idx = a_Style.find('@');
		if ((idx != AString::npos) && (idx != a_Style.length()))
		{
			a_Style.erase(idx, 2);
		}
		a_Style.append(a_AddStyle);
		return;
	}
	a_Style.append(a_AddStyle);
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cBasePart:

cCompositeChat::cBasePart::cBasePart(cCompositeChat::ePartType a_PartType, const AString & a_Text, const AString & a_Style) :
	m_PartType(a_PartType),
	m_Text(a_Text),
	m_Style(a_Style)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cTextPart:

cCompositeChat::cTextPart::cTextPart(const AString & a_Text, const AString &a_Style) :
	super(ptText, a_Text, a_Style)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cClientTranslatedPart:

cCompositeChat::cClientTranslatedPart::cClientTranslatedPart(const AString & a_TranslationID, const AStringVector & a_Parameters, const AString & a_Style) :
	super(ptClientTranslated, a_TranslationID, a_Style),
	m_Parameters(a_Parameters)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cUrlPart:

cCompositeChat::cUrlPart::cUrlPart(const AString & a_Text, const AString & a_Url, const AString & a_Style) :
	super(ptUrl, a_Text, a_Style),
	m_Url(a_Url)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cCommandPart:

cCompositeChat::cCommandPart::cCommandPart(ePartType a_PartType, const AString & a_Text, const AString & a_Command, const AString & a_Style) :
	super(a_PartType, a_Text, a_Style),
	m_Command(a_Command)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cRunCommandPart:

cCompositeChat::cRunCommandPart::cRunCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style) :
	super(ptRunCommand, a_Text, a_Command, a_Style)
{
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cSuggestCommandPart:

cCompositeChat::cSuggestCommandPart::cSuggestCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style) :
	super(ptSuggestCommand, a_Text, a_Command, a_Style)
{
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCompositeChat::cShowAchievementPart:

cCompositeChat::cShowAchievementPart::cShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style) :
	super(ptShowAchievement, a_Achievement, a_Style),
	m_PlayerName(a_PlayerName)
{
}