From a6f5184cfe15957f1c6c4bd8b602dc400032b0bc Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 15 Feb 2022 17:18:15 +0000 Subject: [PATCH] Read param values; timeline, stautus fixes FossilOrigin-Name: 09a4b2e86f595f4813cdbf6d0d1e90738e0fd62c38c002c48c5224c7c6f80078 --- include/mastodont_application.h | 20 +++++++++-- include/mastodont_json_helper.h | 7 ++-- include/mastodont_query.h | 2 +- include/mastodont_types.h | 2 ++ src/application.c | 60 ++++++++++++++++++++++++++++++--- src/json_helper.c | 12 +++++++ src/query.c | 8 +++-- src/status.c | 5 +-- src/timeline.c | 38 ++++++++++++++------- 9 files changed, 123 insertions(+), 31 deletions(-) diff --git a/include/mastodont_application.h b/include/mastodont_application.h index 76183ac..1a76ac8 100644 --- a/include/mastodont_application.h +++ b/include/mastodont_application.h @@ -19,8 +19,6 @@ #include #include -/* Status: Complete */ - struct mstdnt_application { char* name; @@ -36,8 +34,24 @@ struct mstdnt_app_register_args char* website; }; +struct mstdnt_app +{ + char* id; + char* name; + char* website; + char* redirect_uri; + char* client_id; + char* client_secret; + char* vapid_key; +}; + +int mstdnt_load_app_result(struct mstdnt_storage* storage, + struct mstdnt_fetch_results* results, + struct mstdnt_app* app); + int mastodont_register_app(mastodont_t* data, struct mstdnt_app_register_args* args, - struct mstdnt_storage* storage); + struct mstdnt_storage* storage, + struct mstdnt_app* app); #endif /* MASTODONT_ACCOUNT */ diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h index 4b292fc..028855c 100644 --- a/include/mastodont_json_helper.h +++ b/include/mastodont_json_helper.h @@ -16,8 +16,7 @@ #ifndef MASTODONT_JSON_HELPER_H #define MASTODONT_JSON_HELPER_H #include "mastodont_types.h" - -#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0])) +#include "mastodont_fetch.h" struct _mstdnt_str_val { @@ -31,6 +30,10 @@ struct _mstdnt_bool_val mstdnt_bool* bool_ptr; }; +int _mstdnt_json_init(cJSON** root, + struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage); + int _mstdnt_key_val_iter(cJSON* v, struct _mstdnt_str_val* str, size_t str_len, diff --git a/include/mastodont_query.h b/include/mastodont_query.h index 1e727c8..b0b10b5 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -28,7 +28,7 @@ struct _mstdnt_query_param { enum _mstdnt_query_type type; char* key; - union { + union param_value { char* s; int i; } value; diff --git a/include/mastodont_types.h b/include/mastodont_types.h index b25fd87..53e822d 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -18,10 +18,12 @@ #include #include +#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0])) #define MSTDNT_URLSIZE 2048 #define MSTDNT_URISIZE 512 typedef unsigned char mstdnt_bool; + typedef struct mastodont { char* url; diff --git a/src/application.c b/src/application.c index 6ab0d95..d8fa70f 100644 --- a/src/application.c +++ b/src/application.c @@ -13,11 +13,44 @@ * along with this program. If not, see . */ +#include +#include #include +#include + +int mstdnt_load_app_result(struct mstdnt_storage* storage, + struct mstdnt_fetch_results* results, + struct mstdnt_app* app) +{ + cJSON* root, *v; + if (_mstdnt_json_init(&root, results, storage)) + return 1; + + struct _mstdnt_str_val strings[] = { + { "id", &(app->id) }, + { "name", &(app->name) }, + { "website", &(app->website) }, + { "redirect_uri", &(app->redirect_uri) }, + { "client_id", &(app->client_id) }, + { "client_secret", &(app->client_secret) }, + { "vapid_key", &(app->vapid_key) }, + }; + + for (v = root->child; v; v = v->next) + { + if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), + NULL, 0) == 1) + { + return 1; + } + } +} + int mastodont_register_app(mastodont_t* data, struct mstdnt_app_register_args* args, - struct mstdnt_storage* storage) + struct mstdnt_storage* storage, + struct mstdnt_app* app) { int res; struct mstdnt_fetch_results results = { 0 }; @@ -34,13 +67,30 @@ int mastodont_register_app(mastodont_t* data, } storage->needs_cleanup = 0; - if (mastodont_fetch_curl(data, "api/v1/apps", &results) != CURLE_OK) - return 1; + union param_value u_client_name, u_redirect_uris, + u_scopes, u_website; - /*res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);*/ + struct _mstdnt_query_param params[] = { + { _MSTDNT_QUERY_STRING, "client_name", u_client_name }, + { _MSTDNT_QUERY_STRING, "redirect_uris", u_redirect_uris }, + { _MSTDNT_QUERY_STRING, "scopes", u_scopes }, + { _MSTDNT_QUERY_STRING, "website", u_website }, + }; + + char* url = _mstdnt_query_string("api/v1/apps", params, _mstdnt_arr_len(params)); + + if (mastodont_fetch_curl(data, url, &results) != CURLE_OK) + { + res = 1; + goto cleanup; + } + + res = mstdnt_load_app_result(storage, &results, app); mastodont_fetch_results_cleanup(&results); - + +cleanup: + free(url); return res; } diff --git a/src/json_helper.c b/src/json_helper.c index 9eec902..8728508 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -15,6 +15,18 @@ #include +int _mstdnt_json_init(cJSON** root, + struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage) +{ + *root = cJSON_ParseWithLength(results->response, results->size); + if (*root == NULL) + return 1; + storage->root = *root; + storage->needs_cleanup = 1; + return 0; +} + int _mstdnt_key_val_iter(cJSON* v, struct _mstdnt_str_val* str, size_t str_len, diff --git a/src/query.c b/src/query.c index 3ed8244..11e6f80 100644 --- a/src/query.c +++ b/src/query.c @@ -45,7 +45,9 @@ char* _mstdnt_query_string(char* src, for (i = 0; i < param_len; ++i) { - if (params[i].key) + if (params[i].key && + !(params[i].type == _MSTDNT_QUERY_STRING && + params[i].value.s == NULL)) { if (res_count++ == 0) /* Replaces Null terminator */ @@ -80,8 +82,8 @@ char* _mstdnt_query_string(char* src, /* Copy over strings (skip & sign ;; +1) */ strcpy(result + res_prev, params[i].key); - result[res_prev - (res_count - 1 != 0) + key_len] = '='; - strcpy(result + res_prev + 1 + key_len - (res_count - 1 != 0), val_ptr); + result[res_prev + key_len] = '='; + strcpy(result + res_prev + 1 + key_len, val_ptr); } } diff --git a/src/status.c b/src/status.c index cc2e820..03e7688 100644 --- a/src/status.c +++ b/src/status.c @@ -65,11 +65,8 @@ int mstdnt_load_statuses_from_result(struct mstdnt_status* statuses[], { size_t i = 0; cJSON* root, *status_j_list; - root = cJSON_ParseWithLength(results->response, results->size); - if (root == NULL) + if (_mstdnt_json_init(&root, results, storage)) return 1; - storage->root = root; - storage->needs_cleanup = 1; if (!cJSON_IsArray(root)) return 1; diff --git a/src/timeline.c b/src/timeline.c index 49d6dff..695371f 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -43,27 +43,39 @@ int mastodont_timeline_public(mastodont_t* data, } storage->needs_cleanup = 0; + union param_value u_local, u_remote, u_only_media, + u_max_id, u_since_id, u_min_id, u_limit; + u_local.i = args->local; + u_remote.i = args->remote; + u_only_media.i = args->only_media; + u_max_id.s = args->max_id; + u_since_id.s = args->since_id; + u_min_id.s = args->min_id; + u_limit.i = args->limit; + struct _mstdnt_query_param params[] = { - { _MSTDNT_QUERY_STRING, NULL, "rat" }, - { _MSTDNT_QUERY_STRING, "lolled", "ratted" }, + { _MSTDNT_QUERY_INT, "local", u_local }, + { _MSTDNT_QUERY_INT, "remote", u_remote }, + { _MSTDNT_QUERY_INT, "only_media", u_only_media }, + { _MSTDNT_QUERY_STRING, "max_id", u_max_id }, + { _MSTDNT_QUERY_STRING, "since_id", u_since_id }, + { _MSTDNT_QUERY_STRING, "min_id", u_min_id }, + { _MSTDNT_QUERY_INT, "limit", u_limit }, }; - + char* url = _mstdnt_query_string("api/v1/timelines/public", params, _mstdnt_arr_len(params)); - char* url = _mstdnt_query_string("api/v1/timelines/public", - params, 2); - - puts(url); - free (url); - return 1; - - - if (mastodont_fetch_curl(data, "api/v1/timelines/public", &results) != CURLE_OK) - return 1; + if (mastodont_fetch_curl(data, url, &results) != CURLE_OK) + { + res = 1; + goto cleanup; + } res = mstdnt_load_statuses_from_result(statuses, storage, &results, size); mastodont_fetch_results_cleanup(&results); +cleanup: + free(url); return res; }