diff options
author | George Hazan <ghazan@miranda.im> | 2016-12-28 20:30:46 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2016-12-28 20:30:46 +0300 |
commit | 3e34adae711d9ddf79b4959143a696b6a403a522 (patch) | |
tree | aebc8cc38c164558e6da1d9c30971592045e1617 /protocols/Discord | |
parent | e7a87b3c82204e048f23750a18b456e567a6f436 (diff) |
first version that logs in successfully
Diffstat (limited to 'protocols/Discord')
-rw-r--r-- | protocols/Discord/src/connection.cpp | 7 | ||||
-rw-r--r-- | protocols/Discord/src/http.cpp | 27 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 20 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 24 | ||||
-rw-r--r-- | protocols/Discord/src/version.h | 2 |
5 files changed, 61 insertions, 19 deletions
diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index 13a12129e2..df2479a2a4 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -107,10 +107,9 @@ bool CDiscordProto::TryToConnect(void) ConnectionFailed(LOGINERR_WRONGPASSWORD); return false; } - - Push(new AsyncHttpRequest(this, REQUEST_GET, "/user/login", &CDiscordProto::OnReceiveToken) - << WCHAR_PARAM("email", wszLogin) - << WCHAR_PARAM("password", wszPassword)); + + JSONNode root; root << WCHAR_PARAM("email", wszLogin) << WCHAR_PARAM("password", wszPassword); + Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/login", &CDiscordProto::OnReceiveToken, &root)); return true; } diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 5333d87a65..3e513ee8d0 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -35,27 +35,32 @@ static LONG g_reqNum = 0; AsyncHttpRequest::AsyncHttpRequest() { cbSize = sizeof(NETLIBHTTPREQUEST); - AddHeader("Connection", "keep-alive"); - pUserInfo = NULL; - m_iErrorCode = 0; - m_pCallback = NULL; m_iReqNum = ::InterlockedIncrement(&g_reqNum); } -AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR _url, HttpCallback pFunc) +AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR _url, HttpCallback pFunc, JSONNode *pRoot) { cbSize = sizeof(NETLIBHTTPREQUEST); - AddHeader("Connection", "keep-alive"); - + if (*_url == '/') { // relative url leads to a site - m_szUrl = "https://discardapp.com"; + m_szUrl = "https://discordapp.com/api"; m_szUrl += _url; } else m_szUrl = _url; - flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT | NLHRF_SSL; - if (ppro->m_szAccessToken != NULL) - this << CHAR_PARAM("access_token", ppro->m_szAccessToken); + flags = NLHRF_HTTP11 | NLHRF_REDIRECT | NLHRF_SSL; + if (ppro->m_szAccessToken == NULL) + flags |= NLHRF_NODUMPSEND; + else + flags |= NLHRF_DUMPASTEXT; + + if (pRoot != NULL) { + ptrW text(json_write(pRoot)); + pData = mir_utf8encodeW(text); + dataLength = (int)mir_strlen(pData); + + AddHeader("Content-Type", "application/json"); + } requestType = iRequestType; m_pCallback = pFunc; diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 5a381e52f9..7f4c1e4c52 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -5,7 +5,7 @@ typedef void (CDiscordProto::*HttpCallback)(NETLIBHTTPREQUEST*, struct AsyncHttp struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject { AsyncHttpRequest(); - AsyncHttpRequest(CDiscordProto*, int iRequestType, LPCSTR szUrl, HttpCallback pFunc); + AsyncHttpRequest(CDiscordProto*, int iRequestType, LPCSTR szUrl, HttpCallback pFunc, JSONNode *pNode = NULL); ~AsyncHttpRequest(); void AddHeader(LPCSTR, LPCSTR); @@ -51,6 +51,24 @@ struct WCHAR_PARAM : public PARAM }; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const WCHAR_PARAM&); +__forceinline JSONNode& operator<<(JSONNode &json, const INT_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, param.iValue)); + return json; +} + +__forceinline JSONNode& operator<<(JSONNode &json, const CHAR_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, param.szValue)); + return json; +} + +__forceinline JSONNode& operator<<(JSONNode &json, const WCHAR_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, ptrA(mir_utf8encodeW(param.wszValue)))); + return json; +} + ///////////////////////////////////////////////////////////////////////////////////////// class CDiscordProto : public PROTO<CDiscordProto> diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 4b7c2afa13..62d2dfb17c 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -24,10 +24,30 @@ void CDiscordProto::RetrieveMyInfo() void CDiscordProto::SetServerStatus(int iStatus) { + if (!m_bOnline) + return; + int iOldStatus = m_iStatus; m_iStatus = iStatus; + ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)iOldStatus, m_iStatus); } -void CDiscordProto::OnReceiveToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq) +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; + + CMStringA szToken = root->at("token").as_mstring(); + if (szToken.IsEmpty()) + goto LBL_Error; + + m_szAccessToken = szToken.Detach(); + setString("AccessToken", m_szAccessToken); + OnLoggedIn(); } diff --git a/protocols/Discord/src/version.h b/protocols/Discord/src/version.h index 9a855bc969..056cd06fa5 100644 --- a/protocols/Discord/src/version.h +++ b/protocols/Discord/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 0 #define __RELEASE_NUM 0 -#define __BUILD_NUM 1 +#define __BUILD_NUM 2 #include <stdver.h> |