Application and query handler

FossilOrigin-Name: 088d4ef622e89f72366f6d28054c219aa02f35f9d37c71d5a7a7e0acc2a18253
This commit is contained in:
me@ow.nekobit.net 2022-02-15 03:54:46 +00:00
parent c4187bbd90
commit a321593346
4 changed files with 177 additions and 0 deletions

View file

@ -15,6 +15,9 @@
#ifndef MASTODONT_APPLICATION
#define MASTODONT_APPLICATION
#include "mastodont_types.h"
#include <cjson/cJSON.h>
#include <mastodont_status.h>
/* Status: Complete */
@ -25,4 +28,16 @@ struct mstdnt_application
char* vapid_key;
};
struct mstdnt_app_register_args
{
char* client_name;
char* redirect_uris;
char* scopes;
char* website;
};
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
struct mstdnt_storage* storage);
#endif /* MASTODONT_ACCOUNT */

41
include/mastodont_query.h Normal file
View file

@ -0,0 +1,41 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_QUERY_H
#define MASTODONT_QUERY_H
#include <stddef.h>
#include "mastodont_types.h"
enum _mstdnt_query_type
{
_MSTDNT_QUERY_STRING,
_MSTDNT_QUERY_INT
}
struct _mstdnt_query_param
{
enum _mstdnt_query_type type;
char* key;
union {
char* s;
int i;
} value;
};
char* _mstdnt_query_string(char* src,
struct _mstdnt_query_param* params,
size_t param_len);
#endif /* MASTODONT_QUERY_H */

49
src/application.c Normal file
View file

@ -0,0 +1,49 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <mastodont_application.h>
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
struct mstdnt_storage* storage)
{
int res;
struct mstdnt_fetch_results results = { 0 };
/* Default args */
struct mstdnt_timeline_public_args _args;
if (args == NULL)
{
_args.local = 0; /* Defaults to false */
_args.remote = 0;
_args.only_media = 0;
_args.max_id = NULL;
_args.since_id = NULL;
_args.min_id = NULL;
_args.limit = 20;
args = &_args;
}
storage->needs_cleanup = 0;
if (mastodont_fetch_curl(data, "api/v1/apps", &results) != CURLE_OK)
return 1;
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
return res;
}

72
src/query.c Normal file
View file

@ -0,0 +1,72 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <mastodont_query.h>
#define CONV_SIZE 64
char* _mstdnt_query_string(char* src,
struct _mstdnt_query_param* params,
size_t param_len)
{
size_t i;
int res_prev;
/* If value type is an int, convert it with int->str */
char conv_val[CONV_SIZE];
size_t src_l = strlen(src);
/* Result */
size_t res_len = src_l+1;
char* result = malloc(res_len);
strncpy(result, src, res_len);
/* We'll call them res to represent the query parameters */
int res_count = 0;
for (i = 0; i < param_len; ++i)
{
if (params[i].key)
{
if (res_count++ == 0)
{
result = realloc(result, ++res_len);
result[res_len-1] = '?';
}
/* Convert value */
if (params[i].type == _MSTDNT_QUERY_INT)
snprintf(conv_val, CONV_SIZE, "%d", params[i].value.i);
else /* Point to it */
conv_val = params[i].value.s;
res_prev = res_len;
/* |v| & character |v| account for '=' */
res_len += 1 + strlen(params[i].key) + 1 + strlen(conv_val);
result = realloc(result, res_len + 1);
if (res_count) result[res_prev] = '&';
if (res_count) result[res_len-2] = '\0';
else result[res_len-1] = '\0';
/* TODO */
}
}
return result;
}