123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- //
- // Created by Memer on 24.08.18.
- // Copyright (c) 2018 Alexander Memer. All rights reserved.
- //
-
- #include <discord.h>
- #include <requests.h>
- #include <jansson.h>
- #include <log.h>
-
- void _ld_set_last_rest_error(struct ld_context *ctx, req_t *req)
- {
- log_warn("_ld_set_last_rest_error triggered");
-
- ctx->last_rest_status = malloc(sizeof(struct ld_rest_error));
-
- ctx->last_rest_status->what = (int)req->code;
-
- json_error_t err;
- json_t *res = json_loads(req->text, 0, &err);
-
- if (!res)
- {
- log_error("failed to parse json at line %d: %s", err.line, err.text);
- ctx->last_rest_status->text = "";
- json_decref(res);
- return;
- }
-
- json_t *message = json_object_get(res, "message");
- if (!json_is_string(message))
- {
- log_error("message is not a string");
- ctx->last_rest_status->text = "";
- json_decref(res);
- return;
- }
- char *_message = (char*)json_string_value(message);
-
- ctx->last_rest_status->text = malloc(strlen(_message) + 1);
- strcpy(ctx->last_rest_status->text, _message);
- }
-
- json_t *ld_get_request(struct ld_context *ctx, char *url)
- {
- req_t req;
- ctx->curl_handle = requests_init(&req);
-
- char *target = malloc(strlen(LD_API_ENDPOINT) + strlen(url) + 1);
- char *auth = malloc(strlen("Authorization: Bot ") + strlen(ctx->bot_token) + 1);
-
- sprintf(target, "%s%s", LD_API_ENDPOINT, url);
- sprintf(auth, "%s%s", "Authorization: Bot ", ctx->bot_token);
-
- requests_get_headers(ctx->curl_handle, &req, target, &auth, 1);
-
- free(target);
- free(auth);
-
- if(!req.ok)
- {
- _ld_set_last_rest_error(ctx, &req);
- return NULL;
- }
-
- json_t *res;
- json_error_t err;
-
- res = json_loads(req.text, 0, &err);
-
- requests_close(&req);
-
- if (!res)
- {
- log_error("failed to parse json on line %d: %s", err.line, err.text);
- return NULL;
- }
-
- return res;
-
- }
-
- json_t *ld_post_request(struct ld_context *ctx, char *url, json_t *contents)
- {
- req_t req;
- ctx->curl_handle = requests_init(&req);
-
- char *target = malloc(strlen(LD_API_ENDPOINT) + strlen(url) + 1);
- char *auth = malloc(strlen("Authorization: Bot ") + strlen(ctx->bot_token) + 1);
-
- sprintf(target, "%s%s", LD_API_ENDPOINT, url);
- sprintf(auth, "%s%s", "Authorization: Bot ", ctx->bot_token);
-
- if (!contents)
- {
- requests_post_headers(ctx->curl_handle, &req, target, "", &auth, 1);
- }
- else
- {
- requests_post_headers(ctx->curl_handle, &req, target, json_dumps(contents, 0), &auth, 1);
- }
-
- free(target);
- free(auth);
-
- if(!req.ok)
- {
- _ld_set_last_rest_error(ctx, &req);
- return NULL;
- }
-
- json_t *res;
- json_error_t err;
-
- res = json_loads(req.text, 0, &err);
-
- requests_close(&req);
-
- if (!res)
- {
- log_error("failed to parse json on line %d: %s", err.line, err.text);
- return NULL;
- }
-
- return res;
- }
-
- char *ld_get_gateway(struct ld_context *ctx)
- {
- json_t *res = ld_get_request(ctx, "/gateway");
-
- if (!res)
- {
- log_error("failed to get gateway url");
- return NULL;
- }
-
- json_t *gateway = json_object_get(res, "url");
- if (!json_is_string(gateway))
- {
- log_error("failed to parse url of gateway");
- json_decref(res);
- return NULL;
- }
-
- char *url = strdup(json_string_value(gateway));
-
- json_decref(gateway);
- json_decref(res);
-
- return url;
- }
-
- struct ld_gateway_bot_resp *ld_get_gateway_bot(struct ld_context *ctx)
- {
- json_t *res = ld_get_request(ctx, "/gateway/bot");
-
- if (!res)
- {
- log_error("failed to get gateway url");
- return NULL;
- }
-
- struct ld_gateway_bot_resp *r;
- r = malloc(sizeof(struct ld_gateway_bot_resp));
-
- json_t *gateway = json_object_get(res, "url");
- if (!json_is_string(gateway))
- {
- log_error("failed to parse json");
- json_decref(res);
- return NULL;
- }
-
- json_t *shards = json_object_get(res, "shards");
- if (!json_is_integer(shards))
- {
- log_error("failed to parse shards (not integer?)");
- json_decref(res);
- return NULL;
- }
-
- r->url = strdup(json_string_value(gateway));
- r->shards = (int)json_integer_value(shards);
-
- json_decref(gateway);
- json_decref(shards);
- json_decref(res);
-
- return r;
- }
-
- int ld_create_message(struct ld_context *ctx, char *channel_id, char *content)
- {
- char *target = malloc(39);
-
- sprintf(target, "/channels/%s/messages", channel_id);
-
- json_t *contents = json_object();
- json_t *_content = json_string(content);
-
- json_object_set_new(contents, "content", _content);
-
- json_t *res = ld_post_request(ctx, target, contents);
-
- free(target);
-
- if (json_object_size(res) <= 0)
- {
- log_warn("probably failed to create message");
- return -1;
- }
-
- json_decref(res);
-
- return 0;
- }
-
- int ld_get_channel(struct ld_context *ctx, char *channel_id, guild_channel_t *channel)
- {
- char *target = malloc(28);
-
- sprintf(target, "/channels/%s", channel_id);
-
- json_t *res = ld_get_request(ctx, target);
-
- free(target);
-
- if (json_object_size(res) <= 0)
- {
- log_warn("probably failed to get channel");
- return -1;
- }
-
- log_trace("%s", json_dumps(res, 0));
-
- json_t *id = json_object_get(res, "id");
- channel->id = strdup(json_string_value(id));
- json_decref(id);
-
- json_t *type = json_object_get(res, "type");
- channel->type = (int)json_integer_value(type);
- json_decref(type);
-
- if (channel->type == GUILD_TEXT || channel->type == GUILD_CATEGORY)
- {
- json_t *guild_id = json_object_get(res, "guild_id");
- channel->guild_id = strdup(json_string_value(guild_id));
- json_decref(guild_id);
-
- // TODO: permission_overwrites
-
- json_t *name = json_object_get(res, "name");
- channel->name = strdup(json_string_value(name));
- json_decref(name);
- if (channel->type == GUILD_TEXT)
- {
- // Topic is nullable
- json_t *topic = json_object_get(res, "topic");
- if (json_is_string(topic)) {
- channel->topic = strdup(json_string_value(topic));
- }
- json_decref(topic);
-
- json_t *nsfw = json_object_get(res, "nsfw");
- channel->nsfw = json_boolean_value(nsfw);
- json_decref(nsfw);
-
- json_t *last_message_id = json_object_get(res, "last_message_id");
- if (last_message_id) {
- if (json_is_string(last_message_id)) {
- channel->last_message_id = strdup(json_string_value(last_message_id));
- }
- json_decref(last_message_id);
- }
- }
-
- json_t *parent_id = json_object_get(res, "parent_id");
- if (json_is_string(parent_id))
- {
- channel->parent_id = strdup(json_string_value(parent_id));
- }
- json_decref(parent_id);
-
- json_t *position = json_object_get(res, "position");
- channel->position = (int)json_integer_value(position);
- json_decref(position);
- }
- else if (channel->type == GUILD_VOICE)
- {
- json_t *bitrate = json_object_get(res, "bitrate");
- channel->bitrate = (int)json_integer_value(bitrate);
- json_decref(bitrate);
-
- json_t *user_limit = json_object_get(res, "user_limit");
- channel->user_limit = (int)json_integer_value(user_limit);
- json_decref(user_limit);
- }
- else if (channel->type == DM || channel->type == GROUP_DM)
- {
- // TODO: recipients
- json_t *icon = json_object_get(res, "icon");
- if (icon)
- {
- if (json_is_string(icon))
- {
- channel->icon = strdup(json_string_value(icon));
- }
- json_decref(icon);
- }
-
- json_t *owner_id = json_object_get(res, "owner_id");
- channel->owner_id = strdup(json_string_value(owner_id));
- json_decref(owner_id);
-
- json_t *application_id = json_object_get(res, "application_id");
- if (application_id)
- {
- channel->application_id = strdup(json_string_value(application_id));
- json_decref(application_id);
- }
- }
-
- return 0;
- }
|