summaryrefslogtreecommitdiff
path: root/protocols/Discord/src/gateway.cpp
blob: d6b7846eeb8b487c97ccc074346699c2141b6b37 (plain)
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
/*
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"

//////////////////////////////////////////////////////////////////////////////////////
// sends a piece of JSON to a server via a websocket, masked

bool CDiscordProto::GatewaySend(const JSONNode &pRoot)
{
	if (m_ws == nullptr)
		return false;

	json_string szText = pRoot.write();
	debugLogA("Gateway send: %s", szText.c_str());
	m_ws->sendText(szText.c_str());
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////
// gateway worker thread

void CDiscordProto::GatewayThread(void*)
{
	while (GatewayThreadWorker())
		;
	ShutdownSession();
}

bool CDiscordProto::GatewayThreadWorker()
{
	bool bHasCookie = false;
	MHttpHeaders hdrs;
	hdrs.AddHeader("Origin", "https://discord.com");
	if (!m_szWSCookie.IsEmpty()) {
		bHasCookie = true;
		hdrs.AddHeader("Cookie", m_szWSCookie);
	}

	JsonWebSocket<CDiscordProto> ws(this);
	NLHR_PTR pReply(ws.connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", &hdrs));
	if (pReply == nullptr) {
		debugLogA("Gateway connection failed, exiting");
		return false;
	}

	m_szWSCookie = pReply->GetCookies();

	if (pReply->resultCode != 101) {
		// if there's no cookie & Miranda is bounced with error 404, simply apply the cookie and try again
		if (pReply->resultCode == 404) {
			if (!bHasCookie)
				return true;

			m_szWSCookie.Empty(); // don't use the same cookie twice
		}
		return false;
	}

	// succeeded!
	debugLogA("Gateway connection succeeded");

	m_ws = &ws;
	ws.run();

	m_ws = nullptr;
	return true;
}

//////////////////////////////////////////////////////////////////////////////////////
// handles server commands

void JsonWebSocket<CDiscordProto>::process(const JSONNode &json)
{
	int opCode = json["op"].as_int();
	switch (opCode) {
	case OPCODE_DISPATCH:  // process incoming command
		{
			int iSeq = json["s"].as_int();
			if (iSeq != 0)
				p->m_iGatewaySeq = iSeq;

			CMStringW wszCommand = json["t"].as_mstring();
			p->debugLogA("got a server command to dispatch: %S", wszCommand.c_str());

			GatewayHandlerFunc pFunc = p->GetHandler(wszCommand);
			if (pFunc)
				(p->*pFunc)(json["d"]);
		}
		break;

	case OPCODE_RECONNECT:  // we need to reconnect asap
		p->debugLogA("we need to reconnect, leaving worker thread");
		p->m_bTerminated = true;
		return;

	case OPCODE_INVALID_SESSION:  // session invalidated
		if (json["d"].as_bool()) // session can be resumed
			p->GatewaySendResume();
		else {
			Sleep(5000); // 5 seconds - recommended timeout
			p->GatewaySendIdentify();
		}
		break;

	case OPCODE_HELLO: // hello
		p->m_iHartbeatInterval = json["d"]["heartbeat_interval"].as_int();

		p->GatewaySendIdentify();
		break;

	case OPCODE_HEARTBEAT_ACK: // heartbeat ack
		break;

	default:
		p->debugLogA("ACHTUNG! Unknown opcode: %d, report it to developer", opCode);
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// requests to be sent to a gateway

void CDiscordProto::GatewaySendGuildInfo(CDiscordGuild *pGuild)
{
	if (!pGuild->arChannels.getCount())
		return;

	JSONNode a1(JSON_ARRAY); a1 << INT_PARAM("", 0) << INT_PARAM("", 99);

	CMStringA szId(FORMAT, "%lld", pGuild->arChannels[0]->id);
	JSONNode chl(JSON_ARRAY); chl.set_name(szId.c_str()); chl << a1;

	JSONNode channels; channels.set_name("channels"); channels << chl;

	JSONNode payload; payload.set_name("d");
	payload << SINT64_PARAM("guild_id", pGuild->m_id) << BOOL_PARAM("typing", true) << BOOL_PARAM("activities", true) << BOOL_PARAM("presences", true) << channels;
		
	JSONNode root;
	root << INT_PARAM("op", OPCODE_REQUEST_SYNC_CHANNEL) << payload;
	GatewaySend(root);
}

void CDiscordProto::GatewaySendHeartbeat()
{
	// we don't send heartbeat packets until we get logged in
	if (!m_iHartbeatInterval || !m_iGatewaySeq)
		return;

	JSONNode root;
	root << INT_PARAM("op", OPCODE_HEARTBEAT) << INT_PARAM("d", m_iGatewaySeq);
	GatewaySend(root);
}

void CDiscordProto::GatewaySendIdentify()
{
	if (m_szAccessToken == nullptr) {
		ConnectionFailed(LOGINERR_WRONGPASSWORD);
		return;
	}

	char szOs[256];
	OS_GetDisplayString(szOs, _countof(szOs));
	
	char szVersion[256];
	Miranda_GetVersionText(szVersion, _countof(szVersion));

	JSONNode props; props.set_name("properties");
	props << CHAR_PARAM("os", szOs) << CHAR_PARAM("browser", "Chrome") << CHAR_PARAM("device", szVersion)
		<< CHAR_PARAM("referrer", "https://miranda-ng.org") << CHAR_PARAM("referring_domain", "miranda-ng.org");

	JSONNode payload; payload.set_name("d");
	payload << CHAR_PARAM("token", m_szAccessToken) << props << BOOL_PARAM("compress", false) << INT_PARAM("large_threshold", 250);

	JSONNode root;
	root << INT_PARAM("op", OPCODE_IDENTIFY) << payload;
	GatewaySend(root);
}

void CDiscordProto::GatewaySendResume()
{
	char szRandom[40];
	uint8_t random[16];
	Utils_GetRandom(random, _countof(random));
	bin2hex(random, _countof(random), szRandom);

	JSONNode root;
	root << CHAR_PARAM("token", szRandom) << CHAR_PARAM("session_id", m_szGatewaySessionId) << INT_PARAM("seq", m_iGatewaySeq);
	GatewaySend(root);
}

/////////////////////////////////////////////////////////////////////////////////////////

bool CDiscordProto::GatewaySendStatus(int iStatus, const wchar_t *pwszStatusText)
{
	// if (iStatus == ID_STATUS_OFFLINE)
	//	return true;

	const char *pszStatus;
	switch (iStatus) {
	case ID_STATUS_AWAY:
	case ID_STATUS_NA:
		pszStatus = "idle"; break;
	case ID_STATUS_DND:
		pszStatus = "dnd"; break;
	case ID_STATUS_INVISIBLE:
		pszStatus = "invisible"; break;
	case ID_STATUS_OFFLINE:
		pszStatus = "offline"; break;
	default:
		pszStatus = "online"; break;
	}

	JSONNode payload; payload.set_name("d");
	payload << INT64_PARAM("since", __int64(time(0)) * 1000) << BOOL_PARAM("afk", true) << CHAR_PARAM("status", pszStatus);
	if (pwszStatusText == nullptr)
		payload << CHAR_PARAM("game", nullptr);
	else {
		JSONNode game; game.set_name("game"); game << WCHAR_PARAM("name", pwszStatusText) << INT_PARAM("type", 0);
		payload << game;
	}
	
	JSONNode root; root << INT_PARAM("op", OPCODE_STATUS_UPDATE) << payload;
	return GatewaySend(root);
}

/////////////////////////////////////////////////////////////////////////////////////////

bool CDiscordProto::GatewaySendVoice(JSONNode &payload)
{
	payload.set_name("d");

	JSONNode root; root << INT_PARAM("op", OPCODE_VOICE_UPDATE) << payload;
	return GatewaySend(root);
}