summaryrefslogtreecommitdiff
path: root/protocols/Telegram/src/utils.cpp
blob: 48445a4a45de15734fb281c29e2c2a89804a383d (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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

struct
{
	const wchar_t *begin, *end;
	unsigned len1, len2;
}
static bbCodes[] =
{
	{ L"[b]", L"[/b]", 3, 4 },
	{ L"[i]", L"[/i]", 3, 4 },
	{ L"[s]", L"[/s]", 3, 4 },
	{ L"[u]", L"[/u]", 3, 4 },
};

TD::object_ptr<TD::formattedText> formatBbcodes(const char *pszText)
{
	auto res = TD::make_object<TD::formattedText>();
	if (mir_strlen(pszText)) {
		std::wstring str = Utf2T(pszText).get();
		for (auto &it : bbCodes) {
			while (true) {
				int i1 = (int)str.find(it.begin);
				if (i1 == str.npos)
					break;

				int i2 = (int)str.find(it.end, i1);
				if (i2 == str.npos)
					break;

				for (auto &jt : res->entities_) {
					if (jt->offset_ > i1)
						jt->offset_ -= it.len1;
					if (jt->offset_ > i2)
						jt->offset_ -= it.len2;
				}

				str.erase(i2, it.len2); i2 -= it.len1;
				str.erase(i1, it.len1);

				TD::object_ptr<TD::TextEntityType> pNew;
				switch (it.begin[1]) {
				case 'b': pNew = TD::make_object<TD::textEntityTypeBold>(); break;
				case 'i': pNew = TD::make_object<TD::textEntityTypeItalic>(); break;
				case 's': pNew = TD::make_object<TD::textEntityTypeStrikethrough>(); break;
				case 'u': pNew = TD::make_object<TD::textEntityTypeUnderline>(); break;
				}

				res->entities_.push_back(TD::make_object<TD::textEntity>(TD::int32(i1), TD::int32(i2 - i1), std::move(pNew)));
			}
		}
		res->text_ = T2Utf(str.c_str()).get();
	}
	
	return res;
}

static CMStringA getFormattedText(TD::object_ptr<TD::formattedText> &pText)
{
	if (pText->get_id() == TD::formattedText::ID) {
		CMStringW ret(Utf2T(pText->text_.c_str()));
		unsigned offset = 0;

		for (auto &it : pText->entities_) {
			int iCode;
			switch (it->type_->get_id()) {
			case TD::textEntityTypeBold::ID: iCode = 0; break;
			case TD::textEntityTypeItalic::ID: iCode = 1; break;
			case TD::textEntityTypeStrikethrough::ID: iCode = 2; break;
			case TD::textEntityTypeUnderline::ID: iCode = 3; break;
			default:
				continue;
			}

			auto &bb = bbCodes[iCode];
			ret.Insert(offset + it->offset_ + it->length_, bb.end);
			ret.Insert(offset + it->offset_, bb.begin);
			offset += bb.len1 + bb.len2;
		}
		return T2Utf(ret).get();
	}
	
	return "";
}

/////////////////////////////////////////////////////////////////////////////////////////

CMStringA msg2id(TD::int53 chatId, TD::int53 msgId)
{
	return CMStringA(FORMAT, "%lld_%lld", chatId, msgId);
}

CMStringA msg2id(const TD::message *pMsg)
{
	return CMStringA(FORMAT, "%lld_%lld", pMsg->chat_id_, pMsg->id_);
}

TD::int53 dbei2id(const DBEVENTINFO &dbei)
{
	if (dbei.szId == nullptr)
		return -1;

	auto *p = strchr(dbei.szId, '_');
	return _atoi64(p ? p + 1 : dbei.szId);
}

/////////////////////////////////////////////////////////////////////////////////////////

const char *getName(const TD::usernames *pName)
{
	return (pName == nullptr) ? TranslateU("none") : pName->editable_username_.c_str();
}

TD::object_ptr<TD::inputFileLocal> makeFile(const wchar_t *pwszFilename)
{
	std::string szPath = T2Utf(pwszFilename);
	return TD::make_object<TD::inputFileLocal>(std::move(szPath));
}

TG_FILE_REQUEST::Type AutoDetectType(const wchar_t *pwszFilename)
{
	if (ProtoGetAvatarFileFormat(pwszFilename) != PA_FORMAT_UNKNOWN)
		return TG_FILE_REQUEST::PICTURE;

	CMStringW path(pwszFilename);
	int idx = path.ReverseFind('.');
	if (idx == -1 || path.Find('\\', idx) != -1)
		return TG_FILE_REQUEST::FILE;

	auto wszExt = path.Right(path.GetLength() - idx);
	wszExt.MakeLower();
	if (wszExt == L"mp4" || wszExt == L"webm")
		return TG_FILE_REQUEST::VIDEO;
	else if (wszExt == L"mp3" || wszExt == "ogg" || wszExt == "oga" || wszExt == "wav")
		return TG_FILE_REQUEST::VOICE;

	return TG_FILE_REQUEST::FILE;
}

CMStringW TG_USER::getDisplayName() const
{
	if (hContact != INVALID_CONTACT_ID)
		return Clist_GetContactDisplayName(hContact, 0);

	if (!wszFirstName.IsEmpty())
		return (wszLastName.IsEmpty()) ? wszFirstName : wszFirstName + L" " + wszLastName;

	return wszNick;
}

MCONTACT CTelegramProto::GetRealContact(const TG_USER *pUser)
{
	return (pUser->hContact != 0) ? pUser->hContact : m_iSavedMessages;
}

TG_USER* CTelegramProto::GetSender(const TD::MessageSender *pSender)
{
	switch (pSender->get_id()) {
	case TD::messageSenderChat::ID:
		return FindChat(((TD::messageSenderChat *)pSender)->chat_id_);

	case TD::messageSenderUser::ID:
		return FindUser(((TD::messageSenderUser *)pSender)->user_id_);
	}

	return nullptr;
}

/////////////////////////////////////////////////////////////////////////////////////////

bool CTelegramProto::CheckSearchUser(TG_USER *pUser)
{
	auto pSearchId = std::find(m_searchIds.begin(), m_searchIds.end(), pUser->chatId);
	if (pSearchId == m_searchIds.end())
		return false;

	ReportSearchUser(pUser);

	m_searchIds.erase(pSearchId);
	if (m_searchIds.empty())
		ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, this);
	return true;
}

