summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/chat_log.cpp
blob: 9af0f406050daf3a34befd5efde009c957c2f98d (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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (C) 2012-20 Miranda NG team,
// Copyright (c) 2000-09 Miranda ICQ/IM project,
// all portions of this codebase are copyrighted to the people
// listed in contributors.txt.
//
// 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.
//
// part of tabSRMM messaging plugin for Miranda.
//
// (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
//
// Implements the richedit-based message history display for the group
// chat window.

#include "stdafx.h"

/*
 * The code for streaming the text is to a large extent copied from
 * the srmm module and then modified to fit the chat module.
 */

static int EventToIndex(LOGINFO *lin)
{
	switch (lin->iType) {
	case GC_EVENT_MESSAGE:
		if (lin->bIsMe)
			return 10;
		else
			return 9;

	case GC_EVENT_JOIN:
		return 3;
	case GC_EVENT_PART:
		return 4;
	case GC_EVENT_QUIT:
		return 5;
	case GC_EVENT_NICK:
		return 7;
	case GC_EVENT_KICK:
		return 6;
	case GC_EVENT_NOTICE:
		return 8;
	case GC_EVENT_TOPIC:
		return 11;
	case GC_EVENT_INFORMATION:
		return 12;
	case GC_EVENT_ADDSTATUS:
		return 13;
	case GC_EVENT_REMOVESTATUS:
		return 14;
	case GC_EVENT_ACTION:
		return 15;
	}
	return 0;
}

static BYTE EventToSymbol(LOGINFO *lin)
{
	switch (lin->iType) {
	case GC_EVENT_MESSAGE:
		return (lin->bIsMe) ? 0x37 : 0x38;
	case GC_EVENT_JOIN:
		return 0x34;
	case GC_EVENT_PART:
		return 0x33;
	case GC_EVENT_QUIT:
		return 0x39;
	case GC_EVENT_NICK:
		return 0x71;
	case GC_EVENT_KICK:
		return 0x72;
	case GC_EVENT_NOTICE:
		return 0x28;
	case GC_EVENT_INFORMATION:
		return 0x69;
	case GC_EVENT_ADDSTATUS:
		return 0x35;
	case GC_EVENT_REMOVESTATUS:
		return 0x36;
	case GC_EVENT_ACTION:
		return 0x60;
	}
	return 0x73;
}

static int EventToIcon(LOGINFO *lin)
{
	switch (lin->iType) {
	case GC_EVENT_MESSAGE:
		if (lin->bIsMe)
			return ICON_MESSAGEOUT;
		else
			return ICON_MESSAGE;

	case GC_EVENT_JOIN:
		return ICON_JOIN;
	case GC_EVENT_PART:
		return ICON_PART;
	case GC_EVENT_QUIT:
		return ICON_QUIT;
	case GC_EVENT_NICK:
		return ICON_NICK;
	case GC_EVENT_KICK:
		return ICON_KICK;
	case GC_EVENT_NOTICE:
		return ICON_NOTICE;
	case GC_EVENT_TOPIC:
		return ICON_TOPIC;
	case GC_EVENT_INFORMATION:
		return ICON_INFO;
	case GC_EVENT_ADDSTATUS:
		return ICON_ADDSTATUS;
	case GC_EVENT_REMOVESTATUS:
		return ICON_REMSTATUS;
	case GC_EVENT_ACTION:
		return ICON_ACTION;
	}
	return 0;
}

static void Log_AppendRTF(LOGSTREAMDATA *streamData, BOOL simpleMode, CMStringA &str, const wchar_t *fmt, ...)
{
	int textCharsCount = 0;
	wchar_t *line = (wchar_t *)_alloca(8001 * sizeof(wchar_t));

	va_list va;
	va_start(va, fmt);
	int lineLen = mir_vsnwprintf(line, 8000, fmt, va);
	if (lineLen < 0)
		lineLen = 8000;
	line[lineLen] = 0;
	va_end(va);

	CMStringA res;

	for (; *line; line++, textCharsCount++) {
		if (*line == '\r' && line[1] == '\n') {
			res.Append("\\par ");
			line++;
		}
		else if (*line == '\n') {
			res.Append("\\line ");
		}
		else if (*line == '%' && !simpleMode) {
			char szTemp[200]; szTemp[0] = '\0';
			switch (*++line) {
			case '\0':
			case '%':
				res.AppendChar('%');
				break;

			case 'c':
			case 'f':
				if (g_Settings.bStripFormat || streamData->bStripFormat)
					line += 2;
				else if (line[1] != '\0' && line[2] != '\0') {
					wchar_t szTemp3[3], c = *line;
					int col;
					szTemp3[0] = line[1];
					szTemp3[1] = line[2];
					szTemp3[2] = '\0';
					line += 2;

					col = _wtoi(szTemp3);
					col += (OPTIONS_FONTCOUNT + 1);
					res.AppendFormat((c == 'c') ? "\\cf%u " : "\\highlight%u ", col);
				}
				break;
			case 'C':
			case 'F':
				if (!g_Settings.bStripFormat && !streamData->bStripFormat) {
					int j = streamData->lin->bIsHighlighted ? 16 : EventToIndex(streamData->lin);
					if (*line == 'C')
						res.AppendFormat("\\cf%u ", j + 1);
					else
						res.Append("\\highlight0 ");
				}
				break;
			case 'b':
			case 'u':
			case 'i':
				if (!streamData->bStripFormat)
					res.AppendFormat((*line == 'u') ? "\\%cl " : "\\%c ", *line);
				break;

			case 'B':
			case 'U':
			case 'I':
				if (!streamData->bStripFormat)
					res.AppendFormat((*line == 'U') ? "\\%cl0 " : "\\%c0 ", tolower(*line));
				break;

			case 'r':
				if (!streamData->bStripFormat) {
					int index = EventToIndex(streamData->lin);
					res.AppendFormat("%s ", g_chatApi.Log_SetStyle(index));
				}
				break;
			}
		}
		else if (*line == '\t' && !streamData->bStripFormat) {
			res.Append("\\tab ");
		}
		else if ((*line == '\\' || *line == '{' || *line == '}') && !streamData->bStripFormat) {
			res.AppendChar('\\');
			res.AppendChar(*line);
		}
		else if (*line > 0 && *line < 128) {
			res.AppendChar((char)*line);
		}
		else res.AppendFormat("\\u%u ?", (WORD)*line);
	}

	str += res;
}

static void AddEventToBuffer(CMStringA &str, LOGSTREAMDATA *streamData)
{
	if (streamData == nullptr || streamData->lin == nullptr)
		return;

	CMStringW wszNick;
	if (streamData->lin->ptszNick) {
		if (g_Settings.bLogLimitNames && mir_wstrlen(streamData->lin->ptszNick) > 20) {
			wszNick.Append(streamData->lin->ptszNick, 20);
			wszNick.Append(L"...");
		}
		else wszNick.Append(streamData->lin->ptszNick);

		if (g_Settings.bClickableNicks) {
			wszNick.Insert(0, CLICKNICK_BEGIN);
			wszNick.Append(CLICKNICK_END);
		}

		if (streamData->lin->ptszUserInfo && streamData->lin->iType != GC_EVENT_TOPIC)
			wszNick.AppendFormat(L" (%s)", streamData->lin->ptszUserInfo);
	}

	switch (streamData->lin->iType) {
	case GC_EVENT_MESSAGE:
		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, L"%s", streamData->lin->ptszText);
		break;
	case GC_EVENT_ACTION:
		if (streamData->lin->ptszNick && streamData->lin->ptszText) {
			Log_AppendRTF(streamData, TRUE, str, L"%s ", streamData->lin->ptszNick);
			Log_AppendRTF(streamData, FALSE, str, L"%s", streamData->lin->ptszText);
		}
		break;
	case GC_EVENT_JOIN:
		if (!wszNick.IsEmpty()) {
			if (!streamData->lin->bIsMe)
				/* replace nick of a newcomer with a link */
				Log_AppendRTF(streamData, TRUE, str, TranslateT("%s has joined"), wszNick.c_str());
			else
				Log_AppendRTF(streamData, TRUE, str, TranslateT("You have joined %s"), streamData->si->ptszName);
		}
		break;
	case GC_EVENT_PART:
		if (!wszNick.IsEmpty())
			Log_AppendRTF(streamData, TRUE, str, TranslateT("%s has left"), wszNick.c_str());
		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, L": %s", streamData->lin->ptszText);
		break;
	case GC_EVENT_QUIT:
		if (!wszNick.IsEmpty())
			Log_AppendRTF(streamData, TRUE, str, TranslateT("%s has disconnected"), wszNick.c_str());
		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, L": %s", streamData->lin->ptszText);
		break;
	case GC_EVENT_NICK:
		if (!wszNick.IsEmpty() && streamData->lin->ptszText) {
			if (!streamData->lin->bIsMe)
				Log_AppendRTF(streamData, TRUE, str, TranslateT("%s is now known as %s"), wszNick.c_str(), streamData->lin->ptszText);
			else
				Log_AppendRTF(streamData, TRUE, str, TranslateT("You are now known as %s"), streamData->lin->ptszText);
		}
		break;
	case GC_EVENT_KICK:
		if (!wszNick.IsEmpty() && streamData->lin->ptszStatus)
			Log_AppendRTF(streamData, TRUE, str, TranslateT("%s kicked %s"), streamData->lin->ptszStatus, wszNick.c_str());

		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, L": %s", streamData->lin->ptszText);
		break;
	case GC_EVENT_NOTICE:
		if (!wszNick.IsEmpty() && streamData->lin->ptszText) {
			Log_AppendRTF(streamData, TRUE, str, TranslateT("Notice from %s: "), wszNick.c_str());
			Log_AppendRTF(streamData, FALSE, str, L"%s", streamData->lin->ptszText);
		}
		break;
	case GC_EVENT_TOPIC:
		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, TranslateT("The topic is '%s%s'"), streamData->lin->ptszText, L"%r");
		if (!wszNick.IsEmpty())
			Log_AppendRTF(streamData, TRUE, str, (streamData->lin->ptszUserInfo) ? TranslateT(" (set by %s on %s)") : TranslateT(" (set by %s)"), wszNick.c_str(), streamData->lin->ptszUserInfo);
		break;
	case GC_EVENT_INFORMATION:
		if (streamData->lin->ptszText)
			Log_AppendRTF(streamData, FALSE, str, (streamData->lin->bIsMe) ? L"--> %s" : L"%s", streamData->lin->ptszText);
		break;
	case GC_EVENT_ADDSTATUS:
		if (!wszNick.IsEmpty() && streamData->lin->ptszText && streamData->lin->ptszStatus)
			Log_AppendRTF(streamData, TRUE, str, TranslateT("%s enables '%s' status for %s"), streamData->lin->ptszText, streamData->lin->ptszStatus, wszNick.c_str());
		break;
	case GC_EVENT_REMOVESTATUS:
		if (!wszNick.IsEmpty() && streamData->lin->ptszText && streamData->lin->ptszStatus)
			Log_AppendRTF(streamData, TRUE, str, TranslateT("%s disables '%s' status for %s"), streamData->lin->ptszText, streamData->lin->ptszStatus, wszNick.c_str());
		break;
	}
}

