summaryrefslogtreecommitdiff
path: root/protocols/Discord
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2017-02-08 21:59:09 +0300
committerGeorge Hazan <ghazan@miranda.im>2017-02-08 21:59:17 +0300
commit19535676169fea739dec9b582ad1470c768faae9 (patch)
tree59dc056ee5a5aeb5da330ada4b58ee9714aa9c22 /protocols/Discord
parent5b00b966c446a17b23e5ca38504c829e064e08b8 (diff)
Discord: support for changing nick names in chats
Diffstat (limited to 'protocols/Discord')
-rw-r--r--protocols/Discord/src/dispatch.cpp29
-rw-r--r--protocols/Discord/src/groupchat.cpp116
-rw-r--r--protocols/Discord/src/main.cpp2
-rw-r--r--protocols/Discord/src/proto.h13
-rw-r--r--protocols/Discord/src/stdafx.h4
5 files changed, 134 insertions, 30 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index fe581e61b9..e28f6f70d2 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -34,6 +34,7 @@ static handlers[] = // these structures must me sorted alphabetically
{ L"GUILD_CREATE", &CDiscordProto::OnCommandGuildCreate },
{ L"GUILD_DELETE", &CDiscordProto::OnCommandGuildDelete },
{ L"GUILD_MEMBER_REMOVE", &CDiscordProto::OnCommandGuildRemoveMember },
+ { L"GUILD_MEMBER_UPDATE", &CDiscordProto::OnCommandGuildUpdateMember },
{ L"GUILD_SYNC", &CDiscordProto::OnCommandGuildSync },
{ L"MESSAGE_ACK", &CDiscordProto::OnCommandMessageAck },
@@ -260,6 +261,34 @@ void CDiscordProto::OnCommandGuildRemoveMember(const JSONNode &pRoot)
}
}
+void CDiscordProto::OnCommandGuildUpdateMember(const JSONNode &pRoot)
+{
+ SnowFlake guildId = ::getId(pRoot["guild_id"]);
+ CMStringW wszUserId = pRoot["user"]["id"].as_mstring();
+ CMStringW wszUserNick = pRoot["nick"].as_mstring(), wszOldNick;
+
+ for (int i = 0; i < arUsers.getCount(); i++) {
+ CDiscordUser &pUser = arUsers[i];
+ if (pUser.guildId != guildId)
+ continue;
+
+ SESSION_INFO *si = pci->SM_FindSession(pUser.wszUsername, m_szModuleName);
+ if (si != nullptr) {
+ USERINFO *ui = pci->UM_FindUser(si->pUsers, wszUserId);
+ if (ui != nullptr)
+ wszOldNick = ui->pszNick;
+ }
+
+ GCDEST gcd = { m_szModuleName, pUser.wszUsername, GC_EVENT_NICK };
+ GCEVENT gce = { &gcd };
+ gce.ptszUID = wszUserId;
+ gce.ptszNick = wszOldNick;
+ gce.ptszText = wszUserNick;
+ Chat_Event(&gce);
+ }
+}
+
+
/////////////////////////////////////////////////////////////////////////////////////////
// reading a new message
diff --git a/protocols/Discord/src/groupchat.cpp b/protocols/Discord/src/groupchat.cpp
index fa317b5171..13c2690bd2 100644
--- a/protocols/Discord/src/groupchat.cpp
+++ b/protocols/Discord/src/groupchat.cpp
@@ -17,6 +17,92 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
+enum {
+ IDM_CANCEL,
+
+ IDM_CHANGENICK, IDM_INVITE
+};
+
+static gc_item sttLogListItems[] =
+{
+ { LPGENW("Change &nickname"), IDM_CHANGENICK, MENU_ITEM },
+ { LPGENW("&Invite a user"), IDM_INVITE, MENU_ITEM },
+};
+
+int CDiscordProto::GroupchatMenuHook(WPARAM, LPARAM lParam)
+{
+ GCMENUITEMS* gcmi = (GCMENUITEMS*)lParam;
+ if (gcmi == NULL)
+ return 0;
+
+ if (mir_strcmpi(gcmi->pszModule, m_szModuleName))
+ return 0;
+
+ CDiscordUser *pChat = FindUserByChannel(_wtoi64(gcmi->pszID));
+ if (pChat == NULL)
+ return 0;
+
+ if (gcmi->Type == MENU_ON_LOG) {
+ gcmi->nItems = _countof(sttLogListItems);
+ gcmi->Item = sttLogListItems;
+ }
+
+ return 0;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+void CDiscordProto::Chat_SendPrivateMessage(GCHOOK *gch)
+{
+ SnowFlake userId = _wtoi64(gch->ptszUID);
+
+ MCONTACT hContact;
+ CDiscordUser *pUser = FindUser(userId);
+ if (pUser == NULL) {
+ PROTOSEARCHRESULT psr = { sizeof(psr) };
+ psr.id.w = (wchar_t*)gch->ptszUID;
+ psr.nick.w = (wchar_t*)gch->ptszNick;
+ if ((hContact = AddToList(PALF_TEMPORARY, &psr)) == 0)
+ return;
+
+ setId(hContact, DB_KEY_ID, userId);
+ setId(hContact, DB_KEY_CHANNELID, _wtoi64(gch->pDest->ptszID));
+ setWString(hContact, "Nick", gch->ptszNick);
+ db_set_b(hContact, "CList", "Hidden", 1);
+ db_set_dw(hContact, "Ignore", "Mask1", 0);
+ }
+ else hContact = pUser->hContact;
+
+ CallService(MS_MSG_SENDMESSAGE, hContact, 0);
+}
+
+void CDiscordProto::Chat_ProcessLogMenu(GCHOOK *gch)
+{
+ CDiscordUser *pUser = FindUserByChannel(_wtoi64(gch->pDest->ptszID));
+ if (pUser == NULL)
+ return;
+
+ switch (gch->dwData) {
+ case IDM_INVITE:
+ break;
+
+ case IDM_CHANGENICK:
+ ENTER_STRING es = { sizeof(es) };
+ es.caption = TranslateT("Enter your new nick name:");
+ es.type = ESF_COMBO;
+ es.szModuleName = m_szModuleName;
+ es.szDataPrefix = "chat_nick";
+ es.recentCount = 5;
+ if (EnterString(&es)) {
+ JSONNode root; root << WCHAR_PARAM("nick", es.ptszResult);
+ CMStringA szUrl(FORMAT, "/guilds/%lld/members/@me/nick", pUser->guildId);
+ Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, NULL, &root));
+ mir_free(es.ptszResult);
+ }
+ break;
+ }
+}
+
int CDiscordProto::GroupchatEventHook(WPARAM, LPARAM lParam)
{
GCHOOK *gch = (GCHOOK*)lParam;
@@ -43,38 +129,16 @@ int CDiscordProto::GroupchatEventHook(WPARAM, LPARAM lParam)
break;
case GC_USER_PRIVMESS:
- MCONTACT hContact;
- {
- SnowFlake userId = _wtoi64(gch->ptszUID);
-
- CDiscordUser *pUser = FindUser(userId);
- if (pUser == NULL) {
- PROTOSEARCHRESULT psr = { sizeof(psr) };
- psr.id.w = (wchar_t*)gch->ptszUID;
- psr.nick.w = (wchar_t*)gch->ptszNick;
- if ((hContact = AddToList(PALF_TEMPORARY, &psr)) == 0)
- return 0;
-
- setId(hContact, DB_KEY_ID, userId);
- setId(hContact, DB_KEY_CHANNELID, _wtoi64(gch->pDest->ptszID));
- setWString(hContact, "Nick", gch->ptszNick);
- db_set_b(hContact, "CList", "Hidden", 1);
- db_set_dw(hContact, "Ignore", "Mask1", 0);
- }
- else hContact = pUser->hContact;
- }
- CallService(MS_MSG_SENDMESSAGE, hContact, 0);
+ Chat_SendPrivateMessage(gch);
break;
case GC_USER_LOGMENU:
+ Chat_ProcessLogMenu(gch);
+ break;
+
case GC_USER_NICKLISTMENU:
break;
}
return 0;
}
-
-int CDiscordProto::GroupchatMenuHook(WPARAM, LPARAM)
-{
- return 0;
-}
diff --git a/protocols/Discord/src/main.cpp b/protocols/Discord/src/main.cpp
index dbf194d708..76d50752bc 100644
--- a/protocols/Discord/src/main.cpp
+++ b/protocols/Discord/src/main.cpp
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
+CHAT_MANAGER *pci;
HINSTANCE g_hInstance;
int hLangpack = 0;
HWND g_hwndHeartbeat;
@@ -74,6 +75,7 @@ static int protoUninit(PROTO_INTERFACE *proto)
extern "C" int __declspec(dllexport) Load(void)
{
mir_getLP(&pluginInfo);
+ pci = Chat_GetInterface();
g_hwndHeartbeat = CreateWindowEx(0, L"STATIC", NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index 29d83b69d5..fea2cf5ee7 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -199,6 +199,15 @@ class CDiscordProto : public PROTO<CDiscordProto>
HGENMENU m_hMenuLeaveGuild;
//////////////////////////////////////////////////////////////////////////////////////
+ // group chats
+
+ int __cdecl GroupchatEventHook(WPARAM, LPARAM);
+ int __cdecl GroupchatMenuHook(WPARAM, LPARAM);
+
+ void Chat_SendPrivateMessage(GCHOOK *gch);
+ void Chat_ProcessLogMenu(GCHOOK *gch);
+
+ //////////////////////////////////////////////////////////////////////////////////////
// misc methods
SnowFlake getId(const char *szName);
@@ -248,15 +257,13 @@ public:
int __cdecl OnOptionsInit(WPARAM, LPARAM);
int __cdecl OnDbEventRead(WPARAM, LPARAM);
- int __cdecl GroupchatEventHook(WPARAM, LPARAM);
- int __cdecl GroupchatMenuHook(WPARAM, LPARAM);
-
// dispatch commands
void OnCommandChannelCreated(const JSONNode&);
void OnCommandChannelDeleted(const JSONNode&);
void OnCommandGuildCreate(const JSONNode&);
void OnCommandGuildDelete(const JSONNode&);
void OnCommandGuildRemoveMember(const JSONNode&);
+ void OnCommandGuildUpdateMember(const JSONNode&);
void OnCommandGuildSync(const JSONNode&);
void OnCommandFriendAdded(const JSONNode&);
void OnCommandFriendRemoved(const JSONNode&);
diff --git a/protocols/Discord/src/stdafx.h b/protocols/Discord/src/stdafx.h
index c87339aeb8..a5022fb7d6 100644
--- a/protocols/Discord/src/stdafx.h
+++ b/protocols/Discord/src/stdafx.h
@@ -68,4 +68,6 @@ SnowFlake getId(const JSONNode &pNode);
CMStringW PrepareMessageText(const JSONNode &pRoot);
int StrToStatus(const CMStringW &str);
time_t StringToDate(const CMStringW &str);
-int SerialNext(void); \ No newline at end of file
+int SerialNext(void);
+
+struct SESSION_INFO : public GCSessionInfoBase {}; \ No newline at end of file