1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
Copyright © 2016-22 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"
enum {
IDM_CANCEL,
IDM_COPY_ID,
IDM_CHANGENICK, IDM_CHANGETOPIC, IDM_RENAME, IDM_PASSOWNER,
IDM_DESTROY, IDM_LEAVE,
IDM_KICK, IDM_INVITE, IDM_ADD
};
static void sttDisableMenuItem(int nItems, gc_item *items, uint32_t id, bool disabled)
{
for (int i = 0; i < nItems; i++)
if (items[i].dwID == id)
items[i].bDisabled = disabled;
}
static void sttShowGcMenuItem(int nItems, gc_item *items, uint32_t id, int type)
{
for (int i = 0; i < nItems; i++)
if (items[i].dwID == id)
items[i].uType = type;
}
/////////////////////////////////////////////////////////////////////////////////////////
static int SortRolesByPosition(const CDiscordRole *p1, const CDiscordRole *p2)
{
return p1->position - p2->position;
}
void BuildStatusList(const CDiscordGuild *pGuild, SESSION_INFO *si)
{
Chat_AddGroup(si, L"@owner");
LIST<CDiscordRole> roles(pGuild->arRoles.getCount(), SortRolesByPosition);
for (auto &it : pGuild->arRoles)
roles.insert(it);
for (auto &it : roles)
Chat_AddGroup(si, it->wszName);
}
/////////////////////////////////////////////////////////////////////////////////////////
static gc_item sttLogListItems[] =
{
{ LPGENW("Change &nickname"), IDM_CHANGENICK, MENU_ITEM },
{ LPGENW("Invite users"), IDM_INVITE, MENU_ITEM },
{ LPGENW("Channel control"), FALSE, MENU_NEWPOPUP },
{ LPGENW("Change &topic"), IDM_CHANGETOPIC, MENU_POPUPITEM },
{ LPGENW("&Rename channel"), IDM_RENAME, MENU_POPUPITEM },
{ nullptr, 0, MENU_POPUPSEPARATOR },
{ LPGENW("Destroy channel"), IDM_DESTROY, MENU_POPUPITEM },
{ LPGENW("Leave channel"), IDM_LEAVE, MENU_POPUPITEM },
};
static gc_item sttNicklistItems[] =
{
{ LPGENW("Copy ID"), IDM_COPY_ID, MENU_ITEM },
{ LPGENW("Add friend"), IDM_ADD, MENU_ITEM },
{ nullptr, 0, MENU_SEPARATOR },
{ LPGENW("Kick user"), IDM_KICK, MENU_ITEM },
{ LPGENW("Make group owner"), IDM_PASSOWNER, MENU_ITEM },
};
int CDiscordProto::GroupchatMenuHook(WPARAM, LPARAM lParam)
{
GCMENUITEMS* gcmi = (GCMENUITEMS*)lParam;
if (gcmi == nullptr)
return 0;
if (mir_strcmpi(gcmi->pszModule, m_szModuleName))
return 0;
CDiscordUser *pChat = FindUserByChannel(_wtoi64(gcmi->pszID));
if (pChat == nullptr || pChat->si == nullptr)
return 0;
bool isOwner = getId(pChat->hContact, DB_KEY_OWNERID) == m_ownId;
if (gcmi->Type == MENU_ON_LOG) {
if (pChat->pGuild == nullptr)
sttShowGcMenuItem(_countof(sttLogListItems), sttLogListItems, IDM_CHANGENICK, 0);
sttShowGcMenuItem(_countof(sttLogListItems), sttLogListItems, IDM_LEAVE, isOwner ? 0 : MENU_POPUPITEM);
sttShowGcMenuItem(_countof(sttLogListItems), sttLogListItems, IDM_DESTROY, isOwner ? MENU_POPUPITEM : 0);
Chat_AddMenuItems(gcmi->hMenu, _countof(sttLogListItems), sttLogListItems, &g_plugin);
}
else if (gcmi->Type == MENU_ON_NICKLIST) {
SnowFlake userId = (gcmi->pszUID) ? _wtoi64(gcmi->pszUID) : 0;
bool isFriend = (userId == m_ownId) ? true : FindUser(userId) != 0;
sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_ADD, isFriend);
sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_KICK, !isOwner);
sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_PASSOWNER, !isOwner);
Chat_AddMenuItems(gcmi->hMenu, _countof(sttNicklistItems), sttNicklistItems, &g_plugin);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::Chat_SendPrivateMessage(GCHOOK *gch)
{
SnowFlake userId = _wtoi64(gch->ptszUID);
MCONTACT hContact;
CDiscordUser *pUser = FindUser(userId);
if (pUser == nullptr) {
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->si->ptszID));
setWString(hContact, DB_KEY_NICK, gch->ptszNick);
Contact::Hide(hContact);
db_set_dw(hContact, "Ignore", "Mask1", 0);
}
else hContact = pUser->hContact;
CallService(MS_MSG_SENDMESSAGE, hContact, 0);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Invitation dialog
class CGroupchatInviteDlg : public CDiscordDlgBase
{
CCtrlClc m_clc;
SnowFlake m_iChatId;
void FilterList(CCtrlClc *)
{
for (auto &hContact : Contacts()) {
char *proto = Proto_GetBaseAccountName(hContact);
if (mir_strcmp(proto, m_proto->m_szModuleName) || m_proto->isChatRoom(hContact))
if (HANDLE hItem = m_clc.FindContact(hContact))
m_clc.DeleteItem(hItem);
}
}
void ResetListOptions(CCtrlClc *)
{
m_clc.SetHideEmptyGroups(true);
m_clc.SetHideOfflineRoot(true);
}
public:
CGroupchatInviteDlg(CDiscordProto *ppro, SnowFlake chatId) :
CDiscordDlgBase(ppro, IDD_GROUPCHAT_INVITE),
m_clc(this, IDC_CLIST),
m_iChatId(chatId)
{
m_clc.OnNewContact =
m_clc.OnListRebuilt = Callback(this, &CGroupchatInviteDlg::FilterList);
m_clc.OnOptionsChanged = Callback(this, &CGroupchatInviteDlg::ResetListOptions);
}
bool OnInitDialog() override
{
SetWindowLongPtr(m_clc.GetHwnd(), GWL_STYLE,
GetWindowLongPtr(m_clc.GetHwnd(), GWL_STYLE) | CLS_SHOWHIDDEN | CLS_CHECKBOXES | CLS_HIDEEMPTYGROUPS | CLS_USEGROUPS | CLS_GREYALTERNATE | CLS_GROUPCHECKBOXES);
m_clc.SendMsg(CLM_SETEXSTYLE, CLS_EX_DISABLEDRAGDROP | CLS_EX_TRACKSELECT, 0);
ResetListOptions(&m_clc);
FilterList(&m_clc);
return true;
}
bool OnApply() override
{
// invite users from roster
for (auto &hContact : m_proto->AccContacts()) {
if (m_proto->isChatRoom(hContact))
continue;
if (HANDLE hItem = m_clc.FindContact(hContact)) {
if (m_clc.GetCheck(hItem)) {
CMStringA szUrl(FORMAT, "/channels/%lld/recipients/%lld", m_iChatId, m_proto->getId(hContact, DB_KEY_ID));
m_proto->Push(new AsyncHttpRequest(m_proto, REQUEST_PUT, szUrl, 0));
}
}
}
return true;
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// Log menu
void CDiscordProto::LeaveChat(CDiscordUser *pChat)
{
CMStringA szUrl(FORMAT, "/channels/%S?silent=false", pChat->wszUsername.c_str());
Push(new AsyncHttpRequest(this, REQUEST_DELETE, szUrl, nullptr));
}
INT_PTR CDiscordProto::SvcLeaveChat(WPARAM hContact, LPARAM)
{
if (auto *pUser = FindUserByChannel(getId(hContact, DB_KEY_CHANNELID)))
if (pUser->si)
LeaveChat(pUser);
return 0;
}
void CDiscordProto::Chat_ProcessLogMenu(GCHOOK *gch)
{
CDiscordUser *pUser = FindUserByChannel(_wtoi64(gch->si->ptszID));
if (pUser == nullptr)
return;
ENTER_STRING es = {};
es.szModuleName = m_szModuleName;
switch (gch->dwData) {
case IDM_DESTROY:
if (IDYES == MessageBox(nullptr, TranslateT("Do you really want to destroy this channel? This action is non-revertable."), m_tszUserName, MB_YESNO | MB_ICONQUESTION))
LeaveChat(pUser);
break;
case IDM_LEAVE:
LeaveChat(pUser);
break;
case IDM_RENAME:
es.caption = TranslateT("Enter new channel name:");
es.type = ESF_COMBO;
es.szDataPrefix = "chat_rename";
if (EnterString(&es)) {
JSONNode root; root << WCHAR_PARAM("name", es.ptszResult);
CMStringA szUrl(FORMAT, "/channels/%S", pUser->wszUsername.c_str());
Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, nullptr, &root));
mir_free(es.ptszResult);
}
break;
case IDM_CHANGETOPIC:
es.caption = TranslateT("Enter new topic:");
es.type = ESF_RICHEDIT;
es.szDataPrefix = "chat_topic";
if (EnterString(&es)) {
JSONNode root; root << WCHAR_PARAM("topic", es.ptszResult);
CMStringA szUrl(FORMAT, "/channels/%S", pUser->wszUsername.c_str());
Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, nullptr, &root));
mir_free(es.ptszResult);
}
break;
case IDM_CHANGENICK:
es.caption = TranslateT("Enter your new nick name:");
es.type = ESF_COMBO;
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->pGuild->m_id);
Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, nullptr, &root));
mir_free(es.ptszResult);
}
break;
case IDM_INVITE:
CGroupchatInviteDlg dlg(this, pUser->channelId);
if (gch->si->pDlg)
dlg.SetParent(gch->si->pDlg->GetHwnd());
dlg.DoModal();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Nick list menu
void CDiscordProto::KickChatUser(CDiscordUser *pChat, const wchar_t *pszUID)
{
CMStringA szUrl(FORMAT, "/channels/%lld/recipients/%S", pChat->channelId, pszUID);
Push(new AsyncHttpRequest(this, REQUEST_DELETE, szUrl, 0));
}
void CDiscordProto::MakeChatOwner(CDiscordUser *pChat, const wchar_t *pszUID)
{
JSONNode payload; payload << WCHAR_PARAM("owner", pszUID);
CMStringA szUrl(FORMAT, "/channels/%lld", pChat->channelId);
Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, 0, &payload));
}
void CDiscordProto::Chat_ProcessNickMenu(GCHOOK* gch)
{
auto *pChannel = FindUserByChannel(_wtoi64(gch->si->ptszID));
if (pChannel == nullptr)
return;
switch (gch->dwData) {
case IDM_COPY_ID:
CopyId(gch->ptszUID);
break;
case IDM_ADD:
AddFriend(_wtoi64(gch->ptszUID));
break;
case IDM_KICK:
KickChatUser(pChannel, gch->ptszUID);
break;
case IDM_PASSOWNER:
MakeChatOwner(pChannel, gch->ptszUID);
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
int CDiscordProto::GroupchatEventHook(WPARAM, LPARAM lParam)
{
GCHOOK *gch = (GCHOOK*)lParam;
if (gch == nullptr)
return 0;
auto *si = gch->si;
if (mir_strcmpi(si->pszModule, m_szModuleName))
return 0;
switch (gch->iType) {
case GC_USER_MESSAGE:
if (m_bOnline && mir_wstrlen(gch->ptszText) > 0) {
CMStringW wszText(gch->ptszText);
wszText.TrimRight();
int pos = wszText.Find(':');
if (pos != -1) {
auto wszWord = wszText.Left(pos);
wszWord.Trim();
USERINFO *pUser = nullptr;
for (auto &U : si->getUserList())
if (wszWord == U->pszNick) {
pUser = U;
break;
}
if (pUser) {
wszText.Delete(0, pos);
wszText.Insert(0, L"<@" + CMStringW(pUser->pszUID) + L">");
}
}
SendMsg(si->hContact, (si->pDlg) ? si->pDlg->m_hQuoteEvent : 0, T2Utf(wszText));
}
break;
case GC_USER_PRIVMESS:
Chat_SendPrivateMessage(gch);
break;
case GC_USER_LOGMENU:
Chat_ProcessLogMenu(gch);
break;
case GC_USER_NICKLISTMENU:
Chat_ProcessNickMenu(gch);
break;
case GC_USER_TYPNOTIFY:
UserIsTyping(gch->si->hContact, (int)gch->dwData);
break;
}
return 1;
}
|