diff options
author | George Hazan <ghazan@miranda.im> | 2017-01-17 20:57:02 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2017-01-17 20:57:02 +0300 |
commit | ad61413a521ea9cebd849b659653a21c2c33ff8a (patch) | |
tree | 9029a623fbbe1491f86a68cbaa92b6b45c786521 | |
parent | b327ed7872ca83c3a4249039ba1a3d8dd3ece630 (diff) |
Discord: auth support
-rw-r--r-- | protocols/Discord/src/dispatch.cpp | 5 | ||||
-rw-r--r-- | protocols/Discord/src/proto.cpp | 41 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 4 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 9 | ||||
-rw-r--r-- | protocols/Discord/src/stdafx.h | 1 | ||||
-rw-r--r-- | protocols/Discord/src/utils.cpp | 6 | ||||
-rw-r--r-- | protocols/MRA/src/MraProto.cpp | 3 | ||||
-rw-r--r-- | protocols/MSN/src/msn_proto.cpp | 3 |
8 files changed, 60 insertions, 12 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index ba58f4cd16..1886e9d106 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -166,9 +166,8 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot) for (auto it = relations.begin(); it != relations.end(); ++it) { const JSONNode &p = *it; - const JSONNode &user = p["user"]; - if (user) - PrepareUser(user); + CDiscordUser *pUser = PrepareUser(p["user"]); + ProcessType(pUser, p); } const JSONNode &channels = pRoot["private_channels"]; diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 31a12fbdac..814ca19c0d 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -228,6 +228,9 @@ HANDLE CDiscordProto::SearchBasic(const wchar_t *wszId) return (HANDLE)1; // Success } +//////////////////////////////////////////////////////////////////////////////////////// +// Authorization + int CDiscordProto::AuthRequest(MCONTACT hContact, const wchar_t*) { ptrW wszUsername(getWStringA(hContact, DB_KEY_NICK)); @@ -242,6 +245,42 @@ int CDiscordProto::AuthRequest(MCONTACT hContact, const wchar_t*) return 0; } +int CDiscordProto::AuthRecv(MCONTACT, PROTORECVEVENT *pre) +{ + return Proto_AuthRecv(m_szModuleName, pre); +} + +int CDiscordProto::Authorize(MEVENT hDbEvent) +{ + DBEVENTINFO dbei = {}; + if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) return 1; + if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == NULL) return 1; + if (db_event_get(hDbEvent, &dbei)) return 1; + if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; + if (mir_strcmp(dbei.szModule, m_szModuleName)) return 1; + + MCONTACT hContact = DbGetAuthEventContact(&dbei); + CMStringA szUrl(FORMAT, "/users/@me/relationships/%lld", getId(hContact, DB_KEY_ID)); + Push(new AsyncHttpRequest(this, REQUEST_PUT, szUrl, NULL)); + return 0; +} + +int CDiscordProto::AuthDeny(MEVENT hDbEvent, const wchar_t*) +{ + DBEVENTINFO dbei = {}; + if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) return 1; + if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == NULL) return 1; + if (db_event_get(hDbEvent, &dbei)) return 1; + if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; + if (mir_strcmp(dbei.szModule, m_szModuleName)) return 1; + + MCONTACT hContact = DbGetAuthEventContact(&dbei); + RemoveFriend(getId(hContact, DB_KEY_ID)); + return 0; +} + +//////////////////////////////////////////////////////////////////////////////////////// + MCONTACT CDiscordProto::AddToList(int flags, PROTOSEARCHRESULT *psr) { if (psr->id.w == NULL) @@ -367,7 +406,7 @@ int CDiscordProto::OnDeleteContact(MCONTACT hContact) Push(new AsyncHttpRequest(this, REQUEST_DELETE, CMStringA(FORMAT, "/channels/%lld", pUser->channelId), NULL)); if (pUser->id) - Push(new AsyncHttpRequest(this, REQUEST_DELETE, CMStringA(FORMAT, "/users/@me/relationships/%lld", pUser->id), NULL)); + RemoveFriend(pUser->id); return 0; } diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index f201306e9b..33a316c634 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -203,6 +203,9 @@ public: virtual HANDLE __cdecl SearchBasic(const wchar_t* id) override; virtual MCONTACT __cdecl AddToList(int flags, PROTOSEARCHRESULT* psr) override; + virtual int __cdecl AuthRecv(MCONTACT, PROTORECVEVENT* pre) override; + virtual int __cdecl Authorize(MEVENT hDbEvent) override; + virtual int __cdecl AuthDeny(MEVENT hDbEvent, const wchar_t* szReason) override; virtual int __cdecl AuthRequest(MCONTACT hContact, const wchar_t*) override; virtual int __cdecl RecvMsg(MCONTACT hContact, PROTORECVEVENT *evt) override; @@ -261,6 +264,7 @@ public: // Misc void ProcessType(CDiscordUser *pUser, const JSONNode&); void SetServerStatus(int iStatus); + void RemoveFriend(SnowFlake id); CMStringW GetAvatarFilename(MCONTACT hContact); diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 58742a63d4..ee4aa23204 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -18,6 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "stdafx.h" ///////////////////////////////////////////////////////////////////////////////////////// +// removes a friend from the server + +void CDiscordProto::RemoveFriend(SnowFlake id) +{ + Push(new AsyncHttpRequest(this, REQUEST_DELETE, CMStringA(FORMAT, "/users/@me/relationships/%lld", id), NULL)); +} + +///////////////////////////////////////////////////////////////////////////////////////// // retrieves server history void CDiscordProto::RetrieveHistory(MCONTACT hContact, CDiscordHitoryOp iOp, SnowFlake msgid, int iLimit) @@ -51,7 +59,6 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest return; DBEVENTINFO dbei = {}; - dbei.cbSize = sizeof(dbei); dbei.szModule = m_szModuleName; dbei.flags = DBEF_READ | DBEF_UTF; dbei.eventType = EVENTTYPE_MESSAGE; diff --git a/protocols/Discord/src/stdafx.h b/protocols/Discord/src/stdafx.h index 9bf1ba7cc1..1db7e3f848 100644 --- a/protocols/Discord/src/stdafx.h +++ b/protocols/Discord/src/stdafx.h @@ -58,6 +58,7 @@ extern HWND g_hwndHeartbeat; #define DB_KEY_AVHASH "AvatarHash" #define DB_KEY_CHANNELID "ChannelID" #define DB_KEY_LASTMSGID "LastMessageID" +#define DB_KEY_REQAUTH "ReqAuth" #define DB_KEY_GROUP "GroupName" #define DB_KEYVAL_GROUP L"Discord" diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index 4d1747c3f9..15246ce46e 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -187,13 +187,13 @@ void CDiscordProto::ProcessType(CDiscordUser *pUser, const JSONNode &pRoot) switch (pRoot["type"].as_int()) { case 1: // confirmed db_unset(pUser->hContact, "CList", "NotOnList"); - delSetting(pUser->hContact, "FakeAuth"); + delSetting(pUser->hContact, DB_KEY_REQAUTH); break; case 3: // expecting authorization db_set_b(pUser->hContact, "CList", "NotOnList", 1); - if (!getByte(pUser->hContact, "FakeAuth", 0)) { - setByte(pUser->hContact, "FakeAuth", 1); + if (!getByte(pUser->hContact, DB_KEY_REQAUTH, 0)) { + setByte(pUser->hContact, DB_KEY_REQAUTH, 1); CMStringA szId(FORMAT, "%lld", pUser->id); DB_AUTH_BLOB blob(pUser->hContact, T2Utf(pUser->wszUsername), NULL, NULL, szId, NULL); diff --git a/protocols/MRA/src/MraProto.cpp b/protocols/MRA/src/MraProto.cpp index ede27df68b..75a684f233 100644 --- a/protocols/MRA/src/MraProto.cpp +++ b/protocols/MRA/src/MraProto.cpp @@ -214,8 +214,7 @@ int CMraProto::AuthDeny(MEVENT hDBEvent, const wchar_t* szReason) int CMraProto::AuthRecv(MCONTACT, PROTORECVEVENT* pre)
{
- Proto_AuthRecv(m_szModuleName, pre);
- return 0;
+ return Proto_AuthRecv(m_szModuleName, pre);
}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/protocols/MSN/src/msn_proto.cpp b/protocols/MSN/src/msn_proto.cpp index bde9e9c7f2..f159dfac32 100644 --- a/protocols/MSN/src/msn_proto.cpp +++ b/protocols/MSN/src/msn_proto.cpp @@ -282,8 +282,7 @@ MCONTACT __cdecl CMsnProto::AddToListByEvent(int flags, int, MEVENT hDbEvent) int CMsnProto::AuthRecv(MCONTACT, PROTORECVEVENT* pre)
{
- Proto_AuthRecv(m_szModuleName, pre);
- return 0;
+ return Proto_AuthRecv(m_szModuleName, pre);
}
// PSS_AUTHREQUEST
|