123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- //
- // 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;
- }
-
- json_t *ld_patch_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_patch(ctx->curl_handle, &req, target, "", &auth, 1, 1);
- }
- else
- {
- requests_patch(ctx->curl_handle, &req, target, json_dumps(contents, 0), &auth, 1, 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;
- }
-
- ld_channel_obj_to_type(res, channel);
-
- return 0;
- }
-
- int ld_modify_channel(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify)
- {
- ld_modify_channel_result(ctx, channel_id, modify, NULL);
- }
-
- int ld_modify_channel_result(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify, guild_channel_t *result)
- {
-
- json_t *contents = json_object();
-
- if (modify->name)
- {
- json_object_set_new(contents, "name", json_string(modify->name));
- }
-
- if (modify->position != -1)
- {
- json_object_set_new(contents, "position", json_integer(modify->position));
- }
-
- if (modify->topic)
- {
- json_object_set_new(contents, "topic", json_string(modify->topic));
- }
-
- if (modify->parent_id)
- {
- json_object_set_new(contents, "parent_id", json_string(modify->parent_id));
- }
-
- if (modify->nsfw != -1)
- {
- json_object_set_new(contents, "nsfw", json_boolean(modify->nsfw));
- }
-
- if (modify->bitrate != -1)
- {
- json_object_set_new(contents, "bitrate", json_integer(modify->bitrate));
- }
-
- if (modify->user_limit != -1)
- {
- json_object_set_new(contents, "user_limit", json_integer(modify->user_limit));
- }
-
- if (modify->permission_overwrites && modify->overwrites_size > 0)
- {
- json_t *overwrites = json_array();
-
- for (int i = 0; i < modify->overwrites_size; i++)
- {
- json_t *overwrite = json_object();
-
- json_object_set_new(overwrite, "id", json_string(modify->permission_overwrites[i].id));
- json_object_set_new(overwrite, "type", json_string(modify->permission_overwrites[i].type));
- json_object_set_new(overwrite, "deny", json_integer(modify->permission_overwrites[i].deny));
- json_object_set_new(overwrite, "allow", json_integer(modify->permission_overwrites[i].allow));
-
- json_array_append_new(overwrites, overwrite);
- }
-
- json_object_set_new(contents, "permission_overwrites", overwrites);
- }
-
- char *target = malloc(strlen("/channels/") + strlen(channel_id));
-
- sprintf(target, "/channels/%s", channel_id);
-
- json_t *res = ld_patch_request(ctx, target, contents);
-
- free(target);
- free(contents);
-
- if (!res)
- {
- log_error("probably failed to modify channel");
- return -1;
- }
-
- if (!result)
- {
- json_decref(res);
- return 0;
- }
-
- ld_channel_obj_to_type(res, result);
-
- return 0;
-
- }
|