diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 34b9587..64276fe 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -94,6 +94,22 @@ struct mstdnt_account_statuses_args { int limit; }; +struct mstdnt_create_status_args +{ + char* content_type; + int expires_in; + char* in_reply_to_conversation_id; + char* in_reply_to_id; + char* language; + char** media_ids; + void* poll; /* TODO */ + int preview; + char* scheduled_at; + int sensitive; + char* spoiler_text; + char* status; + char* visibility; +}; int mstdnt_load_statuses_from_result(struct mstdnt_status* status[], struct mstdnt_storage* storage, @@ -108,5 +124,8 @@ int mastodont_account_statuses(mastodont_t* data, struct mstdnt_status* statuses[], size_t* size); +int mastodont_create_status(mastodont_t* data, + struct mstdnt_create_status_args* args, + struct mstdnt_storage* storage); #endif /* MASTODONT_STATUS */ diff --git a/include/mastodont_types.h b/include/mastodont_types.h index 53e822d..56ce1f5 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -28,6 +28,7 @@ typedef struct mastodont { char* url; CURL* curl; + char* token; } mastodont_t; struct mstdnt_storage diff --git a/src/fetch.c b/src/fetch.c index d2026dd..141d8b5 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -43,17 +43,30 @@ void mastodont_fetch_results_cleanup(struct mstdnt_fetch_results* res) free(res->response); } +#include +#define TOKEN_STR_SIZE 512 int mastodont_fetch_curl(mastodont_t* mstdnt, char* _url, struct mstdnt_fetch_results* results) { int res; + char token[TOKEN_STR_SIZE] = { 0 }; + struct curl_slist* list = NULL; /* Setup URL */ char url[MSTDNT_URLSIZE]; strncpy(url, mstdnt->url, MSTDNT_URLSIZE-1); strncat(url, _url, MSTDNT_URLSIZE-1); + /* Setup token */ + if (mstdnt->token) + { + snprintf(token, TOKEN_STR_SIZE, "Authorization: Bearer %s", + mstdnt->token); + list = curl_slist_append(list, token); + curl_easy_setopt(mstdnt->curl, CURLOPT_HTTPHEADER, list); + } + /* Set options */ curl_easy_setopt(mstdnt->curl, CURLOPT_URL, url); curl_easy_setopt(mstdnt->curl, CURLOPT_WRITEFUNCTION, write_callback); @@ -61,5 +74,7 @@ int mastodont_fetch_curl(mastodont_t* mstdnt, res = curl_easy_perform(mstdnt->curl); + if (list) curl_slist_free_all(list); + return res; } diff --git a/src/mastodont.c b/src/mastodont.c index 15d6210..6eeb4da 100644 --- a/src/mastodont.c +++ b/src/mastodont.c @@ -15,6 +15,7 @@ void mastodont_global_curl_cleanup() int mastodont_init(mastodont_t* data) { data->curl = curl_easy_init(); + data->token = NULL; return data->curl == NULL; } diff --git a/src/status.c b/src/status.c index 03e7688..a2bc69e 100644 --- a/src/status.c +++ b/src/status.c @@ -18,6 +18,7 @@ #include #include #include +#include int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { @@ -125,3 +126,68 @@ int mastodont_account_statuses(mastodont_t* data, return res; } + +int mastodont_create_status(mastodont_t* data, + struct mstdnt_create_status_args* args, + struct mstdnt_storage* storage) +{ + int res = 0; + char* post; + struct mstdnt_fetch_results results = { 0 }; + + /* Default args */ + struct mstdnt_create_status_args _args; + if (args == NULL) + { + _args.content_type = "html"; /* TODO */ + _args.expires_in = 0; + _args.in_reply_to_conversation_id = NULL; + _args.in_reply_to_id = NULL; + _args.language = NULL; + _args.media_ids = NULL; + _args.poll = NULL; + _args.preview = 0; + _args.scheduled_at = NULL; + _args.sensitive = 0; + _args.spoiler_text = NULL; + _args.status = NULL; + _args.visibility = "public"; + args = &_args; + } + storage->needs_cleanup = 0; + + union param_value u_content_type, u_expires_in, + u_in_reply_to_conversation_id, u_in_reply_to_id, + u_language, u_media_ids, u_poll, u_preview, u_scheduled_at, + u_sensitive, u_spoiler_text, u_status, u_visibility; + u_content_type.s = args->content_type; + u_expires_in.i = args->expires_in; + u_in_reply_to_conversation_id.s = args->in_reply_to_conversation_id; + u_in_reply_to_id.s = args->in_reply_to_id; + u_language.s = args->language; + /*u_media_ids.s = args->media_ids;*/ + /* poll */ + u_preview.i = args->preview; + u_scheduled_at.s = args->scheduled_at; + u_sensitive.i = args->sensitive; + u_spoiler_text.s = args->spoiler_text; + u_status.s = args->status; + u_visibility.s = args->visibility; + + struct _mstdnt_query_param params[] = { + { _MSTDNT_QUERY_STRING, "content_type", u_content_type }, + { _MSTDNT_QUERY_STRING, "status", u_status }, + { _MSTDNT_QUERY_STRING, "visibility", u_visibility }, + }; + + post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params)); + + curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post); + + if (mastodont_fetch_curl(data, "api/v1/statuses", &results) != CURLE_OK) + return 1; + + mastodont_fetch_results_cleanup(&results); + return 0; +} +