From 6b1d05a40f8e07ab37a9779944c334880a4aa67f Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 17 Jan 2023 21:37:59 +0300 Subject: Telegram: stickers reading --- protocols/Telegram/src/avatars.cpp | 17 +++++++++++++++-- protocols/Telegram/src/proto.cpp | 8 ++++++++ protocols/Telegram/src/proto.h | 20 +++++++++++++++++++- protocols/Telegram/src/server.cpp | 2 +- protocols/Telegram/src/stdafx.h | 1 + protocols/Telegram/src/utils.cpp | 35 +++++++++++++++++++++++++++++++++-- protocols/Telegram/src/utils.h | 2 -- 7 files changed, 77 insertions(+), 8 deletions(-) diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp index a4c841f3f5..5c309c17be 100644 --- a/protocols/Telegram/src/avatars.cpp +++ b/protocols/Telegram/src/avatars.cpp @@ -83,10 +83,23 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) if (!pFile->local_->is_downloading_completed_) return; + Utf2T wszExistingFile(pFile->local_->path_.c_str()); + + for (auto &it : m_arFiles) { + if (it->m_uniqueId == pFile->remote_->unique_id_.c_str()) { + MoveFileW(wszExistingFile, it->m_destPath); + if (it->m_type == it->AVATAR) { + SMADD_CONT cont = {1, m_szModuleName, it->m_destPath}; + CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, LPARAM(&cont)); + } + m_arFiles.remove(m_arFiles.indexOf(&it)); + return; + } + } + for (auto &it : m_arUsers) { if (it->szAvatarHash == pFile->remote_->unique_id_.c_str()) { - Utf2T wszExistingFile(pFile->local_->path_.c_str()); - + PROTO_AVATAR_INFORMATION pai; pai.hContact = it->hContact; pai.format = ProtoGetAvatarFileFormat(wszExistingFile); diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index 84944a6b49..ba9565f09f 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -19,6 +19,7 @@ static int CompareUsers(const TG_USER *p1, const TG_USER *p2) CTelegramProto::CTelegramProto(const char* protoName, const wchar_t* userName) : PROTO(protoName, userName), m_impl(*this), + m_arFiles(1), m_arUsers(10, CompareUsers), m_arRequests(10, CompareRequests), m_szOwnPhone(this, "Phone"), @@ -89,6 +90,13 @@ void CTelegramProto::OnModulesLoaded() m_arUsers.insert(pUser); } } + + m_bSmileyAdd = ServiceExists(MS_SMILEYADD_LOADCONTACTSMILEYS); + if (m_bSmileyAdd) { + CMStringW wszStickersPath(GetAvatarPath() + L"\\Stickers\\*.*"); + SMADD_CONT cont = {2, m_szModuleName, wszStickersPath}; + CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, LPARAM(&cont)); + } } void CTelegramProto::OnShutdown() diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index bdb911ac51..77c9da70d7 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -59,6 +59,21 @@ struct TG_REQUEST_FULL : public TG_REQUEST_BASE ///////////////////////////////////////////////////////////////////////////////////////// +struct TG_FILE_REQUEST +{ + enum Type { AVATAR = 1 }; + + TG_FILE_REQUEST(Type _1, const char *_2, const wchar_t *_3) : + m_type(_1), + m_uniqueId(_2), + m_destPath(_3) + {} + + Type m_type; + CMStringA m_uniqueId; + CMStringW m_destPath; +}; + struct TG_USER { TG_USER(uint64_t _1, MCONTACT _2, bool _3 = false) : @@ -111,11 +126,12 @@ class CTelegramProto : public PROTO MCONTACT m_markContact = 0; TD::array m_markIds; - bool m_bAuthorized, m_bTerminated, m_bUnregister = false; + bool m_bAuthorized, m_bTerminated, m_bUnregister = false, m_bSmileyAdd = false; int32_t m_iClientId, m_iMsgId; uint64_t m_iQueryId; OBJLIST m_arRequests; + OBJLIST m_arFiles; static INT_PTR CALLBACK EnterPhoneCode(void *param); static INT_PTR CALLBACK EnterPassword(void *param); @@ -148,6 +164,8 @@ class CTelegramProto : public PROTO void ProcessStatus(TD::updateUserStatus *pObj); void ProcessUser(TD::updateUser *pObj); + CMStringA GetMessageText(TD::MessageContent *pBody); + void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str); // Avatars diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index befa3fb7ab..b8567a320b 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -340,7 +340,7 @@ void CTelegramProto::ProcessMessage(TD::updateNewMessage *pObj) return; } - CMStringA szText(getMessageText(pMessage->content_.get())); + CMStringA szText(GetMessageText(pMessage->content_.get())); if (szText.IsEmpty()) { debugLogA("this message was not processed, ignored"); return; diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index 340270df0a..1ded1b61dd 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "td/telegram/Client.h" #include "td/telegram/td_api.h" diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp index b2eaeac1ca..4a122f70c1 100644 --- a/protocols/Telegram/src/utils.cpp +++ b/protocols/Telegram/src/utils.cpp @@ -114,12 +114,43 @@ void CTelegramProto::Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_ ///////////////////////////////////////////////////////////////////////////////////////// -CMStringA getMessageText(TD::MessageContent *pBody) +CMStringA CTelegramProto::GetMessageText(TD::MessageContent *pBody) { - if (pBody->get_id() == TD::messageText::ID) { + switch (pBody->get_id()) { + case TD::messageSticker::ID: + if (m_bSmileyAdd) { + auto pSticker = ((TD::messageSticker *)pBody)->sticker_.get(); + if (pSticker->type_->get_id() != TD::stickerTypeRegular::ID) + break; + + auto *pFile = pSticker->thumbnail_->file_.get(); + auto *pFileId = pFile->remote_->unique_id_.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; + } + + CMStringW wszDest(GetAvatarPath() + L"\\Stickers"); + CreateDirectoryW(wszDest, 0); + wszDest.AppendFormat(L"\\STK{%S}.%S", pFileId, pwszFileExt); + + m_arFiles.insert(new TG_FILE_REQUEST(TG_FILE_REQUEST::AVATAR, pFileId, wszDest)); + + SendQuery(new TD::downloadFile(pFile->id_, 10, 0, 0, true)); + return CMStringA(FORMAT, "STK{%s}", pFileId); + } + else debugLogA("SmileyAdd plugin isn't installed, skipping sticker"); + break; + + case TD::messageText::ID: auto pText = ((TD::messageText *)pBody)->text_.get(); if (pText->get_id() == TD::formattedText::ID) return CMStringA(((TD::formattedText *)pText)->text_.c_str()); + break; } return CMStringA(); diff --git a/protocols/Telegram/src/utils.h b/protocols/Telegram/src/utils.h index a0811da20e..50e96676b7 100644 --- a/protocols/Telegram/src/utils.h +++ b/protocols/Telegram/src/utils.h @@ -1,3 +1 @@ #pragma once - -CMStringA getMessageText(TD::MessageContent *pBody); -- cgit v1.2.3