char* Log_CreateRtfHeader(void)
{
	// get the number of pixels per logical inch
	if (g_chatApi.logPixelSY == 0) {
		HDC hdc = GetDC(nullptr);
		g_chatApi.logPixelSY = GetDeviceCaps(hdc, LOGPIXELSY);
		g_chatApi.logPixelSX = GetDeviceCaps(hdc, LOGPIXELSX);
		ReleaseDC(nullptr, hdc);
	}

	// ### RTF HEADER

	// font table
	CMStringA str("{\\rtf1\\ansi\\deff0{\\fonttbl");
	for (int i = 0; i < OPTIONS_FONTCOUNT; i++)
		str.AppendFormat("{\\f%u\\fnil\\fcharset%u%S;}", i, g_chatApi.aFonts[i].lf.lfCharSet, g_chatApi.aFonts[i].lf.lfFaceName);

	// colour table
	str.Append("}{\\colortbl ;");

	for (auto &it : g_chatApi.aFonts)
		str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(it.color), GetGValue(it.color), GetBValue(it.color));

	for (auto &it : Utils::rtf_clrs)
		str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(it->clr), GetGValue(it->clr), GetBValue(it->clr));

	for (int i = 0; i < STATUSICONCOUNT; i++)
		str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(g_Settings.nickColors[i]), GetGValue(g_Settings.nickColors[i]), GetBValue(g_Settings.nickColors[i]));

	// new paragraph
	str.AppendFormat("}\\pard\\sl%d", 1000);

	// set tabs and indents
	int iIndent = 0;

	if (g_Settings.bLogSymbols) {
		wchar_t szString[2];
		LOGFONT lf;

		szString[1] = 0;
		szString[0] = 0x28;
		g_chatApi.LoadMsgDlgFont(17, &lf, nullptr);
		HFONT hFont = CreateFontIndirect(&lf);
		int iText = Chat_GetTextPixelSize(szString, hFont, true) + 3;
		DeleteObject(hFont);
		iIndent += (iText * 1440) / g_chatApi.logPixelSX;
		str.AppendFormat("\\tx%u", iIndent);
	}
	else if (g_Settings.dwIconFlags) {
		iIndent += ((g_Settings.bScaleIcons ? 14 : 20) * 1440) / g_chatApi.logPixelSX;
		str.AppendFormat("\\tx%u", iIndent);
	}
	if (g_Settings.bShowTime) {
		int iSize = (g_Settings.LogTextIndent * 1440) / g_chatApi.logPixelSX;
		str.AppendFormat("\\tx%u", iIndent + iSize);
		if (g_Settings.bLogIndentEnabled)
			iIndent += iSize;
	}
	str.AppendFormat("\\fi-%u\\li%u", iIndent, iIndent);

	return str.Detach();
}

