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
|
/*
Copyright © 2016-17 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"
int compareUsers(const CDiscordUser *p1, const CDiscordUser *p2);
static int compareRoles(const CDiscordRole *p1, const CDiscordRole *p2)
{
return p1->id - p2->id;
}
static int compareChatUsers(const CDiscordGuildMember *p1, const CDiscordGuildMember *p2)
{
return p1->userId - p2->userId;
}
CDiscordGuild::CDiscordGuild(SnowFlake _id)
: id(_id),
arChatUsers(30, compareChatUsers),
arRoles(10, compareRoles)
{
}
CDiscordGuild::~CDiscordGuild()
{
}
/////////////////////////////////////////////////////////////////////////////////////////
// reads a role from json
void CDiscordProto::ProcessRole(CDiscordGuild *guild, const JSONNode &role)
{
SnowFlake id = ::getId(role["id"]);
CDiscordRole *p = guild->arRoles.find((CDiscordRole*)&id);
if (p == nullptr) {
p = new CDiscordRole();
p->id = id;
guild->arRoles.insert(p);
}
p->color = role["color"].as_int();
p->position = role["position"].as_int();
p->permissions = role["permissions"].as_int();
p->wszName = role["name"].as_mstring();
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::ProcessGuild(const JSONNode &p)
{
SnowFlake guildId = ::getId(p["id"]);
GatewaySendGuildInfo(guildId);
CDiscordGuild *pGuild = FindGuild(guildId);
if (pGuild == nullptr) {
pGuild = new CDiscordGuild(guildId);
arGuilds.insert(pGuild);
}
pGuild->ownerId = ::getId(p["owner_id"]);
pGuild->wszName = p["name"].as_mstring();
GCSessionInfoBase *si = Chat_NewSession(GCW_SERVER, m_szModuleName, pGuild->wszName, pGuild->wszName, pGuild);
Chat_Control(m_szModuleName, pGuild->wszName, WINDOW_HIDDEN);
Chat_Control(m_szModuleName, pGuild->wszName, SESSION_ONLINE);
pGuild->hContact = si->hContact;
setId(si->hContact, DB_KEY_CHANNELID, guildId);
const JSONNode &roles = p["roles"];
for (auto itr = roles.begin(); itr != roles.end(); ++itr)
ProcessRole(pGuild, *itr);
const JSONNode &channels = p["channels"];
for (auto itc = channels.begin(); itc != channels.end(); ++itc)
ProcessGuildChannel(pGuild, *itc);
}
/////////////////////////////////////////////////////////////////////////////////////////
CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JSONNode &pch)
{
// filter our all channels but the text ones
if (pch["type"].as_int() != 0)
return nullptr;
CMStringW wszChannelName = pGuild->wszName + L"#" + pch["name"].as_mstring();
CMStringW wszChannelId = pch["id"].as_mstring();
CMStringW wszTopic = pch["topic"].as_mstring();
SnowFlake channelId = _wtoi64(wszChannelId);
GCSessionInfoBase *si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszChannelId, wszChannelName);
BuildStatusList(pGuild, wszChannelId);
Chat_Control(m_szModuleName, wszChannelId, WINDOW_HIDDEN);
Chat_Control(m_szModuleName, wszChannelId, SESSION_ONLINE);
if (!wszTopic.IsEmpty()) {
Chat_SetStatusbarText(m_szModuleName, wszChannelId, wszTopic);
GCEVENT gce = { m_szModuleName, wszChannelId, GC_EVENT_TOPIC };
gce.time = time(0);
gce.ptszText = wszTopic;
Chat_Event(&gce);
}
CDiscordUser *pUser = FindUserByChannel(channelId);
if (pUser == nullptr) {
// missing channel - create it
pUser = new CDiscordUser(channelId);
pUser->bIsPrivate = false;
pUser->hContact = si->hContact;
pUser->id = channelId;
pUser->channelId = channelId;
arUsers.insert(pUser);
}
pUser->wszUsername = wszChannelId;
pUser->guildId = pGuild->id;
pUser->lastMsg = CDiscordMessage(::getId(pch["last_message_id"]));
setId(pUser->hContact, DB_KEY_ID, channelId);
setId(pUser->hContact, DB_KEY_CHANNELID, channelId);
SnowFlake oldMsgId = getId(pUser->hContact, DB_KEY_LASTMSGID);
if (oldMsgId != 0 && pUser->lastMsg.id > oldMsgId)
RetrieveHistory(pUser->hContact, MSG_AFTER, oldMsgId, 99);
return pUser;
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::ApplyUsersToChannel(CDiscordGuild *pGuild, const CDiscordUser &pUser)
{
GCEVENT gce = { m_szModuleName, pUser.wszUsername, GC_EVENT_JOIN };
gce.time = time(0);
gce.dwFlags = GCEF_SILENT;
for (int i = 0; i < pGuild->arChatUsers.getCount(); i++) {
CDiscordGuildMember &m = pGuild->arChatUsers[i];
wchar_t wszUserId[100];
_i64tow_s(m.userId, wszUserId, _countof(wszUserId), 10);
gce.ptszStatus = m.wszRole;
gce.bIsMe = (m.userId == m_ownId);
gce.ptszUID = wszUserId;
gce.ptszNick = m.wszNick;
Chat_Event(&gce);
int flags = GC_SSE_ONLYLISTED;
switch (m.iStatus) {
case ID_STATUS_ONLINE: case ID_STATUS_NA: case ID_STATUS_DND:
flags += GC_SSE_ONLINE;
break;
default:
flags += GC_SSE_OFFLINE;
}
Chat_SetStatusEx(m_szModuleName, pUser.wszUsername, flags, wszUserId);
}
}
|