summaryrefslogtreecommitdiff
path: root/protocols/MRA/MraRTFMsg.cpp
blob: e0c3a1d67fe9d993e8a080d4f4aa671b82548782 (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
#include "Mra.h"
#include "MraRTFMsg.h"



#define COLORTABLE_COUNT	8
#define RTF_COLORTBLCOLOURS "\\red255\\green0\\blue0;\\red0\\green255\\blue0;\\red0\\green0\\blue255;\\red255\\green0\\blue255;\\red255\\green255\\blue0;\\red0\\green255\\blue255;\\red0\\green0\\blue0;\\red255\\green255\\blue255;" 
static const LPSTR lpszColours[COLORTABLE_COUNT]=
{
	"red",
	"green",
	"blue",
	"magenta",
	"yellow",
	"cyan",
	"black",
	"white"
};



#define CRLF "\r\n"
#define PAR "\\par "


#define BB_COLOR_TAG	"[color="
#define SIMBOLS_COUNT	19
static const LPSTR lpszSimbols[SIMBOLS_COUNT]=
{
	"\r\n",
	"\\",
	"{",
	"}",
	"[b]",
	"[/b]",
	"[u]",
	"[/u]",
	"[i]",
	"[/i]",
	"[/color]",
	"[color=red]",
	"[color=green]",
	"[color=blue]",
	"[color=magenta]",
	"[color=yellow]",
	"[color=cyan]",
	"[color=black]",
	"[color=white]",
};

static const SIZE_T dwcSimbolsCount[SIMBOLS_COUNT]=
{
	2,
	1,
	1,
	1,
	3,
	4,
	3,
	4,
	3,
	4,
	8,
	11,
	13,
	12,
	15,
	14,
	12,
	13,
	13,
};

static const LPSTR lpszRTFTags[SIMBOLS_COUNT]=
{
	"\\par",
	"\\\\",
	"\\{",
	"\\}",
	"{\\b ",
	"}",
	"{\\ul ",
	"}",
	"{\\i ",
	"}",
	"}",
	"{\\cf2 ",
	"{\\cf3 ",
	"{\\cf4 ",
	"{\\cf5 ",
	"{\\cf6 ",
	"{\\cf7 ",
	"{\\cf8 ",
	"{\\cf9 ",
};

static const SIZE_T dwcRTFTagsCount[SIMBOLS_COUNT]=
{
	4,
	2,
	2,
	2,
	4,
	1,
	5,
	1,
	4,
	1,
	1,
	6,
	6,
	6,
	6,
	6,
	6,
	6,
	6,
};


DWORD MraTextToRTFData(LPSTR lpszMessage,SIZE_T dwMessageSize,LPSTR lpszMessageConverted,SIZE_T dwMessageConvertedBuffSize,SIZE_T *pdwMessageConvertedSize);


BOOL MraIsMessageFlashAnimation(LPWSTR lpwszMessage,SIZE_T dwMessageSize)
{
	BOOL bRet=FALSE;
	LPWSTR lpwszFounded;

	dwMessageSize*=sizeof(WCHAR);
	lpwszFounded=(LPWSTR)MemoryFind(0,lpwszMessage,dwMessageSize,L"<SMILE>id=flas",28);
	if (lpwszFounded)
	{
		if (MemoryFind(((lpwszFounded-lpwszMessage)+32),lpwszMessage,dwMessageSize,L"'</SMILE>",18)) bRet=TRUE;
	}
return(bRet);
}



DWORD MraTextToRTFData(LPSTR lpszMessage,SIZE_T dwMessageSize,LPSTR lpszMessageConverted,SIZE_T dwMessageConvertedBuffSize,SIZE_T *pdwMessageConvertedSize)
{
	DWORD dwRetErrorCode=NO_ERROR;

	if (lpszMessage && dwMessageSize && lpszMessageConverted && dwMessageConvertedBuffSize)
	{
		BYTE tm,bCurByte;
		LPSTR lpszMessageConvertedCur,lpszMessageConvertedMax;
		SIZE_T i;

		lpszMessageConvertedCur=lpszMessageConverted;
		lpszMessageConvertedMax=(lpszMessageConverted+dwMessageConvertedBuffSize);
		for(i=0;(i<dwMessageSize && (lpszMessageConvertedMax-lpszMessageConvertedCur)>3);i++)
		{
			//lpszMessageConvertedCur+=mir_snprintf(lpszMessageConvertedCur,8,"\\'%x",(*((BYTE*)lpszMessage+i)));

			(*((WORD*)lpszMessageConvertedCur))=(*((WORD*)"\\'"));
			bCurByte=(*((BYTE*)lpszMessage+i));
			tm=(bCurByte>>4)&0xf;
			lpszMessageConvertedCur[2]=(tm>9)? ('a'+tm-10):('0'+tm);

			tm=bCurByte&0xf;
			lpszMessageConvertedCur[3]=(tm>9)? ('a'+tm-10):('0'+tm);
			lpszMessageConvertedCur+=4;
			(*((BYTE*)lpszMessageConvertedCur))=0;
		}
		if (pdwMessageConvertedSize) (*pdwMessageConvertedSize)=(lpszMessageConvertedCur-lpszMessageConverted);
	}else{
		if (pdwMessageConvertedSize) (*pdwMessageConvertedSize)=0;
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


DWORD MraSybolsToRTFTags(DWORD dwFlags,LPSTR lpszMessage,SIZE_T dwMessageSize,LPSTR lpszMessageConverted,SIZE_T dwMessageConvertedBuffSize,SIZE_T *pdwMessageConvertedSize)
{
	DWORD dwRetErrorCode=NO_ERROR;
	LPSTR lpszFounded[SIMBOLS_COUNT],lpszMessageConvertedCur,lpszMessageCur,lpszMessageCurPrev,lpszMessageConvertedMax;
	SIZE_T i,dwFirstFoundedIndex=0,dwFoundedCount=0,dwMemPartToCopy;

#ifdef _DEBUG //check table
	for(i=0;i<SIMBOLS_COUNT;i++)
	{
		if (lstrlenA(lpszSimbols[i])!=dwcSimbolsCount[i]) DebugBreak();
		if (lstrlenA(lpszRTFTags[i])!=dwcRTFTagsCount[i]) DebugBreak();
	}
#endif

	lpszMessageCurPrev=lpszMessage;
	lpszMessageConvertedCur=lpszMessageConverted;
	lpszMessageConvertedMax=(lpszMessageConverted+dwMessageConvertedBuffSize);
	for(i=0;i<SIMBOLS_COUNT;i++)
	{// loking for first time
		lpszFounded[i]=(LPSTR)MemoryFind((lpszMessageCurPrev-lpszMessage),lpszMessage,dwMessageSize,lpszSimbols[i],dwcSimbolsCount[i]);
		if (lpszFounded[i]) dwFoundedCount++;
	}

	while(dwFoundedCount)
	{
		for(i=0;i<SIMBOLS_COUNT;i++)
		{// looking for first to replace
			if (lpszFounded[i] && (lpszFounded[i]<lpszFounded[dwFirstFoundedIndex] || lpszFounded[dwFirstFoundedIndex]==NULL)) dwFirstFoundedIndex=i;
		}

		if (lpszFounded[dwFirstFoundedIndex])
		{// founded
			dwMemPartToCopy=(lpszFounded[dwFirstFoundedIndex]-lpszMessageCurPrev);
			if (lpszMessageConvertedMax>(lpszMessageConvertedCur+(dwMemPartToCopy+dwcRTFTagsCount[dwFirstFoundedIndex])))
			{
				//memmove(lpszMessageConvertedCur,lpszMessageCurPrev,dwMemPartToCopy);lpszMessageConvertedCur+=dwMemPartToCopy;
				MraTextToRTFData(lpszMessageCurPrev,dwMemPartToCopy,lpszMessageConvertedCur,(lpszMessageConvertedMax-lpszMessageConvertedCur),&i);lpszMessageConvertedCur+=i;
				memmove(lpszMessageConvertedCur,lpszRTFTags[dwFirstFoundedIndex],dwcRTFTagsCount[dwFirstFoundedIndex]);lpszMessageConvertedCur+=dwcRTFTagsCount[dwFirstFoundedIndex];
				lpszMessageCurPrev=(lpszFounded[dwFirstFoundedIndex]+dwcSimbolsCount[dwFirstFoundedIndex]);

				for(i=0;i<SIMBOLS_COUNT;i++)
				{// loking for next time
					if (lpszFounded[i] && lpszFounded[i]<lpszMessageCurPrev)
					{
						dwFoundedCount--;// âû÷èòàåì òóò, ÷òîáû ó÷åñòü ñõîæèå ñìàéëû: "):-(" è ":-("
						lpszFounded[i]=(LPSTR)MemoryFind((lpszMessageCurPrev-lpszMessage),lpszMessage,dwMessageSize,lpszSimbols[i],dwcSimbolsCount[i]);
						if (lpszFounded[i]) dwFoundedCount++;
					}
				}
			}else{
				dwRetErrorCode=ERROR_BUFFER_OVERFLOW;
				DebugBreak();
				break;
			}
		}else{// ñþäà ïî èäåå íèêîãäà íå ïîïàä¸ì, íà âñÿêèé ñëó÷àé.
			DebugBreak();
			break;
		}
	}
	lpszMessageCur=(lpszMessage+dwMessageSize);
	//memmove(lpszMessageConvertedCur,lpszMessageCurPrev,(lpszMessageCur-lpszMessageCurPrev));lpszMessageConvertedCur+=(lpszMessageCur-lpszMessageCurPrev);
	MraTextToRTFData(lpszMessageCurPrev,(lpszMessageCur-lpszMessageCurPrev),lpszMessageConvertedCur,(lpszMessageConvertedMax-lpszMessageConvertedCur),&i);lpszMessageConvertedCur+=i;
	(*((WORD*)lpszMessageConvertedCur))=0;

	if (pdwMessageConvertedSize) (*pdwMessageConvertedSize)=(lpszMessageConvertedCur-lpszMessageConverted);

return(dwRetErrorCode);
}


DWORD MraConvertToRTFW(LPWSTR lpwszMessage,SIZE_T dwMessageSize,LPSTR lpszMessageRTF,SIZE_T dwMessageRTFBuffSize,SIZE_T *pdwMessageRTFSize)
{
	DWORD dwRetErrorCode;

	if (lpwszMessage && lpszMessageRTF)
	{
		if ((dwMessageSize+1024)<=dwMessageRTFBuffSize)
		{
			LPSTR lpszMessage;

			lpszMessage=(LPSTR)MEMALLOC(dwMessageSize+32);
			if (lpszMessage)
			{
				LPSTR lpszMessageRTFCur=lpszMessageRTF;
				SIZE_T dwtm;
				DWORD dwRTFFontColour,dwFontSize;
				LOGFONT lf={0};

				WideCharToMultiByte(MRA_CODE_PAGE,0,lpwszMessage,dwMessageSize,lpszMessage,(dwMessageSize+32),NULL,NULL);

				dwRTFFontColour=DB_Mra_GetDword(NULL,"RTFFontColour",MRA_DEFAULT_RTF_FONT_COLOUR);
				if (DB_Mra_GetContactSettingBlob(NULL,"RTFFont",&lf,sizeof(LOGFONT),NULL))
				{
					/*HDC hDC=GetDC(NULL);// kegl
					dwFontSize=-MulDiv(lf.lfHeight,72,GetDeviceCaps(hDC,LOGPIXELSY));
					ReleaseDC(NULL,hDC);*/
				}else{
					HDC hDC=GetDC(NULL);// kegl
					lf.lfCharSet=MRA_DEFAULT_RTF_FONT_CHARSET;
					lf.lfHeight=-MulDiv(MRA_DEFAULT_RTF_FONT_SIZE,GetDeviceCaps(hDC,LOGPIXELSY),72);
					lstrcpynW(lf.lfFaceName,MRA_DEFAULT_RTF_FONT_NAME,LF_FACESIZE);
					ReleaseDC(NULL,hDC);
				}
				dwFontSize=((-lf.lfHeight)+(((-lf.lfHeight)+4)/8));
/* mail agent: ïðèâåò
{\rtf1\ansi\ansicpg1251\deff0\deflang1049{\fonttbl{\f0\fnil\fcharset204 Tahoma;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs18\'ef\'f0\'e8\'e2\'e5\'f2\par
}

{\rtf1\ansi\ansicpg1251\deff0\deflang1049{\fonttbl{\f0\fnil\fcharset204 Tahoma;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\f0\fs18 1\par
2\par
3\par
}
*/
				lpszMessageRTFCur+=mir_snprintf(lpszMessageRTFCur,(dwMessageRTFBuffSize-((SIZE_T)lpszMessageRTFCur-(SIZE_T)lpszMessageRTF)),"{\\rtf1\\ansi\\ansicpg1251\\deff0\\deflang1049{\\fonttbl{\\f0\\fnil\\fcharset%lu %s;}}\r\n",lf.lfCharSet,lf.lfFaceName);

				if (MemoryFind(0,lpszMessage,dwMessageSize,BB_COLOR_TAG,(sizeof(BB_COLOR_TAG)-1)))
				{//BB_COLOR_TAG// äîáàâëÿåì òàáëèöó ñ öâåòàìè, òîëüêî åñëè òåêñò ïîäêðàøåí
					lpszMessageRTFCur+=mir_snprintf(lpszMessageRTFCur,(dwMessageRTFBuffSize-((SIZE_T)lpszMessageRTFCur-(SIZE_T)lpszMessageRTF)),"{\\colortbl;\\red%lu\\green%lu\\blue%lu;%s}\r\n",(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtBlue,(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtGreen,(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtRed,RTF_COLORTBLCOLOURS);
				}else{
					lpszMessageRTFCur+=mir_snprintf(lpszMessageRTFCur,(dwMessageRTFBuffSize-((SIZE_T)lpszMessageRTFCur-(SIZE_T)lpszMessageRTF)),"{\\colortbl;\\red%lu\\green%lu\\blue%lu;}\r\n",(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtBlue,(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtGreen,(*((RGBTRIPLE*)&dwRTFFontColour)).rgbtRed);
				}

				{
					LPSTR	lpszNotfink="",
							lpszBold=((lf.lfWeight==FW_BOLD)? "\\b1":lpszNotfink),
							lpszItalic=(lf.lfItalic? "\\i1":lpszNotfink),
							lpszUnderline=(lf.lfUnderline? "\\ul1":lpszNotfink),
							lpszStrikeOut=(lf.lfStrikeOut? "\\strike1":lpszNotfink);
					lpszMessageRTFCur+=mir_snprintf(lpszMessageRTFCur,(dwMessageRTFBuffSize-((SIZE_T)lpszMessageRTFCur-(SIZE_T)lpszMessageRTF)),"\\viewkind4\\uc1\\pard\\cf1\\f0\\fs%lu%s%s%s%s",dwFontSize,lpszBold,lpszItalic,lpszUnderline,lpszStrikeOut);
				}

				if ((dwRetErrorCode=MraSybolsToRTFTags(0,lpszMessage,dwMessageSize,lpszMessageRTFCur,(dwMessageRTFBuffSize-(lpszMessageRTFCur-lpszMessageRTF)),&dwtm))==NO_ERROR)
				{
					lpszMessageRTFCur+=dwtm;
					if ((lpszMessageRTF+dwMessageRTFBuffSize)>=(lpszMessageRTFCur+sizeof(PAR)+sizeof(CRLF)+2))
					{
						memmove(lpszMessageRTFCur,PAR,sizeof(PAR));lpszMessageRTFCur+=(sizeof(PAR)-1);
						memmove(lpszMessageRTFCur,CRLF,sizeof(CRLF));lpszMessageRTFCur+=(sizeof(CRLF)-1);
						memmove(lpszMessageRTFCur,"}",2);lpszMessageRTFCur+=2;
						if (pdwMessageRTFSize) (*pdwMessageRTFSize)=(lpszMessageRTFCur-lpszMessageRTF);
						DebugPrintCRLFA(lpszMessageRTF);
						dwRetErrorCode=NO_ERROR;
					}else{
						if (pdwMessageRTFSize) (*pdwMessageRTFSize)=dwMessageRTFBuffSize+1024;
						dwRetErrorCode=ERROR_BUFFER_OVERFLOW;
						DebugBreak();
					}
				}
				MEMFREE(lpszMessage);
			}
		}else{
			if (pdwMessageRTFSize) (*pdwMessageRTFSize)=dwMessageSize;
			dwRetErrorCode=ERROR_BUFFER_OVERFLOW;
			DebugBreak();
		}
	}else{
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}