char* Log_CreateRTF(LOGSTREAMDATA *streamData)
{
	LOGINFO *lin = streamData->lin;
	SESSION_INFO *si = streamData->si;
	MODULEINFO *mi = si->pMI;

	// ### RTF HEADER

	if (mi->pszHeader == nullptr)
		mi->pszHeader = Log_CreateRtfHeader();

	char *header = mi->pszHeader;
	streamData->crCount = 0;

	CMStringA str;
	if (header)
		str.Append(header);

	// ### RTF BODY (one iteration per event that should be streamed in)
	while (lin) {
		// filter
		if ((si->iType != GCW_CHATROOM && si->iType != GCW_PRIVMESS) || !si->pDlg->m_bFilterEnabled || (si->pDlg->m_iLogFilterFlags & lin->iType) != 0) {
			if (lin->next != nullptr)
				str.Append("\\par ");

			if (streamData->dat->m_bDividerWanted) {
				static char szStyle_div[128] = "\0";
				if (szStyle_div[0] == 0)
					mir_snprintf(szStyle_div, "\\f%u\\cf%u\\ul0\\b%d\\i%d\\fs%u", 17, 18, 0, 0, 5);

				if (lin->prev || !streamData->bRedraw)
					str.AppendFormat("\\qc\\sl-1\\highlight%d %s ---------------------------------------------------------------------------------------\\par ", 18, szStyle_div);
				streamData->dat->m_bDividerWanted = false;
			}
			// create new line, and set font and color
			str.AppendFormat("\\ql\\sl0%s ", g_chatApi.Log_SetStyle(0));
			str.AppendFormat("\\v~-+%p+-~\\v0 ", lin);

			// Insert icon
			if (g_Settings.bLogSymbols)                // use symbols
				str.AppendFormat("%s %c", g_chatApi.Log_SetStyle(17), EventToSymbol(lin));
			else if (g_Settings.dwIconFlags) {
				int iIndex = lin->bIsHighlighted ? ICON_HIGHLIGHT : EventToIcon(lin);
				str.Append("\\f0\\fs14");
				str.Append(g_chatApi.pLogIconBmpBits[iIndex]);
			}

			if (g_Settings.bTimeStampEventColour) {
				// colored timestamps
				static char szStyle[256];
				LOGFONT &F = g_chatApi.aFonts[0].lf;
				int iii;
				if (lin->ptszNick && lin->iType == GC_EVENT_MESSAGE) {
					iii = lin->bIsHighlighted ? 16 : (lin->bIsMe ? 2 : 1);
					mir_snprintf(szStyle, "\\f0\\cf%u\\ul0\\highlight0\\b%d\\i%d\\ul%d\\fs%u",
						iii + 1, F.lfWeight >= FW_BOLD ? 1 : 0, F.lfItalic, F.lfUnderline, 2 * abs(F.lfHeight) * 74 / g_chatApi.logPixelSY);
					str.Append(szStyle);
				}
				else {
					iii = lin->bIsHighlighted ? 16 : EventToIndex(lin);
					mir_snprintf(szStyle, "\\f0\\cf%u\\ul0\\highlight0\\b%d\\i%d\\ul%d\\fs%u",
						iii + 1, F.lfWeight >= FW_BOLD ? 1 : 0, F.lfItalic, F.lfUnderline, 2 * abs(F.lfHeight) * 74 / g_chatApi.logPixelSY);
					str.Append(szStyle);
				}
			}
			else str.Append(g_chatApi.Log_SetStyle(0));
			str.AppendChar(' ');

			// insert a TAB if necessary to put the timestamp in the right position
			if (g_Settings.dwIconFlags)
				str.Append("\\tab ");

			// insert timestamp
			if (g_Settings.bShowTime) {
				wchar_t szTimeStamp[30], szOldTimeStamp[30];

				wcsncpy_s(szTimeStamp, g_chatApi.MakeTimeStamp(g_Settings.pszTimeStamp, lin->time), _TRUNCATE);
				wcsncpy_s(szOldTimeStamp, g_chatApi.MakeTimeStamp(g_Settings.pszTimeStamp, si->LastTime), _TRUNCATE);
				if (!g_Settings.bShowTimeIfChanged || si->LastTime == 0 || mir_wstrcmp(szTimeStamp, szOldTimeStamp)) {
					si->LastTime = lin->time;
					Log_AppendRTF(streamData, TRUE, str, L"%s", szTimeStamp);
				}
				str.Append("\\tab ");
			}

			// Insert the nick
			if (lin->ptszNick && lin->iType == GC_EVENT_MESSAGE) {
				char pszIndicator[3] = "\0\0";
				int  crNickIndex = 0;

				if (g_Settings.bLogClassicIndicators || g_Settings.bColorizeNicksInLog)
					pszIndicator[0] = GetIndicator(si, lin->ptszNick, &crNickIndex);

				str.Append(g_chatApi.Log_SetStyle(lin->bIsMe ? 2 : 1));
				str.AppendChar(' ');

				if (g_Settings.bLogClassicIndicators)
					str.Append(pszIndicator);

				CMStringW pszTemp(lin->bIsMe ? g_Settings.pszOutgoingNick : g_Settings.pszIncomingNick);
				pszTemp.Replace(L"%n", L"%s");
				if (!lin->bIsMe) {
					if (g_Settings.bClickableNicks)
						pszTemp.Replace(L"%s", CLICKNICK_BEGIN L"%s" CLICKNICK_END);

					if (g_Settings.bColorizeNicksInLog && pszIndicator[0])
						str.AppendFormat("\\cf%u ", OPTIONS_FONTCOUNT + Utils::rtf_clrs.getCount() + streamData->crCount + crNickIndex);
				}

				if (g_Settings.bNewLineAfterNames)
					pszTemp.AppendChar('\n');

				Log_AppendRTF(streamData, TRUE, str, pszTemp, lin->ptszNick);
				str.AppendChar(' ');
			}

			// Insert the message
			str.Append(g_chatApi.Log_SetStyle(lin->bIsHighlighted ? 16 : EventToIndex(lin)));
			str.AppendChar(' ');

			streamData->lin = lin;
			AddEventToBuffer(str, streamData);
		}
		lin = lin->prev;
	}

	// ### RTF END
	if (streamData->bRedraw)
		str.Append("\\par}");
	else
		str.Append("}");
	return str.Detach();
}