void CTelegramProto::ReportSearchUser(TG_USER *pUser)
{
	CMStringW wszId(FORMAT, L"%lld", pUser->id), wszNick, wszLastName, wszFirstName;

	PROTOSEARCHRESULT psr = {};
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_UNICODE;
	psr.id.w = wszId.GetBuffer();

	if (pUser->hContact != INVALID_CONTACT_ID) {
		wszNick = getMStringW(pUser->hContact, "Nick");
		wszLastName = getMStringW(pUser->hContact, "LastName");
		wszFirstName = getMStringW(pUser->hContact, "FirstName");

		psr.nick.w = wszNick.GetBuffer();
		psr.lastName.w = wszLastName.GetBuffer();
		psr.firstName.w = wszFirstName.GetBuffer();
	}
	else {
		psr.firstName.w = pUser->wszFirstName.GetBuffer();
		psr.lastName.w = pUser->wszLastName.GetBuffer();
		psr.nick.w = pUser->wszNick.GetBuffer();
	}

	ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_DATA, this, (LPARAM)&psr);
}

/////////////////////////////////////////////////////////////////////////////////////////

int64_t CTelegramProto::GetId(MCONTACT hContact)
{
	return _atoi64(getMStringA(hContact, DBKEY_ID));
}

void CTelegramProto::SetId(MCONTACT hContact, int64_t id)
{
	char szId[100];
	_i64toa(id, szId, 10);
	setString(hContact, DBKEY_ID, szId);
}

