forked from dylex/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack-api.c
154 lines (131 loc) · 4.2 KB
/
slack-api.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
#include <debug.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;
PurpleUtilFetchUrlData *fetch;
SlackAPICallback *callback;
gpointer data;
};
static void api_error(SlackAPICall *call, const char *error) {
if (call->callback)
call->callback(call->sa, call->data, NULL, error);
g_free(call->url);
g_free(call);
};
static gboolean api_retry(gpointer data);
static void api_cb(G_GNUC_UNUSED PurpleUtilFetchUrlData *fetch, gpointer data, const gchar *buf, gsize len, const gchar *error) {
SlackAPICall *call = data;
purple_debug_misc("slack", "api response: %s\n", error ?: buf);
if (error) {
api_error(call, error);
return;
}
json_value *json = json_parse(buf, len);
if (!json) {
api_error(call, "Invalid JSON response");
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... */
purple_timeout_add_seconds(purple_account_get_int(call->sa->account, "ratelimit_delay", 15), api_retry, call);
json_value_free(json);
return;
}
api_error(call, err ?: "Unknown error");
json_value_free(json);
return;
}
if (call->callback) {
call->callback(call->sa, call->data, json, NULL);
}
json_value_free(json);
g_free(call->url);
g_free(call);
}
static gboolean api_retry(gpointer data) {
SlackAPICall *call = data;
call->fetch = purple_util_fetch_url_request_len_with_account(call->sa->account,
call->url, TRUE, NULL, TRUE, NULL, FALSE, 4096*1024,
api_cb, call);
return FALSE;
}
static GString *slack_api_encode_url(SlackAccount *sa, const char *pfx, const char *method, va_list qargs) {
GString *url = g_string_new(NULL);
g_string_printf(url, "%s/%s%s?token=%s", sa->api_url, pfx, method, sa->token);
const char *param;
while ((param = va_arg(qargs, const char*))) {
const char *val = va_arg(qargs, const char*);
g_string_append_printf(url, "&%s=%s", param, purple_url_encode(val));
}
return url;
}
static void slack_api_call_url(SlackAccount *sa, SlackAPICallback callback, gpointer user_data, const char *url) {
SlackAPICall *call = g_new0(SlackAPICall, 1);
call->sa = sa;
call->callback = callback;
call->url = g_strdup(url);
call->data = user_data;
purple_debug_misc("slack", "api call: %s\n", url);
api_retry(call);
}
void slack_api_call(SlackAccount *sa, SlackAPICallback callback, gpointer user_data, const char *method, ...)
{
va_list qargs;
va_start(qargs, method);
GString *url = slack_api_encode_url(sa, "", method, qargs);
va_end(qargs);
slack_api_call_url(sa, callback, user_data, url->str);
g_string_free(url, TRUE);
}
gboolean slack_api_channel_call(SlackAccount *sa, SlackAPICallback callback, gpointer user_data, SlackObject *obj, const char *method, ...) {
g_return_val_if_fail(obj, FALSE);
const char *type = NULL, *id = NULL;
if (SLACK_IS_CHANNEL(obj)) {
SlackChannel *chan = (SlackChannel*)obj;
switch (chan->type) {
case SLACK_CHANNEL_MEMBER:
type = "channels.";
break;
case SLACK_CHANNEL_GROUP:
type = "groups.";
break;
case SLACK_CHANNEL_MPIM:
type = "mpim.";
break;
default:
break;
}
id = chan->object.id;
} else if (SLACK_IS_USER(obj)) {
SlackUser *user = (SlackUser*)obj;
if (*user->im) {
type = "im.";
id = user->im;
}
}
if (!type || !id)
return FALSE;
va_list qargs;
va_start(qargs, method);
GString *url = slack_api_encode_url(sa, type, method, qargs);
va_end(qargs);
g_string_append_printf(url, "&channel=%s", purple_url_encode(id));
slack_api_call_url(sa, callback, user_data, url->str);
g_string_free(url, TRUE);
return TRUE;
}