From 0f77335cd29dff59fc7587e62e5f94a04936e664 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 1 Dec 2023 21:34:08 +0300 Subject: =?UTF-8?q?fixes=20#4004=20(Telegram:=20=D1=81=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=BE=D0=BA=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/Telegram/src/options.cpp | 142 +++++++++++++++++++++++++++++++++++++ protocols/Telegram/src/proto.h | 3 + protocols/Telegram/src/resource.h | 8 ++- protocols/Telegram/src/stdafx.h | 1 + 4 files changed, 152 insertions(+), 2 deletions(-) (limited to 'protocols/Telegram/src') diff --git a/protocols/Telegram/src/options.cpp b/protocols/Telegram/src/options.cpp index e1c8d89c68..4960ad124d 100644 --- a/protocols/Telegram/src/options.cpp +++ b/protocols/Telegram/src/options.cpp @@ -179,6 +179,143 @@ public: } }; +///////////////////////////////////////////////////////////////////////////////////////// +// Session list + +class COptSessionsDlg : public CTelegramDlgBase +{ + CCtrlBase m_ipAddress, m_software; + CCtrlListView m_list; + +public: + COptSessionsDlg(CTelegramProto *ppro) : + CTelegramDlgBase(ppro, IDD_OPTIONS_SESSIONS), + m_list(this, IDC_SESSIONS), + m_software(this, IDC_SOFTWARE), + m_ipAddress(this, IDC_IPADDRESS) + { + m_list.OnBuildMenu = Callback(this, &COptSessionsDlg::onContextMenu); + m_list.OnDeleteItem = Callback(this, &COptSessionsDlg::onDeleteItem); + m_list.OnItemChanged = Callback(this, &COptSessionsDlg::onItemChanged); + } + + bool OnInitDialog() override + { + LVCOLUMN lvc = {}; + lvc.mask = LVCF_TEXT | LVCF_WIDTH; + + lvc.pszText = TranslateT("Name"); + lvc.cx = 150; + m_list.InsertColumn(0, &lvc); + + lvc.pszText = TranslateT("Platform"); + lvc.cx = 110; + m_list.InsertColumn(1, &lvc); + + lvc.pszText = TranslateT("Country"); + lvc.cx = 120; + m_list.InsertColumn(2, &lvc); + + lvc.pszText = TranslateT("Last active"); + lvc.cx = 110; + m_list.InsertColumn(3, &lvc); + + BuildList(); + return true; + } + + void OnDestroy() override + { + int iCount = m_list.GetItemCount(); + for (int i = 0; i < iCount; i++) { + auto *pSession = (TD::session *)m_list.GetItemData(i); + delete pSession; + } + } + + void onContextMenu(CContextMenuPos *pos) + { + HMENU hMenu = CreatePopupMenu(); + AppendMenuW(hMenu, MF_STRING, 100, TranslateT("Kill session")); + + if (100 == TrackPopupMenu(hMenu, TPM_RETURNCMD, pos->pt.x, pos->pt.y, 0, m_hwnd, 0)) { + auto *pSession = (TD::session *)m_list.GetItemData(pos->iCurr); + m_proto->SendQuery(new TD::terminateSession(pSession->id_), &CTelegramProto::OnKillSession, this); + } + } + + void onDeleteItem(CCtrlListView::TEventInfo *ev) + { + auto *pSession = (TD::session *)m_list.GetItemData(ev->nmlv->iItem); + delete pSession; + } + + void onItemChanged(CCtrlListView::TEventInfo *ev) + { + auto *pSession = (TD::session*)m_list.GetItemData(ev->nmlv->iItem); + m_ipAddress.SetTextA(pSession->ip_.c_str()); + m_software.SetTextA((pSession->application_name_ + " " + pSession->application_version_).c_str()); + } + + void BuildList() + { + m_list.DeleteAllItems(); + + m_proto->SendQuery(new TD::getActiveSessions(), &CTelegramProto::OnGetSessions, &m_list); + } +}; + +void CTelegramProto::OnGetSessions(td::ClientManager::Response &response, void *pUserInfo) +{ + if (!response.object) + return; + + if (response.object->get_id() != TD::sessions::ID) { + debugLogA("Gotten class ID %d instead of %d, exiting", response.object->get_id(), TD::chats::ID); + return; + } + + auto *pSessions = (TD::sessions *)response.object.get(); + auto *pList = (CCtrlListView *)pUserInfo; + + for (auto &it : pSessions->sessions_) { + auto *pSession = it.release(); + int iItem = pList->AddItem(Utf2T(pSession->device_model_.c_str()), 0, (LPARAM)pSession); + + const wchar_t *pwszType; + switch (pSession->type_->get_id()) { + case TD::sessionTypeAndroid::ID: pwszType = L"Android"; break; + case TD::sessionTypeApple::ID: pwszType = L"Apple"; break; + case TD::sessionTypeChrome::ID: pwszType = L"Chrome"; break; + case TD::sessionTypeEdge::ID: pwszType = L"MS Edge"; break; + case TD::sessionTypeFirefox::ID: pwszType = L"Firefox"; break; + case TD::sessionTypeIpad::ID: pwszType = L"Ipad"; break; + case TD::sessionTypeIphone::ID: pwszType = L"Iphone"; break; + case TD::sessionTypeLinux::ID: pwszType = L"Linux"; break; + case TD::sessionTypeMac::ID: pwszType = L"Mac"; break; + case TD::sessionTypeOpera::ID: pwszType = L"Opera"; break; + case TD::sessionTypeSafari::ID: pwszType = L"Safari"; break; + case TD::sessionTypeUbuntu::ID: pwszType = L"Ubuntu"; break; + case TD::sessionTypeVivaldi::ID: pwszType = L"Vivaldi"; break; + case TD::sessionTypeWindows::ID: pwszType = L"Windows"; break; + default: + pwszType = TranslateT("Unknown"); + } + pList->SetItemText(iItem, 1, pwszType); + pList->SetItemText(iItem, 2, Utf2T(pSession->country_.c_str())); + + wchar_t wszLastLogin[100]; + TimeZone_PrintTimeStamp(0, pSession->last_active_date_, L"d t", wszLastLogin, _countof(wszLastLogin), 0); + pList->SetItemText(iItem, 3, wszLastLogin); + } +} + +void CTelegramProto::OnKillSession(td::ClientManager::Response&, void *pUserInfo) +{ + auto *pDlg = (COptSessionsDlg *)pUserInfo; + pDlg->BuildList(); +} + ///////////////////////////////////////////////////////////////////////////////////////// MWindow CTelegramProto::OnCreateAccMgrUI(MWindow hwndParent) @@ -205,5 +342,10 @@ int CTelegramProto::OnOptionsInit(WPARAM wParam, LPARAM) odp.szTab.w = LPGENW("Advanced"); odp.pDialog = new CAdvOptionsDlg(this); g_plugin.addOptions(wParam, &odp); + + odp.position = 3; + odp.szTab.w = LPGENW("Sessions"); + odp.pDialog = new COptSessionsDlg(this); + g_plugin.addOptions(wParam, &odp); return 0; } diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index 32cec7fa60..db995e49be 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -150,6 +150,7 @@ class CTelegramProto : public PROTO friend class CReplyDlg; friend class CForwardDlg; friend class CReactionsDlg; + friend class COptSessionsDlg; friend class CAddPhoneContactDlg; class CProtoImpl @@ -224,6 +225,8 @@ class CTelegramProto : public PROTO void OnGetFileInfo(td::ClientManager::Response &response, void *pUserInfo); void OnGetFileLink(td::ClientManager::Response &response); void OnGetHistory(td::ClientManager::Response &response, void *pUserInfo); + void OnGetSessions(td::ClientManager::Response &response, void *pUserInfo); + void OnKillSession(td::ClientManager::Response &response, void *pUserInfo); void OnSendFile(td::ClientManager::Response &response, void *pUserInfo); void OnSendMessage(td::ClientManager::Response &response); void OnUpdateAuth(td::ClientManager::Response &response); diff --git a/protocols/Telegram/src/resource.h b/protocols/Telegram/src/resource.h index 0ffa5e1a47..9838a6cb40 100644 --- a/protocols/Telegram/src/resource.h +++ b/protocols/Telegram/src/resource.h @@ -15,6 +15,7 @@ #define IDI_BOT 110 #define IDI_REPLY 111 #define IDD_REPLY 112 +#define IDD_OPTIONS_SESSIONS 113 #define IDC_PHONE 1001 #define IDC_DEFGROUP 1002 #define IDC_HIDECHATS 1003 @@ -38,14 +39,17 @@ #define IDC_REPLYTO 1019 #define IDC_TEXT 1020 #define IDC_ATTACH 1021 +#define IDC_SESSIONS 1023 +#define IDC_IPADDRESS 1024 +#define IDC_SOFTWARE 1025 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 115 +#define _APS_NEXT_RESOURCE_VALUE 116 #define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1023 +#define _APS_NEXT_CONTROL_VALUE 1025 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index f6e7100f53..bb331c192a 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -30,6 +30,7 @@ #include #include #include +#include #include "../../libs/freeimage/src/FreeImage.h" -- cgit v1.2.3