2023-04-08 04:09:05 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2023-07-28 09:34:18 +00:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
|
2023-04-08 04:09:05 +00:00
|
|
|
|
2023-04-21 21:17:58 +00:00
|
|
|
#ifndef NO_MASTODON_API
|
|
|
|
|
2023-04-08 04:09:05 +00:00
|
|
|
#include "xs.h"
|
2023-04-09 22:08:51 +00:00
|
|
|
#include "xs_openssl.h"
|
2023-04-08 04:09:05 +00:00
|
|
|
#include "xs_json.h"
|
2023-04-08 08:29:33 +00:00
|
|
|
#include "xs_io.h"
|
2023-04-08 04:09:05 +00:00
|
|
|
#include "xs_time.h"
|
2023-04-28 06:58:33 +00:00
|
|
|
#include "xs_glob.h"
|
2023-05-15 09:23:36 +00:00
|
|
|
#include "xs_set.h"
|
2023-06-05 16:29:25 +00:00
|
|
|
#include "xs_random.h"
|
2023-09-25 16:25:09 +00:00
|
|
|
#include "xs_url.h"
|
2023-08-17 16:06:36 +00:00
|
|
|
#include "xs_mime.h"
|
2023-10-16 18:49:58 +00:00
|
|
|
#include "xs_match.h"
|
2023-04-08 04:09:05 +00:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2023-04-08 07:09:43 +00:00
|
|
|
static xs_str *random_str(void)
|
|
|
|
/* just what is says in the tin */
|
|
|
|
{
|
|
|
|
unsigned int data[4] = {0};
|
|
|
|
|
2023-06-05 16:29:25 +00:00
|
|
|
xs_rnd_buf(data, sizeof(data));
|
2023-04-08 07:09:43 +00:00
|
|
|
return xs_hex_enc((char *)data, sizeof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-08 08:29:33 +00:00
|
|
|
int app_add(const char *id, const xs_dict *app)
|
|
|
|
/* stores an app */
|
|
|
|
{
|
2023-04-10 07:21:14 +00:00
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return 500;
|
|
|
|
|
2023-04-08 08:29:33 +00:00
|
|
|
int status = 201;
|
|
|
|
xs *fn = xs_fmt("%s/app/", srv_basedir);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
mkdirx(fn);
|
|
|
|
fn = xs_str_cat(fn, id);
|
|
|
|
fn = xs_str_cat(fn, ".json");
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(app, 4, f);
|
2023-04-08 08:29:33 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-28 06:58:33 +00:00
|
|
|
xs_str *_app_fn(const char *id)
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/app/%s.json", srv_basedir, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-08 08:29:33 +00:00
|
|
|
xs_dict *app_get(const char *id)
|
|
|
|
/* gets an app */
|
|
|
|
{
|
2023-04-10 07:21:14 +00:00
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return NULL;
|
|
|
|
|
2023-04-28 06:58:33 +00:00
|
|
|
xs *fn = _app_fn(id);
|
2023-04-08 08:29:33 +00:00
|
|
|
xs_dict *app = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
app = xs_json_load(f);
|
2023-04-08 08:29:33 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-10 07:21:14 +00:00
|
|
|
int app_del(const char *id)
|
|
|
|
/* deletes an app */
|
|
|
|
{
|
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return -1;
|
|
|
|
|
2023-04-28 06:58:33 +00:00
|
|
|
xs *fn = _app_fn(id);
|
2023-04-10 07:21:14 +00:00
|
|
|
|
|
|
|
return unlink(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
int token_add(const char *id, const xs_dict *token)
|
|
|
|
/* stores a token */
|
|
|
|
{
|
2023-04-10 07:21:14 +00:00
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return 500;
|
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
int status = 201;
|
|
|
|
xs *fn = xs_fmt("%s/token/", srv_basedir);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
mkdirx(fn);
|
|
|
|
fn = xs_str_cat(fn, id);
|
|
|
|
fn = xs_str_cat(fn, ".json");
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(token, 4, f);
|
2023-04-09 19:31:56 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_dict *token_get(const char *id)
|
|
|
|
/* gets a token */
|
|
|
|
{
|
2023-04-10 07:21:14 +00:00
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return NULL;
|
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
|
|
|
|
xs_dict *token = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
token = xs_json_load(f);
|
2023-04-09 19:31:56 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int token_del(const char *id)
|
|
|
|
/* deletes a token */
|
|
|
|
{
|
2023-04-10 07:21:14 +00:00
|
|
|
if (!xs_is_hex(id))
|
|
|
|
return -1;
|
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
|
|
|
|
|
|
|
|
return unlink(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-08 07:27:22 +00:00
|
|
|
const char *login_page = ""
|
|
|
|
"<!DOCTYPE html>\n"
|
2023-09-21 01:03:57 +00:00
|
|
|
"<html>\n"
|
|
|
|
"<head>\n"
|
2023-09-21 01:16:13 +00:00
|
|
|
"<title>%s OAuth - Snac2</title>\n"
|
2023-09-21 01:03:57 +00:00
|
|
|
"<style>:root {color-scheme: light dark}</style>\n"
|
|
|
|
"</head>\n"
|
2023-04-09 18:34:05 +00:00
|
|
|
"<body><h1>%s OAuth identify</h1>\n"
|
|
|
|
"<div style=\"background-color: red; color: white\">%s</div>\n"
|
2023-05-04 09:08:35 +00:00
|
|
|
"<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
|
2023-04-08 07:27:22 +00:00
|
|
|
"<p>Login: <input type=\"text\" name=\"login\"></p>\n"
|
|
|
|
"<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
|
|
|
|
"<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
|
2023-04-08 08:29:33 +00:00
|
|
|
"<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
|
2023-04-10 10:55:03 +00:00
|
|
|
"<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
|
2023-04-09 18:34:05 +00:00
|
|
|
"<input type=\"submit\" value=\"OK\">\n"
|
2023-09-21 01:03:57 +00:00
|
|
|
"</form><p>%s</p></body></html>\n"
|
2023-04-08 07:27:22 +00:00
|
|
|
"";
|
|
|
|
|
2023-04-08 07:09:43 +00:00
|
|
|
int oauth_get_handler(const xs_dict *req, const char *q_path,
|
2023-04-08 04:09:05 +00:00
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)b_size;
|
|
|
|
|
2023-04-08 05:04:40 +00:00
|
|
|
if (!xs_startswith(q_path, "/oauth/"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int status = 404;
|
2023-04-08 07:09:43 +00:00
|
|
|
xs_dict *msg = xs_dict_get(req, "q_vars");
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/authorize") == 0) { /** **/
|
2023-04-08 05:04:40 +00:00
|
|
|
const char *cid = xs_dict_get(msg, "client_id");
|
|
|
|
const char *ruri = xs_dict_get(msg, "redirect_uri");
|
|
|
|
const char *rtype = xs_dict_get(msg, "response_type");
|
2023-04-10 10:55:03 +00:00
|
|
|
const char *state = xs_dict_get(msg, "state");
|
2023-04-08 08:29:33 +00:00
|
|
|
|
|
|
|
status = 400;
|
2023-04-08 05:04:40 +00:00
|
|
|
|
|
|
|
if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
|
2023-04-08 08:29:33 +00:00
|
|
|
xs *app = app_get(cid);
|
2023-04-08 07:27:22 +00:00
|
|
|
|
2023-04-08 08:29:33 +00:00
|
|
|
if (app != NULL) {
|
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
|
|
|
|
2023-04-10 10:55:03 +00:00
|
|
|
if (xs_is_null(state))
|
|
|
|
state = "";
|
|
|
|
|
2023-09-21 01:16:13 +00:00
|
|
|
*body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-login",
|
2023-05-04 09:08:35 +00:00
|
|
|
ruri, cid, state, USER_AGENT);
|
2023-04-08 08:29:33 +00:00
|
|
|
*ctype = "text/html";
|
|
|
|
status = 200;
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth authorize: generating login page"));
|
2023-04-08 08:29:33 +00:00
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
else
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
else
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
2023-05-04 09:52:04 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
|
2023-05-04 09:52:04 +00:00
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
|
|
|
|
2023-09-21 01:16:13 +00:00
|
|
|
*body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-get-token",
|
2023-05-04 09:52:04 +00:00
|
|
|
"", "", "", USER_AGENT);
|
|
|
|
*ctype = "text/html";
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
}
|
2023-04-08 07:09:43 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int oauth_post_handler(const xs_dict *req, const char *q_path,
|
2023-04-09 18:34:05 +00:00
|
|
|
const char *payload, int p_size,
|
|
|
|
char **body, int *b_size, char **ctype)
|
2023-04-08 07:09:43 +00:00
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)p_size;
|
|
|
|
(void)b_size;
|
|
|
|
|
2023-04-08 07:09:43 +00:00
|
|
|
if (!xs_startswith(q_path, "/oauth/"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int status = 404;
|
2023-04-15 07:44:09 +00:00
|
|
|
|
|
|
|
char *i_ctype = xs_dict_get(req, "content-type");
|
|
|
|
xs *args = NULL;
|
|
|
|
|
|
|
|
if (i_ctype && xs_startswith(i_ctype, "application/json"))
|
|
|
|
args = xs_json_loads(payload);
|
2023-08-12 19:46:54 +00:00
|
|
|
else
|
|
|
|
if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
|
|
|
|
xs *upl = xs_url_dec(payload);
|
|
|
|
args = xs_url_vars(upl);
|
|
|
|
}
|
2023-04-15 07:44:09 +00:00
|
|
|
else
|
|
|
|
args = xs_dup(xs_dict_get(req, "p_vars"));
|
|
|
|
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
|
2023-04-08 07:09:43 +00:00
|
|
|
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
|
2023-04-15 07:44:09 +00:00
|
|
|
const char *login = xs_dict_get(args, "login");
|
|
|
|
const char *passwd = xs_dict_get(args, "passwd");
|
|
|
|
const char *redir = xs_dict_get(args, "redir");
|
|
|
|
const char *cid = xs_dict_get(args, "cid");
|
|
|
|
const char *state = xs_dict_get(args, "state");
|
2023-04-09 18:34:05 +00:00
|
|
|
|
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
|
|
|
|
|
|
|
/* by default, generate another login form with an error */
|
2023-09-21 01:16:13 +00:00
|
|
|
*body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
|
2023-05-04 09:08:35 +00:00
|
|
|
redir, cid, state, USER_AGENT);
|
2023-04-09 18:34:05 +00:00
|
|
|
*ctype = "text/html";
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
if (login && passwd && redir && cid) {
|
|
|
|
snac snac;
|
2023-04-08 07:09:43 +00:00
|
|
|
|
2023-04-09 18:34:05 +00:00
|
|
|
if (user_open(&snac, login)) {
|
|
|
|
/* check the login + password */
|
2023-05-04 09:08:35 +00:00
|
|
|
if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
|
2023-04-09 18:34:05 +00:00
|
|
|
/* success! redirect to the desired uri */
|
|
|
|
xs *code = random_str();
|
|
|
|
|
|
|
|
xs_free(*body);
|
|
|
|
*body = xs_fmt("%s?code=%s", redir, code);
|
|
|
|
status = 303;
|
|
|
|
|
2023-04-10 10:55:03 +00:00
|
|
|
/* if there is a state, add it */
|
|
|
|
if (!xs_is_null(state) && *state) {
|
|
|
|
*body = xs_str_cat(*body, "&state=");
|
|
|
|
*body = xs_str_cat(*body, state);
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
|
|
|
|
login, *body));
|
2023-04-09 19:31:56 +00:00
|
|
|
|
|
|
|
/* assign the login to the app */
|
|
|
|
xs *app = app_get(cid);
|
|
|
|
|
|
|
|
if (app != NULL) {
|
|
|
|
app = xs_dict_set(app, "uid", login);
|
|
|
|
app = xs_dict_set(app, "code", code);
|
|
|
|
app_add(cid, app);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
|
2023-04-09 18:34:05 +00:00
|
|
|
}
|
|
|
|
else
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
|
2023-04-09 18:34:05 +00:00
|
|
|
|
|
|
|
user_free(&snac);
|
|
|
|
}
|
|
|
|
else
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
|
2023-04-09 18:34:05 +00:00
|
|
|
}
|
|
|
|
else
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
|
2023-04-09 18:34:05 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/token") == 0) { /** **/
|
2023-04-15 16:14:24 +00:00
|
|
|
xs *wrk = NULL;
|
2023-04-15 07:44:09 +00:00
|
|
|
const char *gtype = xs_dict_get(args, "grant_type");
|
|
|
|
const char *code = xs_dict_get(args, "code");
|
|
|
|
const char *cid = xs_dict_get(args, "client_id");
|
|
|
|
const char *csec = xs_dict_get(args, "client_secret");
|
|
|
|
const char *ruri = xs_dict_get(args, "redirect_uri");
|
2023-04-15 16:14:24 +00:00
|
|
|
/* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
|
|
|
|
but if it's enabled, it makes it crash after some more steps, which
|
|
|
|
is FAR WORSE */
|
|
|
|
// const char *scope = xs_dict_get(args, "scope");
|
|
|
|
const char *scope = NULL;
|
2023-04-10 10:55:03 +00:00
|
|
|
|
|
|
|
/* no client_secret? check if it's inside an authorization header
|
|
|
|
(AndStatus does it this way) */
|
|
|
|
if (xs_is_null(csec)) {
|
|
|
|
const char *auhdr = xs_dict_get(req, "authorization");
|
|
|
|
|
|
|
|
if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
|
2023-04-10 10:55:03 +00:00
|
|
|
int size;
|
|
|
|
xs *s2 = xs_base64_dec(s1, &size);
|
|
|
|
|
|
|
|
if (!xs_is_null(s2)) {
|
|
|
|
xs *l1 = xs_split(s2, ":");
|
|
|
|
|
|
|
|
if (xs_list_len(l1) == 2) {
|
|
|
|
wrk = xs_dup(xs_list_get(l1, 1));
|
|
|
|
csec = wrk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 05:04:40 +00:00
|
|
|
|
|
|
|
if (gtype && code && cid && csec && ruri) {
|
2023-04-09 19:31:56 +00:00
|
|
|
xs *app = app_get(cid);
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
if (app == NULL) {
|
|
|
|
status = 401;
|
|
|
|
srv_log(xs_fmt("oauth token: invalid app %s", cid));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
|
|
|
|
status = 401;
|
|
|
|
srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xs *rsp = xs_dict_new();
|
|
|
|
xs *cat = xs_number_new(time(NULL));
|
|
|
|
xs *tokid = random_str();
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
rsp = xs_dict_append(rsp, "access_token", tokid);
|
|
|
|
rsp = xs_dict_append(rsp, "token_type", "Bearer");
|
|
|
|
rsp = xs_dict_append(rsp, "created_at", cat);
|
|
|
|
|
2023-04-15 07:44:09 +00:00
|
|
|
if (!xs_is_null(scope))
|
|
|
|
rsp = xs_dict_append(rsp, "scope", scope);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(rsp, 4);
|
2023-04-09 19:31:56 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
const char *uid = xs_dict_get(app, "uid");
|
|
|
|
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth token: "
|
2023-04-09 19:31:56 +00:00
|
|
|
"successful login for %s, new token %s", uid, tokid));
|
|
|
|
|
|
|
|
xs *token = xs_dict_new();
|
|
|
|
token = xs_dict_append(token, "token", tokid);
|
|
|
|
token = xs_dict_append(token, "client_id", cid);
|
|
|
|
token = xs_dict_append(token, "client_secret", csec);
|
|
|
|
token = xs_dict_append(token, "uid", uid);
|
|
|
|
token = xs_dict_append(token, "code", code);
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
token_add(tokid, token);
|
|
|
|
}
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
else {
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
|
2023-04-08 05:04:40 +00:00
|
|
|
status = 400;
|
2023-04-09 18:34:05 +00:00
|
|
|
}
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/revoke") == 0) { /** **/
|
2023-04-15 07:44:09 +00:00
|
|
|
const char *cid = xs_dict_get(args, "client_id");
|
|
|
|
const char *csec = xs_dict_get(args, "client_secret");
|
|
|
|
const char *tokid = xs_dict_get(args, "token");
|
2023-04-09 19:31:56 +00:00
|
|
|
|
|
|
|
if (cid && csec && tokid) {
|
|
|
|
xs *token = token_get(tokid);
|
2023-04-08 07:09:43 +00:00
|
|
|
|
|
|
|
*body = xs_str_new("{}");
|
|
|
|
*ctype = "application/json";
|
2023-04-09 19:31:56 +00:00
|
|
|
|
|
|
|
if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
|
2023-04-09 19:31:56 +00:00
|
|
|
status = 403;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
token_del(tokid);
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
|
2023-04-09 19:31:56 +00:00
|
|
|
status = 200;
|
2023-04-10 07:21:14 +00:00
|
|
|
|
|
|
|
/* also delete the app, as it serves no purpose from now on */
|
|
|
|
app_del(cid);
|
2023-04-09 19:31:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
|
2023-04-09 19:31:56 +00:00
|
|
|
status = 403;
|
2023-04-08 07:09:43 +00:00
|
|
|
}
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
|
2023-05-04 09:52:04 +00:00
|
|
|
const char *login = xs_dict_get(args, "login");
|
|
|
|
const char *passwd = xs_dict_get(args, "passwd");
|
|
|
|
|
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
|
|
|
|
|
|
|
/* by default, generate another login form with an error */
|
2023-09-21 01:16:13 +00:00
|
|
|
*body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
|
2023-05-04 09:52:04 +00:00
|
|
|
"", "", "", USER_AGENT);
|
|
|
|
*ctype = "text/html";
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
if (login && passwd) {
|
|
|
|
snac user;
|
|
|
|
|
|
|
|
if (user_open(&user, login)) {
|
|
|
|
/* check the login + password */
|
|
|
|
if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
|
|
|
|
/* success! create a new token */
|
|
|
|
xs *tokid = random_str();
|
|
|
|
|
|
|
|
srv_debug(1, xs_fmt("x-snac-new-token: "
|
|
|
|
"successful login for %s, new token %s", login, tokid));
|
|
|
|
|
|
|
|
xs *token = xs_dict_new();
|
|
|
|
token = xs_dict_append(token, "token", tokid);
|
|
|
|
token = xs_dict_append(token, "client_id", "snac-client");
|
|
|
|
token = xs_dict_append(token, "client_secret", "");
|
|
|
|
token = xs_dict_append(token, "uid", login);
|
|
|
|
token = xs_dict_append(token, "code", "");
|
|
|
|
|
|
|
|
token_add(tokid, token);
|
|
|
|
|
|
|
|
*ctype = "text/plain";
|
|
|
|
xs_free(*body);
|
|
|
|
*body = xs_dup(tokid);
|
|
|
|
}
|
|
|
|
|
|
|
|
user_free(&user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 05:04:40 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-04-08 04:09:05 +00:00
|
|
|
|
2023-04-10 10:14:16 +00:00
|
|
|
xs_str *mastoapi_id(const xs_dict *msg)
|
|
|
|
/* returns a somewhat Mastodon-compatible status id */
|
|
|
|
{
|
2023-04-12 08:19:43 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2023-04-10 10:14:16 +00:00
|
|
|
|
2023-04-12 08:19:43 +00:00
|
|
|
return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
|
2023-04-10 10:14:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:19:43 +00:00
|
|
|
#define MID_TO_MD5(id) (id + 10)
|
|
|
|
|
2023-04-10 10:14:16 +00:00
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
xs_dict *mastoapi_account(const xs_dict *actor)
|
2023-04-12 13:28:43 +00:00
|
|
|
/* converts an ActivityPub actor to a Mastodon account */
|
2023-04-11 07:50:12 +00:00
|
|
|
{
|
2023-05-15 09:38:29 +00:00
|
|
|
xs_dict *acct = xs_dict_new();
|
|
|
|
const char *prefu = xs_dict_get(actor, "preferredUsername");
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
const char *display_name = xs_dict_get(actor, "name");
|
|
|
|
if (xs_is_null(display_name) || *display_name == '\0')
|
2023-05-15 09:38:29 +00:00
|
|
|
display_name = prefu;
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
const char *id = xs_dict_get(actor, "id");
|
|
|
|
const char *pub = xs_dict_get(actor, "published");
|
|
|
|
xs *acct_md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
acct = xs_dict_append(acct, "id", acct_md5);
|
2023-05-15 09:38:29 +00:00
|
|
|
acct = xs_dict_append(acct, "username", prefu);
|
2023-04-11 07:50:12 +00:00
|
|
|
acct = xs_dict_append(acct, "display_name", display_name);
|
|
|
|
|
2023-05-15 09:36:09 +00:00
|
|
|
{
|
|
|
|
/* create the acct field as user@host */
|
|
|
|
xs *l = xs_split(id, "/");
|
2023-05-15 09:38:29 +00:00
|
|
|
xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
|
2023-05-15 09:36:09 +00:00
|
|
|
acct = xs_dict_append(acct, "acct", fquid);
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
if (pub)
|
|
|
|
acct = xs_dict_append(acct, "created_at", pub);
|
2023-04-14 06:14:11 +00:00
|
|
|
else {
|
|
|
|
/* unset created_at crashes Tusky, so lie like a mf */
|
2023-05-29 07:07:27 +00:00
|
|
|
xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
|
2023-04-14 06:14:11 +00:00
|
|
|
acct = xs_dict_append(acct, "created_at", date);
|
|
|
|
}
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-04-23 13:23:20 +00:00
|
|
|
const char *note = xs_dict_get(actor, "summary");
|
|
|
|
if (xs_is_null(note))
|
|
|
|
note = "";
|
|
|
|
|
2023-06-11 14:53:50 +00:00
|
|
|
if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
|
2023-06-15 15:51:24 +00:00
|
|
|
acct = xs_dict_append(acct, "bot", xs_stock_true);
|
2023-06-11 14:53:50 +00:00
|
|
|
else
|
2023-06-15 15:51:24 +00:00
|
|
|
acct = xs_dict_append(acct, "bot", xs_stock_false);
|
2023-06-11 14:53:50 +00:00
|
|
|
|
2023-04-23 13:23:20 +00:00
|
|
|
acct = xs_dict_append(acct, "note", note);
|
|
|
|
|
2023-04-24 15:37:59 +00:00
|
|
|
acct = xs_dict_append(acct, "url", id);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
xs *avatar = NULL;
|
|
|
|
xs_dict *av = xs_dict_get(actor, "icon");
|
|
|
|
|
2023-10-16 16:40:43 +00:00
|
|
|
if (xs_type(av) == XSTYPE_DICT) {
|
|
|
|
char *url = xs_dict_get(av, "url");
|
|
|
|
|
|
|
|
if (url != NULL)
|
|
|
|
avatar = xs_dup(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avatar == NULL)
|
2023-04-11 07:50:12 +00:00
|
|
|
avatar = xs_fmt("%s/susie.png", srv_baseurl);
|
|
|
|
|
|
|
|
acct = xs_dict_append(acct, "avatar", avatar);
|
2023-06-12 08:42:59 +00:00
|
|
|
acct = xs_dict_append(acct, "avatar_static", avatar);
|
|
|
|
|
|
|
|
xs *header = NULL;
|
|
|
|
xs_dict *hd = xs_dict_get(actor, "image");
|
|
|
|
|
|
|
|
if (xs_type(hd) == XSTYPE_DICT)
|
|
|
|
header = xs_dup(xs_dict_get(hd, "url"));
|
|
|
|
|
2023-06-12 17:39:15 +00:00
|
|
|
if (xs_is_null(header))
|
|
|
|
header = xs_dup("");
|
|
|
|
|
2023-06-12 08:42:59 +00:00
|
|
|
acct = xs_dict_append(acct, "header", header);
|
|
|
|
acct = xs_dict_append(acct, "header_static", header);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-04-24 15:37:59 +00:00
|
|
|
/* emojis */
|
|
|
|
xs_list *p;
|
|
|
|
if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
|
|
|
|
xs *eml = xs_list_new();
|
|
|
|
xs_dict *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
const char *type = xs_dict_get(v, "type");
|
|
|
|
|
|
|
|
if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
|
|
|
const xs_dict *icon = xs_dict_get(v, "icon");
|
|
|
|
|
|
|
|
if (!xs_is_null(name) && !xs_is_null(icon)) {
|
|
|
|
const char *url = xs_dict_get(icon, "url");
|
|
|
|
|
|
|
|
if (!xs_is_null(url)) {
|
|
|
|
xs *nm = xs_strip_chars_i(xs_dup(name), ":");
|
|
|
|
xs *d1 = xs_dict_new();
|
|
|
|
|
|
|
|
d1 = xs_dict_append(d1, "shortcode", nm);
|
|
|
|
d1 = xs_dict_append(d1, "url", url);
|
|
|
|
d1 = xs_dict_append(d1, "static_url", url);
|
2023-06-15 15:51:24 +00:00
|
|
|
d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
|
2023-04-24 15:37:59 +00:00
|
|
|
|
|
|
|
eml = xs_list_append(eml, d1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acct = xs_dict_append(acct, "emojis", eml);
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:28:43 +00:00
|
|
|
return acct;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-31 16:35:50 +00:00
|
|
|
xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
|
|
|
|
/* creates a mastoapi Poll object */
|
|
|
|
{
|
2023-06-01 06:13:58 +00:00
|
|
|
xs_dict *poll = xs_dict_new();
|
|
|
|
xs *mid = mastoapi_id(msg);
|
|
|
|
xs_list *opts = NULL;
|
|
|
|
xs_val *v;
|
|
|
|
int num_votes = 0;
|
|
|
|
xs *options = xs_list_new();
|
|
|
|
|
|
|
|
poll = xs_dict_append(poll, "id", mid);
|
|
|
|
poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
|
2023-06-15 15:51:24 +00:00
|
|
|
poll = xs_dict_append(poll, "expired",
|
|
|
|
xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
|
2023-06-01 06:13:58 +00:00
|
|
|
|
|
|
|
if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
|
2023-06-15 15:51:24 +00:00
|
|
|
poll = xs_dict_append(poll, "multiple", xs_stock_false);
|
2023-06-01 06:13:58 +00:00
|
|
|
else {
|
|
|
|
opts = xs_dict_get(msg, "anyOf");
|
2023-06-15 15:51:24 +00:00
|
|
|
poll = xs_dict_append(poll, "multiple", xs_stock_true);
|
2023-06-01 06:13:58 +00:00
|
|
|
}
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
while (xs_list_iter(&opts, &v)) {
|
|
|
|
const char *title = xs_dict_get(v, "name");
|
|
|
|
const char *replies = xs_dict_get(v, "replies");
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
if (title && replies) {
|
|
|
|
const char *votes_count = xs_dict_get(replies, "totalItems");
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
if (xs_type(votes_count) == XSTYPE_NUMBER) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
d = xs_dict_append(d, "title", title);
|
|
|
|
d = xs_dict_append(d, "votes_count", votes_count);
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
options = xs_list_append(options, d);
|
|
|
|
num_votes += xs_number_get(votes_count);
|
2023-05-31 16:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 06:13:58 +00:00
|
|
|
}
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
poll = xs_dict_append(poll, "options", options);
|
|
|
|
xs *vc = xs_number_new(num_votes);
|
|
|
|
poll = xs_dict_append(poll, "votes_count", vc);
|
2023-05-31 16:35:50 +00:00
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
poll = xs_dict_append(poll, "voted",
|
2023-08-12 10:10:42 +00:00
|
|
|
(snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
|
|
|
|
xs_stock_true : xs_stock_false);
|
2023-05-31 16:35:50 +00:00
|
|
|
|
|
|
|
return poll;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-12 13:28:43 +00:00
|
|
|
xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
|
|
|
|
/* converts an ActivityPub note to a Mastodon status */
|
|
|
|
{
|
|
|
|
xs *actor = NULL;
|
2023-08-12 09:23:01 +00:00
|
|
|
actor_get(xs_dict_get(msg, "attributedTo"), &actor);
|
2023-04-12 13:28:43 +00:00
|
|
|
|
|
|
|
/* if the author is not here, discard */
|
|
|
|
if (actor == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
const char *type = xs_dict_get(msg, "type");
|
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
xs *acct = mastoapi_account(actor);
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
xs *idx = NULL;
|
|
|
|
xs *ixc = NULL;
|
|
|
|
char *tmp;
|
2023-04-12 13:28:43 +00:00
|
|
|
xs *mid = mastoapi_id(msg);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
xs_dict *st = xs_dict_new();
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "id", mid);
|
|
|
|
st = xs_dict_append(st, "uri", id);
|
|
|
|
st = xs_dict_append(st, "url", id);
|
|
|
|
st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
|
|
|
|
st = xs_dict_append(st, "account", acct);
|
2023-08-14 18:11:03 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const char *content = xs_dict_get(msg, "content");
|
|
|
|
const char *name = xs_dict_get(msg, "name");
|
|
|
|
xs *s1 = NULL;
|
|
|
|
|
2023-08-20 14:03:46 +00:00
|
|
|
if (name && content)
|
2023-08-14 18:11:03 +00:00
|
|
|
s1 = xs_fmt("%s<br><br>%s", name, content);
|
|
|
|
else
|
2023-08-20 14:03:46 +00:00
|
|
|
if (name)
|
|
|
|
s1 = xs_dup(name);
|
|
|
|
else
|
|
|
|
if (content)
|
2023-08-14 18:11:03 +00:00
|
|
|
s1 = xs_dup(content);
|
2023-08-20 14:03:46 +00:00
|
|
|
else
|
|
|
|
s1 = xs_str_new(NULL);
|
2023-08-14 18:11:03 +00:00
|
|
|
|
|
|
|
st = xs_dict_append(st, "content", s1);
|
|
|
|
}
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
st = xs_dict_append(st, "visibility",
|
2023-08-12 07:43:01 +00:00
|
|
|
is_msg_public(msg) ? "public" : "private");
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
tmp = xs_dict_get(msg, "sensitive");
|
|
|
|
if (xs_is_null(tmp))
|
2023-06-15 15:51:24 +00:00
|
|
|
tmp = xs_stock_false;
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
st = xs_dict_append(st, "sensitive", tmp);
|
|
|
|
|
|
|
|
tmp = xs_dict_get(msg, "summary");
|
|
|
|
if (xs_is_null(tmp))
|
|
|
|
tmp = "";
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "spoiler_text", tmp);
|
|
|
|
|
|
|
|
/* create the list of attachments */
|
|
|
|
xs *matt = xs_list_new();
|
|
|
|
xs_list *att = xs_dict_get(msg, "attachment");
|
|
|
|
xs_str *aobj;
|
2023-08-14 18:11:03 +00:00
|
|
|
xs *attr_list = NULL;
|
|
|
|
|
|
|
|
if (xs_type(att) == XSTYPE_DICT) {
|
|
|
|
attr_list = xs_list_new();
|
|
|
|
attr_list = xs_list_append(attr_list, att);
|
|
|
|
}
|
|
|
|
else
|
2023-08-25 05:47:18 +00:00
|
|
|
if (xs_type(att) == XSTYPE_LIST)
|
2023-08-14 18:11:03 +00:00
|
|
|
attr_list = xs_dup(att);
|
2023-08-25 05:47:18 +00:00
|
|
|
else
|
|
|
|
attr_list = xs_list_new();
|
2023-08-14 18:11:03 +00:00
|
|
|
|
|
|
|
/* if it has an image, add it as an attachment */
|
|
|
|
xs_dict *image = xs_dict_get(msg, "image");
|
|
|
|
if (!xs_is_null(image))
|
|
|
|
attr_list = xs_list_append(attr_list, image);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-08-14 18:11:03 +00:00
|
|
|
att = attr_list;
|
2023-04-11 07:50:12 +00:00
|
|
|
while (xs_list_iter(&att, &aobj)) {
|
|
|
|
const char *mtype = xs_dict_get(aobj, "mediaType");
|
2023-08-14 18:11:03 +00:00
|
|
|
if (xs_is_null(mtype))
|
|
|
|
mtype = xs_dict_get(aobj, "type");
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-08-20 14:03:46 +00:00
|
|
|
const char *url = xs_dict_get(aobj, "url");
|
|
|
|
if (xs_is_null(url))
|
|
|
|
url = xs_dict_get(aobj, "href");
|
|
|
|
if (xs_is_null(url))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* if it's a plain Link, check if it can be "rewritten" */
|
|
|
|
if (xs_list_len(attr_list) < 2 && strcmp(mtype, "Link") == 0) {
|
|
|
|
const char *mt = xs_mime_by_ext(url);
|
|
|
|
|
|
|
|
if (xs_startswith(mt, "image/") ||
|
|
|
|
xs_startswith(mt, "audio/") ||
|
|
|
|
xs_startswith(mt, "video/"))
|
|
|
|
mtype = mt;
|
|
|
|
}
|
|
|
|
|
2023-04-26 19:07:00 +00:00
|
|
|
if (!xs_is_null(mtype)) {
|
2023-08-14 18:11:03 +00:00
|
|
|
if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/") ||
|
2023-10-09 20:56:11 +00:00
|
|
|
strcmp(mtype, "Image") == 0 || strcmp(mtype, "Document") == 0) {
|
2023-04-26 19:07:00 +00:00
|
|
|
xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
|
|
|
|
xs *matte = xs_dict_new();
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-04-26 19:07:00 +00:00
|
|
|
matte = xs_dict_append(matte, "id", matteid);
|
2023-08-14 18:11:03 +00:00
|
|
|
matte = xs_dict_append(matte, "type", *mtype == 'v' ? "video" : "image");
|
2023-08-20 14:03:46 +00:00
|
|
|
matte = xs_dict_append(matte, "url", url);
|
|
|
|
matte = xs_dict_append(matte, "preview_url", url);
|
|
|
|
matte = xs_dict_append(matte, "remote_url", url);
|
2023-04-12 08:41:15 +00:00
|
|
|
|
2023-04-26 19:07:00 +00:00
|
|
|
const char *name = xs_dict_get(aobj, "name");
|
|
|
|
if (xs_is_null(name))
|
|
|
|
name = "";
|
2023-04-12 08:41:15 +00:00
|
|
|
|
2023-04-26 19:07:00 +00:00
|
|
|
matte = xs_dict_append(matte, "description", name);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-04-26 19:07:00 +00:00
|
|
|
matt = xs_list_append(matt, matte);
|
|
|
|
}
|
2023-04-11 07:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "media_attachments", matt);
|
|
|
|
|
2023-04-24 15:05:34 +00:00
|
|
|
{
|
|
|
|
xs *ml = xs_list_new();
|
|
|
|
xs *htl = xs_list_new();
|
|
|
|
xs *eml = xs_list_new();
|
2023-08-23 16:32:08 +00:00
|
|
|
xs_list *tag = xs_dict_get(msg, "tag");
|
2023-04-24 15:05:34 +00:00
|
|
|
int n = 0;
|
|
|
|
|
2023-08-23 16:32:08 +00:00
|
|
|
xs *tag_list = NULL;
|
|
|
|
|
|
|
|
if (xs_type(tag) == XSTYPE_DICT) {
|
|
|
|
tag_list = xs_list_new();
|
|
|
|
tag_list = xs_list_append(tag_list, tag);
|
|
|
|
}
|
|
|
|
else
|
2023-08-25 05:47:18 +00:00
|
|
|
if (xs_type(tag) == XSTYPE_LIST)
|
2023-08-23 16:32:08 +00:00
|
|
|
tag_list = xs_dup(tag);
|
2023-08-25 05:47:18 +00:00
|
|
|
else
|
|
|
|
tag_list = xs_list_new();
|
2023-08-23 16:32:08 +00:00
|
|
|
|
|
|
|
tag = tag_list;
|
|
|
|
xs_dict *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&tag, &v)) {
|
2023-04-24 15:05:34 +00:00
|
|
|
const char *type = xs_dict_get(v, "type");
|
|
|
|
|
|
|
|
if (xs_is_null(type))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
xs *d1 = xs_dict_new();
|
|
|
|
|
|
|
|
if (strcmp(type, "Mention") == 0) {
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
|
|
|
const char *href = xs_dict_get(v, "href");
|
|
|
|
|
|
|
|
if (!xs_is_null(name) && !xs_is_null(href) &&
|
2023-08-12 10:10:42 +00:00
|
|
|
(snac == NULL || strcmp(href, snac->actor) != 0)) {
|
2023-04-24 15:05:34 +00:00
|
|
|
xs *nm = xs_strip_chars_i(xs_dup(name), "@");
|
|
|
|
|
|
|
|
xs *id = xs_fmt("%d", n++);
|
|
|
|
d1 = xs_dict_append(d1, "id", id);
|
|
|
|
d1 = xs_dict_append(d1, "username", nm);
|
|
|
|
d1 = xs_dict_append(d1, "acct", nm);
|
|
|
|
d1 = xs_dict_append(d1, "url", href);
|
|
|
|
|
|
|
|
ml = xs_list_append(ml, d1);
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 15:11:12 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Hashtag") == 0) {
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
|
|
|
const char *href = xs_dict_get(v, "href");
|
|
|
|
|
|
|
|
if (!xs_is_null(name) && !xs_is_null(href)) {
|
|
|
|
xs *nm = xs_strip_chars_i(xs_dup(name), "#");
|
|
|
|
|
|
|
|
d1 = xs_dict_append(d1, "name", nm);
|
|
|
|
d1 = xs_dict_append(d1, "url", href);
|
|
|
|
|
|
|
|
htl = xs_list_append(htl, d1);
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 15:25:32 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Emoji") == 0) {
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
|
|
|
const xs_dict *icon = xs_dict_get(v, "icon");
|
|
|
|
|
|
|
|
if (!xs_is_null(name) && !xs_is_null(icon)) {
|
|
|
|
const char *url = xs_dict_get(icon, "url");
|
|
|
|
|
|
|
|
if (!xs_is_null(url)) {
|
|
|
|
xs *nm = xs_strip_chars_i(xs_dup(name), ":");
|
|
|
|
|
|
|
|
d1 = xs_dict_append(d1, "shortcode", nm);
|
|
|
|
d1 = xs_dict_append(d1, "url", url);
|
|
|
|
d1 = xs_dict_append(d1, "static_url", url);
|
2023-06-15 15:51:24 +00:00
|
|
|
d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
|
2023-04-24 15:25:32 +00:00
|
|
|
d1 = xs_dict_append(d1, "category", "Emojis");
|
|
|
|
|
|
|
|
eml = xs_list_append(eml, d1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 15:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "mentions", ml);
|
|
|
|
st = xs_dict_append(st, "tags", htl);
|
|
|
|
st = xs_dict_append(st, "emojis", eml);
|
|
|
|
}
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
xs_free(idx);
|
|
|
|
xs_free(ixc);
|
|
|
|
idx = object_likes(id);
|
|
|
|
ixc = xs_number_new(xs_list_len(idx));
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "favourites_count", ixc);
|
|
|
|
st = xs_dict_append(st, "favourited",
|
2023-08-12 10:10:42 +00:00
|
|
|
(snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
xs_free(idx);
|
|
|
|
xs_free(ixc);
|
|
|
|
idx = object_announces(id);
|
|
|
|
ixc = xs_number_new(xs_list_len(idx));
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "reblogs_count", ixc);
|
|
|
|
st = xs_dict_append(st, "reblogged",
|
2023-08-12 10:10:42 +00:00
|
|
|
(snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
2023-10-13 07:01:07 +00:00
|
|
|
/* get the last person who boosted this */
|
|
|
|
xs *boosted_by_md5 = NULL;
|
|
|
|
if (xs_list_len(idx))
|
|
|
|
boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
xs_free(idx);
|
|
|
|
xs_free(ixc);
|
|
|
|
idx = object_children(id);
|
|
|
|
ixc = xs_number_new(xs_list_len(idx));
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "replies_count", ixc);
|
|
|
|
|
|
|
|
/* default in_reply_to values */
|
2023-06-15 15:51:24 +00:00
|
|
|
st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
|
|
|
|
st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
tmp = xs_dict_get(msg, "inReplyTo");
|
|
|
|
if (!xs_is_null(tmp)) {
|
|
|
|
xs *irto = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get(tmp, &irto))) {
|
|
|
|
xs *irt_mid = mastoapi_id(irto);
|
|
|
|
st = xs_dict_set(st, "in_reply_to_id", irt_mid);
|
|
|
|
|
|
|
|
char *at = NULL;
|
|
|
|
if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
|
|
|
|
xs *at_md5 = xs_md5_hex(at, strlen(at));
|
|
|
|
st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 15:51:24 +00:00
|
|
|
st = xs_dict_append(st, "reblog", xs_stock_null);
|
|
|
|
st = xs_dict_append(st, "card", xs_stock_null);
|
|
|
|
st = xs_dict_append(st, "language", xs_stock_null);
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
tmp = xs_dict_get(msg, "sourceContent");
|
|
|
|
if (xs_is_null(tmp))
|
|
|
|
tmp = "";
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "text", tmp);
|
|
|
|
|
|
|
|
tmp = xs_dict_get(msg, "updated");
|
|
|
|
if (xs_is_null(tmp))
|
2023-06-15 15:51:24 +00:00
|
|
|
tmp = xs_stock_null;
|
2023-04-11 07:50:12 +00:00
|
|
|
|
|
|
|
st = xs_dict_append(st, "edited_at", tmp);
|
|
|
|
|
2023-06-01 06:13:58 +00:00
|
|
|
if (strcmp(type, "Question") == 0) {
|
|
|
|
xs *poll = mastoapi_poll(snac, msg);
|
2023-05-31 08:49:08 +00:00
|
|
|
st = xs_dict_append(st, "poll", poll);
|
2023-06-01 06:13:58 +00:00
|
|
|
}
|
2023-06-28 17:53:25 +00:00
|
|
|
else
|
|
|
|
st = xs_dict_append(st, "poll", xs_stock_null);
|
|
|
|
|
|
|
|
st = xs_dict_append(st, "bookmarked", xs_stock_false);
|
2023-06-28 18:36:23 +00:00
|
|
|
|
2023-08-12 10:10:42 +00:00
|
|
|
st = xs_dict_append(st, "pinned",
|
|
|
|
(snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
|
2023-05-31 08:49:08 +00:00
|
|
|
|
2023-10-13 07:01:07 +00:00
|
|
|
/* is it a boost? */
|
|
|
|
if (!xs_is_null(boosted_by_md5)) {
|
|
|
|
/* create a new dummy status, using st as the 'reblog' field */
|
2023-10-13 07:29:55 +00:00
|
|
|
xs_dict *bst = xs_dup(st);
|
2023-10-13 07:01:07 +00:00
|
|
|
xs *b_actor = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
|
|
|
|
xs *b_acct = mastoapi_account(b_actor);
|
2023-10-13 07:14:11 +00:00
|
|
|
xs *fake_uri = NULL;
|
|
|
|
|
|
|
|
if (snac)
|
|
|
|
fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
|
|
|
|
else
|
|
|
|
fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
|
2023-10-13 07:01:07 +00:00
|
|
|
|
2023-10-13 07:29:55 +00:00
|
|
|
bst = xs_dict_set(bst, "uri", fake_uri);
|
|
|
|
bst = xs_dict_set(bst, "url", fake_uri);
|
|
|
|
bst = xs_dict_set(bst, "account", b_acct);
|
|
|
|
bst = xs_dict_set(bst, "content", "");
|
|
|
|
bst = xs_dict_set(bst, "reblog", st);
|
2023-10-13 07:01:07 +00:00
|
|
|
|
|
|
|
xs_free(st);
|
|
|
|
st = bst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 06:44:26 +00:00
|
|
|
xs_dict *mastoapi_relationship(snac *snac, const char *md5)
|
|
|
|
{
|
|
|
|
xs_dict *rel = NULL;
|
|
|
|
xs *actor_o = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get_by_md5(md5, &actor_o))) {
|
|
|
|
rel = xs_dict_new();
|
|
|
|
|
|
|
|
const char *actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
rel = xs_dict_append(rel, "id", md5);
|
|
|
|
rel = xs_dict_append(rel, "following",
|
2023-06-15 15:51:24 +00:00
|
|
|
following_check(snac, actor) ? xs_stock_true : xs_stock_false);
|
2023-04-23 06:44:26 +00:00
|
|
|
|
2023-06-15 15:51:24 +00:00
|
|
|
rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
|
|
|
|
rel = xs_dict_append(rel, "notifying", xs_stock_false);
|
2023-04-23 06:44:26 +00:00
|
|
|
rel = xs_dict_append(rel, "followed_by",
|
2023-06-15 15:51:24 +00:00
|
|
|
follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
|
2023-04-23 06:44:26 +00:00
|
|
|
|
|
|
|
rel = xs_dict_append(rel, "blocking",
|
2023-06-15 15:51:24 +00:00
|
|
|
is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
|
2023-04-23 06:44:26 +00:00
|
|
|
|
2023-06-15 15:51:24 +00:00
|
|
|
rel = xs_dict_append(rel, "muting", xs_stock_false);
|
|
|
|
rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
|
|
|
|
rel = xs_dict_append(rel, "requested", xs_stock_false);
|
|
|
|
rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
|
|
|
|
rel = xs_dict_append(rel, "endorsed", xs_stock_false);
|
2023-04-23 06:44:26 +00:00
|
|
|
rel = xs_dict_append(rel, "note", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
int process_auth_token(snac *snac, const xs_dict *req)
|
|
|
|
/* processes an authorization token, if there is one */
|
2023-04-09 18:34:05 +00:00
|
|
|
{
|
2023-04-09 19:31:56 +00:00
|
|
|
int logged_in = 0;
|
2023-04-11 19:07:47 +00:00
|
|
|
char *v;
|
2023-04-09 19:31:56 +00:00
|
|
|
|
|
|
|
/* if there is an authorization field, try to validate it */
|
|
|
|
if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
|
2023-04-09 19:31:56 +00:00
|
|
|
xs *token = token_get(tokid);
|
|
|
|
|
|
|
|
if (token != NULL) {
|
|
|
|
const char *uid = xs_dict_get(token, "uid");
|
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
if (!xs_is_null(uid) && user_open(snac, uid)) {
|
2023-04-09 19:31:56 +00:00
|
|
|
logged_in = 1;
|
2023-04-26 04:37:37 +00:00
|
|
|
|
|
|
|
/* this counts as a 'login' */
|
2023-05-08 07:02:45 +00:00
|
|
|
lastlog_write(snac, "mastoapi");
|
2023-04-26 04:37:37 +00:00
|
|
|
|
2023-11-08 17:15:51 +00:00
|
|
|
srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
|
2023-04-09 19:31:56 +00:00
|
|
|
}
|
|
|
|
else
|
2023-11-08 17:15:51 +00:00
|
|
|
srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
|
2023-04-09 19:31:56 +00:00
|
|
|
}
|
|
|
|
else
|
2023-11-08 17:15:51 +00:00
|
|
|
srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
|
2023-04-09 19:31:56 +00:00
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
return logged_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mastoapi_get_handler(const xs_dict *req, const char *q_path,
|
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)b_size;
|
|
|
|
|
2023-04-16 06:13:45 +00:00
|
|
|
if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
|
2023-04-11 19:07:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
int status = 404;
|
|
|
|
xs_dict *args = xs_dict_get(req, "q_vars");
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *cmd = xs_replace_n(q_path, "/api", "", 1);
|
2023-04-11 19:07:47 +00:00
|
|
|
|
2023-04-12 16:44:15 +00:00
|
|
|
snac snac1 = {0};
|
|
|
|
int logged_in = process_auth_token(&snac1, req);
|
2023-04-11 19:07:47 +00:00
|
|
|
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
|
2023-04-09 19:31:56 +00:00
|
|
|
if (logged_in) {
|
2023-04-09 22:08:51 +00:00
|
|
|
xs *acct = xs_dict_new();
|
2023-04-09 19:31:56 +00:00
|
|
|
|
2023-08-16 16:18:46 +00:00
|
|
|
acct = xs_dict_append(acct, "id", snac1.md5);
|
2023-04-12 16:44:15 +00:00
|
|
|
acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
|
|
|
|
acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
|
|
|
|
acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
|
|
|
|
acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
|
|
|
|
acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
|
|
|
|
acct = xs_dict_append(acct, "url", snac1.actor);
|
2023-04-15 07:52:48 +00:00
|
|
|
acct = xs_dict_append(acct, "header", "");
|
2023-04-09 20:10:49 +00:00
|
|
|
|
2023-08-12 19:46:54 +00:00
|
|
|
xs *src = xs_json_loads("{\"privacy\":\"public\","
|
|
|
|
"\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
|
|
|
|
acct = xs_dict_append(acct, "source", src);
|
|
|
|
|
2023-04-09 22:08:51 +00:00
|
|
|
xs *avatar = NULL;
|
2023-04-12 16:44:15 +00:00
|
|
|
char *av = xs_dict_get(snac1.config, "avatar");
|
2023-04-09 20:10:49 +00:00
|
|
|
|
2023-04-09 22:08:51 +00:00
|
|
|
if (xs_is_null(av) || *av == '\0')
|
2023-04-09 20:10:49 +00:00
|
|
|
avatar = xs_fmt("%s/susie.png", srv_baseurl);
|
2023-04-09 22:08:51 +00:00
|
|
|
else
|
|
|
|
avatar = xs_dup(av);
|
2023-04-09 20:10:49 +00:00
|
|
|
|
|
|
|
acct = xs_dict_append(acct, "avatar", avatar);
|
2023-08-12 19:53:35 +00:00
|
|
|
acct = xs_dict_append(acct, "avatar_static", avatar);
|
2023-04-09 19:31:56 +00:00
|
|
|
|
2023-09-18 09:57:55 +00:00
|
|
|
xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
|
|
|
|
if (xs_type(metadata) == XSTYPE_DICT) {
|
|
|
|
xs *fields = xs_list_new();
|
|
|
|
xs_str *k;
|
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
while (xs_dict_iter(&metadata, &k, &v)) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
|
|
|
|
d = xs_dict_append(d, "name", k);
|
|
|
|
d = xs_dict_append(d, "value", v);
|
|
|
|
d = xs_dict_append(d, "verified_at", xs_stock_null);
|
|
|
|
|
|
|
|
fields = xs_list_append(fields, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
acct = xs_dict_set(acct, "fields", fields);
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(acct, 4);
|
2023-04-09 19:31:56 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status = 422; // "Unprocessable entity" (no login)
|
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
}
|
2023-04-09 22:08:51 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
|
2023-04-12 18:25:41 +00:00
|
|
|
/* find if an account is followed, blocked, etc. */
|
2023-04-12 18:33:42 +00:00
|
|
|
/* the account to get relationships about is in args "id[]" */
|
2023-04-23 04:05:35 +00:00
|
|
|
|
2023-04-12 18:33:42 +00:00
|
|
|
if (logged_in) {
|
2023-04-23 04:05:35 +00:00
|
|
|
xs *res = xs_list_new();
|
|
|
|
const char *md5 = xs_dict_get(args, "id[]");
|
|
|
|
|
2023-08-13 11:00:36 +00:00
|
|
|
if (xs_is_null(md5))
|
|
|
|
md5 = xs_dict_get(args, "id");
|
|
|
|
|
2023-04-23 06:44:26 +00:00
|
|
|
if (!xs_is_null(md5)) {
|
2023-05-11 08:44:27 +00:00
|
|
|
if (xs_type(md5) == XSTYPE_LIST)
|
|
|
|
md5 = xs_list_get(md5, 0);
|
|
|
|
|
2023-04-23 06:44:26 +00:00
|
|
|
xs *rel = mastoapi_relationship(&snac1, md5);
|
2023-04-23 04:05:35 +00:00
|
|
|
|
2023-04-23 06:44:26 +00:00
|
|
|
if (rel != NULL)
|
|
|
|
res = xs_list_append(res, rel);
|
2023-04-23 04:05:35 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(res, 4);
|
2023-04-12 18:33:42 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
2023-04-23 04:05:35 +00:00
|
|
|
else
|
|
|
|
status = 422;
|
2023-04-12 18:25:41 +00:00
|
|
|
}
|
|
|
|
else
|
2023-10-28 05:10:21 +00:00
|
|
|
if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
|
|
|
|
/* lookup an account */
|
|
|
|
char *acct = xs_dict_get(args, "acct");
|
|
|
|
|
|
|
|
if (!xs_is_null(acct)) {
|
|
|
|
xs *s = xs_strip_chars_i(xs_dup(acct), "@");
|
|
|
|
xs *l = xs_split_n(s, "@", 1);
|
|
|
|
char *uid = xs_list_get(l, 0);
|
|
|
|
char *host = xs_list_get(l, 1);
|
|
|
|
|
|
|
|
if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
|
|
|
|
snac user;
|
|
|
|
|
|
|
|
if (user_open(&user, uid)) {
|
|
|
|
xs *actor = msg_actor(&user);
|
|
|
|
xs *macct = mastoapi_account(actor);
|
|
|
|
|
|
|
|
*body = xs_json_dumps(macct, 4);
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
user_free(&user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
|
2023-04-12 16:54:20 +00:00
|
|
|
/* account-related information */
|
2023-04-12 16:44:15 +00:00
|
|
|
xs *l = xs_split(cmd, "/");
|
2023-04-16 05:12:57 +00:00
|
|
|
const char *uid = xs_list_get(l, 3);
|
|
|
|
const char *opt = xs_list_get(l, 4);
|
2023-04-12 16:44:15 +00:00
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
if (uid != NULL) {
|
|
|
|
snac snac2;
|
|
|
|
xs *out = NULL;
|
|
|
|
xs *actor = NULL;
|
|
|
|
|
2023-05-15 09:15:28 +00:00
|
|
|
if (logged_in && strcmp(uid, "search") == 0) { /** **/
|
|
|
|
/* search for accounts starting with q */
|
2023-05-15 09:28:59 +00:00
|
|
|
const char *aq = xs_dict_get(args, "q");
|
2023-05-15 09:15:28 +00:00
|
|
|
|
2023-05-15 09:28:59 +00:00
|
|
|
if (!xs_is_null(aq)) {
|
|
|
|
xs *q = xs_tolower_i(xs_dup(aq));
|
2023-05-15 09:15:28 +00:00
|
|
|
out = xs_list_new();
|
|
|
|
xs *wing = following_list(&snac1);
|
|
|
|
xs *wers = follower_list(&snac1);
|
2023-05-15 11:04:30 +00:00
|
|
|
xs *ulst = user_list();
|
2023-05-15 09:15:28 +00:00
|
|
|
xs_list *p;
|
2023-05-15 11:04:30 +00:00
|
|
|
xs_str *v;
|
2023-05-15 09:23:36 +00:00
|
|
|
xs_set seen;
|
|
|
|
|
|
|
|
xs_set_init(&seen);
|
2023-05-15 09:15:28 +00:00
|
|
|
|
2023-05-15 11:04:30 +00:00
|
|
|
/* user relations */
|
2023-05-15 09:15:28 +00:00
|
|
|
xs_list *lsts[] = { wing, wers, NULL };
|
|
|
|
int n;
|
|
|
|
for (n = 0; (p = lsts[n]) != NULL; n++) {
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2023-05-15 09:23:36 +00:00
|
|
|
/* already seen? skip */
|
|
|
|
if (xs_set_add(&seen, v) == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-05-15 09:15:28 +00:00
|
|
|
xs *actor = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get(v, &actor))) {
|
|
|
|
const char *uname = xs_dict_get(actor, "preferredUsername");
|
|
|
|
|
2023-05-15 09:28:59 +00:00
|
|
|
if (!xs_is_null(uname)) {
|
|
|
|
xs *luname = xs_tolower_i(xs_dup(uname));
|
2023-05-15 09:15:28 +00:00
|
|
|
|
2023-05-15 09:28:59 +00:00
|
|
|
if (xs_startswith(luname, q)) {
|
|
|
|
xs *acct = mastoapi_account(actor);
|
|
|
|
|
|
|
|
out = xs_list_append(out, acct);
|
|
|
|
}
|
2023-05-15 09:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 09:23:36 +00:00
|
|
|
|
2023-05-17 05:27:08 +00:00
|
|
|
/* local users */
|
|
|
|
p = ulst;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
snac user;
|
|
|
|
|
2023-05-17 06:03:46 +00:00
|
|
|
/* skip this same user */
|
2023-05-17 05:27:08 +00:00
|
|
|
if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-05-17 06:03:46 +00:00
|
|
|
/* skip if the uid does not start with the query */
|
|
|
|
xs *v2 = xs_tolower_i(xs_dup(v));
|
|
|
|
if (!xs_startswith(v2, q))
|
|
|
|
continue;
|
2023-05-17 05:27:08 +00:00
|
|
|
|
2023-05-17 06:03:46 +00:00
|
|
|
if (user_open(&user, v)) {
|
|
|
|
/* if it's not already seen, add it */
|
|
|
|
if (xs_set_add(&seen, user.actor) == 1) {
|
2023-05-17 05:27:08 +00:00
|
|
|
xs *actor = msg_actor(&user);
|
|
|
|
xs *acct = mastoapi_account(actor);
|
|
|
|
|
|
|
|
out = xs_list_append(out, acct);
|
|
|
|
}
|
|
|
|
|
|
|
|
user_free(&user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:23:36 +00:00
|
|
|
xs_set_free(&seen);
|
2023-05-15 09:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-04-12 18:20:46 +00:00
|
|
|
/* is it a local user? */
|
2023-04-16 05:43:41 +00:00
|
|
|
if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
|
2023-04-12 18:20:46 +00:00
|
|
|
if (opt == NULL) {
|
|
|
|
/* account information */
|
|
|
|
actor = msg_actor(&snac2);
|
|
|
|
out = mastoapi_account(actor);
|
|
|
|
}
|
|
|
|
else
|
2023-05-15 09:15:28 +00:00
|
|
|
if (strcmp(opt, "statuses") == 0) { /** **/
|
2023-04-12 18:20:46 +00:00
|
|
|
/* the public list of posts of a user */
|
|
|
|
xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
|
|
|
|
xs_list *p = timeline;
|
|
|
|
xs_str *v;
|
2023-04-12 16:44:15 +00:00
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
out = xs_list_new();
|
2023-04-12 16:54:20 +00:00
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *msg = NULL;
|
2023-04-12 16:44:15 +00:00
|
|
|
|
2023-04-12 18:20:46 +00:00
|
|
|
if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
|
|
|
|
/* add only posts by the author */
|
|
|
|
if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
|
|
|
|
xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
|
|
|
|
xs *st = mastoapi_status(&snac2, msg);
|
2023-04-12 16:44:15 +00:00
|
|
|
|
2023-08-10 17:37:33 +00:00
|
|
|
if (st)
|
|
|
|
out = xs_list_append(out, st);
|
2023-04-12 18:20:46 +00:00
|
|
|
}
|
2023-04-12 16:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 18:20:46 +00:00
|
|
|
|
|
|
|
user_free(&snac2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* try the uid as the md5 of a possibly loaded actor */
|
|
|
|
if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
|
|
|
|
if (opt == NULL) {
|
|
|
|
/* account information */
|
|
|
|
out = mastoapi_account(actor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(opt, "statuses") == 0) {
|
|
|
|
/* we don't serve statuses of others; return the empty list */
|
|
|
|
out = xs_list_new();
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 16:54:20 +00:00
|
|
|
}
|
2023-04-12 16:44:15 +00:00
|
|
|
|
2023-04-12 16:54:20 +00:00
|
|
|
if (out != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-04-12 16:44:15 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
|
2023-04-09 22:08:51 +00:00
|
|
|
/* the private timeline */
|
|
|
|
if (logged_in) {
|
|
|
|
const char *max_id = xs_dict_get(args, "max_id");
|
2023-04-10 08:23:14 +00:00
|
|
|
const char *since_id = xs_dict_get(args, "since_id");
|
2023-04-10 10:14:16 +00:00
|
|
|
const char *min_id = xs_dict_get(args, "min_id");
|
2023-04-09 22:08:51 +00:00
|
|
|
const char *limit_s = xs_dict_get(args, "limit");
|
2023-04-10 09:30:00 +00:00
|
|
|
int limit = 0;
|
|
|
|
int cnt = 0;
|
2023-04-09 22:08:51 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(limit_s))
|
|
|
|
limit = atoi(limit_s);
|
|
|
|
|
2023-04-10 09:30:00 +00:00
|
|
|
if (limit == 0)
|
|
|
|
limit = 20;
|
|
|
|
|
2023-10-09 09:49:45 +00:00
|
|
|
xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
|
2023-04-10 06:52:04 +00:00
|
|
|
|
|
|
|
xs *out = xs_list_new();
|
2023-04-09 22:40:48 +00:00
|
|
|
xs_list *p = timeline;
|
2023-04-09 22:08:51 +00:00
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v) && cnt < limit) {
|
|
|
|
xs *msg = NULL;
|
|
|
|
|
2023-04-09 22:40:48 +00:00
|
|
|
/* only return entries older that max_id */
|
|
|
|
if (max_id) {
|
2023-06-10 06:35:16 +00:00
|
|
|
if (strcmp(v, MID_TO_MD5(max_id)) == 0)
|
2023-04-09 22:40:48 +00:00
|
|
|
max_id = NULL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-09 22:08:51 +00:00
|
|
|
|
2023-04-10 08:23:14 +00:00
|
|
|
/* only returns entries newer than since_id */
|
|
|
|
if (since_id) {
|
2023-06-10 06:35:16 +00:00
|
|
|
if (strcmp(v, MID_TO_MD5(since_id)) == 0)
|
2023-04-10 08:23:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-10 10:14:16 +00:00
|
|
|
/* only returns entries newer than min_id */
|
|
|
|
/* what does really "Return results immediately newer than ID" mean? */
|
|
|
|
if (min_id) {
|
2023-06-10 06:35:16 +00:00
|
|
|
if (strcmp(v, MID_TO_MD5(min_id)) == 0)
|
2023-04-10 10:14:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:08:51 +00:00
|
|
|
/* get the entry */
|
2023-04-12 16:44:15 +00:00
|
|
|
if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
|
2023-04-09 22:08:51 +00:00
|
|
|
continue;
|
|
|
|
|
2023-04-10 08:23:14 +00:00
|
|
|
/* discard non-Notes */
|
2023-10-16 18:49:58 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
2023-05-31 08:49:08 +00:00
|
|
|
const char *type = xs_dict_get(msg, "type");
|
2023-10-16 18:49:58 +00:00
|
|
|
if (!xs_match(type, "Note|Question|Page|Article"))
|
2023-04-09 22:08:51 +00:00
|
|
|
continue;
|
|
|
|
|
2023-10-16 18:49:58 +00:00
|
|
|
const char *from;
|
|
|
|
if (strcmp(type, "Page") == 0)
|
|
|
|
from = xs_dict_get(msg, "audience");
|
|
|
|
else
|
|
|
|
from = xs_dict_get(msg, "attributedTo");
|
2023-10-09 09:49:45 +00:00
|
|
|
|
|
|
|
/* is this message from a person we don't follow? */
|
2023-10-16 18:49:58 +00:00
|
|
|
if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
|
2023-10-13 06:33:57 +00:00
|
|
|
/* discard if it was not boosted */
|
2023-10-09 09:49:45 +00:00
|
|
|
xs *idx = object_announces(id);
|
|
|
|
|
2023-10-13 06:33:57 +00:00
|
|
|
if (xs_list_len(idx) == 0)
|
2023-10-09 09:49:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-10 06:07:58 +00:00
|
|
|
|
2023-04-27 06:35:23 +00:00
|
|
|
/* discard notes from muted morons */
|
2023-10-16 18:49:58 +00:00
|
|
|
if (is_muted(&snac1, from))
|
2023-04-24 15:49:08 +00:00
|
|
|
continue;
|
|
|
|
|
2023-04-27 06:35:23 +00:00
|
|
|
/* discard hidden notes */
|
2023-10-09 09:49:45 +00:00
|
|
|
if (is_hidden(&snac1, id))
|
2023-04-27 06:35:23 +00:00
|
|
|
continue;
|
|
|
|
|
2023-05-24 12:12:21 +00:00
|
|
|
/* discard poll votes (they have a name) */
|
2023-10-16 18:49:58 +00:00
|
|
|
if (strcmp(type, "Page") != 0 && !xs_is_null(xs_dict_get(msg, "name")))
|
2023-05-24 12:12:21 +00:00
|
|
|
continue;
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
/* convert the Note into a Mastodon status */
|
2023-04-12 16:44:15 +00:00
|
|
|
xs *st = mastoapi_status(&snac1, msg);
|
2023-04-09 22:08:51 +00:00
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
if (st != NULL)
|
|
|
|
out = xs_list_append(out, st);
|
2023-04-09 22:08:51 +00:00
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-04-09 22:08:51 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
2023-04-10 09:30:00 +00:00
|
|
|
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
|
2023-04-09 22:08:51 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status = 401; // unauthorized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
|
2023-04-30 05:00:49 +00:00
|
|
|
/* the instance public timeline (public timelines for all users) */
|
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
const char *limit_s = xs_dict_get(args, "limit");
|
2023-04-23 07:41:43 +00:00
|
|
|
int limit = 0;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
if (!xs_is_null(limit_s))
|
|
|
|
limit = atoi(limit_s);
|
|
|
|
|
|
|
|
if (limit == 0)
|
|
|
|
limit = 20;
|
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
xs *timeline = timeline_instance_list(0, limit);
|
|
|
|
xs *out = xs_list_new();
|
|
|
|
xs_list *p = timeline;
|
|
|
|
xs_str *md5;
|
2023-04-23 07:41:43 +00:00
|
|
|
|
2023-08-12 10:10:42 +00:00
|
|
|
snac *user = NULL;
|
|
|
|
if (logged_in)
|
|
|
|
user = &snac1;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &md5) && cnt < limit) {
|
2023-04-30 04:39:55 +00:00
|
|
|
xs *msg = NULL;
|
2023-04-23 07:41:43 +00:00
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
/* get the entry */
|
|
|
|
if (!valid_status(object_get_by_md5(md5, &msg)))
|
|
|
|
continue;
|
2023-04-23 07:41:43 +00:00
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
/* discard non-Notes */
|
2023-05-31 08:49:08 +00:00
|
|
|
const char *type = xs_dict_get(msg, "type");
|
|
|
|
if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
|
2023-04-30 04:39:55 +00:00
|
|
|
continue;
|
2023-04-23 07:41:43 +00:00
|
|
|
|
2023-10-07 22:06:37 +00:00
|
|
|
/* discard private users */
|
|
|
|
{
|
|
|
|
const char *atto = xs_dict_get(msg, "attributedTo");
|
|
|
|
xs *l = xs_split(atto, "/");
|
|
|
|
const char *uid = xs_list_get(l, -1);
|
|
|
|
snac p_user;
|
|
|
|
int skip = 1;
|
|
|
|
|
|
|
|
if (uid && user_open(&p_user, uid)) {
|
|
|
|
if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
|
|
|
|
skip = 0;
|
|
|
|
|
|
|
|
user_free(&p_user);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-30 05:00:49 +00:00
|
|
|
/* convert the Note into a Mastodon status */
|
2023-08-12 10:10:42 +00:00
|
|
|
xs *st = mastoapi_status(user, msg);
|
2023-04-23 07:41:43 +00:00
|
|
|
|
2023-04-30 05:00:49 +00:00
|
|
|
if (st != NULL) {
|
|
|
|
out = xs_list_append(out, st);
|
2023-04-30 04:39:55 +00:00
|
|
|
cnt++;
|
2023-04-23 07:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-04-11 18:10:24 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
|
2023-04-11 18:10:24 +00:00
|
|
|
/* TBD */
|
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
|
2023-04-13 15:34:48 +00:00
|
|
|
if (logged_in) {
|
2023-04-13 15:56:00 +00:00
|
|
|
xs *l = notify_list(&snac1, 0);
|
|
|
|
xs *out = xs_list_new();
|
|
|
|
xs_list *p = l;
|
|
|
|
xs_dict *v;
|
2023-05-11 08:54:56 +00:00
|
|
|
xs_list *excl = xs_dict_get(args, "exclude_types[]");
|
2023-04-13 15:56:00 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2023-04-14 06:37:33 +00:00
|
|
|
xs *noti = notify_get(&snac1, v);
|
|
|
|
|
|
|
|
if (noti == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *type = xs_dict_get(noti, "type");
|
2023-05-30 13:01:45 +00:00
|
|
|
const char *utype = xs_dict_get(noti, "utype");
|
2023-04-14 06:37:33 +00:00
|
|
|
const char *objid = xs_dict_get(noti, "objid");
|
2023-04-13 15:56:00 +00:00
|
|
|
xs *actor = NULL;
|
|
|
|
xs *entry = NULL;
|
|
|
|
|
2023-08-12 09:23:01 +00:00
|
|
|
if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
|
2023-04-13 15:56:00 +00:00
|
|
|
continue;
|
2023-04-13 15:34:48 +00:00
|
|
|
|
2023-04-13 15:56:00 +00:00
|
|
|
if (objid != NULL && !valid_status(object_get(objid, &entry)))
|
|
|
|
continue;
|
|
|
|
|
2023-04-14 10:23:32 +00:00
|
|
|
if (is_hidden(&snac1, objid))
|
|
|
|
continue;
|
|
|
|
|
2023-04-13 15:56:00 +00:00
|
|
|
/* convert the type */
|
|
|
|
if (strcmp(type, "Like") == 0)
|
|
|
|
type = "favourite";
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Announce") == 0)
|
|
|
|
type = "reblog";
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Follow") == 0)
|
|
|
|
type = "follow";
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Create") == 0)
|
|
|
|
type = "mention";
|
2023-05-30 13:01:45 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
|
|
|
|
type = "poll";
|
2023-04-13 15:56:00 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2023-05-11 08:54:56 +00:00
|
|
|
/* excluded type? */
|
|
|
|
if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
|
|
|
|
continue;
|
|
|
|
|
2023-04-13 15:56:00 +00:00
|
|
|
xs *mn = xs_dict_new();
|
|
|
|
|
|
|
|
mn = xs_dict_append(mn, "type", type);
|
|
|
|
|
2023-04-14 06:37:33 +00:00
|
|
|
xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
|
2023-04-13 15:56:00 +00:00
|
|
|
mn = xs_dict_append(mn, "id", id);
|
|
|
|
|
2023-04-14 06:37:33 +00:00
|
|
|
mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
|
2023-04-13 15:56:00 +00:00
|
|
|
|
|
|
|
xs *acct = mastoapi_account(actor);
|
|
|
|
mn = xs_dict_append(mn, "account", acct);
|
|
|
|
|
2023-04-14 06:37:33 +00:00
|
|
|
if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
|
2023-04-13 15:56:00 +00:00
|
|
|
xs *st = mastoapi_status(&snac1, entry);
|
2023-08-10 17:37:33 +00:00
|
|
|
|
|
|
|
if (st)
|
|
|
|
mn = xs_dict_append(mn, "status", st);
|
2023-04-13 15:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out = xs_list_append(out, mn);
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-04-13 15:34:48 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-11 18:10:24 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/filters") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac will never have filters */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac will never support a list of favourites */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac does not support bookmarks */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/lists") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac does not support lists */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
|
|
|
|
/* snac does not schedule notes */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac does not support optional follow confirmations */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* snac has no announcements (yet?) */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
|
2023-04-11 18:16:12 +00:00
|
|
|
/* are you kidding me? */
|
2023-04-11 18:10:24 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
2023-04-09 22:08:51 +00:00
|
|
|
}
|
2023-04-10 14:08:26 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/instance") == 0) { /** **/
|
2023-04-10 14:08:26 +00:00
|
|
|
/* returns an instance object */
|
|
|
|
xs *ins = xs_dict_new();
|
2023-08-14 09:31:11 +00:00
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
|
|
|
const char *title = xs_dict_get(srv_config, "title");
|
|
|
|
const char *sdesc = xs_dict_get(srv_config, "short_description");
|
2023-04-10 14:08:26 +00:00
|
|
|
|
2023-04-15 07:44:09 +00:00
|
|
|
ins = xs_dict_append(ins, "uri", host);
|
2023-04-10 14:08:26 +00:00
|
|
|
ins = xs_dict_append(ins, "domain", host);
|
2023-08-14 09:31:11 +00:00
|
|
|
ins = xs_dict_append(ins, "title", title && *title ? title : host);
|
2023-04-10 14:08:26 +00:00
|
|
|
ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
|
2023-04-25 04:40:46 +00:00
|
|
|
ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
|
2023-04-10 14:08:26 +00:00
|
|
|
ins = xs_dict_append(ins, "description", host);
|
|
|
|
|
2023-08-14 09:31:11 +00:00
|
|
|
ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
|
2023-04-15 16:06:18 +00:00
|
|
|
|
2023-04-10 14:08:26 +00:00
|
|
|
xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
|
2023-04-15 07:44:09 +00:00
|
|
|
ins = xs_dict_append(ins, "thumbnail", susie);
|
2023-04-10 14:08:26 +00:00
|
|
|
|
2023-04-20 15:59:09 +00:00
|
|
|
const char *v = xs_dict_get(srv_config, "admin_email");
|
|
|
|
if (xs_is_null(v) || *v == '\0')
|
|
|
|
v = "admin@localhost";
|
|
|
|
|
|
|
|
ins = xs_dict_append(ins, "email", v);
|
2023-04-10 14:08:26 +00:00
|
|
|
|
2023-08-13 10:09:35 +00:00
|
|
|
ins = xs_dict_append(ins, "rules", xs_stock_list);
|
2023-04-15 16:06:18 +00:00
|
|
|
|
2023-08-13 10:09:35 +00:00
|
|
|
xs *l1 = xs_list_append(xs_list_new(), "en");
|
2023-04-10 14:08:26 +00:00
|
|
|
ins = xs_dict_append(ins, "languages", l1);
|
|
|
|
|
2023-08-13 10:09:35 +00:00
|
|
|
ins = xs_dict_append(ins, "urls", xs_stock_dict);
|
2023-04-15 16:06:18 +00:00
|
|
|
|
2023-08-13 10:09:35 +00:00
|
|
|
xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
|
|
|
|
d2 = xs_dict_append(d2, "status_count", xs_stock_0);
|
|
|
|
d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
|
2023-08-12 16:42:11 +00:00
|
|
|
ins = xs_dict_append(ins, "stats", d2);
|
2023-04-20 16:37:09 +00:00
|
|
|
|
2023-06-15 15:51:24 +00:00
|
|
|
ins = xs_dict_append(ins, "registrations", xs_stock_false);
|
|
|
|
ins = xs_dict_append(ins, "approval_required", xs_stock_false);
|
|
|
|
ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
|
2023-04-20 16:37:09 +00:00
|
|
|
|
2023-04-24 14:29:21 +00:00
|
|
|
xs *cfg = xs_dict_new();
|
|
|
|
|
|
|
|
{
|
2023-08-12 16:38:55 +00:00
|
|
|
xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
|
|
|
|
"\"max_characters\":100000,\"max_media_attachments\":8}");
|
2023-04-24 14:29:21 +00:00
|
|
|
cfg = xs_dict_append(cfg, "statuses", d11);
|
2023-08-12 16:38:55 +00:00
|
|
|
|
|
|
|
xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
|
|
|
|
cfg = xs_dict_append(cfg, "accounts", d12);
|
|
|
|
|
|
|
|
xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
|
|
|
|
"\"image_size_limit\":16777216,"
|
|
|
|
"\"video_frame_rate_limit\":120,"
|
|
|
|
"\"video_matrix_limit\":8294400,"
|
|
|
|
"\"video_size_limit\":103809024}"
|
|
|
|
);
|
2023-08-17 16:06:36 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/* get the supported mime types from the internal list */
|
|
|
|
const char **p = xs_mime_types;
|
|
|
|
xs_set mtypes;
|
|
|
|
xs_set_init(&mtypes);
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
const char *type = p[1];
|
|
|
|
|
|
|
|
if (xs_startswith(type, "image/") ||
|
|
|
|
xs_startswith(type, "video/") ||
|
|
|
|
xs_startswith(type, "audio/"))
|
|
|
|
xs_set_add(&mtypes, type);
|
|
|
|
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
xs *l = xs_set_result(&mtypes);
|
|
|
|
d13 = xs_dict_append(d13, "supported_mime_types", l);
|
|
|
|
}
|
|
|
|
|
2023-08-12 16:38:55 +00:00
|
|
|
cfg = xs_dict_append(cfg, "media_attachments", d13);
|
|
|
|
|
|
|
|
xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
|
|
|
|
"\"max_expiration\":2629746,"
|
|
|
|
"\"max_options\":8,\"min_expiration\":300}");
|
|
|
|
cfg = xs_dict_append(cfg, "polls", d14);
|
2023-04-24 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ins = xs_dict_append(ins, "configuration", cfg);
|
|
|
|
|
2023-05-08 08:13:43 +00:00
|
|
|
const char *admin_account = xs_dict_get(srv_config, "admin_account");
|
|
|
|
|
|
|
|
if (!xs_is_null(admin_account) && *admin_account) {
|
|
|
|
snac admin;
|
|
|
|
|
|
|
|
if (user_open(&admin, admin_account)) {
|
|
|
|
xs *actor = msg_actor(&admin);
|
|
|
|
xs *acct = mastoapi_account(actor);
|
|
|
|
|
|
|
|
ins = xs_dict_append(ins, "contact_account", acct);
|
|
|
|
|
|
|
|
user_free(&admin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(ins, 4);
|
2023-04-10 14:08:26 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
2023-04-11 09:00:06 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
|
2023-04-24 15:49:08 +00:00
|
|
|
/* information about a status */
|
2023-06-11 04:06:37 +00:00
|
|
|
if (logged_in) {
|
|
|
|
xs *l = xs_split(cmd, "/");
|
|
|
|
const char *id = xs_list_get(l, 3);
|
|
|
|
const char *op = xs_list_get(l, 4);
|
2023-04-11 09:00:06 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (!xs_is_null(id)) {
|
|
|
|
xs *msg = NULL;
|
|
|
|
xs *out = NULL;
|
2023-04-11 09:00:06 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
/* skip the 'fake' part of the id */
|
|
|
|
id = MID_TO_MD5(id);
|
2023-04-11 09:00:06 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (valid_status(object_get_by_md5(id, &msg))) {
|
|
|
|
if (op == NULL) {
|
|
|
|
if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
|
|
|
|
/* return the status itself */
|
|
|
|
out = mastoapi_status(&snac1, msg);
|
|
|
|
}
|
2023-04-24 15:49:08 +00:00
|
|
|
}
|
2023-06-11 04:06:37 +00:00
|
|
|
else
|
|
|
|
if (strcmp(op, "context") == 0) { /** **/
|
|
|
|
/* return ancestors and children */
|
|
|
|
xs *anc = xs_list_new();
|
|
|
|
xs *des = xs_list_new();
|
|
|
|
xs_list *p;
|
|
|
|
xs_str *v;
|
|
|
|
char pid[64];
|
2023-04-11 17:59:02 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
/* build the [grand]parent list, moving up */
|
|
|
|
strncpy(pid, id, sizeof(pid));
|
2023-04-11 17:59:02 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
while (object_parent(pid, pid, sizeof(pid))) {
|
|
|
|
xs *m2 = NULL;
|
2023-04-11 17:59:02 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
|
|
|
|
xs *st = mastoapi_status(&snac1, m2);
|
2023-08-10 17:37:33 +00:00
|
|
|
|
|
|
|
if (st)
|
|
|
|
anc = xs_list_insert(anc, 0, st);
|
2023-06-11 04:06:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2023-04-11 17:59:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
/* build the children list */
|
|
|
|
xs *children = object_children(xs_dict_get(msg, "id"));
|
|
|
|
p = children;
|
2023-04-12 08:19:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *m2 = NULL;
|
2023-04-11 17:59:02 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
|
|
|
|
if (xs_is_null(xs_dict_get(m2, "name"))) {
|
|
|
|
xs *st = mastoapi_status(&snac1, m2);
|
2023-08-10 17:37:33 +00:00
|
|
|
|
|
|
|
if (st)
|
|
|
|
des = xs_list_append(des, st);
|
2023-06-11 04:06:37 +00:00
|
|
|
}
|
2023-05-31 08:51:37 +00:00
|
|
|
}
|
2023-04-11 17:59:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
out = xs_dict_new();
|
|
|
|
out = xs_dict_append(out, "ancestors", anc);
|
|
|
|
out = xs_dict_append(out, "descendants", des);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(op, "reblogged_by") == 0 || /** **/
|
|
|
|
strcmp(op, "favourited_by") == 0) { /** **/
|
|
|
|
/* return the list of people who liked or boosted this */
|
|
|
|
out = xs_list_new();
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
xs *l = NULL;
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (op[0] == 'r')
|
|
|
|
l = object_announces(xs_dict_get(msg, "id"));
|
|
|
|
else
|
|
|
|
l = object_likes(xs_dict_get(msg, "id"));
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
xs_list *p = l;
|
|
|
|
xs_str *v;
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *actor2 = NULL;
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (valid_status(object_get_by_md5(v, &actor2))) {
|
|
|
|
xs *acct2 = mastoapi_account(actor2);
|
2023-04-12 13:28:43 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
out = xs_list_append(out, acct2);
|
|
|
|
}
|
2023-04-12 13:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 09:00:06 +00:00
|
|
|
}
|
2023-06-11 04:06:37 +00:00
|
|
|
else
|
|
|
|
srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
|
2023-04-11 09:00:06 +00:00
|
|
|
|
2023-06-11 04:06:37 +00:00
|
|
|
if (out != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-06-11 04:06:37 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
2023-04-11 09:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-11 04:06:37 +00:00
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-11 09:00:06 +00:00
|
|
|
}
|
2023-04-13 04:38:05 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
|
2023-04-15 14:40:48 +00:00
|
|
|
*body = xs_dup("{}");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/markers") == 0) { /** **/
|
2023-04-15 14:40:48 +00:00
|
|
|
*body = xs_dup("{}");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
|
2023-04-15 14:40:48 +00:00
|
|
|
*body = xs_dup("[]");
|
|
|
|
*ctype = "application/json";
|
2023-04-13 04:38:05 +00:00
|
|
|
status = 200;
|
|
|
|
}
|
2023-04-23 03:33:54 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v2/search") == 0) { /** **/
|
2023-06-10 19:04:08 +00:00
|
|
|
if (logged_in) {
|
|
|
|
const char *q = xs_dict_get(args, "q");
|
|
|
|
const char *type = xs_dict_get(args, "type");
|
|
|
|
const char *offset = xs_dict_get(args, "offset");
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
xs *acl = xs_list_new();
|
|
|
|
xs *stl = xs_list_new();
|
|
|
|
xs *htl = xs_list_new();
|
|
|
|
xs *res = xs_dict_new();
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
|
|
|
|
/* reply something only for offset 0; otherwise,
|
|
|
|
apps like Tusky keep asking again and again */
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
|
|
|
|
/* do a webfinger query */
|
|
|
|
char *actor = NULL;
|
|
|
|
char *user = NULL;
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
if (valid_status(webfinger_request(q, &actor, &user))) {
|
|
|
|
xs *actor_o = NULL;
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
if (valid_status(actor_request(&snac1, actor, &actor_o))) {
|
|
|
|
xs *acct = mastoapi_account(actor_o);
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
acl = xs_list_append(acl, acct);
|
|
|
|
}
|
2023-04-23 03:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 19:04:08 +00:00
|
|
|
res = xs_dict_append(res, "accounts", acl);
|
|
|
|
res = xs_dict_append(res, "statuses", stl);
|
|
|
|
res = xs_dict_append(res, "hashtags", htl);
|
2023-04-23 03:33:54 +00:00
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(res, 4);
|
2023-06-10 19:04:08 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-23 03:33:54 +00:00
|
|
|
}
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-04-09 19:31:56 +00:00
|
|
|
/* user cleanup */
|
|
|
|
if (logged_in)
|
2023-04-12 16:44:15 +00:00
|
|
|
user_free(&snac1);
|
2023-04-09 19:31:56 +00:00
|
|
|
|
2023-10-28 05:10:21 +00:00
|
|
|
srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
|
|
|
|
|
2023-04-09 18:34:05 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-08 07:09:43 +00:00
|
|
|
int mastoapi_post_handler(const xs_dict *req, const char *q_path,
|
2023-04-09 18:34:05 +00:00
|
|
|
const char *payload, int p_size,
|
|
|
|
char **body, int *b_size, char **ctype)
|
2023-04-08 05:04:40 +00:00
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)p_size;
|
|
|
|
(void)b_size;
|
|
|
|
|
2023-04-16 06:13:45 +00:00
|
|
|
if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
|
2023-04-08 04:09:05 +00:00
|
|
|
return 0;
|
|
|
|
|
2023-04-13 07:59:00 +00:00
|
|
|
srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
|
2023-04-09 18:34:05 +00:00
|
|
|
|
2023-04-08 05:04:40 +00:00
|
|
|
int status = 404;
|
2023-04-11 19:07:47 +00:00
|
|
|
xs *args = NULL;
|
2023-04-08 05:04:40 +00:00
|
|
|
char *i_ctype = xs_dict_get(req, "content-type");
|
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
if (i_ctype && xs_startswith(i_ctype, "application/json"))
|
|
|
|
args = xs_json_loads(payload);
|
2023-04-08 05:04:40 +00:00
|
|
|
else
|
2023-04-11 19:07:47 +00:00
|
|
|
args = xs_dup(xs_dict_get(req, "p_vars"));
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
if (args == NULL)
|
2023-04-08 05:04:40 +00:00
|
|
|
return 400;
|
|
|
|
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *cmd = xs_replace_n(q_path, "/api", "", 1);
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-04-11 19:07:47 +00:00
|
|
|
snac snac = {0};
|
2023-04-12 10:16:23 +00:00
|
|
|
int logged_in = process_auth_token(&snac, req);
|
2023-04-11 19:07:47 +00:00
|
|
|
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/apps") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
const char *name = xs_dict_get(args, "client_name");
|
|
|
|
const char *ruri = xs_dict_get(args, "redirect_uris");
|
|
|
|
const char *scope = xs_dict_get(args, "scope");
|
2023-04-09 20:10:49 +00:00
|
|
|
|
|
|
|
if (xs_type(ruri) == XSTYPE_LIST)
|
|
|
|
ruri = xs_dict_get(ruri, 0);
|
2023-04-08 05:04:40 +00:00
|
|
|
|
|
|
|
if (name && ruri) {
|
2023-04-08 07:09:43 +00:00
|
|
|
xs *app = xs_dict_new();
|
|
|
|
xs *id = xs_replace_i(tid(0), ".", "");
|
|
|
|
xs *csec = random_str();
|
|
|
|
xs *vkey = random_str();
|
2023-04-28 06:28:17 +00:00
|
|
|
xs *cid = NULL;
|
|
|
|
|
|
|
|
/* pick a non-existent random cid */
|
|
|
|
for (;;) {
|
|
|
|
cid = random_str();
|
|
|
|
xs *p_app = app_get(cid);
|
|
|
|
|
|
|
|
if (p_app == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
xs_free(cid);
|
|
|
|
}
|
2023-04-08 07:09:43 +00:00
|
|
|
|
|
|
|
app = xs_dict_append(app, "name", name);
|
|
|
|
app = xs_dict_append(app, "redirect_uri", ruri);
|
|
|
|
app = xs_dict_append(app, "client_id", cid);
|
|
|
|
app = xs_dict_append(app, "client_secret", csec);
|
|
|
|
app = xs_dict_append(app, "vapid_key", vkey);
|
|
|
|
app = xs_dict_append(app, "id", id);
|
2023-04-08 05:04:40 +00:00
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(app, 4);
|
2023-04-08 05:04:40 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
2023-04-08 08:29:33 +00:00
|
|
|
|
|
|
|
app = xs_dict_append(app, "code", "");
|
2023-04-09 20:10:49 +00:00
|
|
|
|
|
|
|
if (scope)
|
|
|
|
app = xs_dict_append(app, "scope", scope);
|
|
|
|
|
2023-04-08 08:29:33 +00:00
|
|
|
app_add(cid, app);
|
2023-04-09 20:10:49 +00:00
|
|
|
|
2023-05-04 09:53:17 +00:00
|
|
|
srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
|
2023-04-08 05:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 19:07:47 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
if (logged_in) {
|
|
|
|
/* post a new Note */
|
2023-04-15 17:05:26 +00:00
|
|
|
const char *content = xs_dict_get(args, "status");
|
|
|
|
const char *mid = xs_dict_get(args, "in_reply_to_id");
|
|
|
|
const char *visibility = xs_dict_get(args, "visibility");
|
|
|
|
const char *summary = xs_dict_get(args, "spoiler_text");
|
2023-04-21 22:17:42 +00:00
|
|
|
const char *media_ids = xs_dict_get(args, "media_ids");
|
|
|
|
|
|
|
|
if (xs_is_null(media_ids))
|
|
|
|
media_ids = xs_dict_get(args, "media_ids[]");
|
2023-04-15 17:05:26 +00:00
|
|
|
|
2023-08-13 11:00:36 +00:00
|
|
|
if (xs_is_null(media_ids))
|
|
|
|
media_ids = xs_dict_get(args, "media_ids");
|
|
|
|
|
2023-05-04 04:27:13 +00:00
|
|
|
if (xs_is_null(visibility))
|
|
|
|
visibility = "public";
|
|
|
|
|
2023-04-15 17:05:26 +00:00
|
|
|
xs *attach_list = xs_list_new();
|
2023-04-16 05:12:57 +00:00
|
|
|
xs *irt = NULL;
|
2023-04-15 17:05:26 +00:00
|
|
|
|
|
|
|
/* is it a reply? */
|
|
|
|
if (mid != NULL) {
|
|
|
|
xs *r_msg = NULL;
|
|
|
|
const char *md5 = MID_TO_MD5(mid);
|
|
|
|
|
|
|
|
if (valid_status(object_get_by_md5(md5, &r_msg)))
|
2023-04-16 05:12:57 +00:00
|
|
|
irt = xs_dup(xs_dict_get(r_msg, "id"));
|
2023-04-15 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 22:17:42 +00:00
|
|
|
/* does it have attachments? */
|
|
|
|
if (!xs_is_null(media_ids)) {
|
|
|
|
xs *mi = NULL;
|
|
|
|
|
|
|
|
if (xs_type(media_ids) == XSTYPE_LIST)
|
|
|
|
mi = xs_dup(media_ids);
|
|
|
|
else {
|
|
|
|
mi = xs_list_new();
|
|
|
|
mi = xs_list_append(mi, media_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
xs_list *p = mi;
|
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2023-04-21 22:51:06 +00:00
|
|
|
xs *l = xs_list_new();
|
|
|
|
xs *url = xs_fmt("%s/s/%s", snac.actor, v);
|
|
|
|
xs *desc = static_get_meta(&snac, v);
|
2023-04-21 22:17:42 +00:00
|
|
|
|
|
|
|
l = xs_list_append(l, url);
|
2023-04-21 22:51:06 +00:00
|
|
|
l = xs_list_append(l, desc);
|
2023-04-21 22:17:42 +00:00
|
|
|
|
|
|
|
attach_list = xs_list_append(attach_list, l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 17:05:26 +00:00
|
|
|
/* prepare the message */
|
|
|
|
xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
|
|
|
|
strcmp(visibility, "public") == 0 ? 0 : 1);
|
|
|
|
|
|
|
|
if (!xs_is_null(summary) && *summary) {
|
2023-06-15 15:51:24 +00:00
|
|
|
msg = xs_dict_set(msg, "sensitive", xs_stock_true);
|
2023-04-15 17:05:26 +00:00
|
|
|
msg = xs_dict_set(msg, "summary", summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store */
|
|
|
|
timeline_add(&snac, xs_dict_get(msg, "id"), msg);
|
|
|
|
|
|
|
|
/* 'Create' message */
|
|
|
|
xs *c_msg = msg_create(&snac, msg);
|
|
|
|
enqueue_message(&snac, c_msg);
|
|
|
|
|
|
|
|
timeline_touch(&snac);
|
|
|
|
|
|
|
|
/* convert to a mastodon status as a response code */
|
|
|
|
xs *st = mastoapi_status(&snac, msg);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(st, 4);
|
2023-04-15 17:05:26 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
2023-04-11 19:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (xs_startswith(cmd, "/v1/statuses")) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
if (logged_in) {
|
|
|
|
/* operations on a status */
|
|
|
|
xs *l = xs_split(cmd, "/");
|
2023-04-16 05:12:57 +00:00
|
|
|
const char *mid = xs_list_get(l, 3);
|
|
|
|
const char *op = xs_list_get(l, 4);
|
2023-04-11 19:07:47 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(mid)) {
|
|
|
|
xs *msg = NULL;
|
|
|
|
xs *out = NULL;
|
|
|
|
|
2023-04-12 10:16:23 +00:00
|
|
|
/* skip the 'fake' part of the id */
|
2023-04-12 08:19:43 +00:00
|
|
|
mid = MID_TO_MD5(mid);
|
2023-04-11 19:07:47 +00:00
|
|
|
|
|
|
|
if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
|
|
|
|
char *id = xs_dict_get(msg, "id");
|
|
|
|
|
|
|
|
if (op == NULL) {
|
|
|
|
/* no operation (?) */
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "favourite") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
xs *n_msg = msg_admiration(&snac, id, "Like");
|
|
|
|
|
|
|
|
if (n_msg != NULL) {
|
|
|
|
enqueue_message(&snac, n_msg);
|
|
|
|
timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
|
|
|
|
|
|
|
|
out = mastoapi_status(&snac, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "unfavourite") == 0) { /** **/
|
2023-05-01 15:20:49 +00:00
|
|
|
/* partial support: as the original Like message
|
|
|
|
is not stored anywhere here, it's not possible
|
|
|
|
to send an Undo + Like; the only thing done here
|
|
|
|
is to delete the actor from the list of likes */
|
|
|
|
object_unadmire(id, snac.actor, 1);
|
2023-04-11 19:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "reblog") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
xs *n_msg = msg_admiration(&snac, id, "Announce");
|
|
|
|
|
|
|
|
if (n_msg != NULL) {
|
|
|
|
enqueue_message(&snac, n_msg);
|
|
|
|
timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
|
|
|
|
|
|
|
|
out = mastoapi_status(&snac, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "unreblog") == 0) { /** **/
|
2023-05-01 15:20:49 +00:00
|
|
|
/* partial support: see comment in 'unfavourite' */
|
|
|
|
object_unadmire(id, snac.actor, 0);
|
2023-04-11 19:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "bookmark") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
/* snac does not support bookmarks */
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "unbookmark") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
/* snac does not support bookmarks */
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "pin") == 0) { /** **/
|
2023-06-28 18:36:23 +00:00
|
|
|
/* pin this message */
|
|
|
|
if (pin(&snac, id))
|
|
|
|
out = mastoapi_status(&snac, msg);
|
|
|
|
else
|
|
|
|
status = 422;
|
2023-04-11 19:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "unpin") == 0) { /** **/
|
2023-06-28 18:36:23 +00:00
|
|
|
/* unpin this message */
|
|
|
|
unpin(&snac, id);
|
|
|
|
out = mastoapi_status(&snac, msg);
|
2023-04-11 19:07:47 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "mute") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
/* Mastodon's mute is snac's hide */
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(op, "unmute") == 0) { /** **/
|
2023-04-11 19:07:47 +00:00
|
|
|
/* Mastodon's unmute is snac's unhide */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-04-11 19:07:47 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
|
|
|
}
|
2023-04-14 17:39:31 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
|
2023-04-14 17:39:31 +00:00
|
|
|
if (logged_in) {
|
|
|
|
notify_clear(&snac);
|
|
|
|
timeline_touch(&snac);
|
|
|
|
|
|
|
|
*body = xs_dup("{}");
|
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
|
|
|
}
|
2023-04-15 16:06:18 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
|
2023-04-15 16:06:18 +00:00
|
|
|
/* I don't know what I'm doing */
|
|
|
|
if (logged_in) {
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
xs *wpush = xs_dict_new();
|
|
|
|
|
|
|
|
wpush = xs_dict_append(wpush, "id", "1");
|
|
|
|
|
|
|
|
v = xs_dict_get(args, "data");
|
|
|
|
v = xs_dict_get(v, "alerts");
|
|
|
|
wpush = xs_dict_append(wpush, "alerts", v);
|
|
|
|
|
|
|
|
v = xs_dict_get(args, "subscription");
|
|
|
|
v = xs_dict_get(v, "endpoint");
|
|
|
|
wpush = xs_dict_append(wpush, "endpoint", v);
|
|
|
|
|
|
|
|
xs *server_key = random_str();
|
|
|
|
wpush = xs_dict_append(wpush, "server_key", server_key);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(wpush, 4);
|
2023-04-15 16:06:18 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-16 06:13:45 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
|
2023-04-16 06:13:45 +00:00
|
|
|
if (logged_in) {
|
2023-04-21 22:17:42 +00:00
|
|
|
const xs_list *file = xs_dict_get(args, "file");
|
|
|
|
const char *desc = xs_dict_get(args, "description");
|
|
|
|
|
|
|
|
if (xs_is_null(desc))
|
|
|
|
desc = "";
|
|
|
|
|
|
|
|
status = 400;
|
|
|
|
|
|
|
|
if (xs_type(file) == XSTYPE_LIST) {
|
|
|
|
const char *fn = xs_list_get(file, 0);
|
|
|
|
|
|
|
|
if (*fn != '\0') {
|
|
|
|
char *ext = strrchr(fn, '.');
|
|
|
|
xs *hash = xs_md5_hex(fn, strlen(fn));
|
|
|
|
xs *id = xs_fmt("%s%s", hash, ext);
|
|
|
|
xs *url = xs_fmt("%s/s/%s", snac.actor, id);
|
|
|
|
int fo = xs_number_get(xs_list_get(file, 1));
|
|
|
|
int fs = xs_number_get(xs_list_get(file, 2));
|
|
|
|
|
|
|
|
/* store */
|
|
|
|
static_put(&snac, id, payload + fo, fs);
|
2023-04-21 22:51:06 +00:00
|
|
|
static_put_meta(&snac, id, desc);
|
2023-04-21 22:17:42 +00:00
|
|
|
|
|
|
|
/* prepare a response */
|
|
|
|
xs *rsp = xs_dict_new();
|
|
|
|
|
|
|
|
rsp = xs_dict_append(rsp, "id", id);
|
|
|
|
rsp = xs_dict_append(rsp, "type", "image");
|
|
|
|
rsp = xs_dict_append(rsp, "url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "preview_url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "remote_url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "description", desc);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(rsp, 4);
|
2023-04-21 22:17:42 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
2023-04-16 06:13:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-15 16:06:18 +00:00
|
|
|
}
|
2023-04-23 06:44:26 +00:00
|
|
|
else
|
2023-05-12 10:29:24 +00:00
|
|
|
if (xs_startswith(cmd, "/v1/accounts")) { /** **/
|
2023-04-23 06:44:26 +00:00
|
|
|
if (logged_in) {
|
|
|
|
/* account-related information */
|
|
|
|
xs *l = xs_split(cmd, "/");
|
|
|
|
const char *md5 = xs_list_get(l, 3);
|
|
|
|
const char *opt = xs_list_get(l, 4);
|
|
|
|
xs *rsp = NULL;
|
|
|
|
|
|
|
|
if (!xs_is_null(md5) && *md5) {
|
|
|
|
xs *actor_o = NULL;
|
|
|
|
|
|
|
|
if (xs_is_null(opt)) {
|
|
|
|
/* ? */
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(opt, "follow") == 0) { /** **/
|
2023-04-23 06:44:26 +00:00
|
|
|
if (valid_status(object_get_by_md5(md5, &actor_o))) {
|
|
|
|
const char *actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
xs *msg = msg_follow(&snac, actor);
|
|
|
|
|
|
|
|
if (msg != NULL) {
|
|
|
|
/* reload the actor from the message, in may be different */
|
|
|
|
actor = xs_dict_get(msg, "object");
|
|
|
|
|
|
|
|
following_add(&snac, actor, msg);
|
|
|
|
|
|
|
|
enqueue_output_by_actor(&snac, msg, actor, 0);
|
|
|
|
|
|
|
|
rsp = mastoapi_relationship(&snac, md5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-12 11:06:38 +00:00
|
|
|
if (strcmp(opt, "unfollow") == 0) { /** **/
|
2023-04-23 06:51:02 +00:00
|
|
|
if (valid_status(object_get_by_md5(md5, &actor_o))) {
|
|
|
|
const char *actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
/* get the following object */
|
|
|
|
xs *object = NULL;
|
|
|
|
|
|
|
|
if (valid_status(following_get(&snac, actor, &object))) {
|
|
|
|
xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
|
|
|
|
|
|
|
|
following_del(&snac, actor);
|
|
|
|
|
|
|
|
enqueue_output_by_actor(&snac, msg, actor, 0);
|
|
|
|
|
|
|
|
rsp = mastoapi_relationship(&snac, md5);
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 06:44:26 +00:00
|
|
|
}
|
2023-05-18 07:46:21 +00:00
|
|
|
else
|
|
|
|
if (strcmp(opt, "block") == 0) { /** **/
|
|
|
|
if (valid_status(object_get_by_md5(md5, &actor_o))) {
|
|
|
|
const char *actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
mute(&snac, actor);
|
|
|
|
|
|
|
|
rsp = mastoapi_relationship(&snac, md5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(opt, "unblock") == 0) { /** **/
|
|
|
|
if (valid_status(object_get_by_md5(md5, &actor_o))) {
|
|
|
|
const char *actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
unmute(&snac, actor);
|
|
|
|
|
|
|
|
rsp = mastoapi_relationship(&snac, md5);
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 06:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rsp != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(rsp, 4);
|
2023-04-23 06:44:26 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-05-31 09:13:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (xs_startswith(cmd, "/v1/polls")) { /** **/
|
|
|
|
if (logged_in) {
|
|
|
|
/* operations on a status */
|
|
|
|
xs *l = xs_split(cmd, "/");
|
|
|
|
const char *mid = xs_list_get(l, 3);
|
|
|
|
const char *op = xs_list_get(l, 4);
|
|
|
|
|
|
|
|
if (!xs_is_null(mid)) {
|
|
|
|
xs *msg = NULL;
|
|
|
|
xs *out = NULL;
|
|
|
|
|
|
|
|
/* skip the 'fake' part of the id */
|
|
|
|
mid = MID_TO_MD5(mid);
|
|
|
|
|
|
|
|
if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
|
2023-06-01 06:40:08 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
const char *atto = xs_dict_get(msg, "attributedTo");
|
|
|
|
|
|
|
|
xs_list *opts = xs_dict_get(msg, "oneOf");
|
|
|
|
if (opts == NULL)
|
|
|
|
opts = xs_dict_get(msg, "anyOf");
|
|
|
|
|
2023-05-31 09:13:06 +00:00
|
|
|
if (op == NULL) {
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(op, "votes") == 0) {
|
2023-06-01 06:40:08 +00:00
|
|
|
xs_list *choices = xs_dict_get(args, "choices[]");
|
|
|
|
|
2023-08-13 11:00:36 +00:00
|
|
|
if (xs_is_null(choices))
|
|
|
|
choices = xs_dict_get(args, "choices");
|
|
|
|
|
2023-06-01 06:40:08 +00:00
|
|
|
if (xs_type(choices) == XSTYPE_LIST) {
|
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&choices, &v)) {
|
|
|
|
int io = atoi(v);
|
|
|
|
const xs_dict *o = xs_list_get(opts, io);
|
|
|
|
|
|
|
|
if (o) {
|
|
|
|
const char *name = xs_dict_get(o, "name");
|
|
|
|
|
|
|
|
xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
|
|
|
|
msg = xs_dict_append(msg, "name", name);
|
|
|
|
|
|
|
|
xs *c_msg = msg_create(&snac, msg);
|
|
|
|
enqueue_message(&snac, c_msg);
|
|
|
|
timeline_add(&snac, xs_dict_get(msg, "id"), msg);
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 09:13:06 +00:00
|
|
|
|
2023-06-01 06:40:08 +00:00
|
|
|
out = mastoapi_poll(&snac, msg);
|
2023-05-31 09:13:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(out, 4);
|
2023-05-31 09:13:06 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
2023-04-23 06:44:26 +00:00
|
|
|
}
|
2023-04-08 04:09:05 +00:00
|
|
|
|
2023-04-12 10:16:23 +00:00
|
|
|
/* user cleanup */
|
|
|
|
if (logged_in)
|
|
|
|
user_free(&snac);
|
|
|
|
|
2023-04-08 04:09:05 +00:00
|
|
|
return status;
|
|
|
|
}
|
2023-04-21 21:17:58 +00:00
|
|
|
|
2023-04-21 23:21:09 +00:00
|
|
|
|
|
|
|
int mastoapi_put_handler(const xs_dict *req, const char *q_path,
|
|
|
|
const char *payload, int p_size,
|
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)p_size;
|
|
|
|
(void)b_size;
|
|
|
|
|
2023-04-21 23:21:09 +00:00
|
|
|
if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
|
|
|
|
|
|
|
|
int status = 404;
|
|
|
|
xs *args = NULL;
|
|
|
|
char *i_ctype = xs_dict_get(req, "content-type");
|
|
|
|
|
|
|
|
if (i_ctype && xs_startswith(i_ctype, "application/json"))
|
|
|
|
args = xs_json_loads(payload);
|
|
|
|
else
|
|
|
|
args = xs_dup(xs_dict_get(req, "p_vars"));
|
|
|
|
|
|
|
|
if (args == NULL)
|
|
|
|
return 400;
|
|
|
|
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *cmd = xs_replace_n(q_path, "/api", "", 1);
|
2023-04-21 23:21:09 +00:00
|
|
|
|
|
|
|
snac snac = {0};
|
|
|
|
int logged_in = process_auth_token(&snac, req);
|
|
|
|
|
2023-05-12 10:29:24 +00:00
|
|
|
if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
|
2023-04-21 23:21:09 +00:00
|
|
|
if (logged_in) {
|
|
|
|
xs *l = xs_split(cmd, "/");
|
|
|
|
const char *stid = xs_list_get(l, 3);
|
|
|
|
|
|
|
|
if (!xs_is_null(stid)) {
|
|
|
|
const char *desc = xs_dict_get(args, "description");
|
|
|
|
|
|
|
|
/* set the image metadata */
|
|
|
|
static_put_meta(&snac, stid, desc);
|
|
|
|
|
|
|
|
/* prepare a response */
|
|
|
|
xs *rsp = xs_dict_new();
|
|
|
|
xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
|
|
|
|
|
|
|
|
rsp = xs_dict_append(rsp, "id", stid);
|
|
|
|
rsp = xs_dict_append(rsp, "type", "image");
|
|
|
|
rsp = xs_dict_append(rsp, "url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "preview_url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "remote_url", url);
|
|
|
|
rsp = xs_dict_append(rsp, "description", desc);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(rsp, 4);
|
2023-04-21 23:21:09 +00:00
|
|
|
*ctype = "application/json";
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 401;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* user cleanup */
|
|
|
|
if (logged_in)
|
|
|
|
user_free(&snac);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-28 06:33:02 +00:00
|
|
|
void mastoapi_purge(void)
|
|
|
|
{
|
2023-04-28 06:58:33 +00:00
|
|
|
xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
|
|
|
|
xs *files = xs_glob(spec, 1, 0);
|
|
|
|
xs_list *p = files;
|
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
time_t mt = time(NULL) - 3600;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *cid = xs_replace(v, ".json", "");
|
|
|
|
xs *fn = _app_fn(cid);
|
|
|
|
|
|
|
|
if (mtime(fn) < mt) {
|
|
|
|
/* get the app */
|
|
|
|
xs *app = app_get(cid);
|
|
|
|
|
|
|
|
if (app) {
|
|
|
|
/* old apps with no uid are incomplete cruft */
|
|
|
|
const char *uid = xs_dict_get(app, "uid");
|
|
|
|
|
|
|
|
if (xs_is_null(uid) || *uid == '\0') {
|
|
|
|
unlink(fn);
|
|
|
|
srv_debug(2, xs_fmt("purged %s", fn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-28 06:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-21 21:17:58 +00:00
|
|
|
#endif /* #ifndef NO_MASTODON_API */
|