forked from cee-studio/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-user.c
182 lines (154 loc) · 4.29 KB
/
discord-user.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "discord.h"
#include "discord-internal.h"
#include "orka-utils.h"
ORCAcode
discord_get_user(struct discord *client, const u64_snowflake_t user_id, struct discord_user *p_user)
{
if (!user_id) {
log_error("Missing 'user_id'");
return ORCA_MISSING_PARAMETER;
}
if (!p_user) {
log_error("Missing 'p_user'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = &discord_user_from_json_v,
.ok_obj = p_user
};
return discord_adapter_run(
&client->adapter,
&resp_handle,
NULL,
HTTP_GET,
"/users/%"PRIu64, user_id);
}
ORCAcode
discord_modify_current_user(struct discord *client, const char username[], const char avatar[], struct discord_user *p_user)
{
struct ua_resp_handle resp_handle = {
.ok_cb = p_user ? &discord_user_from_json_v : NULL,
.ok_obj = p_user
};
char payload[MAX_PAYLOAD_LEN];
void *A[2]={}; // pointer availability array
if (!IS_EMPTY_STRING(username))
A[0] = (void*)username;
if (!IS_EMPTY_STRING(avatar))
A[1] = (void*)avatar;
size_t ret = json_inject(payload, sizeof(payload),
"(username):s"
"(avatar):s"
"@arg_switches",
username,
avatar,
A, sizeof(A));
struct sized_buffer req_body = { payload, ret };
return discord_adapter_run(
&client->adapter,
&resp_handle,
&req_body,
HTTP_PATCH,
"/users/@me");
}
ORCAcode
discord_get_current_user(struct discord *client, struct discord_user *p_user)
{
if (!p_user) {
log_error("Missing 'p_user'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = &discord_user_from_json_v,
.ok_obj = p_user
};
return discord_adapter_run(
&client->adapter,
&resp_handle,
NULL,
HTTP_GET,
"/users/@me");
}
/* @todo this is a temporary solution for wrapping with JS */
static void
json_to_sb(char *json, size_t len, void *p_sb_user)
{
struct sized_buffer *sb_user = (struct sized_buffer*)p_sb_user;
sb_user->start = strndup(json, len);
}
ORCAcode /* @todo this is a temporary solution for easily wrapping JS */
sb_discord_get_current_user(struct discord *client, struct sized_buffer *p_sb_user)
{
if (!p_sb_user) {
log_error("Missing 'p_sb_user'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = &json_to_sb,
.ok_obj = p_sb_user
};
return discord_adapter_run(
&client->adapter,
&resp_handle,
NULL,
HTTP_GET,
"/users/@me");
}
ORCAcode
discord_get_current_user_guilds(struct discord *client, NTL_T(struct discord_guild) *p_guilds)
{
if (!p_guilds) {
log_error("Missing 'p_guilds'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = &discord_guild_list_from_json_v,
.ok_obj = p_guilds
};
return discord_adapter_run(
&client->adapter,
&resp_handle,
NULL,
HTTP_GET,
"/users/@me/guilds");
}
ORCAcode
discord_leave_guild(struct discord *client, const u64_snowflake_t guild_id)
{
if (!guild_id) {
log_error("Missing 'guild_id'");
return ORCA_MISSING_PARAMETER;
}
struct sized_buffer req_body = {"{}", 2};
return discord_adapter_run(
&client->adapter,
NULL,
&req_body,
HTTP_DELETE,
"/users/@me/guilds/%"PRIu64, guild_id);
}
ORCAcode
discord_create_dm(struct discord *client, const u64_snowflake_t recipient_id, struct discord_channel *p_dm_channel)
{
if (!recipient_id) {
log_error("Missing 'recipient_id'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = p_dm_channel ? &discord_channel_from_json_v : NULL,
.ok_obj = p_dm_channel
};
char payload[256];
size_t ret = json_inject(payload, sizeof(payload), \
"(recipient_id):s_as_u64", &recipient_id);
struct sized_buffer req_body = { payload, ret };
return discord_adapter_run(
&client->adapter,
&resp_handle,
&req_body,
HTTP_POST,
"/users/@me/channels");
}