From 21ffdeca1a3f8d9f65dc9b33d9b2379ba0f1a4ac Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 10 May 2024 14:30:56 +0300 Subject: for #629: message removing for Discord --- protocols/Discord/src/proto.cpp | 198 ++++++++++++++++++++++------------------ protocols/Discord/src/proto.h | 2 + 2 files changed, 111 insertions(+), 89 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 7d260bf6dd..4c129de23b 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -63,7 +63,9 @@ CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : CreateProtoService(PS_SETMYAVATAR, &CDiscordProto::SetMyAvatar); CreateProtoService(PS_MENU_REQAUTH, &CDiscordProto::RequestFriendship); + CreateProtoService(PS_MENU_LOADHISTORY, &CDiscordProto::OnMenuLoadHistory); + CreateProtoService(PS_EMPTY_SRV_HISTORY, &CDiscordProto::SvcEmptyServerHistory); CreateProtoService(PS_LEAVECHAT, &CDiscordProto::SvcLeaveChat); @@ -268,97 +270,10 @@ int CDiscordProto::SetStatus(int iNewStatus) return 0; } -///////////////////////////////////////////////////////////////////////////////////////// - -static INT_PTR CALLBACK AdvancedSearchDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) -{ - switch (msg) { - case WM_INITDIALOG: - TranslateDialogDefault(hwndDlg); - SetFocus(GetDlgItem(hwndDlg, IDC_NICK)); - return TRUE; - - case WM_COMMAND: - if (HIWORD(wParam) == EN_SETFOCUS) - PostMessage(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)hwndDlg); - } - return FALSE; -} - -HWND CDiscordProto::CreateExtendedSearchUI(HWND hwndParent) -{ - if (hwndParent) - return CreateDialogParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_EXTSEARCH), hwndParent, AdvancedSearchDlgProc, 0); - - return nullptr; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -void CDiscordProto::SearchThread(void *param) -{ - Sleep(100); - - PROTOSEARCHRESULT psr = { 0 }; - psr.cbSize = sizeof(psr); - psr.flags = PSR_UNICODE; - psr.nick.w = (wchar_t*)param; - psr.firstName.w = L""; - psr.lastName.w = L""; - psr.id.w = L""; - ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_DATA, this, (LPARAM)&psr); - - ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, this, 0); - mir_free(param); -} - -HANDLE CDiscordProto::SearchAdvanced(HWND hwndDlg) -{ - if (!m_bOnline || !IsWindow(hwndDlg)) - return nullptr; - - wchar_t wszNick[200]; - GetDlgItemTextW(hwndDlg, IDC_NICK, wszNick, _countof(wszNick)); - if (wszNick[0] == 0) // empty string? reject - return nullptr; - - wchar_t *p = wcschr(wszNick, '#'); - if (p == nullptr) // wrong user id - return nullptr; - - ForkThread(&CDiscordProto::SearchThread, mir_wstrdup(wszNick)); - return this; -} - -///////////////////////////////////////////////////////////////////////////////////////// -// Basic search - by SnowFlake - -void CDiscordProto::OnReceiveUserinfo(MHttpResponse *pReply, AsyncHttpRequest*) -{ - JsonReply root(pReply); - if (!root) { - ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_FAILED, this); - return; - } - - ForkThread(&CDiscordProto::SearchThread, getNick(root.data()).Detach()); -} - -HANDLE CDiscordProto::SearchBasic(const wchar_t *wszId) -{ - if (!m_bOnline) - return nullptr; - - CMStringA szUrl = "/users/"; - szUrl.AppendFormat(ptrA(mir_utf8encodeW(wszId))); - Push(new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveUserinfo)); - return (HANDLE)1; // Success -} - //////////////////////////////////////////////////////////////////////////////////////// // Authorization -int CDiscordProto::AuthRequest(MCONTACT hContact, const wchar_t*) +int CDiscordProto::AuthRequest(MCONTACT hContact, const wchar_t *) { ptrW wszUsername(getWStringA(hContact, DB_KEY_NICK)); int iDiscriminator(getDword(hContact, DB_KEY_DISCR, -1)); @@ -390,7 +305,7 @@ int CDiscordProto::Authorize(MEVENT hDbEvent) return 0; } -int CDiscordProto::AuthDeny(MEVENT hDbEvent, const wchar_t*) +int CDiscordProto::AuthDeny(MEVENT hDbEvent, const wchar_t *) { DB::EventInfo dbei; dbei.cbBlob = -1; @@ -403,6 +318,31 @@ int CDiscordProto::AuthDeny(MEVENT hDbEvent, const wchar_t*) return 0; } +///////////////////////////////////////////////////////////////////////////////////////// + +static INT_PTR CALLBACK AdvancedSearchDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) +{ + switch (msg) { + case WM_INITDIALOG: + TranslateDialogDefault(hwndDlg); + SetFocus(GetDlgItem(hwndDlg, IDC_NICK)); + return TRUE; + + case WM_COMMAND: + if (HIWORD(wParam) == EN_SETFOCUS) + PostMessage(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)hwndDlg); + } + return FALSE; +} + +HWND CDiscordProto::CreateExtendedSearchUI(HWND hwndParent) +{ + if (hwndParent) + return CreateDialogParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_EXTSEARCH), hwndParent, AdvancedSearchDlgProc, 0); + + return nullptr; +} + //////////////////////////////////////////////////////////////////////////////////////// MCONTACT CDiscordProto::AddToList(int flags, PROTOSEARCHRESULT *psr) @@ -458,6 +398,68 @@ MCONTACT CDiscordProto::AddToListByEvent(int flags, int, MEVENT hDbEvent) return blob.get_contact(); } +///////////////////////////////////////////////////////////////////////////////////////// + +void CDiscordProto::SearchThread(void *param) +{ + Sleep(100); + + PROTOSEARCHRESULT psr = { 0 }; + psr.cbSize = sizeof(psr); + psr.flags = PSR_UNICODE; + psr.nick.w = (wchar_t *)param; + psr.firstName.w = L""; + psr.lastName.w = L""; + psr.id.w = L""; + ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_DATA, this, (LPARAM)&psr); + + ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, this, 0); + mir_free(param); +} + +HANDLE CDiscordProto::SearchAdvanced(HWND hwndDlg) +{ + if (!m_bOnline || !IsWindow(hwndDlg)) + return nullptr; + + wchar_t wszNick[200]; + GetDlgItemTextW(hwndDlg, IDC_NICK, wszNick, _countof(wszNick)); + if (wszNick[0] == 0) // empty string? reject + return nullptr; + + wchar_t *p = wcschr(wszNick, '#'); + if (p == nullptr) // wrong user id + return nullptr; + + ForkThread(&CDiscordProto::SearchThread, mir_wstrdup(wszNick)); + return this; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// Basic search - by SnowFlake + +void CDiscordProto::OnReceiveUserinfo(MHttpResponse *pReply, AsyncHttpRequest *) +{ + JsonReply root(pReply); + if (!root) { + ProtoBroadcastAck(0, ACKTYPE_SEARCH, ACKRESULT_FAILED, this); + return; + } + + ForkThread(&CDiscordProto::SearchThread, getNick(root.data()).Detach()); +} + +HANDLE CDiscordProto::SearchBasic(const wchar_t *wszId) +{ + if (!m_bOnline) + return nullptr; + + CMStringA szUrl = "/users/"; + szUrl.AppendFormat(ptrA(mir_utf8encodeW(wszId))); + Push(new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveUserinfo)); + return (HANDLE)1; // Success +} + //////////////////////////////////////////////////////////////////////////////////////// // SendMsg @@ -643,8 +645,26 @@ bool CDiscordProto::OnContactDeleted(MCONTACT hContact, uint32_t flags) return true; } +void CDiscordProto::OnEventDeleted(MCONTACT hContact, MEVENT hEvent, int flags) +{ + // the command arrived from the server, don't send it back then + if (!(flags & CDF_DEL_HISTORY)) + return; + + DB::EventInfo dbei(hEvent, false); + if (dbei && dbei.szId) { + CMStringA szUrl(FORMAT, "/channels/%lld/messages/%s", getId(hContact, DB_KEY_ID), dbei.szId); + Push(new AsyncHttpRequest(this, REQUEST_DELETE, szUrl, 0)); + } +} + ///////////////////////////////////////////////////////////////////////////////////////// +INT_PTR CDiscordProto::CDiscordProto::SvcEmptyServerHistory(WPARAM, LPARAM) +{ + return 0; +} + INT_PTR CDiscordProto::RequestFriendship(WPARAM hContact, LPARAM) { AuthRequest(hContact, 0); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 7099600f52..53eac9d78d 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -452,6 +452,7 @@ public: void OnBuildProtoMenu() override; bool OnContactDeleted(MCONTACT, uint32_t flags) override; + void OnEventDeleted(MCONTACT, MEVENT, int flags) override; MWindow OnCreateAccMgrUI(MWindow) override; void OnMarkRead(MCONTACT, MEVENT) override; void OnModulesLoaded() override; @@ -463,6 +464,7 @@ public: INT_PTR __cdecl RequestFriendship(WPARAM, LPARAM); INT_PTR __cdecl SvcLeaveChat(WPARAM, LPARAM); + INT_PTR __cdecl SvcEmptyServerHistory(WPARAM, LPARAM); INT_PTR __cdecl GetAvatarCaps(WPARAM, LPARAM); INT_PTR __cdecl GetAvatarInfo(WPARAM, LPARAM); -- cgit v1.2.3