diff options
-rw-r--r-- | protocols/Discord/src/connection.cpp | 37 | ||||
-rw-r--r-- | protocols/Discord/src/http.cpp | 10 | ||||
-rw-r--r-- | protocols/Discord/src/options.cpp | 27 | ||||
-rw-r--r-- | protocols/Discord/src/proto.cpp | 7 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 11 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 59 |
6 files changed, 104 insertions, 47 deletions
diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index df2479a2a4..df3dbfa440 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -63,6 +63,9 @@ void CDiscordProto::OnLoggedIn() debugLogA("CDiscordProto::OnLoggedIn"); m_bOnline = true; SetServerStatus(m_iDesiredStatus); + + Push(new AsyncHttpRequest(this, REQUEST_GET, "/users/@me/guilds", &CDiscordProto::OnReceiveGuilds)); + Push(new AsyncHttpRequest(this, REQUEST_GET, "/users/@me/channels", &CDiscordProto::OnReceiveChannels)); } void CDiscordProto::OnLoggedOut() @@ -96,23 +99,6 @@ void CDiscordProto::ConnectionFailed(int iReason) ShutdownSession(); } -bool CDiscordProto::TryToConnect(void) -{ - ptrW wszLogin(getWStringA(DB_KEY_EMAIL)), wszPassword(getWStringA(DB_KEY_PASSWORD)); - if (wszLogin == NULL) { - ConnectionFailed(LOGINERR_BADUSERID); - return false; - } - if (wszPassword == NULL) { - ConnectionFailed(LOGINERR_WRONGPASSWORD); - return false; - } - - JSONNode root; root << WCHAR_PARAM("email", wszLogin) << WCHAR_PARAM("password", wszPassword); - Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/login", &CDiscordProto::OnReceiveToken, &root)); - return true; -} - void CDiscordProto::ServerThread(void*) { m_szAccessToken = getStringA("AccessToken"); @@ -124,8 +110,21 @@ void CDiscordProto::ServerThread(void*) if (m_szAccessToken != NULL) // try to receive a response from server RetrieveMyInfo(); - else if (!TryToConnect()) - return; + else { + if (mir_wstrlen(m_wszEmail) == NULL) { + ConnectionFailed(LOGINERR_BADUSERID); + return; + } + + ptrW wszPassword(getWStringA(DB_KEY_PASSWORD)); + if (wszPassword == NULL) { + ConnectionFailed(LOGINERR_WRONGPASSWORD); + return; + } + + JSONNode root; root << WCHAR_PARAM("email", m_wszEmail) << WCHAR_PARAM("password", wszPassword); + Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/login", &CDiscordProto::OnReceiveToken, &root)); + } while (true) { WaitForSingleObject(m_evRequestsQueue, 1000); diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 3e513ee8d0..ed154569d3 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -49,17 +49,17 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR else m_szUrl = _url; flags = NLHRF_HTTP11 | NLHRF_REDIRECT | NLHRF_SSL; - if (ppro->m_szAccessToken == NULL) - flags |= NLHRF_NODUMPSEND; - else + if (ppro->m_szAccessToken != NULL) { + AddHeader("Authorization", ppro->m_szAccessToken); flags |= NLHRF_DUMPASTEXT; + } + else flags |= NLHRF_NODUMPSEND; + AddHeader("Content-Type", "application/json"); if (pRoot != NULL) { ptrW text(json_write(pRoot)); pData = mir_utf8encodeW(text); dataLength = (int)mir_strlen(pData); - - AddHeader("Content-Type", "application/json"); } requestType = iRequestType; diff --git a/protocols/Discord/src/options.cpp b/protocols/Discord/src/options.cpp index 2f16811b6a..d57f8d27af 100644 --- a/protocols/Discord/src/options.cpp +++ b/protocols/Discord/src/options.cpp @@ -20,39 +20,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. class CDiscardAccountOptions : public CProtoDlgBase<CDiscordProto> { CCtrlEdit m_edGroup, m_edUserName, m_edPassword; + ptrW m_wszOldGroup; public: CDiscardAccountOptions(CDiscordProto *ppro) : CProtoDlgBase<CDiscordProto>(ppro, IDD_OPTIONS_ACCOUNT), m_edGroup(this, IDC_GROUP), m_edUserName(this, IDC_USERNAME), - m_edPassword(this, IDC_PASSWORD) - {} + m_edPassword(this, IDC_PASSWORD), + m_wszOldGroup(mir_wstrdup(ppro->m_wszDefaultGroup)) + { + CreateLink(m_edGroup, ppro->m_wszDefaultGroup); + CreateLink(m_edUserName, ppro->m_wszEmail); + } virtual void OnInitDialog() override { - ptrW buf(m_proto->getWStringA(DB_KEY_EMAIL)); - if (buf) - m_edUserName.SetText(buf); - - buf = m_proto->getWStringA(DB_KEY_PASSWORD); + ptrW buf(m_proto->getWStringA(DB_KEY_PASSWORD)); if (buf) m_edPassword.SetText(buf); - - buf = m_proto->getWStringA(DB_KEY_GROUP); - m_edGroup.SetText(buf ? buf : DB_KEYVAL_GROUP); } virtual void OnApply() override { - ptrW buf(m_edUserName.GetText()); - m_proto->setWString(DB_KEY_EMAIL, buf); + if (mir_wstrcmp(m_proto->m_wszDefaultGroup, m_wszOldGroup)) + Clist_GroupCreate(NULL, m_proto->m_wszDefaultGroup); - buf = m_edPassword.GetText(); + ptrW buf(m_edPassword.GetText()); m_proto->setWString(DB_KEY_PASSWORD, buf); - - buf = m_edGroup.GetText(); - m_proto->setWString(DB_KEY_GROUP, buf); } }; diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index c44cab39bc..39ec101779 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -25,7 +25,9 @@ static int compareRequests(const AsyncHttpRequest *p1, const AsyncHttpRequest *p CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : PROTO<CDiscordProto>(proto_name, username), m_arHttpQueue(10, compareRequests), - m_evRequestsQueue(CreateEvent(NULL, FALSE, FALSE, NULL)) + m_evRequestsQueue(CreateEvent(NULL, FALSE, FALSE, NULL)), + m_wszDefaultGroup(this, DB_KEY_GROUP, DB_KEYVAL_GROUP), + m_wszEmail(this, DB_KEY_EMAIL, L"") { // Services CreateProtoService(PS_GETNAME, &CDiscordProto::GetName); @@ -34,6 +36,9 @@ CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : // Events HookProtoEvent(ME_OPT_INITIALISE, &CDiscordProto::OnOptionsInit); + // Clist + Clist_GroupCreate(NULL, m_wszDefaultGroup); + // Network initialization CMStringW descr(FORMAT, TranslateT("%s server connection"), m_tszUserName); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 7f4c1e4c52..a39325b77e 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -74,11 +74,11 @@ __forceinline JSONNode& operator<<(JSONNode &json, const WCHAR_PARAM ¶m) class CDiscordProto : public PROTO<CDiscordProto> { friend struct AsyncHttpRequest; + friend class CDiscardAccountOptions; void __cdecl ServerThread(void*); void SetAllContactStatuses(int iStatus); - bool TryToConnect(void); void ConnectionFailed(int iReason); void ShutdownSession(void); @@ -99,6 +99,9 @@ class CDiscordProto : public PROTO<CDiscordProto> m_bOnline, // protocol is online m_bTerminated; // Miranda's going down + CMOption<wchar_t*> m_wszEmail; // my own email + CMOption<wchar_t*> m_wszDefaultGroup; // clist group to store contacts + public: CDiscordProto(const char*,const wchar_t*); ~CDiscordProto(); @@ -123,8 +126,12 @@ public: void OnLoggedOut(); void OnReceiveToken(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveMyInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveGuilds(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveChannels(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + + void RetrieveMyInfo(); // Misc - void RetrieveMyInfo(void); void SetServerStatus(int iStatus); }; diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 62d2dfb17c..d0550dccbb 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -19,29 +19,77 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. void CDiscordProto::RetrieveMyInfo() { + Push(new AsyncHttpRequest(this, REQUEST_GET, "/users/@me", &CDiscordProto::OnReceiveMyInfo)); +} +void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +{ + if (pReply->resultCode != 200) { + ConnectionFailed(LOGINERR_WRONGPASSWORD); + return; + } + + JSONNode *root = json_parse(pReply->pData); + if (root == NULL) { + ConnectionFailed(LOGINERR_NOSERVER); + return; + } + + setWString("Username", root->at("username").as_mstring()); + setByte("MfaEnabled", root->at("mfa_enabled").as_bool()); + setWString("id", root->at("id").as_mstring()); + setWString("AvatarHash", root->at("avatar").as_mstring()); + setDword("Discriminator", root->at("discriminator").as_int()); + setWString("Email", root->at("email").as_mstring()); + + OnLoggedIn(); } +///////////////////////////////////////////////////////////////////////////////////////// + void CDiscordProto::SetServerStatus(int iStatus) { if (!m_bOnline) return; + if (iStatus == ID_STATUS_OFFLINE) + Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/logout", NULL)); + int iOldStatus = m_iStatus; m_iStatus = iStatus; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)iOldStatus, m_iStatus); } +///////////////////////////////////////////////////////////////////////////////////////// + +void CDiscordProto::OnReceiveChannels(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +{ + if (pReply->resultCode != 200) + return; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +void CDiscordProto::OnReceiveGuilds(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) +{ + if (pReply->resultCode != 200) + return; +} + +///////////////////////////////////////////////////////////////////////////////////////// + void CDiscordProto::OnReceiveToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { if (pReply->resultCode != 200) { -LBL_Error: ConnectionFailed(LOGINERR_WRONGPASSWORD); return; } JSONNode *root = json_parse(pReply->pData); - if (root == NULL) - goto LBL_Error; + if (root == NULL) { +LBL_Error: + ConnectionFailed(LOGINERR_NOSERVER); + return; + } CMStringA szToken = root->at("token").as_mstring(); if (szToken.IsEmpty()) @@ -49,5 +97,8 @@ LBL_Error: m_szAccessToken = szToken.Detach(); setString("AccessToken", m_szAccessToken); - OnLoggedIn(); + + RetrieveMyInfo(); } + +///////////////////////////////////////////////////////////////////////////////////////// |