summaryrefslogtreecommitdiff
path: root/protocols/Telegram/src/utils.cpp
blob: 849f5c2399ccad49e95554b02928e94bdb4f7b72 (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
/*
Copyright (C) 2012-23 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"

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

TD::object_ptr<TD::inputFileLocal> makeFile(const CMStringW &wszFile)
{
	std::string szPath = T2Utf(wszFile);
	return TD::make_object<TD::inputFileLocal>(std::move(szPath));
}

void TG_FILE_REQUEST::AutoDetectType()
{
	if (ProtoGetAvatarFileFormat(m_fileName) != PA_FORMAT_UNKNOWN) {
		m_type = this->PICTURE;
		return;
	}

	int idx = m_fileName.ReverseFind('.');
	if (idx == -1 || m_fileName.Find('\\', idx) != -1)
		return;

	auto wszExt = m_fileName.Right(m_fileName.GetLength() - idx);
	wszExt.MakeLower();
	if (wszExt == L"mp4" || wszExt == L"webm")
		m_type = this->VIDEO;
	else if (wszExt == L"mp3" || wszExt == "ogg" || wszExt == "oga" || wszExt == "wav")
		m_type = this->VOICE;
}

CMStringW TG_USER::getDisplayName() const
{
	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);
		if (!bIsChat)
			m_arChats.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) {
		Clist_SetGroup(hContact, TranslateT("Chat rooms"));
		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);
		if (!bIsChat)
			m_arChats.insert(pUser);
	}
	else {
		pUser->hContact = hContact;
		setWString(hContact, "Nick", pUser->wszNick);
		setWString(hContact, "FirstName", pUser->wszFirstName);
		setWString(hContact, "LastName", pUser->wszLastName);
	}

	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_;
	{
		mir_cslock lck(m_csFiles);
		m_arFiles.insert(pRequest);
	}

	PROTORECVFILE pre = {};
	pre.dwFlags = PRFF_UTF | PRFF_SILENT | PRFF_READ;
	pre.fileCount = 1;
	pre.timestamp = pMsg->date_;
	pre.files.a = &pszFileName;
	pre.lParam = (LPARAM)pRequest;
	pre.szId = pszId;
	pre.szUserId = pszUserId;
	if (!caption.empty())
		pre.descr.a = caption.c_str();
	if (pMsg->is_outgoing_)
		pre.dwFlags |= PRFF_SENT;
	ProtoChainRecvFile(GetRealContact(pUser), &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;
	}	
}

static const char *getFormattedText(TD::object_ptr<TD::formattedText> &pText)
{
	if (pText->get_id() == TD::formattedText::ID)
		return pText->text_.c_str();
	return nullptr;
}

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

	char szId[100], szUserId[100], *pszUserId = nullptr;
	_i64toa(pMsg->id_, szId, 10);
	if (GetGcUserId(pUser, pMsg, szUserId))
		pszUserId = szUserId;

	switch (pBody->get_id()) {
	case TD::messagePhoto::ID:
		{
			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_, szId, pszUserId, pMsg);
		}
		break;

	case TD::messageVideo::ID:
		{
			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, szId, pszUserId, pMsg);
		}
		break;

	case TD::messageAnimation::ID:
		{
			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, szId, pszUserId, pMsg);
		}
		break;

	case TD::messageVoiceNote::ID:
		{
			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_, szId, pszUserId, pMsg);
		}
		break;

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

	case TD::messageAnimatedEmoji::ID:
		{
			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:
		{
			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", ((TD::formattedText *)pInvoice->description_.get())->text_.c_str());
			return ret;
		}

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

	return CMStringA();
}