forked from necrosis/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathslack-api.c
More file actions
302 lines (259 loc) · 8.13 KB
/
slack-api.c
File metadata and controls
302 lines (259 loc) · 8.13 KB
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
#include <debug.h>
#include <zlib.h>
#include "slack-api.h"
#include "slack-json.h"
#include "slack-channel.h"
#include "slack-user.h"
PurpleConnectionError slack_api_connection_error(const gchar *error) {
if (!g_strcmp0(error, "not_authed"))
return PURPLE_CONNECTION_ERROR_INVALID_USERNAME;
if (!g_strcmp0(error, "invalid_auth") ||
!g_strcmp0(error, "account_inactive"))
return PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED;
return PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
}
struct _SlackAPICall {
SlackAccount *sa;
char *url;
char *request;
PurpleUtilFetchUrlData *fetch;
guint timeout;
SlackAPICallback *callback;
gpointer data;
};
static void api_free(SlackAPICall *call) {
g_free(call->request);
g_free(call->url);
g_free(call);
}
static void api_error(SlackAPICall *call, const char *error) {
if (call->fetch)
purple_util_fetch_url_cancel(call->fetch);
if (call->timeout)
purple_timeout_remove(call->timeout);
if (call->callback)
call->callback(call->sa, call->data, NULL, error);
api_free(call);
};
static gboolean api_retry(SlackAPICall *call);
static void api_run(SlackAccount *sa);
gchar *api_gunzip(const guchar *gzip_data, gsize *len_ptr);
static void api_cb(PurpleUtilFetchUrlData *fetch, gpointer data, const gchar *buf_h, gsize len_h, const gchar *error) {
gboolean free_buf = FALSE;
SlackAccount *sa = data;
SlackAPICall *call = g_queue_pop_head(&sa->api_calls);
g_return_if_fail(call && (call->fetch == fetch || (call->fetch == NULL && error)));
call->fetch = NULL;
gsize len = len_h;
const gchar *buf = g_strstr_len(buf_h, len_h, "\r\n\r\n");
if (buf) {
buf += 4; // skip the headers
len = len_h - (buf - buf_h);
} else {
buf = buf_h;
len = len_h;
}
if (g_strstr_len(buf_h, len_h - len, "Content-Encoding: gzip") != NULL ||
g_strstr_len(buf_h, len_h - len, "content-encoding: gzip") != NULL) {
gchar *gunzip = api_gunzip((const guchar *)buf, &len);
if (!gunzip) {
api_error(call, "Failed to gunzip response");
api_run(sa);
return;
}
free_buf = TRUE;
buf = gunzip;
}
purple_debug_misc("slack", "api response: %s\n", error ?: buf);
if (error) {
if (free_buf) g_free((gchar *)buf);
api_error(call, error);
api_run(sa);
return;
}
json_value *json = json_parse(buf, len);
if (free_buf) g_free((gchar *)buf);
if (!json) {
api_error(call, "Invalid JSON response");
api_run(sa);
return;
}
if (!json_get_prop_boolean(json, "ok", FALSE)) {
const char *err = json_get_prop_strptr(json, "error");
if (!g_strcmp0(err, "ratelimited")) {
/* #27: correct thing to do on 429 status is parse the "Retry-After" header and wait that many seconds,
* but getting access to the headers here requires more work, so we just heuristically make up a number... */
g_queue_push_head(&sa->api_calls, call);
call->timeout = purple_timeout_add_seconds(purple_account_get_int(sa->account, "ratelimit_delay", 15), (GSourceFunc)api_retry, call);
json_value_free(json);
return;
}
api_error(call, err ?: "Unknown error");
json_value_free(json);
api_run(sa);
return;
}
if (call->callback)
if (call->callback(call->sa, call->data, json, NULL))
json = NULL;
if (json)
json_value_free(json);
api_free(call);
api_run(sa);
}
static gboolean api_retry(SlackAPICall *call) {
g_return_val_if_fail(call == g_queue_peek_head(&call->sa->api_calls), FALSE);
call->timeout = 0;
purple_debug_misc("slack", "api call: %s\n%s\n", call->url, call->request ?: "");
PurpleUtilFetchUrlData *fetch =
purple_util_fetch_url_request_len_with_account(call->sa->account,
call->url, TRUE, NULL, TRUE, call->request, TRUE, 4096*1024,
api_cb, call->sa);
if (fetch)
call->fetch = fetch;
return FALSE;
}
static void api_run(SlackAccount *sa) {
SlackAPICall *call = g_queue_peek_head(&sa->api_calls);
if (!call || call->fetch || call->timeout)
return;
api_retry(call);
}
static char *slack_api_encode_post_request(SlackAccount *sa, const char *url, va_list qargs) {
GString *request;
gchar *host = NULL, *path = NULL;
purple_url_parse(url, &host, NULL, &path, NULL, NULL);
GString *postdata;
const char *param;
// Just a long random number.
guint64 delim = ((guint64)g_random_int() << 32) | (guint64)g_random_int();
postdata = g_string_new("");
g_string_printf(postdata, "-----------------------------%" G_GUINT64_FORMAT "\r\n", delim);
while ((param = va_arg(qargs, const char*))) {
const char *val = va_arg(qargs, const char*);
g_string_append_printf(postdata, "\
Content-Disposition: form-data; name=\"%s\"\r\n\
\r\n\
%s\r\n\
-----------------------------%" G_GUINT64_FORMAT "\r\n",
param, val, delim);
}
g_string_append_printf(postdata,
"Content-Disposition: form-data; name=\"token\"\r\n\
\r\n\
%s\r\n\
-----------------------------%" G_GUINT64_FORMAT "\r\n",
sa->token, delim);
request = g_string_new(NULL);
g_string_append_printf(request, "\
POST /%s HTTP/1.0\r\n\
Host: %s\r\n\
Content-Type: multipart/form-data; boundary=---------------------------%" G_GUINT64_FORMAT "\r\n\
Content-Length: %" G_GSIZE_FORMAT "\r\n",
path, host, delim, postdata->len);
if (sa->d_cookie) {
g_string_append_printf(request, "Cookie: d=%s\r\n", sa->d_cookie);
}
g_string_append(request, "Accept-Encoding: gzip\r\n");
g_string_append(request, "\r\n");
g_string_append(request, postdata->str);
g_free(host);
g_free(path);
g_string_free(postdata, TRUE);
return g_string_free(request, FALSE);
}
static void slack_api_call_url(SlackAccount *sa, SlackAPICallback callback, gpointer user_data, const char *url, const char *request) {
SlackAPICall *call = g_new0(SlackAPICall, 1);
call->sa = sa;
call->callback = callback;
call->url = g_strdup(url);
call->request = g_strdup(request);
call->data = user_data;
gboolean empty = g_queue_is_empty(&sa->api_calls);
g_queue_push_tail(&sa->api_calls, call);
if (empty)
api_retry(call);
}
void slack_api_post(SlackAccount *sa, SlackAPICallback callback, gpointer user_data, const gchar *endpoint, ...)
{
GString *url = g_string_new(NULL);
g_string_printf(url, "%s/%s", sa->api_url, endpoint);
va_list qargs;
va_start(qargs, endpoint);
char *request = slack_api_encode_post_request(sa, url->str, qargs);
va_end(qargs);
slack_api_call_url(sa, callback, user_data, url->str, request);
g_string_free(url, TRUE);
g_free(request);
}
void slack_api_disconnect(SlackAccount *sa) {
SlackAPICall *call;
while ((call = g_queue_pop_head(&sa->api_calls)))
api_error(call, "disconnected");
}
#include <zlib.h>
gchar *
api_gunzip(const guchar *gzip_data, gsize *len_ptr)
{
gsize gzip_data_len = *len_ptr;
z_stream zstr;
int gzip_err = 0;
gchar *data_buffer;
gulong gzip_len = G_MAXUINT16;
GString *output_string = NULL;
data_buffer = g_new0(gchar, gzip_len);
zstr.next_in = NULL;
zstr.avail_in = 0;
zstr.zalloc = Z_NULL;
zstr.zfree = Z_NULL;
zstr.opaque = 0;
gzip_err = inflateInit2(&zstr, MAX_WBITS+32);
if (gzip_err != Z_OK)
{
g_free(data_buffer);
purple_debug_error("slack", "no built-in gzip support in zlib\n");
return NULL;
}
zstr.next_in = (Bytef *)gzip_data;
zstr.avail_in = gzip_data_len;
zstr.next_out = (Bytef *)data_buffer;
zstr.avail_out = gzip_len;
gzip_err = inflate(&zstr, Z_SYNC_FLUSH);
if (gzip_err == Z_DATA_ERROR)
{
inflateEnd(&zstr);
gzip_err = inflateInit2(&zstr, -MAX_WBITS);
if (gzip_err != Z_OK)
{
g_free(data_buffer);
purple_debug_error("slack", "Cannot decode gzip header\n");
return NULL;
}
zstr.next_in = (Bytef *)gzip_data;
zstr.avail_in = gzip_data_len;
zstr.next_out = (Bytef *)data_buffer;
zstr.avail_out = gzip_len;
gzip_err = inflate(&zstr, Z_SYNC_FLUSH);
}
output_string = g_string_new("");
while (gzip_err == Z_OK)
{
//append data to buffer
output_string = g_string_append_len(output_string, data_buffer, gzip_len - zstr.avail_out);
//reset buffer pointer
zstr.next_out = (Bytef *)data_buffer;
zstr.avail_out = gzip_len;
gzip_err = inflate(&zstr, Z_SYNC_FLUSH);
}
if (gzip_err == Z_STREAM_END)
{
output_string = g_string_append_len(output_string, data_buffer, gzip_len - zstr.avail_out);
} else {
purple_debug_error("slack", "gzip inflate error\n");
}
inflateEnd(&zstr);
g_free(data_buffer);
if (len_ptr)
*len_ptr = output_string->len;
return g_string_free(output_string, FALSE);
}