summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-12-19 21:53:22 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-12-19 21:53:22 +0300
commitd78fdd24a3f26f928a55f527e0bfbfc5fa069d55 (patch)
tree986ef11cc22add13f538b08c34fd862e5212571e
parent77a1f91e61f6d63591ec06db9ff2b3ef54af832f (diff)
Telegram: simple text message reading
-rw-r--r--protocols/Telegram/src/mt_proto.cpp1
-rw-r--r--protocols/Telegram/src/mt_proto.h2
-rw-r--r--protocols/Telegram/src/server.cpp41
-rw-r--r--protocols/Telegram/src/stdafx.h1
-rw-r--r--protocols/Telegram/src/utils.cpp13
-rw-r--r--protocols/Telegram/src/utils.h3
6 files changed, 61 insertions, 0 deletions
diff --git a/protocols/Telegram/src/mt_proto.cpp b/protocols/Telegram/src/mt_proto.cpp
index 4b7c6fb211..9e0cffd97c 100644
--- a/protocols/Telegram/src/mt_proto.cpp
+++ b/protocols/Telegram/src/mt_proto.cpp
@@ -19,6 +19,7 @@ CMTProto::CMTProto(const char* protoName, const wchar_t* userName) :
m_bHideGroupchats(this, "HideChats", true)
{
m_iClientId = m_pClientMmanager->create_client_id();
+ m_iOwnId = _atoi64(getMStringA(DBKEY_ID));
CreateProtoService(PS_CREATEACCMGRUI, &CMTProto::SvcCreateAccMgrUI);
diff --git a/protocols/Telegram/src/mt_proto.h b/protocols/Telegram/src/mt_proto.h
index 6376a00610..402dee432f 100644
--- a/protocols/Telegram/src/mt_proto.h
+++ b/protocols/Telegram/src/mt_proto.h
@@ -57,11 +57,13 @@ class CMTProto : public PROTO<CMTProto>
void ProcessAuth(TD::updateAuthorizationState *pObj);
void ProcessChat(TD::updateNewChat *pObj);
void ProcessGroups(TD::updateChatFilters *pObj);
+ void ProcessMessage(TD::updateNewMessage *pObj);
void ProcessUser(TD::updateUser *pObj);
void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str);
// Users
+ int64_t m_iOwnId;
OBJLIST<TG_USER> m_arUsers;
TG_USER* FindUser(uint64_t id);
diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp
index b5670b27fc..89db787400 100644
--- a/protocols/Telegram/src/server.cpp
+++ b/protocols/Telegram/src/server.cpp
@@ -89,6 +89,10 @@ void CMTProto::ProcessResponse(td::ClientManager::Response response)
ProcessChat((TD::updateNewChat *)response.object.get());
break;
+ case TD::updateNewMessage::ID:
+ ProcessMessage((TD::updateNewMessage *)response.object.get());
+ break;
+
case TD::updateUser::ID:
ProcessUser((TD::updateUser *)response.object.get());
break;
@@ -143,6 +147,40 @@ void CMTProto::ProcessGroups(TD::updateChatFilters *pObj)
}
}
+void CMTProto::ProcessMessage(TD::updateNewMessage *pObj)
+{
+ auto &pMessage = pObj->message_;
+
+ auto *pUser = FindUser(pMessage->chat_id_);
+ if (pUser == nullptr) {
+ debugLogA("message from unknown chat/user, ignored");
+ return;
+ }
+
+ if (pUser->isGroupChat) {
+ debugLogA("message from group chat, ignored");
+ return;
+ }
+
+ CMStringA szText(getMessageText(pMessage->content_.get()));
+ if (szText.IsEmpty()) {
+ debugLogA("this message was not processed, ignored");
+ return;
+ }
+
+ char szId[100];
+ _i64toa(pMessage->id_, szId, _countof(szId));
+
+ PROTORECVEVENT pre = {};
+ pre.szMessage = szText.GetBuffer();
+ pre.szMsgId = szId;
+ pre.timestamp = pMessage->date_;
+ if (pMessage->sender_id_->get_id() == TD::messageSenderUser::ID)
+ if (((TD::messageSenderUser *)pMessage->sender_id_.get())->user_id_ == m_iOwnId)
+ pre.flags |= PREF_SENT;
+ ProtoChainRecvMsg(pUser->hContact, &pre);
+}
+
void CMTProto::ProcessUser(TD::updateUser *pObj)
{
auto *pUser = pObj->user_.get();
@@ -154,6 +192,9 @@ void CMTProto::ProcessUser(TD::updateUser *pObj)
if (pUser->usernames_)
UpdateString(pu->hContact, "Nick", pUser->usernames_->editable_username_);
+ if (pUser->phone_number_ == _T2A(m_szOwnPhone).get())
+ m_iOwnId = pUser->id_;
+
if (pUser->is_premium_)
ExtraIcon_SetIconByName(g_plugin.m_hIcon, pu->hContact, "tg_premium");
else
diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h
index 83217608d0..cfbb5fa8b0 100644
--- a/protocols/Telegram/src/stdafx.h
+++ b/protocols/Telegram/src/stdafx.h
@@ -32,6 +32,7 @@ namespace TD = td::td_api;
#include "version.h"
#include "resource.h"
#include "mt_proto.h"
+#include "utils.h"
struct CMPlugin : public ACCPROTOPLUGIN<CMTProto>
{
diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp
index baf10e031d..3ea60b4c19 100644
--- a/protocols/Telegram/src/utils.cpp
+++ b/protocols/Telegram/src/utils.cpp
@@ -111,3 +111,16 @@ void CMTProto::Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t *szT
ppd.hContact = hContact;
Popup_AddClass(&ppd);
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+CMStringA getMessageText(TD::MessageContent *pBody)
+{
+ if (pBody->get_id() == 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());
+ }
+
+ return CMStringA();
+}
diff --git a/protocols/Telegram/src/utils.h b/protocols/Telegram/src/utils.h
new file mode 100644
index 0000000000..2526393a5f
--- /dev/null
+++ b/protocols/Telegram/src/utils.h
@@ -0,0 +1,3 @@
+#pragma once
+
+CMStringA getMessageText(TD::MessageContent *pBody);