From a9692464ad7e695d2224e7f84b441828437f8c73 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Thu, 10 Feb 2022 16:06:25 +0000 Subject: [PATCH] URI Functions, account lookup FossilOrigin-Name: 11852ad563b470e8ffa2c89b05982a5078c88e0957712e8738e6b4e2360537d0 --- include/mastodont_account.h | 4 ++++ include/mastodont_types.h | 1 + include/mastodont_uri.h | 30 +++++++++++++++++++++++++ src/account.c | 5 ++++- src/uri.c | 44 +++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 include/mastodont_uri.h create mode 100644 src/uri.c diff --git a/include/mastodont_account.h b/include/mastodont_account.h index 2229369..4f699ec 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -18,6 +18,9 @@ #include "mastodont_types.h" #include +#define MSTDNT_LOOKUP_ACCT 0 +#define MSTDNT_LOOPUP_ID 1 + struct mstdnt_account { char* id; @@ -52,6 +55,7 @@ struct mstdnt_account }; int mastodont_account(mastodont_t* data, + int lookup_type, char* id, struct mstdnt_account* acct, struct mstdnt_storage* storage, diff --git a/include/mastodont_types.h b/include/mastodont_types.h index c7b45a0..b25fd87 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -19,6 +19,7 @@ #include #define MSTDNT_URLSIZE 2048 +#define MSTDNT_URISIZE 512 typedef unsigned char mstdnt_bool; typedef struct mastodont diff --git a/include/mastodont_uri.h b/include/mastodont_uri.h new file mode 100644 index 0000000..ba64211 --- /dev/null +++ b/include/mastodont_uri.h @@ -0,0 +1,30 @@ +/* + * 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 . + */ + +#ifndef MASTODONT_URI_H +#define MASTODONT_URI_H +#include +#include + +struct mstdnt_uri +{ + char* user; + char* domain; + char storage[MSTDNT_URISIZE]; +}; + +struct mstdnt_uri mastodont_uristr_to_uri(char* uri, size_t len); + +#endif /* MASTODONT_URI_H */ diff --git a/src/account.c b/src/account.c index e11fc2f..71c0620 100644 --- a/src/account.c +++ b/src/account.c @@ -19,6 +19,7 @@ #include "mastodont_fetch.h" int mastodont_account(mastodont_t* data, + int lookup, char* id, struct mstdnt_account* acct, struct mstdnt_storage* storage, @@ -28,7 +29,9 @@ int mastodont_account(mastodont_t* data, cJSON* root; char url[MSTDNT_URLSIZE]; struct mstdnt_fetch_results results = { 0 }; - snprintf(url, MSTDNT_URLSIZE, "api/v1/accounts/%s", id); + snprintf(url, MSTDNT_URLSIZE, + lookup ? "/api/v1/accounts/%s" : "/api/v1/accounts/lookup?acct=%s", + id); storage->needs_cleanup = 0; res = mastodont_fetch_curl(data, url, &results); diff --git a/src/uri.c b/src/uri.c new file mode 100644 index 0000000..7bb8145 --- /dev/null +++ b/src/uri.c @@ -0,0 +1,44 @@ +/* + * 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 . + */ + +#include +#include + +struct mstdnt_uri mastodont_uristr_to_uri(char* uri, size_t len) +{ + int i; + struct mstdnt_uri ret; + if (len <= 0) + len = strlen(uri); + + ret.user = ret.storage; + /* Copy information over */ + for (i = 0; i < len; ++i) + { + switch (uri[i]) + { + case '@': + ret.storage[i] = '\0'; + ret.domain = ret.storage + i + 1; + break; + default: + ret.storage[i] = uri[i]; + break; + } + } + + ret.storage[i] = '\0'; + return ret; +}