summaryrefslogtreecommitdiff
path: root/protocols/Discord
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-03-11 20:31:30 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-03-11 20:31:30 +0300
commit9e44f9c0c456278e8f77bfd94042f1337854e1c9 (patch)
tree55ad9eed2031cc09868458e766a50620318c3343 /protocols/Discord
parent367731ae14fcc861178a20527ee8be83b91f8ef3 (diff)
Discord: voice part of protocol moved to the separate file
Diffstat (limited to 'protocols/Discord')
-rw-r--r--protocols/Discord/src/dispatch.cpp68
-rw-r--r--protocols/Discord/src/proto.cpp4
-rw-r--r--protocols/Discord/src/proto.h4
-rw-r--r--protocols/Discord/src/voice.cpp116
4 files changed, 124 insertions, 68 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index 801bdd396c..04cb8a9b8e 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -76,74 +76,6 @@ GatewayHandlerFunc CDiscordProto::GetHandler(const wchar_t *pwszCommand)
}
/////////////////////////////////////////////////////////////////////////////////////////
-// call operations (voice & video)
-
-void CDiscordProto::OnCommandCallCreated(const JSONNode &pRoot)
-{
- for (auto &it : pRoot["voice_states"]) {
- SnowFlake channelId = ::getId(pRoot["channel_id"]);
- auto *pUser = FindUserByChannel(channelId);
- if (pUser == nullptr) {
- debugLogA("Call from unknown channel %lld, skipping", channelId);
- continue;
- }
-
- auto *pCall = new CDiscordVoiceCall();
- pCall->szId = it["session_id"].as_mstring();
- pCall->channelId = channelId;
- pCall->startTime = time(0);
- arVoiceCalls.insert(pCall);
-
- char *szMessage = TranslateU("Incoming call");
- DBEVENTINFO dbei = {};
- dbei.szModule = m_szModuleName;
- dbei.timestamp = pCall->startTime;
- dbei.eventType = EVENT_INCOMING_CALL;
- dbei.cbBlob = DWORD(mir_strlen(szMessage)+1);
- dbei.pBlob = (BYTE*)szMessage;
- dbei.flags = DBEF_UTF;
- db_event_add(pUser->hContact, &dbei);
- }
-}
-
-void CDiscordProto::OnCommandCallDeleted(const JSONNode &pRoot)
-{
- SnowFlake channelId = ::getId(pRoot["channel_id"]);
- auto *pUser = FindUserByChannel(channelId);
- if (pUser == nullptr) {
- debugLogA("Call from unknown channel %lld, skipping", channelId);
- return;
- }
-
- int elapsed = 0, currTime = time(0);
- for (auto &call : arVoiceCalls.rev_iter())
- if (call->channelId == channelId) {
- elapsed = currTime - call->startTime;
- arVoiceCalls.removeItem(&call);
- break;
- }
-
- if (!elapsed) {
- debugLogA("Call from channel %lld isn't registered, skipping", channelId);
- return;
- }
-
- CMStringA szMessage(FORMAT, TranslateU("Call ended, %d seconds long"), elapsed);
- DBEVENTINFO dbei = {};
- dbei.szModule = m_szModuleName;
- dbei.timestamp = currTime;
- dbei.eventType = EVENT_CALL_FINISHED;
- dbei.cbBlob = DWORD(szMessage.GetLength() + 1);
- dbei.pBlob = (BYTE *)szMessage.c_str();
- dbei.flags = DBEF_UTF;
- db_event_add(pUser->hContact, &dbei);
-}
-
-void CDiscordProto::OnCommandCallUpdated(const JSONNode &pRoot)
-{
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
// channel operations
void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot)
diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp
index 25cfdaffdc..232ffe94d7 100644
--- a/protocols/Discord/src/proto.cpp
+++ b/protocols/Discord/src/proto.cpp
@@ -64,10 +64,14 @@ CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) :
CreateProtoService(PS_MENU_REQAUTH, &CDiscordProto::RequestFriendship);
+ CreateProtoService(PS_VOICE_CAPS, &CDiscordProto::VoiceCaps);
+
// Events
HookProtoEvent(ME_OPT_INITIALISE, &CDiscordProto::OnOptionsInit);
HookProtoEvent(ME_DB_EVENT_MARKED_READ, &CDiscordProto::OnDbEventRead);
HookProtoEvent(ME_PROTO_ACCLISTCHANGED, &CDiscordProto::OnAccountChanged);
+
+ HookProtoEvent(PE_VOICE_CALL_STATE, &CDiscordProto::OnVoiceState);
// database
db_set_resident(m_szModuleName, "XStatusMsg");
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index d1a15f63f4..105b1368fc 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -373,6 +373,8 @@ public:
INT_PTR __cdecl GetMyAvatar(WPARAM, LPARAM);
INT_PTR __cdecl SetMyAvatar(WPARAM, LPARAM);
+ INT_PTR __cdecl VoiceCaps(WPARAM, LPARAM);
+
//////////////////////////////////////////////////////////////////////////////////////
// Events
@@ -380,6 +382,8 @@ public:
int __cdecl OnAccountChanged(WPARAM, LPARAM);
int __cdecl OnDbEventRead(WPARAM, LPARAM);
+ int __cdecl OnVoiceState(WPARAM, LPARAM);
+
//////////////////////////////////////////////////////////////////////////////////////
// dispatch commands
diff --git a/protocols/Discord/src/voice.cpp b/protocols/Discord/src/voice.cpp
new file mode 100644
index 0000000000..5a0fbc039f
--- /dev/null
+++ b/protocols/Discord/src/voice.cpp
@@ -0,0 +1,116 @@
+/*
+Copyright © 2016-20 Miranda NG team
+
+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, either version 2 of the License, or
+(at your option) any later version.
+
+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"
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// call operations (voice & video)
+
+void CDiscordProto::OnCommandCallCreated(const JSONNode &pRoot)
+{
+ for (auto &it : pRoot["voice_states"]) {
+ SnowFlake channelId = ::getId(pRoot["channel_id"]);
+ auto *pUser = FindUserByChannel(channelId);
+ if (pUser == nullptr) {
+ debugLogA("Call from unknown channel %lld, skipping", channelId);
+ continue;
+ }
+
+ auto *pCall = new CDiscordVoiceCall();
+ pCall->szId = it["session_id"].as_mstring();
+ pCall->channelId = channelId;
+ pCall->startTime = time(0);
+ arVoiceCalls.insert(pCall);
+
+ char *szMessage = TranslateU("Incoming call");
+ DBEVENTINFO dbei = {};
+ dbei.szModule = m_szModuleName;
+ dbei.timestamp = pCall->startTime;
+ dbei.eventType = EVENT_INCOMING_CALL;
+ dbei.cbBlob = DWORD(mir_strlen(szMessage) + 1);
+ dbei.pBlob = (BYTE *)szMessage;
+ dbei.flags = DBEF_UTF;
+ db_event_add(pUser->hContact, &dbei);
+ }
+}
+
+void CDiscordProto::OnCommandCallDeleted(const JSONNode &pRoot)
+{
+ SnowFlake channelId = ::getId(pRoot["channel_id"]);
+ auto *pUser = FindUserByChannel(channelId);
+ if (pUser == nullptr) {
+ debugLogA("Call from unknown channel %lld, skipping", channelId);
+ return;
+ }
+
+ int elapsed = 0, currTime = time(0);
+ for (auto &call : arVoiceCalls.rev_iter())
+ if (call->channelId == channelId) {
+ elapsed = currTime - call->startTime;
+ arVoiceCalls.removeItem(&call);
+ break;
+ }
+
+ if (!elapsed) {
+ debugLogA("Call from channel %lld isn't registered, skipping", channelId);
+ return;
+ }
+
+ CMStringA szMessage(FORMAT, TranslateU("Call ended, %d seconds long"), elapsed);
+ DBEVENTINFO dbei = {};
+ dbei.szModule = m_szModuleName;
+ dbei.timestamp = currTime;
+ dbei.eventType = EVENT_CALL_FINISHED;
+ dbei.cbBlob = DWORD(szMessage.GetLength() + 1);
+ dbei.pBlob = (BYTE *)szMessage.c_str();
+ dbei.flags = DBEF_UTF;
+ db_event_add(pUser->hContact, &dbei);
+}
+
+void CDiscordProto::OnCommandCallUpdated(const JSONNode &pRoot)
+{
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Events & services
+
+INT_PTR __cdecl CDiscordProto::VoiceCaps(WPARAM, LPARAM)
+{
+ return VOICE_CAPS_VOICE | VOICE_CAPS_CALL_CONTACT;
+}
+
+int __cdecl CDiscordProto::OnVoiceState(WPARAM wParam, LPARAM)
+{
+ auto *pVoice = (VOICE_CALL *)wParam;
+ if (mir_strcmp(pVoice->moduleName, m_szModuleName))
+ return 0;
+
+ CDiscordVoiceCall *pCall = nullptr;
+ for (auto &it : arVoiceCalls)
+ if (it->szId == pVoice->id) {
+ pCall = it;
+ break;
+ }
+
+ if (pCall == nullptr) {
+ debugLogA("Unknown call: %s, exiting", pVoice->id);
+ return 0;
+ }
+
+ debugLogA("Call %s state changed to %d", pVoice->id, pVoice->state);
+ return 0;
+}