/////////////////////////////////////////////////////////////////////////////////////////

void CTelegramProto::UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str)
{
	if (str.empty())
		delSetting(hContact, pszSetting);
	else
		setUString(hContact, pszSetting, str.c_str());
}

/////////////////////////////////////////////////////////////////////////////////////////
// Users

TG_USER* CTelegramProto::FindChat(int64_t id)
{
	auto *tmp = (TG_USER *)_alloca(sizeof(TG_USER));
	tmp->chatId = id;
	return m_arChats.find(tmp);
}

TG_USER* CTelegramProto::FindUser(int64_t id)
{
	return m_arUsers.find((TG_USER *)&id);
}

TG_USER* CTelegramProto::AddFakeUser(int64_t id, bool bIsChat)
{
	auto *pu = FindUser(id);
	if (pu == nullptr) {
		pu = new TG_USER(id, INVALID_CONTACT_ID, bIsChat);
		m_arUsers.insert(pu);
	}
	return pu;
}

TG_USER* CTelegramProto::AddUser(int64_t id, bool bIsChat)
{
	auto *pUser = FindUser(id);
	if (pUser != nullptr)
		if (pUser->hContact != INVALID_CONTACT_ID)
			return pUser;

	MCONTACT hContact = db_add_contact();
	Proto_AddToContact(hContact, m_szModuleName);

	SetId(hContact, id);

	if (bIsChat) {
		setByte(hContact, "ChatRoom", 1);
	}
	else if (mir_wstrlen(m_wszDefaultGroup))
		Clist_SetGroup(hContact, m_wszDefaultGroup);

	if (pUser == nullptr) {
		pUser = new TG_USER(id, hContact, bIsChat);
		m_arUsers.insert(pUser);
	}
	else {
		pUser->hContact = hContact;
		setWString(hContact, "Nick", pUser->wszNick);
		if (!pUser->isGroupChat) {
			setWString(hContact, "FirstName", pUser->wszFirstName);
			setWString(hContact, "LastName", pUser->wszLastName);
		}
		else pUser->bStartChat = true;
	}

	return pUser;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Popups

void CTelegramProto::InitPopups(void)
{
	g_plugin.addPopupOption(CMStringW(FORMAT, TranslateT("%s error notifications"), m_tszUserName), m_bUsePopups);

	char name[256];
	mir_snprintf(name, "%s_%s", m_szModuleName, "Error");

	wchar_t desc[256];
	mir_snwprintf(desc, L"%s/%s", m_tszUserName, TranslateT("Errors"));

	POPUPCLASS ppc = {};
	ppc.flags = PCF_UNICODE;
	ppc.pszName = name;
	ppc.pszDescription.w = desc;
	ppc.hIcon = IcoLib_GetIconByHandle(m_hProtoIcon);
	ppc.colorBack = RGB(191, 0, 0); //Red
	ppc.colorText = RGB(255, 245, 225); //Yellow
	ppc.iSeconds = 60;
	m_hPopupClass = Popup_RegisterClass(&ppc);

	IcoLib_ReleaseIcon(ppc.hIcon);
}

void CTelegramProto::Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t *szTitle)
{
	if (!m_bUsePopups)
		return;

	char name[256];
	mir_snprintf(name, "%s_%s", m_szModuleName, "Error");

	CMStringW wszTitle(szTitle);
	if (hContact == 0) {
		wszTitle.Insert(0, L": ");
		wszTitle.Insert(0, m_tszUserName);
	}

	POPUPDATACLASS ppd = {};
	ppd.szTitle.w = wszTitle;
	ppd.szText.w = szMsg;
	ppd.pszClassName = name;
	ppd.hContact = hContact;
	Popup_AddClass(&ppd);
}

/////////////////////////////////////////////////////////////////////////////////////////

