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
|
/*
IEView Plugin for Miranda IM
Copyright (C) 2005-2010 Piotr Piastucki
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.
*/
#include "ieview_common.h"
int HTMLBuilder::mimFlags = 0;
HTMLBuilder::HTMLBuilder()
{
lastIEViewEvent.cbSize = sizeof(IEVIEWEVENT);
lastIEViewEvent.iType = IEE_LOG_MEM_EVENTS;
lastIEViewEvent.codepage = CP_ACP;
lastIEViewEvent.pszProto = NULL;
lastIEViewEvent.count = 0;
lastIEViewEvent.dwFlags = 0;
lastIEViewEvent.hContact = NULL;
lastIEViewEvent.hwnd = NULL;
lastIEViewEvent.eventData = NULL;
}
HTMLBuilder::~HTMLBuilder()
{
if (lastIEViewEvent.pszProto != NULL)
mir_free((void*)lastIEViewEvent.pszProto);
}
bool HTMLBuilder::encode(MCONTACT hContact, const char *proto, const wchar_t *text, wchar_t **output, int *outputSize, int level, int flags, bool isSent)
{
TextToken *token = NULL, *token2;
switch (level) {
case 0:
if (flags & ENF_CHAT_FORMATTING) {
token = TextToken::tokenizeChatFormatting(text);
break;
}
level++;
case 1:
if ((Options::getGeneralFlags()&Options::GENERAL_ENABLE_BBCODES) && (flags & ENF_BBCODES)) {
token = TextToken::tokenizeBBCodes(text);
break;
}
level++;
case 2:
token = TextToken::tokenizeLinks(text);
break;
case 3:
if ((flags & ENF_SMILEYS) || ((Options::getGeneralFlags() & Options::GENERAL_SMILEYINNAMES) && (flags & ENF_NAMESMILEYS)))
token = TextToken::tokenizeSmileys(hContact, proto, text, isSent);
break;
}
if (token != NULL) {
for (token2 = token; token != NULL; token = token2) {
bool skip = false;
token2 = token->getNext();
if (token->getType() == TextToken::TEXT)
skip = encode(hContact, proto, token->getTextW(), output, outputSize, level + 1, flags, isSent);
if (!skip)
token->toString(output, outputSize);
delete token;
}
return true;
}
return false;
}
wchar_t* HTMLBuilder::encode(MCONTACT hContact, const char *proto, const wchar_t *text, int flags, bool isSent)
{
int outputSize;
wchar_t *output = NULL;
if (text != NULL)
encode(hContact, proto, text, &output, &outputSize, 0, flags, isSent);
return output;
}
char* HTMLBuilder::encodeUTF8(MCONTACT hContact, const char *proto, const wchar_t *wtext, int flags, bool isSent)
{
char *outputStr = NULL;
if (wtext != NULL) {
wchar_t *output = encode(hContact, proto, wtext, flags, isSent);
outputStr = mir_utf8encodeT(output);
if (output != NULL)
free(output);
}
return outputStr;
}
char* HTMLBuilder::encodeUTF8(MCONTACT hContact, const char *proto, const char *text, int flags, bool isSent)
{
char *outputStr = NULL;
if (text != NULL) {
wchar_t *wtext = mir_a2t(text);
outputStr = encodeUTF8(hContact, proto, wtext, flags, isSent);
mir_free(wtext);
}
return outputStr;
}
char* HTMLBuilder::encodeUTF8(MCONTACT hContact, const char *proto, const char *text, int cp, int flags, bool isSent)
{
char *outputStr = NULL;
if (text != NULL) {
wchar_t *wtext = mir_a2t_cp(text, cp);
outputStr = encodeUTF8(hContact, proto, wtext, flags, isSent);
mir_free(wtext);
}
return outputStr;
}
char* HTMLBuilder::getProto(MCONTACT hContact)
{
return mir_strdup(GetContactProto(hContact));
}
char* HTMLBuilder::getProto(const char *proto, MCONTACT hContact)
{
if (proto != NULL)
return mir_strdup(proto);
return mir_strdup(GetContactProto(hContact));
}
char* HTMLBuilder::getRealProto(MCONTACT hContact)
{
if (hContact == NULL)
return NULL;
char *szProto = mir_strdup(GetContactProto(hContact));
if (szProto != NULL && !strcmp(szProto, META_PROTO)) {
hContact = db_mc_getMostOnline(hContact);
if (hContact != NULL) {
mir_free(szProto);
szProto = mir_strdup(GetContactProto(hContact));
}
}
return szProto;
}
char *HTMLBuilder::getRealProto(MCONTACT hContact, const char *szProto)
{
if (szProto != NULL && !strcmp(szProto, META_PROTO)) {
hContact = db_mc_getMostOnline(hContact);
if (hContact != NULL)
return mir_strdup(GetContactProto(hContact));
}
return mir_strdup(szProto);
}
MCONTACT HTMLBuilder::getRealContact(MCONTACT hContact)
{
char *szProto = GetContactProto(hContact);
if (szProto != NULL && !strcmp(szProto, META_PROTO))
hContact = db_mc_getMostOnline(hContact);
return hContact;
}
int HTMLBuilder::getLastEventType()
{
return iLastEventType;
}
void HTMLBuilder::setLastEventType(int t)
{
iLastEventType = t;
}
DWORD HTMLBuilder::getLastEventTime()
{
return lastEventTime;
}
void HTMLBuilder::setLastEventTime(DWORD t)
{
lastEventTime = t;
}
bool HTMLBuilder::isSameDate(time_t time1, time_t time2)
{
struct tm tm_t1, tm_t2;
tm_t1 = *localtime((time_t *)(&time1));
tm_t2 = *localtime((time_t *)(&time2));
if (tm_t1.tm_year == tm_t2.tm_year && tm_t1.tm_mon == tm_t2.tm_mon
&& tm_t1.tm_mday == tm_t2.tm_mday) {
return true;
}
return false;
}
void HTMLBuilder::getUINs(MCONTACT hContact, char *&uinIn, char *&uinOut)
{
CONTACTINFO ci;
char buf[128];
char *szProto;
hContact = getRealContact(hContact);
szProto = getProto(hContact);
memset(&ci, 0, sizeof(ci));
ci.cbSize = sizeof(ci);
ci.hContact = hContact;
ci.szProto = szProto;
ci.dwFlag = CNF_UNIQUEID;
buf[0] = 0;
if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)& ci)) {
switch (ci.type) {
case CNFT_ASCIIZ:
strncpy_s(buf, (char*)ci.pszVal, _TRUNCATE);
mir_free(ci.pszVal);
break;
case CNFT_DWORD:
mir_snprintf(buf, SIZEOF(buf), "%u", ci.dVal);
break;
}
}
uinIn = mir_utf8encode(buf);
ci.hContact = NULL;
buf[0] = 0;
if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)& ci)) {
switch (ci.type) {
case CNFT_ASCIIZ:
strncpy_s(buf, (char*)ci.pszVal, _TRUNCATE);
mir_free(ci.pszVal);
break;
case CNFT_DWORD:
mir_snprintf(buf, SIZEOF(buf), "%u", ci.dVal);
break;
}
}
uinOut = mir_utf8encode(buf);
mir_free(szProto);
}
wchar_t *HTMLBuilder::getContactName(MCONTACT hContact, const char *szProto)
{
CONTACTINFO ci = { 0 };
wchar_t *szName = NULL;
ci.cbSize = sizeof(ci);
ci.hContact = hContact;
ci.szProto = (char *)szProto;
ci.dwFlag = CNF_DISPLAY | CNF_UNICODE;
if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci)) {
if (ci.type == CNFT_ASCIIZ) {
if (ci.pszVal) {
szName = mir_tstrdup(ci.pszVal);
mir_free(ci.pszVal);
}
}
}
if (szName != NULL) return szName;
ci.dwFlag = CNF_UNIQUEID;
if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci)) {
if (ci.type == CNFT_ASCIIZ) {
if (ci.pszVal) {
szName = mir_tstrdup(ci.pszVal);
mir_free(ci.pszVal);
}
}
}
if (szName != NULL) return szName;
TCHAR *szNameStr = pcli->pfnGetContactDisplayName(hContact, 0);
if (szNameStr != NULL)
return mir_tstrdup(szNameStr);
return mir_tstrdup(TranslateT("(Unknown Contact)"));
}
char *HTMLBuilder::getEncodedContactName(MCONTACT hContact, const char* szProto, const char* szSmileyProto)
{
char *szName = NULL;
wchar_t *name = getContactName(hContact, szProto);
if (name != NULL) {
szName = encodeUTF8(hContact, szSmileyProto, name, ENF_NAMESMILEYS, true);
mir_free(name);
return szName;
}
return encodeUTF8(hContact, szSmileyProto, TranslateT("(Unknown Contact)"), ENF_NAMESMILEYS, true);
}
void HTMLBuilder::appendEventNew(IEView *view, IEVIEWEVENT *event)
{
setLastIEViewEvent(event);
appendEvent(view, event);
}
void HTMLBuilder::appendEventOld(IEView *view, IEVIEWEVENT *event)
{
IEVIEWEVENTDATA* eventData;
IEVIEWEVENTDATA* prevEventData = NULL;
MEVENT hDbEvent = event->hDbEventFirst;
event->hDbEventFirst = NULL;
ptrA szProto;
if (event->cbSize >= IEVIEWEVENT_SIZE_V3 && event->pszProto != NULL)
szProto = mir_strdup(event->pszProto);
else
szProto = getProto(event->hContact);
IEVIEWEVENT newEvent = { sizeof(IEVIEWEVENT) };
newEvent.iType = IEE_LOG_MEM_EVENTS;
newEvent.codepage = CP_ACP;
if (event->cbSize >= IEVIEWEVENT_SIZE_V2)
newEvent.codepage = event->codepage;
newEvent.pszProto = szProto;
newEvent.count = 0;
newEvent.dwFlags = event->dwFlags;
newEvent.hContact = event->hContact;
newEvent.hwnd = event->hwnd;
newEvent.eventData = NULL;
for (int eventIdx = 0; hDbEvent != NULL && (eventIdx < event->count || event->count == -1); eventIdx++) {
DBEVENTINFO dbei = { sizeof(dbei) };
dbei.cbBlob = db_event_getBlobSize(hDbEvent);
if (dbei.cbBlob == 0xFFFFFFFF) {
hDbEvent = db_event_next(event->hContact, hDbEvent);
continue;
}
dbei.pBlob = (PBYTE)malloc(dbei.cbBlob);
db_event_get(hDbEvent, &dbei);
if (!(dbei.flags & DBEF_SENT) && (dbei.eventType == EVENTTYPE_MESSAGE || dbei.eventType == EVENTTYPE_URL)) {
db_event_markRead(event->hContact, hDbEvent);
CallService(MS_CLIST_REMOVEEVENT, event->hContact, (LPARAM)hDbEvent);
}
if (!isDbEventShown(&dbei)) {
free(dbei.pBlob);
hDbEvent = db_event_next(event->hContact, hDbEvent);
continue;
}
eventData = new IEVIEWEVENTDATA;
eventData->cbSize = sizeof(IEVIEWEVENTDATA);
eventData->dwFlags = IEEDF_UNICODE_TEXT | IEEDF_UNICODE_NICK | IEEDF_UNICODE_TEXT2 |
(dbei.flags & DBEF_READ ? IEEDF_READ : 0) | (dbei.flags & DBEF_SENT ? IEEDF_SENT : 0) | (dbei.flags & DBEF_RTL ? IEEDF_RTL : 0);
if (event->dwFlags & IEEF_RTL)
eventData->dwFlags |= IEEDF_RTL;
eventData->time = dbei.timestamp;
eventData->pszNickW = NULL;
eventData->pszTextW = NULL;
eventData->pszText2W = NULL;
if (dbei.flags & DBEF_SENT) {
eventData->pszNickW = getContactName(NULL, szProto);
eventData->bIsMe = TRUE;
}
else {
eventData->pszNickW = getContactName(event->hContact, szProto);
eventData->bIsMe = FALSE;
}
if (dbei.eventType == EVENTTYPE_MESSAGE || dbei.eventType == EVENTTYPE_URL || Utils::DbEventIsForMsgWindow(&dbei)) {
eventData->pszTextW = DbGetEventTextW(&dbei, newEvent.codepage);
if (dbei.eventType == EVENTTYPE_MESSAGE)
eventData->iType = IEED_EVENT_MESSAGE;
else if (dbei.eventType == EVENTTYPE_URL)
eventData->iType = IEED_EVENT_URL;
else
eventData->iType = IEED_EVENT_STATUSCHANGE;
}
else if (dbei.eventType == EVENTTYPE_FILE) {
//blob is: sequenceid(DWORD),filename(ASCIIZ),description(ASCIIZ)
char* filename = ((char *)dbei.pBlob) + sizeof(DWORD);
char* descr = filename + mir_strlen(filename) + 1;
eventData->ptszText = DbGetEventStringT(&dbei, filename);
if (*descr != '\0')
eventData->ptszText2 = DbGetEventStringT(&dbei, descr);
eventData->iType = IEED_EVENT_FILE;
}
else if (dbei.eventType == EVENTTYPE_AUTHREQUEST) {
//blob is: uin(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ)
eventData->ptszText = mir_tstrdup(TranslateT(" requested authorization"));
eventData->ptszNick = DbGetEventStringT(&dbei, (char *)dbei.pBlob + 8);
eventData->iType = IEED_EVENT_SYSTEM;
}
else if (dbei.eventType == EVENTTYPE_ADDED) {
//blob is: uin(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ)
eventData->ptszText = mir_tstrdup(TranslateT(" was added."));
eventData->ptszNick = DbGetEventStringT(&dbei, (char *)dbei.pBlob + 8);
eventData->iType = IEED_EVENT_SYSTEM;
}
else { // custom event
eventData->pszTextW = DbGetEventTextW(&dbei, newEvent.codepage);
eventData->iType = IEED_EVENT_MESSAGE;
}
free(dbei.pBlob);
eventData->next = NULL;
if (prevEventData != NULL)
prevEventData->next = eventData;
else
newEvent.eventData = eventData;
prevEventData = eventData;
newEvent.count++;
event->hDbEventFirst = hDbEvent;
hDbEvent = db_event_next(event->hContact, hDbEvent);
}
appendEventNew(view, &newEvent);
for (IEVIEWEVENTDATA* eventData2 = newEvent.eventData; eventData2 != NULL; eventData2 = eventData) {
eventData = eventData2->next;
mir_free((void*)eventData2->pszTextW);
mir_free((void*)eventData2->pszText2W);
mir_free((void*)eventData2->pszNickW);
delete eventData2;
}
}
ProtocolSettings* HTMLBuilder::getSRMMProtocolSettings(const char *protocolName)
{
ProtocolSettings *protoSettings = Options::getProtocolSettings(protocolName);
if (protoSettings == NULL || !protoSettings->isSRMMEnable())
protoSettings = Options::getProtocolSettings();
return protoSettings;
}
ProtocolSettings* HTMLBuilder::getSRMMProtocolSettings(MCONTACT hContact)
{
return getSRMMProtocolSettings(ptrA(getRealProto(hContact)));
}
ProtocolSettings* HTMLBuilder::getHistoryProtocolSettings(const char *protocolName)
{
ProtocolSettings *protoSettings = Options::getProtocolSettings(protocolName);
if (protoSettings == NULL || !protoSettings->isHistoryEnable())
protoSettings = Options::getProtocolSettings();
return protoSettings;
}
ProtocolSettings* HTMLBuilder::getHistoryProtocolSettings(MCONTACT hContact)
{
if (hContact != NULL)
return getHistoryProtocolSettings(ptrA(getRealProto(hContact)));
return Options::getProtocolSettings();
}
ProtocolSettings* HTMLBuilder::getChatProtocolSettings(const char *protocolName)
{
ProtocolSettings *protoSettings = Options::getProtocolSettings(protocolName);
if (protoSettings == NULL || !protoSettings->isChatEnable())
protoSettings = Options::getProtocolSettings();
return protoSettings;
}
ProtocolSettings* HTMLBuilder::getChatProtocolSettings(MCONTACT hContact)
{
return getChatProtocolSettings(ptrA(getRealProto(hContact)));
}
void HTMLBuilder::setLastIEViewEvent(IEVIEWEVENT *event)
{
lastIEViewEvent.cbSize = sizeof(IEVIEWEVENT);
lastIEViewEvent.iType = event->iType;
lastIEViewEvent.codepage = CP_ACP;
if (event->cbSize >= IEVIEWEVENT_SIZE_V2)
lastIEViewEvent.codepage = event->codepage;
lastIEViewEvent.count = 0;
lastIEViewEvent.dwFlags = event->dwFlags;
lastIEViewEvent.hContact = event->hContact;
lastIEViewEvent.hwnd = event->hwnd;
lastIEViewEvent.eventData = NULL;
if (lastIEViewEvent.pszProto != NULL)
mir_free((void*)lastIEViewEvent.pszProto);
if (event->cbSize >= IEVIEWEVENT_SIZE_V3 && event->pszProto != NULL)
lastIEViewEvent.pszProto = mir_strdup(event->pszProto);
else
lastIEViewEvent.pszProto = getProto(event->hContact);
}
void HTMLBuilder::clear(IEView *view, IEVIEWEVENT *event)
{
if (event != NULL) {
setLastIEViewEvent(event);
if (lastIEViewEvent.pszProto != NULL || event->hContact == NULL)
buildHead(view, &lastIEViewEvent);
}
}
|