bool CTelegramProto::GetGcUserId(TG_USER *pUser, const TD::message *pMsg, char *dest)
{
	if (pUser->isGroupChat) {
		if (auto *pSender = GetSender(pMsg->sender_id_.get())) {
			_i64toa(pSender->id, dest, 10);
			if (pUser->m_si && !pSender->wszFirstName.IsEmpty())
				g_chatApi.UM_AddUser(pUser->m_si, Utf2T(dest), pSender->getDisplayName(), ID_STATUS_ONLINE);
			return true;
		}
	}

	*dest = 0;
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////

bool CTelegramProto::GetMessageFile(
	TG_FILE_REQUEST::Type fileType,
	TG_USER *pUser,
	const TD::file *pFile,
	const char *pszFileName,
	const std::string &caption,
	const char *pszId,
	const char *pszUserId,
	const TD::message *pMsg)
{
	if (pFile->get_id() != TD::file::ID) {
		debugLogA("Document contains unsupported type %d, exiting", pFile->get_id());
		return false;
	}

	auto *pRequest = new TG_FILE_REQUEST(fileType, pFile->id_, pFile->remote_->id_.c_str());
	pRequest->m_fileName = Utf2T(pszFileName);
	pRequest->m_fileSize = pFile->size_;
	pRequest->m_bRecv = true;
	{
		mir_cslock lck(m_csFiles);
		m_arFiles.insert(pRequest);
	}

	char szReplyId[100];
	MCONTACT hContact = GetRealContact(pUser);
	PROTORECVFILE pre = {};
	pre.dwFlags = PRFF_UTF | PRFF_SILENT;
	pre.fileCount = 1;
	pre.timestamp = pMsg->date_;
	pre.files.a = &pszFileName;
	pre.pUserInfo = pRequest;
	pre.szId = pszId;
	pre.szUserId = pszUserId;
	if (!caption.empty())
		pre.descr.a = caption.c_str();
	if (pMsg->is_outgoing_)
		pre.dwFlags |= PRFF_SENT;
	if (Contact::IsGroupChat(hContact) || !pUser->bInited)
		pre.dwFlags |= PRFF_READ;
	if (pMsg->reply_to_message_id_) {
		_i64toa(pMsg->reply_to_message_id_, szReplyId, 10);
		pre.szReplyId = szReplyId;
	}
	ProtoChainRecvFile(hContact, &pre);
	return true;
}

CMStringA CTelegramProto::GetMessageSticker(const TD::file *pFile, const char *pwszExtension)
{
	auto *pFileId = pFile->remote_->unique_id_.c_str();

	auto *pRequest = new TG_FILE_REQUEST(TG_FILE_REQUEST::AVATAR, pFile->id_, pFileId);
	pRequest->m_destPath = GetAvatarPath() + L"\\Stickers";
	CreateDirectoryW(pRequest->m_destPath, 0);

	pRequest->m_fileName.Format(L"STK{%S}.%S", pFileId, pwszExtension);
	{
		mir_cslock lck(m_csFiles);
		m_arFiles.insert(pRequest);
	}

	SendQuery(new TD::downloadFile(pFile->id_, 10, 0, 0, true));
	return CMStringA(FORMAT, "STK{%s}", pFileId);
}

/////////////////////////////////////////////////////////////////////////////////////////

static const TD::photoSize* GetBiggestPhoto(const TD::photo *pPhoto)
{
	const char *types[] = { "y", "x", "m", "s" };
	for (auto *pType : types)
		for (auto &it : pPhoto->sizes_)
			if (it->type_ == pType)
				return it.get();

	return nullptr;
}

static bool checkStickerType(uint32_t ID)
{
	switch (ID) {
	case TD::stickerTypeRegular::ID:
	case TD::stickerFullTypeRegular::ID:
		return true;
	default:
		return false;
	}	
}

CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg, bool bSkipJoin)
{
	const TD::MessageContent *pBody = pMsg->content_.get();

	char szUserId[100], *pszUserId = nullptr;
	auto szMsgId(msg2id(pMsg));
	if (GetGcUserId(pUser, pMsg, szUserId))
		pszUserId = szUserId;

	switch (pBody->get_id()) {
	case TD::messageChatUpgradeTo::ID:
		if (auto *pUgrade = (TD::messageChatUpgradeTo *)pBody) {
			MCONTACT hContact = pUser->hContact;
			m_arChats.remove(pUser);
			m_arUsers.remove(pUser);
			SetId(hContact, pUgrade->supergroup_id_);
			pUser = new TG_USER(pUgrade->supergroup_id_, hContact, true);
			m_arUsers.insert(pUser);
		}
		break;

	case TD::messageChatAddMembers::ID:
		if (!bSkipJoin)
			if (auto *pDoc = (TD::messageChatAddMembers *)pBody)
				for (auto &it : pDoc->member_user_ids_)
					GcChangeMember(pUser, pszUserId, it, true);
		break;

	case TD::messageChatDeleteMember::ID:
		if (!bSkipJoin)
			if (auto *pDoc = (TD::messageChatDeleteMember *)pBody)
				GcChangeMember(pUser, pszUserId, pDoc->user_id_, false);
		break;

	case TD::messageChatChangeTitle::ID:
		if (auto *pDoc = (TD::messageChatChangeTitle *)pBody)
			GcChangeTopic(pUser, Utf2T(pDoc->title_.c_str()));
		break;

	case TD::messagePhoto::ID:
		if (auto *pDoc = (TD::messagePhoto *)pBody) {
			auto *pPhoto = GetBiggestPhoto(pDoc->photo_.get());
			if (pPhoto == nullptr) {
				debugLogA("cannot find photo, exiting");
				break;
			}

			CMStringA fileName(FORMAT, "%s (%d x %d)", TranslateU("Picture"), pPhoto->width_, pPhoto->height_);
			GetMessageFile(TG_FILE_REQUEST::PICTURE, pUser, pPhoto->photo_.get(), fileName, pDoc->caption_->text_, szMsgId, pszUserId, pMsg);
		}
		break;

	case TD::messageAudio::ID:
		if (auto *pDoc = (TD::messageAudio *)pBody) {
			auto *pAudio = pDoc->audio_.get();
			CMStringA fileName(FORMAT, "%s (%d %s)", TranslateU("Audio"), pAudio->duration_, TranslateU("seconds"));
			std::string caption = fileName.c_str();
			if (!pDoc->caption_->text_.empty()) {
				caption += " ";
				caption += pDoc->caption_->text_;
			}
			GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pAudio->audio_.get(), pAudio->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg);
		}
		break;

	case TD::messageVideo::ID:
		if (auto *pDoc = (TD::messageVideo *)pBody) {
			auto *pVideo = pDoc->video_.get();
			CMStringA fileName(FORMAT, "%s (%d x %d, %d %s)", TranslateU("Video"), pVideo->width_, pVideo->height_, pVideo->duration_, TranslateU("seconds"));
			std::string caption = fileName.c_str();
			if (!pDoc->caption_->text_.empty()) {
				caption += " ";
				caption += pDoc->caption_->text_;
			}
			GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->video_.get(), pVideo->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg);
		}
		break;

	case TD::messageAnimation::ID:
		if (auto *pDoc = (TD::messageAnimation *)pBody) {
			auto *pVideo = pDoc->animation_.get();
			CMStringA fileName(FORMAT, "%s (%d x %d, %d %s)", TranslateU("Video"), pVideo->width_, pVideo->height_, pVideo->duration_, TranslateU("seconds"));
			std::string caption = fileName.c_str();
			if (!pDoc->caption_->text_.empty()) {
				caption += " ";
				caption += pDoc->caption_->text_;
			}
			GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->animation_.get(), pVideo->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg);
		}
		break;

	case TD::messageVoiceNote::ID:
		if (auto *pDoc = (TD::messageVoiceNote *)pBody) {
			CMStringA fileName(FORMAT, "%s (%d %s)", TranslateU("Voice message"), pDoc->voice_note_->duration_, TranslateU("seconds"));
			GetMessageFile(TG_FILE_REQUEST::VOICE, pUser, pDoc->voice_note_->voice_.get(), fileName, pDoc->caption_->text_, szMsgId, pszUserId, pMsg);
		}
		break;

	case TD::messageDocument::ID:
		if (auto *pDoc = (TD::messageDocument *)pBody)
			GetMessageFile(TG_FILE_REQUEST::FILE, pUser, pDoc->document_->document_.get(), pDoc->document_->file_name_.c_str(), pDoc->caption_->text_, szMsgId, pszUserId, pMsg);
		break;

	case TD::messageAnimatedEmoji::ID:
		if (auto *pObj = (TD::messageAnimatedEmoji *)pBody) {
			if (m_bSmileyAdd) {
				if (auto *pAnimated = pObj->animated_emoji_.get()) {
					if (auto *pSticker = pAnimated->sticker_.get()) {
						if (!checkStickerType(pSticker->full_type_->get_id())) {
							debugLogA("You received a sticker of unsupported type %d, ignored", pSticker->full_type_->get_id());
							break;
						}

						const char *pwszFileExt;
						switch (pSticker->thumbnail_->format_->get_id()) {
						case TD::thumbnailFormatGif::ID: pwszFileExt = "gif"; break;
						case TD::thumbnailFormatPng::ID: pwszFileExt = "png"; break;
						case TD::thumbnailFormatTgs::ID: pwszFileExt = "tga"; break;
						case TD::thumbnailFormatJpeg::ID: pwszFileExt = "jpg"; break;
						case TD::thumbnailFormatWebm::ID: pwszFileExt = "webm"; break;
						case TD::thumbnailFormatWebp::ID: pwszFileExt = "webp"; break;
						default:pwszFileExt = "jpeg"; break;
						}

						return GetMessageSticker(pSticker->thumbnail_->file_.get(), pwszFileExt);
					}
				}
			}
			return pObj->emoji_.c_str();
		}

	case TD::messageSticker::ID:
		if (auto *pSticker = ((TD::messageSticker *)pBody)->sticker_.get()) {
			if (!checkStickerType(pSticker->full_type_->get_id())) {
				debugLogA("You received a sticker of unsupported type %d, ignored", pSticker->full_type_->get_id());
				break;
			}
			
			if (!m_bSmileyAdd)
				return CMStringA(FORMAT, "%s: %s", TranslateU("SmileyAdd plugin required to support stickers"), pSticker->emoji_.c_str());
					
			const char *pwszFileExt;
			switch (pSticker->format_->get_id()) {
			case TD::stickerFormatTgs::ID: pwszFileExt = "tga"; break;
			case TD::stickerFormatWebm::ID: pwszFileExt = "webm"; break;
			case TD::stickerFormatWebp::ID: pwszFileExt = "webp"; break;
			default:pwszFileExt = "jpeg"; break;
			}

			return GetMessageSticker(pSticker->thumbnail_->file_.get(), pwszFileExt);
		}

	case TD::messageInvoice::ID:
		{
			auto *pInvoice = ((TD::messageInvoice *)pBody);
			CMStringA ret(FORMAT, "%s: %.2lf %s", TranslateU("You received an invoice"), double(pInvoice->total_amount_)/100.0, pInvoice->currency_.c_str());
			if (!pInvoice->title_.empty())
				ret.AppendFormat("\r\n%s: %s", TranslateU("Title"), pInvoice->title_.c_str());
			if (auto pszText = getFormattedText(pInvoice->description_))
				ret.AppendFormat("\r\n%s", pszText.c_str());
			return ret;
		}

	case TD::messageText::ID:
		if (auto pszText = getFormattedText(((TD::messageText *)pBody)->text_))
			return pszText;
		break;
	}

	return CMStringA();
}