2022-09-23 15:33:33 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2024-01-04 08:22:03 +00:00
|
|
|
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
|
2022-09-23 15:33:33 +00:00
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#include "xs_json.h"
|
|
|
|
#include "xs_curl.h"
|
2022-09-25 19:02:47 +00:00
|
|
|
#include "xs_mime.h"
|
2022-09-27 05:16:46 +00:00
|
|
|
#include "xs_openssl.h"
|
2022-09-27 16:20:25 +00:00
|
|
|
#include "xs_regex.h"
|
2022-10-02 07:27:17 +00:00
|
|
|
#include "xs_time.h"
|
2022-11-28 14:49:56 +00:00
|
|
|
#include "xs_set.h"
|
2023-09-17 01:00:31 +00:00
|
|
|
#include "xs_match.h"
|
2022-09-23 15:33:33 +00:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2023-01-13 12:25:14 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2022-09-23 15:33:33 +00:00
|
|
|
const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
|
|
|
|
|
2023-01-27 17:17:11 +00:00
|
|
|
/* susie.png */
|
2023-01-27 17:21:54 +00:00
|
|
|
|
2023-01-27 17:17:11 +00:00
|
|
|
const char *susie =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
|
|
|
"CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
|
|
|
|
"vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
|
|
|
|
"eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
|
|
|
|
"AYTtEsDU9F34AAAAAElFTkSuQmCC";
|
|
|
|
|
2023-01-27 17:21:54 +00:00
|
|
|
const char *susie_cool =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
|
|
|
"CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
|
|
|
|
"qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
|
|
|
|
"+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
|
|
|
|
"ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
|
|
|
|
|
2023-05-20 17:25:52 +00:00
|
|
|
const char *susie_muertos =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
2023-05-20 17:33:57 +00:00
|
|
|
"CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
|
|
|
|
"ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
|
|
|
|
"xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
|
|
|
|
"QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
|
2023-05-20 17:25:52 +00:00
|
|
|
|
2023-01-27 17:17:11 +00:00
|
|
|
|
|
|
|
const char *default_avatar_base64(void)
|
|
|
|
/* returns the default avatar in base64 */
|
|
|
|
{
|
2023-01-27 17:28:55 +00:00
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm tm;
|
2023-05-20 17:25:52 +00:00
|
|
|
const char *p = susie;
|
2023-01-27 17:28:55 +00:00
|
|
|
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
|
2023-05-20 17:25:52 +00:00
|
|
|
if (tm.tm_mon == 10 && tm.tm_mday == 2)
|
|
|
|
p = susie_muertos;
|
|
|
|
else
|
|
|
|
if (tm.tm_wday == 0 || tm.tm_wday == 6)
|
|
|
|
p = susie_cool;
|
|
|
|
|
|
|
|
return p;
|
2023-01-27 17:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-10 09:27:45 +00:00
|
|
|
int activitypub_request(snac *user, const char *url, xs_dict **data)
|
2022-09-23 15:33:33 +00:00
|
|
|
/* request an object */
|
|
|
|
{
|
2023-12-10 09:27:45 +00:00
|
|
|
int status = 0;
|
2022-09-23 15:33:33 +00:00
|
|
|
xs *response = NULL;
|
2022-09-23 16:15:59 +00:00
|
|
|
xs *payload = NULL;
|
2022-09-23 15:33:33 +00:00
|
|
|
int p_size;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ctype;
|
2022-09-23 15:33:33 +00:00
|
|
|
|
2023-12-10 09:27:45 +00:00
|
|
|
*data = NULL;
|
|
|
|
|
|
|
|
if (user != NULL) {
|
|
|
|
/* get from the net */
|
|
|
|
response = http_signed_request(user, "GET", url,
|
|
|
|
NULL, NULL, 0, &status, &payload, &p_size, 0);
|
|
|
|
}
|
2022-09-23 15:33:33 +00:00
|
|
|
|
2023-02-24 08:00:03 +00:00
|
|
|
if (status == 0 || (status >= 500 && status <= 599)) {
|
|
|
|
/* I found an instance running Misskey that returned
|
|
|
|
500 on signed messages but returned the object
|
|
|
|
perfectly without signing (?), so why not try */
|
|
|
|
xs_free(response);
|
|
|
|
|
|
|
|
xs *hdrs = xs_dict_new();
|
2023-03-02 12:30:29 +00:00
|
|
|
hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
|
|
|
|
hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
|
2023-02-24 08:00:03 +00:00
|
|
|
|
|
|
|
response = xs_http_request("GET", url, hdrs,
|
|
|
|
NULL, 0, &status, &payload, &p_size, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:33:33 +00:00
|
|
|
if (valid_status(status)) {
|
2022-09-23 16:15:59 +00:00
|
|
|
/* ensure it's ActivityPub data */
|
|
|
|
ctype = xs_dict_get(response, "content-type");
|
|
|
|
|
2023-06-19 15:39:53 +00:00
|
|
|
if (xs_is_null(ctype))
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_BAD_REQUEST;
|
2023-06-19 15:39:53 +00:00
|
|
|
else
|
2022-09-27 07:38:46 +00:00
|
|
|
if (xs_str_in(ctype, "application/activity+json") != -1 ||
|
2023-11-29 07:58:31 +00:00
|
|
|
xs_str_in(ctype, "application/ld+json") != -1) {
|
|
|
|
|
|
|
|
/* if there is no payload, fail */
|
|
|
|
if (xs_is_null(payload))
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_BAD_REQUEST;
|
2023-11-29 07:58:31 +00:00
|
|
|
else
|
|
|
|
*data = xs_json_loads(payload);
|
|
|
|
}
|
2022-09-23 16:15:59 +00:00
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
|
2022-09-23 15:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-17 18:53:54 +00:00
|
|
|
int actor_request(snac *user, const char *actor, xs_dict **data)
|
2022-09-23 15:33:33 +00:00
|
|
|
/* request an actor */
|
|
|
|
{
|
2023-12-11 09:41:41 +00:00
|
|
|
int status;
|
2022-09-23 16:15:59 +00:00
|
|
|
xs *payload = NULL;
|
2022-09-23 15:33:33 +00:00
|
|
|
|
2023-03-01 08:22:32 +00:00
|
|
|
if (data)
|
|
|
|
*data = NULL;
|
|
|
|
|
2022-09-23 15:33:33 +00:00
|
|
|
/* get from disk first */
|
2024-03-13 05:30:24 +00:00
|
|
|
status = actor_get_refresh(user, actor, data);
|
2022-09-23 15:33:33 +00:00
|
|
|
|
2024-03-12 18:44:08 +00:00
|
|
|
if (!valid_status(status)) {
|
|
|
|
/* actor data non-existent: get from the net */
|
2023-12-17 18:53:54 +00:00
|
|
|
status = activitypub_request(user, actor, &payload);
|
2023-03-01 08:22:32 +00:00
|
|
|
|
2023-12-11 09:41:41 +00:00
|
|
|
if (valid_status(status)) {
|
2023-03-01 08:22:32 +00:00
|
|
|
/* renew data */
|
2023-05-04 07:19:26 +00:00
|
|
|
status = actor_add(actor, payload);
|
2023-03-01 08:22:32 +00:00
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
*data = payload;
|
|
|
|
payload = NULL;
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 06:19:42 +00:00
|
|
|
else
|
2023-12-14 20:40:50 +00:00
|
|
|
srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
|
2023-03-01 08:22:32 +00:00
|
|
|
}
|
2022-09-23 15:33:33 +00:00
|
|
|
|
2023-03-08 09:14:40 +00:00
|
|
|
/* collect the (presumed) shared inbox in this actor */
|
|
|
|
if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
|
|
|
|
if (valid_status(status) && data && *data)
|
|
|
|
inbox_add_by_actor(*data);
|
|
|
|
}
|
2022-09-23 15:33:33 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2022-09-23 17:07:45 +00:00
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *get_atto(const xs_dict *msg)
|
2024-01-18 22:04:37 +00:00
|
|
|
/* gets the attributedTo field (an actor) */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_val *actor = xs_dict_get(msg, "attributedTo");
|
2024-01-18 22:04:37 +00:00
|
|
|
|
|
|
|
/* if the actor is a list of objects (like on Peertube videos), pick the Person */
|
|
|
|
if (xs_type(actor) == XSTYPE_LIST) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *p = actor;
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_dict *v;
|
2024-01-18 22:04:37 +00:00
|
|
|
actor = NULL;
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
while (actor == NULL && xs_list_next(p, &v, &c)) {
|
2024-01-18 22:04:37 +00:00
|
|
|
if (xs_type(v) == XSTYPE_DICT) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type = xs_dict_get(v, "type");
|
2024-01-18 22:04:37 +00:00
|
|
|
if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
|
|
|
|
actor = xs_dict_get(v, "id");
|
|
|
|
|
|
|
|
if (xs_type(actor) != XSTYPE_STRING)
|
|
|
|
actor = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-19 05:46:14 +00:00
|
|
|
const char *get_in_reply_to(const xs_dict *msg)
|
|
|
|
/* gets the inReplyTo id */
|
|
|
|
{
|
|
|
|
const xs_val *in_reply_to = xs_dict_get(msg, "inReplyTo");
|
|
|
|
|
|
|
|
if (xs_type(in_reply_to) == XSTYPE_DICT)
|
|
|
|
in_reply_to = xs_dict_get(in_reply_to, "id");
|
|
|
|
|
|
|
|
return in_reply_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-24 18:13:46 +00:00
|
|
|
xs_list *get_attachments(const xs_dict *msg)
|
|
|
|
/* unify the garbage fire that are the attachments */
|
|
|
|
{
|
|
|
|
xs_list *l = xs_list_new();
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *p;
|
2024-01-24 18:13:46 +00:00
|
|
|
|
|
|
|
/* try first the attachments list */
|
2024-01-24 18:30:01 +00:00
|
|
|
if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
|
2024-01-24 18:13:46 +00:00
|
|
|
xs *attach = NULL;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2024-01-24 18:13:46 +00:00
|
|
|
|
|
|
|
/* ensure it's a list */
|
|
|
|
if (xs_type(p) == XSTYPE_DICT) {
|
|
|
|
attach = xs_list_new();
|
2024-02-16 17:20:48 +00:00
|
|
|
attach = xs_list_append(attach, p);
|
2024-01-24 18:13:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
attach = xs_dup(p);
|
|
|
|
|
2024-11-07 19:04:02 +00:00
|
|
|
if (xs_type(attach) == XSTYPE_LIST && xs_list_len(attach) == 0) {
|
2024-01-24 18:13:46 +00:00
|
|
|
/* does the message have an image? */
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *d = xs_dict_get(msg, "image");
|
|
|
|
if (xs_type(d) == XSTYPE_DICT) {
|
2024-01-24 18:13:46 +00:00
|
|
|
/* add it to the attachment list */
|
2024-05-21 12:12:15 +00:00
|
|
|
attach = xs_list_append(attach, d);
|
2024-01-24 18:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now iterate the list */
|
2024-05-21 12:12:15 +00:00
|
|
|
int c = 0;
|
|
|
|
while (xs_list_next(attach, &v, &c)) {
|
|
|
|
const char *type = xs_dict_get(v, "mediaType");
|
2024-01-24 18:13:46 +00:00
|
|
|
if (xs_is_null(type))
|
|
|
|
type = xs_dict_get(v, "type");
|
|
|
|
|
|
|
|
if (xs_is_null(type))
|
|
|
|
continue;
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *href = xs_dict_get(v, "url");
|
2024-01-24 18:13:46 +00:00
|
|
|
if (xs_is_null(href))
|
|
|
|
href = xs_dict_get(v, "href");
|
|
|
|
if (xs_is_null(href))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* infer MIME type from non-specific attachments */
|
|
|
|
if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
|
|
|
|
char *mt = (char *)xs_mime_by_ext(href);
|
|
|
|
|
|
|
|
if (xs_match(mt, "image/*|audio/*|video/*")) /* */
|
|
|
|
type = mt;
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *name = xs_dict_get(v, "name");
|
2024-01-24 18:13:46 +00:00
|
|
|
if (xs_is_null(name))
|
|
|
|
name = xs_dict_get(msg, "name");
|
|
|
|
if (xs_is_null(name))
|
2024-05-30 14:12:08 +00:00
|
|
|
name = "";
|
2024-01-24 18:13:46 +00:00
|
|
|
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
d = xs_dict_append(d, "type", type);
|
|
|
|
d = xs_dict_append(d, "href", href);
|
|
|
|
d = xs_dict_append(d, "name", name);
|
|
|
|
|
|
|
|
l = xs_list_append(l, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** urls (attachments from Peertube) **/
|
|
|
|
p = xs_dict_get(msg, "url");
|
|
|
|
|
|
|
|
if (xs_type(p) == XSTYPE_LIST) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *href = NULL;
|
|
|
|
const char *type = NULL;
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2024-01-24 18:13:46 +00:00
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
while (href == NULL && xs_list_next(p, &v, &c)) {
|
2024-01-24 18:13:46 +00:00
|
|
|
if (xs_type(v) == XSTYPE_DICT) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *mtype = xs_dict_get(v, "type");
|
2024-01-24 18:13:46 +00:00
|
|
|
|
2024-01-24 18:30:01 +00:00
|
|
|
if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
|
2024-01-24 18:13:46 +00:00
|
|
|
mtype = xs_dict_get(v, "mediaType");
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *tag = xs_dict_get(v, "tag");
|
2024-01-24 18:13:46 +00:00
|
|
|
|
|
|
|
if (xs_type(mtype) == XSTYPE_STRING &&
|
|
|
|
strcmp(mtype, "application/x-mpegURL") == 0 &&
|
|
|
|
xs_type(tag) == XSTYPE_LIST) {
|
|
|
|
/* now iterate the tag list, looking for a video URL */
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_dict *d;
|
2024-05-21 12:12:15 +00:00
|
|
|
int c = 0;
|
2024-01-24 18:13:46 +00:00
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
while (href == NULL && xs_list_next(tag, &d, &c)) {
|
2024-01-24 18:13:46 +00:00
|
|
|
if (xs_type(d) == XSTYPE_DICT) {
|
|
|
|
if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
|
|
|
|
xs_startswith(mtype, "video/")) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *h = xs_dict_get(d, "href");
|
2024-01-24 18:13:46 +00:00
|
|
|
|
|
|
|
/* this is probably it */
|
|
|
|
if (xs_type(h) == XSTYPE_STRING) {
|
|
|
|
href = h;
|
|
|
|
type = mtype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (href && type) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
d = xs_dict_append(d, "href", href);
|
|
|
|
d = xs_dict_append(d, "type", type);
|
|
|
|
d = xs_dict_append(d, "name", "---");
|
|
|
|
|
|
|
|
l = xs_list_append(l, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
|
2022-09-25 16:28:15 +00:00
|
|
|
/* ensures that an entry and its ancestors are in the timeline */
|
|
|
|
{
|
2022-09-27 12:50:13 +00:00
|
|
|
int status = 0;
|
|
|
|
|
2024-03-08 04:41:08 +00:00
|
|
|
if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
|
2023-07-13 19:01:15 +00:00
|
|
|
xs *msg = NULL;
|
2022-09-25 16:28:15 +00:00
|
|
|
|
2024-03-25 10:44:19 +00:00
|
|
|
/* from a blocked instance? discard and break */
|
|
|
|
if (is_instance_blocked(*id)) {
|
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-06-07 09:15:58 +00:00
|
|
|
/* is the object already there? */
|
2023-07-13 19:01:15 +00:00
|
|
|
if (!valid_status(object_get(*id, &msg))) {
|
2022-09-25 16:28:15 +00:00
|
|
|
/* no; download it */
|
2023-07-13 19:01:15 +00:00
|
|
|
status = activitypub_request(snac, *id, &msg);
|
2022-09-25 16:28:15 +00:00
|
|
|
|
|
|
|
if (valid_status(status)) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *object = msg;
|
2023-07-13 19:01:15 +00:00
|
|
|
const char *type = xs_dict_get(object, "type");
|
2022-10-01 16:48:05 +00:00
|
|
|
|
2022-12-23 09:01:10 +00:00
|
|
|
/* get the id again from the object, as it may be different */
|
2023-07-13 19:01:15 +00:00
|
|
|
const char *nid = xs_dict_get(object, "id");
|
2022-12-23 09:01:10 +00:00
|
|
|
|
2023-07-15 14:13:26 +00:00
|
|
|
if (xs_type(nid) != XSTYPE_STRING)
|
|
|
|
return 0;
|
|
|
|
|
2022-12-23 20:41:52 +00:00
|
|
|
if (wrk && strcmp(nid, *id) != 0) {
|
2022-12-23 11:32:59 +00:00
|
|
|
snac_debug(snac, 1,
|
2022-12-23 20:30:18 +00:00
|
|
|
xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
|
|
|
|
|
2022-12-23 20:41:52 +00:00
|
|
|
*wrk = xs_dup(nid);
|
|
|
|
*id = *wrk;
|
2022-12-23 20:30:18 +00:00
|
|
|
}
|
2022-12-23 09:01:10 +00:00
|
|
|
|
2023-07-13 19:01:15 +00:00
|
|
|
if (xs_is_null(type))
|
|
|
|
type = "(null)";
|
|
|
|
|
2023-12-14 18:58:44 +00:00
|
|
|
srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
|
2023-07-13 19:01:15 +00:00
|
|
|
|
|
|
|
if (strcmp(type, "Create") == 0) {
|
|
|
|
/* some software like lemmy nest Announce + Create + Note */
|
2023-07-14 07:05:27 +00:00
|
|
|
if (!xs_is_null(object = xs_dict_get(object, "object"))) {
|
2023-07-13 19:01:15 +00:00
|
|
|
type = xs_dict_get(object, "type");
|
2023-07-14 07:05:27 +00:00
|
|
|
nid = xs_dict_get(object, "id");
|
|
|
|
}
|
2023-07-13 19:01:15 +00:00
|
|
|
else
|
|
|
|
type = "(null)";
|
|
|
|
}
|
|
|
|
|
2024-05-11 16:46:15 +00:00
|
|
|
if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
|
2024-05-15 11:31:53 +00:00
|
|
|
if (content_match("filter_reject.txt", object))
|
2024-03-11 08:53:20 +00:00
|
|
|
snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
|
|
|
|
else {
|
2024-05-11 17:16:54 +00:00
|
|
|
const char *actor = get_atto(object);
|
|
|
|
|
|
|
|
if (!xs_is_null(actor)) {
|
|
|
|
/* request (and drop) the actor for this entry */
|
|
|
|
if (!valid_status(actor_request(snac, actor, NULL))) {
|
|
|
|
/* failed? retry later */
|
|
|
|
enqueue_actor_refresh(snac, actor, 60);
|
|
|
|
}
|
2022-09-25 16:28:15 +00:00
|
|
|
|
2024-05-11 17:16:54 +00:00
|
|
|
/* does it have an ancestor? */
|
2024-11-19 05:46:14 +00:00
|
|
|
const char *in_reply_to = get_in_reply_to(object);
|
2022-09-25 16:28:15 +00:00
|
|
|
|
2024-05-11 17:16:54 +00:00
|
|
|
/* store */
|
|
|
|
timeline_add(snac, nid, object);
|
2023-06-07 09:15:58 +00:00
|
|
|
|
2024-10-19 20:29:46 +00:00
|
|
|
/* redistribute to lists for this user */
|
|
|
|
list_distribute(snac, actor, object);
|
|
|
|
|
2024-05-11 17:16:54 +00:00
|
|
|
/* recurse! */
|
|
|
|
timeline_request(snac, &in_reply_to, NULL, level + 1);
|
|
|
|
}
|
2024-03-11 08:53:20 +00:00
|
|
|
}
|
2022-11-01 19:13:23 +00:00
|
|
|
}
|
2022-09-25 16:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 12:50:13 +00:00
|
|
|
|
|
|
|
return status;
|
2022-09-25 16:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 09:29:06 +00:00
|
|
|
int send_to_inbox_raw(const char *keyid, const char *seckey,
|
|
|
|
const xs_str *inbox, const xs_dict *msg,
|
|
|
|
xs_val **payload, int *p_size, int timeout)
|
2022-09-23 17:07:45 +00:00
|
|
|
/* sends a message to an Inbox */
|
|
|
|
{
|
|
|
|
int status;
|
2023-02-07 09:29:06 +00:00
|
|
|
xs_dict *response;
|
2023-08-08 17:29:34 +00:00
|
|
|
xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
|
2022-09-23 17:07:45 +00:00
|
|
|
|
2023-02-07 09:29:06 +00:00
|
|
|
response = http_signed_request_raw(keyid, seckey, "POST", inbox,
|
2023-01-24 14:06:58 +00:00
|
|
|
NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
|
2022-09-23 17:07:45 +00:00
|
|
|
|
2022-10-25 11:59:15 +00:00
|
|
|
xs_free(response);
|
2022-09-23 17:07:45 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 09:29:06 +00:00
|
|
|
int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
|
|
|
|
xs_val **payload, int *p_size, int timeout)
|
|
|
|
/* sends a message to an Inbox */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *seckey = xs_dict_get(snac->key, "secret");
|
2023-02-07 09:29:06 +00:00
|
|
|
|
|
|
|
return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 15:37:15 +00:00
|
|
|
xs_str *get_actor_inbox(const char *actor, int shared)
|
2022-11-17 08:04:24 +00:00
|
|
|
/* gets an actor's inbox */
|
2022-09-23 17:07:45 +00:00
|
|
|
{
|
|
|
|
xs *data = NULL;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v = NULL;
|
2022-09-23 17:07:45 +00:00
|
|
|
|
2023-12-17 18:53:54 +00:00
|
|
|
if (valid_status(actor_request(NULL, actor, &data))) {
|
2022-11-17 08:04:24 +00:00
|
|
|
/* try first endpoints/sharedInbox */
|
2024-10-08 15:37:15 +00:00
|
|
|
if (shared && (v = xs_dict_get(data, "endpoints")))
|
2022-11-17 08:04:24 +00:00
|
|
|
v = xs_dict_get(v, "sharedInbox");
|
2022-09-23 17:07:45 +00:00
|
|
|
|
2022-11-17 08:04:24 +00:00
|
|
|
/* try then the regular inbox */
|
|
|
|
if (xs_is_null(v))
|
|
|
|
v = xs_dict_get(data, "inbox");
|
2022-09-23 17:07:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 08:04:24 +00:00
|
|
|
return xs_is_null(v) ? NULL : xs_dup(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-13 16:18:23 +00:00
|
|
|
int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
|
2023-07-13 15:58:18 +00:00
|
|
|
xs_val **payload, int *p_size, int timeout)
|
2022-11-17 08:04:24 +00:00
|
|
|
/* sends a message to an actor */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_BAD_REQUEST;
|
2024-10-08 15:37:15 +00:00
|
|
|
xs *inbox = get_actor_inbox(actor, 1);
|
2022-11-17 08:04:24 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(inbox))
|
2023-01-24 14:06:58 +00:00
|
|
|
status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
|
2022-11-17 08:04:24 +00:00
|
|
|
|
2022-09-23 17:07:45 +00:00
|
|
|
return status;
|
|
|
|
}
|
2022-09-23 17:37:01 +00:00
|
|
|
|
|
|
|
|
2023-07-13 16:18:23 +00:00
|
|
|
void post_message(snac *snac, const char *actor, const xs_dict *msg)
|
|
|
|
/* posts a message immediately (bypassing the output queues) */
|
|
|
|
{
|
|
|
|
xs *payload = NULL;
|
|
|
|
int p_size;
|
|
|
|
|
2023-07-13 16:21:30 +00:00
|
|
|
int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
|
|
|
|
|
|
|
|
srv_log(xs_fmt("post_message to actor %s %d", actor, status));
|
|
|
|
|
|
|
|
if (!valid_status(status))
|
2023-07-13 16:18:23 +00:00
|
|
|
/* cannot send right now, enqueue */
|
|
|
|
enqueue_message(snac, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
|
2022-09-27 12:50:13 +00:00
|
|
|
/* returns the list of recipients for a message */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_val *to = xs_dict_get(msg, "to");
|
|
|
|
const xs_val *cc = xs_dict_get(msg, "cc");
|
2022-11-28 14:49:56 +00:00
|
|
|
xs_set rcpts;
|
2022-09-27 12:50:13 +00:00
|
|
|
int n;
|
|
|
|
|
2022-11-28 14:49:56 +00:00
|
|
|
xs_set_init(&rcpts);
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *lists[] = { to, cc, NULL };
|
2022-09-27 12:50:13 +00:00
|
|
|
for (n = 0; lists[n]; n++) {
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_list *l = (xs_list *)lists[n];
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2022-10-01 17:37:47 +00:00
|
|
|
xs *tl = NULL;
|
2022-09-27 12:50:13 +00:00
|
|
|
|
2022-10-01 17:37:47 +00:00
|
|
|
/* if it's a string, create a list with only one element */
|
2022-09-27 12:50:13 +00:00
|
|
|
if (xs_type(l) == XSTYPE_STRING) {
|
2022-10-01 17:37:47 +00:00
|
|
|
tl = xs_list_new();
|
|
|
|
tl = xs_list_append(tl, l);
|
|
|
|
|
|
|
|
l = tl;
|
2022-09-27 12:50:13 +00:00
|
|
|
}
|
2022-10-01 17:37:47 +00:00
|
|
|
|
2022-09-27 12:50:13 +00:00
|
|
|
while (xs_list_iter(&l, &v)) {
|
|
|
|
if (expand_public && strcmp(v, public_address) == 0) {
|
|
|
|
/* iterate the followers and add them */
|
|
|
|
xs *fwers = follower_list(snac);
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *actor;
|
2022-09-27 12:50:13 +00:00
|
|
|
|
|
|
|
char *p = fwers;
|
2022-11-28 14:49:56 +00:00
|
|
|
while (xs_list_iter(&p, &actor))
|
|
|
|
xs_set_add(&rcpts, actor);
|
2022-09-27 12:50:13 +00:00
|
|
|
}
|
|
|
|
else
|
2022-11-28 14:49:56 +00:00
|
|
|
xs_set_add(&rcpts, v);
|
2022-09-27 12:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 14:49:56 +00:00
|
|
|
return xs_set_result(&rcpts);
|
2022-09-27 12:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-12 07:43:01 +00:00
|
|
|
int is_msg_public(const xs_dict *msg)
|
2022-09-27 12:50:13 +00:00
|
|
|
/* checks if a message is public */
|
|
|
|
{
|
2023-08-12 07:43:01 +00:00
|
|
|
const char *to = xs_dict_get(msg, "to");
|
|
|
|
const char *cc = xs_dict_get(msg, "cc");
|
|
|
|
int n;
|
|
|
|
|
|
|
|
const char *lists[] = { to, cc, NULL };
|
|
|
|
for (n = 0; lists[n]; n++) {
|
|
|
|
const xs_val *l = lists[n];
|
2022-09-27 12:50:13 +00:00
|
|
|
|
2023-08-12 07:43:01 +00:00
|
|
|
if (xs_type(l) == XSTYPE_STRING) {
|
|
|
|
if (strcmp(l, public_address) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (xs_type(l) == XSTYPE_LIST) {
|
|
|
|
if (xs_list_in(l, public_address) != -1)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-09-27 12:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-10 08:08:09 +00:00
|
|
|
int is_msg_from_private_user(const xs_dict *msg)
|
|
|
|
/* checks if a message is from a local, private user */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* is this message from a local user? */
|
|
|
|
if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
|
|
|
|
const char *atto = get_atto(msg);
|
|
|
|
xs *l = xs_split(atto, "/");
|
|
|
|
const char *uid = xs_list_get(l, -1);
|
|
|
|
snac user;
|
|
|
|
|
|
|
|
if (uid && user_open(&user, uid)) {
|
|
|
|
if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
user_free(&user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 07:50:12 +00:00
|
|
|
int is_msg_for_me(snac *snac, const xs_dict *c_msg)
|
2023-03-06 13:28:53 +00:00
|
|
|
/* checks if this message is for me */
|
|
|
|
{
|
2023-12-17 15:25:04 +00:00
|
|
|
const char *type = xs_dict_get(c_msg, "type");
|
|
|
|
const char *actor = xs_dict_get(c_msg, "actor");
|
2023-03-31 17:09:59 +00:00
|
|
|
|
2024-03-24 10:30:07 +00:00
|
|
|
if (strcmp(actor, snac->actor) == 0) {
|
|
|
|
/* message by myself? (most probably via the shared-inbox) reject */
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-07 09:01:10 +00:00
|
|
|
if (xs_match(type, "Like|Announce|EmojiReact")) {
|
2023-04-05 21:23:19 +00:00
|
|
|
const char *object = xs_dict_get(c_msg, "object");
|
|
|
|
|
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2023-06-11 18:10:23 +00:00
|
|
|
/* bad object id? reject */
|
|
|
|
if (xs_type(object) != XSTYPE_STRING)
|
|
|
|
return 0;
|
|
|
|
|
2023-04-05 21:23:19 +00:00
|
|
|
/* if it's about one of our posts, accept it */
|
|
|
|
if (xs_startswith(object, snac->actor))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
/* if it's by someone we don't follow, reject */
|
2023-12-17 15:25:04 +00:00
|
|
|
return following_check(snac, actor);
|
2023-03-31 17:09:59 +00:00
|
|
|
}
|
2023-03-06 13:28:53 +00:00
|
|
|
|
2023-12-18 12:40:39 +00:00
|
|
|
/* if it's an Undo, it must be from someone related to us */
|
2023-12-17 15:39:25 +00:00
|
|
|
if (xs_match(type, "Undo")) {
|
2023-12-18 12:40:39 +00:00
|
|
|
return follower_check(snac, actor) || following_check(snac, actor);
|
2023-12-15 20:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 10:15:53 +00:00
|
|
|
/* if it's an Accept + Follow, it must be for a Follow we created */
|
|
|
|
if (xs_match(type, "Accept")) {
|
|
|
|
return following_check(snac, actor);
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:37:02 +00:00
|
|
|
/* if it's a Follow, it must be explicitly for us */
|
|
|
|
if (xs_match(type, "Follow")) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *object = xs_dict_get(c_msg, "object");
|
2023-12-18 10:37:02 +00:00
|
|
|
return !xs_is_null(object) && strcmp(snac->actor, object) == 0;
|
|
|
|
}
|
|
|
|
|
2024-03-24 05:23:48 +00:00
|
|
|
/* only accept Ping directed to us */
|
|
|
|
if (xs_match(type, "Ping")) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *dest = xs_dict_get(c_msg, "to");
|
2024-03-24 05:23:48 +00:00
|
|
|
return !xs_is_null(dest) && strcmp(snac->actor, dest) == 0;
|
|
|
|
}
|
|
|
|
|
2023-12-17 15:39:25 +00:00
|
|
|
/* if it's not a Create or Update, allow as is */
|
|
|
|
if (!xs_match(type, "Create|Update")) {
|
2023-03-06 19:07:44 +00:00
|
|
|
return 1;
|
2023-12-15 21:25:14 +00:00
|
|
|
}
|
2023-03-06 13:28:53 +00:00
|
|
|
|
2023-12-17 16:07:26 +00:00
|
|
|
int pub_msg = is_msg_public(c_msg);
|
|
|
|
|
|
|
|
/* if this message is public and we follow the actor of this post, allow */
|
|
|
|
if (pub_msg && following_check(snac, actor))
|
2023-12-17 15:25:04 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *msg = xs_dict_get(c_msg, "object");
|
2023-03-06 19:07:44 +00:00
|
|
|
xs *rcpts = recipient_list(snac, msg, 0);
|
|
|
|
xs_list *p = rcpts;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-03-06 13:28:53 +00:00
|
|
|
|
2023-12-17 16:35:59 +00:00
|
|
|
xs *actor_followers = NULL;
|
|
|
|
|
|
|
|
if (!pub_msg) {
|
|
|
|
/* not a public message; get the actor and its followers list */
|
|
|
|
xs *actor_obj = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get(actor, &actor_obj))) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_val *fw = xs_dict_get(actor_obj, "followers");
|
|
|
|
if (fw)
|
|
|
|
actor_followers = xs_dup(fw);
|
2023-12-17 16:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 19:07:44 +00:00
|
|
|
while(xs_list_iter(&p, &v)) {
|
2023-03-07 08:40:55 +00:00
|
|
|
/* explicitly for me? accept */
|
2023-03-06 19:07:44 +00:00
|
|
|
if (strcmp(v, snac->actor) == 0)
|
|
|
|
return 2;
|
2023-03-07 08:40:55 +00:00
|
|
|
|
2023-12-17 16:35:59 +00:00
|
|
|
if (pub_msg) {
|
|
|
|
/* a public message for someone we follow? (probably cc'ed) accept */
|
|
|
|
if (following_check(snac, v))
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (actor_followers && strcmp(v, actor_followers) == 0) {
|
|
|
|
/* if this message is for this actor's followers, are we one of them? */
|
2023-12-17 17:40:26 +00:00
|
|
|
if (following_check(snac, actor))
|
2023-12-17 16:35:59 +00:00
|
|
|
return 6;
|
|
|
|
}
|
2023-03-06 13:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:40:55 +00:00
|
|
|
/* accept if it's by someone we follow */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *atto = get_atto(msg);
|
2023-03-06 19:07:44 +00:00
|
|
|
|
2023-12-17 16:07:26 +00:00
|
|
|
if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
|
2023-03-06 19:07:44 +00:00
|
|
|
return 3;
|
|
|
|
|
|
|
|
/* is this message a reply to another? */
|
2024-11-19 05:46:14 +00:00
|
|
|
const char *irt = get_in_reply_to(msg);
|
2023-03-06 19:07:44 +00:00
|
|
|
if (!xs_is_null(irt)) {
|
|
|
|
xs *r_msg = NULL;
|
|
|
|
|
2023-03-07 08:40:55 +00:00
|
|
|
/* try to get the replied message */
|
2023-03-06 19:07:44 +00:00
|
|
|
if (valid_status(object_get(irt, &r_msg))) {
|
2024-01-18 22:04:37 +00:00
|
|
|
atto = get_atto(r_msg);
|
2023-03-06 19:07:44 +00:00
|
|
|
|
|
|
|
/* accept if the replied message is from someone we follow */
|
2023-12-17 16:07:26 +00:00
|
|
|
if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
|
2023-03-06 19:07:44 +00:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-03-06 13:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-04 14:57:17 +00:00
|
|
|
xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
|
2022-09-27 16:20:25 +00:00
|
|
|
/* parses mentions and tags from content */
|
|
|
|
{
|
2023-05-12 08:15:44 +00:00
|
|
|
xs_str *nc = xs_str_new(NULL);
|
2023-05-12 08:33:59 +00:00
|
|
|
xs_list *tl = *tag;
|
2022-09-27 16:20:25 +00:00
|
|
|
xs *split;
|
2023-05-12 08:15:44 +00:00
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2022-09-27 16:20:25 +00:00
|
|
|
int n = 0;
|
|
|
|
|
2023-05-12 17:01:53 +00:00
|
|
|
/* create a default server for incomplete mentions */
|
|
|
|
xs *def_srv = NULL;
|
|
|
|
|
|
|
|
if (xs_list_len(tl)) {
|
|
|
|
/* if there are any mentions, get the server from
|
|
|
|
the first one, which is the inReplyTo author */
|
|
|
|
p = tl;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
const char *type = xs_dict_get(v, "type");
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
|
|
|
|
|
|
|
if (type && name && strcmp(type, "Mention") == 0) {
|
|
|
|
xs *l = xs_split(name, "@");
|
|
|
|
|
|
|
|
def_srv = xs_dup(xs_list_get(l, -1));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xs_is_null(def_srv))
|
|
|
|
/* use this same server */
|
|
|
|
def_srv = xs_dup(xs_dict_get(srv_config, "host"));
|
|
|
|
|
2024-11-16 06:25:25 +00:00
|
|
|
split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#(_|[^[:punct:][:space:]])+)");
|
2022-12-14 03:55:47 +00:00
|
|
|
|
|
|
|
p = split;
|
2022-09-27 16:20:25 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if ((n & 0x1)) {
|
|
|
|
if (*v == '@') {
|
2023-05-12 09:56:17 +00:00
|
|
|
xs *link = NULL;
|
2023-05-12 17:01:53 +00:00
|
|
|
xs *wuid = NULL;
|
2023-05-12 09:56:17 +00:00
|
|
|
|
|
|
|
if (strchr(v + 1, '@') == NULL) {
|
|
|
|
/* only one @? it's a dumb Mastodon-like mention
|
2023-05-12 17:01:53 +00:00
|
|
|
without server; add the default one */
|
|
|
|
wuid = xs_fmt("%s@%s", v, def_srv);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
|
2023-05-12 09:56:17 +00:00
|
|
|
}
|
2023-05-12 17:01:53 +00:00
|
|
|
else
|
|
|
|
wuid = xs_dup(v);
|
|
|
|
|
|
|
|
/* query the webfinger about this fellow */
|
|
|
|
xs *actor = NULL;
|
|
|
|
xs *uid = NULL;
|
|
|
|
int status;
|
2022-09-27 16:20:25 +00:00
|
|
|
|
2023-05-12 17:01:53 +00:00
|
|
|
status = webfinger_request(wuid, &actor, &uid);
|
2022-09-27 16:20:25 +00:00
|
|
|
|
2024-01-10 18:23:34 +00:00
|
|
|
if (valid_status(status) && actor && uid) {
|
2023-05-12 17:01:53 +00:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
xs *n = xs_fmt("@%s", uid);
|
2022-09-27 16:20:25 +00:00
|
|
|
|
2023-05-12 17:01:53 +00:00
|
|
|
d = xs_dict_append(d, "type", "Mention");
|
|
|
|
d = xs_dict_append(d, "href", actor);
|
|
|
|
d = xs_dict_append(d, "name", n);
|
2022-09-27 16:20:25 +00:00
|
|
|
|
2023-05-12 17:01:53 +00:00
|
|
|
tl = xs_list_append(tl, d);
|
2022-09-27 16:33:25 +00:00
|
|
|
|
2024-10-27 06:39:48 +00:00
|
|
|
link = xs_fmt("<span class=\"h-card\"><a href=\"%s\" class=\"u-url mention\">%s</a></span>", actor, n);
|
2022-09-27 16:20:25 +00:00
|
|
|
}
|
2023-05-12 09:56:17 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(link))
|
|
|
|
nc = xs_str_cat(nc, link);
|
2022-09-27 16:20:25 +00:00
|
|
|
else
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*v == '#') {
|
|
|
|
/* hashtag */
|
2023-01-12 12:49:37 +00:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
xs *n = xs_tolower_i(xs_dup(v));
|
2023-11-08 08:32:02 +00:00
|
|
|
xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
|
2023-01-12 12:49:37 +00:00
|
|
|
xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
|
|
|
|
|
|
|
|
d = xs_dict_append(d, "type", "Hashtag");
|
|
|
|
d = xs_dict_append(d, "href", h);
|
|
|
|
d = xs_dict_append(d, "name", n);
|
|
|
|
|
|
|
|
tl = xs_list_append(tl, d);
|
|
|
|
|
|
|
|
/* add the code */
|
|
|
|
nc = xs_str_cat(nc, l);
|
2022-09-27 16:20:25 +00:00
|
|
|
}
|
2023-01-17 08:37:41 +00:00
|
|
|
else
|
|
|
|
if (*v == '&') {
|
|
|
|
/* HTML Unicode entity, probably part of an emoji */
|
|
|
|
|
|
|
|
/* write as is */
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
}
|
2022-09-27 16:20:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:57:17 +00:00
|
|
|
*tag = tl;
|
|
|
|
|
|
|
|
return nc;
|
2022-09-27 16:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-30 12:52:18 +00:00
|
|
|
void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
|
|
|
|
/* notifies the user of relevant events */
|
|
|
|
{
|
2024-02-05 10:50:59 +00:00
|
|
|
/* skip our own notifications */
|
|
|
|
if (strcmp(snac->actor, actor) == 0)
|
|
|
|
return;
|
|
|
|
|
2023-05-31 20:10:45 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Create") == 0) {
|
|
|
|
/* only notify of notes specifically for us */
|
|
|
|
xs *rcpts = recipient_list(snac, msg, 0);
|
|
|
|
|
|
|
|
if (xs_list_in(rcpts, snac->actor) == -1)
|
|
|
|
return;
|
2023-05-30 16:51:53 +00:00
|
|
|
|
|
|
|
/* discard votes */
|
2023-06-02 08:52:05 +00:00
|
|
|
const xs_dict *note = xs_dict_get(msg, "object");
|
|
|
|
|
|
|
|
if (note && !xs_is_null(xs_dict_get(note, "name")))
|
2023-05-30 16:51:53 +00:00
|
|
|
return;
|
2023-05-30 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* get the object id */
|
|
|
|
const char *objid = xs_dict_get(msg, "object");
|
|
|
|
|
|
|
|
if (xs_type(objid) == XSTYPE_DICT)
|
|
|
|
objid = xs_dict_get(objid, "id");
|
|
|
|
|
2024-08-07 09:01:10 +00:00
|
|
|
if (xs_match(type, "Like|Announce|EmojiReact")) {
|
2023-05-30 12:52:18 +00:00
|
|
|
/* if it's not an admiration about something by us, done */
|
|
|
|
if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-01 06:44:27 +00:00
|
|
|
/* updated poll? */
|
2023-06-01 15:00:17 +00:00
|
|
|
if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
|
2023-06-02 09:48:43 +00:00
|
|
|
const xs_dict *poll;
|
|
|
|
const char *poll_id;
|
|
|
|
|
|
|
|
if ((poll = xs_dict_get(msg, "object")) == NULL)
|
|
|
|
return;
|
|
|
|
|
2023-06-01 06:44:27 +00:00
|
|
|
/* if it's not closed, discard */
|
2023-06-02 09:48:43 +00:00
|
|
|
if (xs_is_null(xs_dict_get(poll, "closed")))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((poll_id = xs_dict_get(poll, "id")) == NULL)
|
2023-06-01 06:44:27 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* if it's not ours and we didn't vote, discard */
|
2023-06-02 09:48:43 +00:00
|
|
|
if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
|
2023-05-31 20:10:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-30 12:52:18 +00:00
|
|
|
/* user will love to know about this! */
|
|
|
|
|
|
|
|
/* prepare message body */
|
|
|
|
xs *body = xs_fmt("User : @%s@%s\n",
|
|
|
|
xs_dict_get(snac->config, "uid"),
|
|
|
|
xs_dict_get(srv_config, "host")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (strcmp(utype, "(null)") != 0) {
|
|
|
|
xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
|
|
|
|
body = xs_str_cat(body, s1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xs *s1 = xs_fmt("Type : %s\n", type);
|
|
|
|
body = xs_str_cat(body, s1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
xs *s1 = xs_fmt("Actor : %s\n", actor);
|
|
|
|
body = xs_str_cat(body, s1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objid != NULL) {
|
|
|
|
xs *s1 = xs_fmt("Object: %s\n", objid);
|
|
|
|
body = xs_str_cat(body, s1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* email */
|
|
|
|
|
|
|
|
const char *email = "[disabled by admin]";
|
|
|
|
|
|
|
|
if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
|
|
|
|
email = xs_dict_get(snac->config_o, "email");
|
|
|
|
if (xs_is_null(email)) {
|
|
|
|
email = xs_dict_get(snac->config, "email");
|
|
|
|
|
|
|
|
if (xs_is_null(email))
|
|
|
|
email = "[empty]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*email != '\0' && *email != '[') {
|
|
|
|
snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
|
|
|
|
|
|
|
|
xs *subject = xs_fmt("snac notify for @%s@%s",
|
|
|
|
xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
|
|
|
|
xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
|
|
|
|
xs *header = xs_fmt(
|
|
|
|
"From: %s\n"
|
|
|
|
"To: %s\n"
|
|
|
|
"Subject: %s\n"
|
|
|
|
"\n",
|
|
|
|
from, email, subject);
|
|
|
|
|
|
|
|
xs *email_body = xs_fmt("%s%s", header, body);
|
|
|
|
|
|
|
|
enqueue_email(email_body, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* telegram */
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *bot = xs_dict_get(snac->config, "telegram_bot");
|
|
|
|
const char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
|
2023-05-30 12:52:18 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
|
|
|
|
enqueue_telegram(body, bot, chat_id);
|
|
|
|
|
2024-01-12 08:54:14 +00:00
|
|
|
/* ntfy */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
|
|
|
|
const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
|
2024-01-12 08:54:14 +00:00
|
|
|
|
2024-01-12 09:07:55 +00:00
|
|
|
if (!xs_is_null(ntfy_server) && *ntfy_server)
|
2024-01-12 08:54:14 +00:00
|
|
|
enqueue_ntfy(body, ntfy_server, ntfy_token);
|
|
|
|
|
2024-06-18 04:40:35 +00:00
|
|
|
/* auto boost */
|
2024-07-01 06:59:32 +00:00
|
|
|
if (xs_match(type, "Create") && xs_is_true(xs_dict_get(snac->config, "auto_boost"))) {
|
2024-06-18 04:40:35 +00:00
|
|
|
xs *msg = msg_admiration(snac, objid, "Announce");
|
|
|
|
enqueue_message(snac, msg);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("auto boosted %s", objid));
|
|
|
|
}
|
|
|
|
|
2024-01-12 08:54:14 +00:00
|
|
|
/* finally, store it in the notification folder */
|
|
|
|
if (strcmp(type, "Follow") == 0)
|
|
|
|
objid = id;
|
|
|
|
else
|
|
|
|
if (strcmp(utype, "Follow") == 0)
|
|
|
|
objid = actor;
|
|
|
|
|
2024-07-31 04:22:13 +00:00
|
|
|
notify_add(snac, type, utype, actor, objid != NULL ? objid : id, msg);
|
2024-01-12 08:54:14 +00:00
|
|
|
}
|
2023-05-30 12:52:18 +00:00
|
|
|
|
2022-09-24 08:43:57 +00:00
|
|
|
/** messages **/
|
|
|
|
|
2023-05-05 07:54:41 +00:00
|
|
|
xs_dict *msg_base(snac *snac, const char *type, const char *id,
|
|
|
|
const char *actor, const char *date, const char *object)
|
2022-09-24 08:43:57 +00:00
|
|
|
/* creates a base ActivityPub message */
|
|
|
|
{
|
2022-09-27 05:54:05 +00:00
|
|
|
xs *did = NULL;
|
|
|
|
xs *published = NULL;
|
2023-01-22 19:31:16 +00:00
|
|
|
xs *ntid = tid(0);
|
2023-08-31 14:11:58 +00:00
|
|
|
const char *obj_id;
|
|
|
|
|
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
obj_id = xs_dict_get(object, "id");
|
|
|
|
else
|
|
|
|
obj_id = object;
|
2022-09-27 05:54:05 +00:00
|
|
|
|
|
|
|
/* generated values */
|
2022-12-14 03:55:47 +00:00
|
|
|
if (date && strcmp(date, "@now") == 0) {
|
2023-05-29 07:07:27 +00:00
|
|
|
published = xs_str_utctime(0, ISO_DATE_SPEC);
|
2022-12-14 03:55:47 +00:00
|
|
|
date = published;
|
|
|
|
}
|
2022-09-27 05:54:05 +00:00
|
|
|
|
|
|
|
if (id != NULL) {
|
|
|
|
if (strcmp(id, "@dummy") == 0) {
|
2022-12-14 03:55:47 +00:00
|
|
|
did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
|
|
|
|
|
|
|
|
id = did;
|
2022-09-27 05:54:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(id, "@object") == 0) {
|
2023-08-31 14:11:58 +00:00
|
|
|
if (obj_id != NULL) {
|
|
|
|
did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
|
2022-12-14 03:55:47 +00:00
|
|
|
id = did;
|
|
|
|
}
|
2022-09-27 05:54:05 +00:00
|
|
|
else
|
|
|
|
id = NULL;
|
|
|
|
}
|
2023-07-29 13:19:52 +00:00
|
|
|
else
|
|
|
|
if (strcmp(id, "@wrapper") == 0) {
|
|
|
|
/* like @object, but always generate the same id */
|
|
|
|
if (object != NULL) {
|
2023-07-29 13:29:11 +00:00
|
|
|
date = xs_dict_get(object, "published");
|
2023-08-31 14:11:58 +00:00
|
|
|
did = xs_fmt("%s/%s", obj_id, type);
|
2023-07-29 13:19:52 +00:00
|
|
|
id = did;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
id = NULL;
|
|
|
|
}
|
2022-09-27 05:54:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:54:41 +00:00
|
|
|
xs_dict *msg = xs_dict_new();
|
2022-09-24 08:43:57 +00:00
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
|
|
|
|
msg = xs_dict_append(msg, "type", type);
|
|
|
|
|
|
|
|
if (id != NULL)
|
|
|
|
msg = xs_dict_append(msg, "id", id);
|
|
|
|
|
2022-09-24 09:04:35 +00:00
|
|
|
if (actor != NULL)
|
2022-09-24 08:43:57 +00:00
|
|
|
msg = xs_dict_append(msg, "actor", actor);
|
2022-09-24 09:04:35 +00:00
|
|
|
|
2022-09-27 05:54:05 +00:00
|
|
|
if (date != NULL)
|
|
|
|
msg = xs_dict_append(msg, "published", date);
|
|
|
|
|
|
|
|
if (object != NULL)
|
|
|
|
msg = xs_dict_append(msg, "object", object);
|
2022-09-24 08:43:57 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_collection(snac *snac, const char *id)
|
2022-09-24 08:43:57 +00:00
|
|
|
/* creates an empty OrderedCollection message */
|
|
|
|
{
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
|
2022-09-24 08:43:57 +00:00
|
|
|
xs *ol = xs_list_new();
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "attributedTo", snac->actor);
|
|
|
|
msg = xs_dict_append(msg, "orderedItems", ol);
|
2024-03-15 03:10:11 +00:00
|
|
|
msg = xs_dict_append(msg, "totalItems", xs_stock(0));
|
2022-09-24 08:43:57 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
|
2022-09-27 05:54:05 +00:00
|
|
|
/* creates an Accept message (as a response to a Follow) */
|
|
|
|
{
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
|
2022-09-27 05:54:05 +00:00
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", to);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_update(snac *snac, const xs_dict *object)
|
2022-09-24 09:54:35 +00:00
|
|
|
/* creates an Update message */
|
2022-09-24 09:04:35 +00:00
|
|
|
{
|
2023-05-17 08:49:46 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
|
2022-09-24 09:04:35 +00:00
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type = xs_dict_get(object, "type");
|
2023-03-06 10:26:43 +00:00
|
|
|
|
|
|
|
if (strcmp(type, "Note") == 0) {
|
|
|
|
msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
|
|
|
|
msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
|
|
|
|
}
|
2023-05-17 08:49:46 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Person") == 0) {
|
|
|
|
msg = xs_dict_append(msg, "to", public_address);
|
|
|
|
|
|
|
|
/* also spam the people being followed, so that
|
|
|
|
they have the newest information about who we are */
|
|
|
|
xs *cc = following_list(snac);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "cc", cc);
|
|
|
|
}
|
2023-03-06 10:26:43 +00:00
|
|
|
else
|
|
|
|
msg = xs_dict_append(msg, "to", public_address);
|
2022-09-24 09:04:35 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
|
2022-09-26 08:08:14 +00:00
|
|
|
/* creates a Like or Announce message */
|
|
|
|
{
|
2023-06-04 08:34:39 +00:00
|
|
|
xs *a_msg = NULL;
|
|
|
|
xs_dict *msg = NULL;
|
|
|
|
xs *wrk = NULL;
|
2022-09-26 08:08:14 +00:00
|
|
|
|
|
|
|
/* call the object */
|
2023-07-13 18:19:50 +00:00
|
|
|
timeline_request(snac, &object, &wrk, 0);
|
2022-09-26 08:08:14 +00:00
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get(object, &a_msg))) {
|
2022-09-26 09:19:45 +00:00
|
|
|
xs *rcpts = xs_list_new();
|
2024-04-10 15:21:58 +00:00
|
|
|
xs *o_md5 = xs_md5_hex(object, strlen(object));
|
|
|
|
xs *id = xs_fmt("%s/%s/%s", snac->actor, *type == 'L' ? "l" : "a", o_md5);
|
2022-09-26 08:08:14 +00:00
|
|
|
|
2024-04-10 15:21:58 +00:00
|
|
|
msg = msg_base(snac, type, id, snac->actor, "@now", object);
|
2022-09-26 09:19:45 +00:00
|
|
|
|
2023-08-12 07:43:01 +00:00
|
|
|
if (is_msg_public(a_msg))
|
2023-03-06 10:16:15 +00:00
|
|
|
rcpts = xs_list_append(rcpts, public_address);
|
|
|
|
|
2024-01-18 22:04:37 +00:00
|
|
|
rcpts = xs_list_append(rcpts, get_atto(a_msg));
|
2022-09-26 09:19:45 +00:00
|
|
|
|
2022-12-23 09:01:10 +00:00
|
|
|
msg = xs_dict_append(msg, "to", rcpts);
|
2022-09-26 09:19:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
|
2022-09-26 08:08:14 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
|
2024-04-10 16:27:17 +00:00
|
|
|
/* creates an Undo + admiration message */
|
|
|
|
{
|
|
|
|
xs *a_msg = NULL;
|
|
|
|
xs_dict *msg = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get(id, &a_msg))) {
|
|
|
|
/* create a clone of the original admiration message */
|
|
|
|
xs *object = msg_admiration(user, id, type);
|
|
|
|
|
|
|
|
/* delete the published date */
|
|
|
|
object = xs_dict_del(object, "published");
|
|
|
|
|
|
|
|
/* create an undo message for this object */
|
|
|
|
msg = msg_undo(user, object);
|
|
|
|
|
|
|
|
/* copy the 'to' field */
|
|
|
|
msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
|
|
|
|
}
|
|
|
|
|
2024-04-11 03:12:13 +00:00
|
|
|
/* now we despise this */
|
|
|
|
object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
|
|
|
|
|
2024-04-10 16:27:17 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg_actor(snac *snac)
|
2022-09-25 19:02:47 +00:00
|
|
|
/* create a Person message for this actor */
|
|
|
|
{
|
2023-06-04 08:34:39 +00:00
|
|
|
xs *ctxt = xs_list_new();
|
|
|
|
xs *icon = xs_dict_new();
|
|
|
|
xs *keys = xs_dict_new();
|
2023-07-04 14:54:41 +00:00
|
|
|
xs *tags = xs_list_new();
|
2023-06-04 08:34:39 +00:00
|
|
|
xs *avtr = NULL;
|
|
|
|
xs *kid = NULL;
|
|
|
|
xs *f_bio = NULL;
|
|
|
|
xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *p;
|
2022-09-25 19:02:47 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/* change the @context (is this really necessary?) */
|
|
|
|
ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
|
|
|
|
ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
|
|
|
|
msg = xs_dict_set(msg, "@context", ctxt);
|
|
|
|
|
|
|
|
msg = xs_dict_set(msg, "url", snac->actor);
|
|
|
|
msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
|
|
|
|
msg = xs_dict_set(msg, "preferredUsername", snac->uid);
|
|
|
|
msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
|
|
|
|
|
2024-10-08 18:30:20 +00:00
|
|
|
xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
|
2023-07-04 14:57:17 +00:00
|
|
|
f_bio = process_tags(snac, f_bio_2, &tags);
|
2022-09-27 12:07:36 +00:00
|
|
|
msg = xs_dict_set(msg, "summary", f_bio);
|
2023-07-04 14:54:41 +00:00
|
|
|
msg = xs_dict_set(msg, "tag", tags);
|
2022-09-25 19:02:47 +00:00
|
|
|
|
2024-08-26 09:26:34 +00:00
|
|
|
char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
|
2022-09-25 19:02:47 +00:00
|
|
|
for (n = 0; folders[n]; n++) {
|
|
|
|
xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
|
|
|
|
msg = xs_dict_set(msg, folders[n], f);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = xs_dict_get(snac->config, "avatar");
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
avtr = xs_fmt("%s/susie.png", srv_baseurl);
|
|
|
|
else
|
|
|
|
avtr = xs_dup(p);
|
|
|
|
|
|
|
|
icon = xs_dict_append(icon, "type", "Image");
|
|
|
|
icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
|
|
|
|
icon = xs_dict_append(icon, "url", avtr);
|
|
|
|
msg = xs_dict_set(msg, "icon", icon);
|
|
|
|
|
|
|
|
kid = xs_fmt("%s#main-key", snac->actor);
|
|
|
|
|
|
|
|
keys = xs_dict_append(keys, "id", kid);
|
|
|
|
keys = xs_dict_append(keys, "owner", snac->actor);
|
|
|
|
keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
|
|
|
|
msg = xs_dict_set(msg, "publicKey", keys);
|
|
|
|
|
2023-06-04 08:48:03 +00:00
|
|
|
/* if the "bot" config field is set to true, change type to "Service" */
|
|
|
|
if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
|
|
|
|
msg = xs_dict_set(msg, "type", "Service");
|
|
|
|
|
2023-08-14 11:12:16 +00:00
|
|
|
/* add the header image, if there is one defined */
|
|
|
|
const char *header = xs_dict_get(snac->config, "header");
|
|
|
|
if (!xs_is_null(header)) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
d = xs_dict_append(d, "type", "Image");
|
|
|
|
d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
|
|
|
|
d = xs_dict_append(d, "url", header);
|
|
|
|
msg = xs_dict_set(msg, "image", d);
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:24:30 +00:00
|
|
|
/* add the metadata as attachments of PropertyValue */
|
2024-11-23 16:08:57 +00:00
|
|
|
xs *metadata = NULL;
|
|
|
|
const xs_dict *md = xs_dict_get(snac->config, "metadata");
|
|
|
|
|
|
|
|
if (xs_type(md) == XSTYPE_DICT)
|
|
|
|
metadata = xs_dup(md);
|
|
|
|
else
|
|
|
|
if (xs_type(md) == XSTYPE_STRING) {
|
|
|
|
metadata = xs_dict_new();
|
|
|
|
xs *l = xs_split(md, "\r\n");
|
|
|
|
const char *ll;
|
|
|
|
|
|
|
|
xs_list_foreach(l, ll) {
|
|
|
|
xs *kv = xs_split_n(ll, "=", 1);
|
|
|
|
const char *k = xs_list_get(kv, 0);
|
|
|
|
const char *v = xs_list_get(kv, 1);
|
|
|
|
|
|
|
|
if (k && v) {
|
|
|
|
xs *kk = xs_strip_i(xs_dup(k));
|
|
|
|
xs *vv = xs_strip_i(xs_dup(v));
|
|
|
|
metadata = xs_dict_set(metadata, kk, vv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:24:30 +00:00
|
|
|
if (xs_type(metadata) == XSTYPE_DICT) {
|
|
|
|
xs *attach = xs_list_new();
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *k;
|
|
|
|
const xs_str *v;
|
2023-09-18 09:24:30 +00:00
|
|
|
|
2024-03-09 07:44:48 +00:00
|
|
|
int c = 0;
|
|
|
|
while (xs_dict_next(metadata, &k, &v, &c)) {
|
2023-09-18 09:24:30 +00:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
|
2023-09-22 10:34:47 +00:00
|
|
|
xs *k2 = encode_html(k);
|
|
|
|
xs *v2 = NULL;
|
|
|
|
|
2024-04-18 15:13:31 +00:00
|
|
|
if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
|
2023-09-22 10:34:47 +00:00
|
|
|
xs *t = encode_html(v);
|
2024-02-14 23:17:33 +00:00
|
|
|
v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
|
2023-09-22 10:34:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
v2 = encode_html(v);
|
|
|
|
|
2023-09-18 09:24:30 +00:00
|
|
|
d = xs_dict_append(d, "type", "PropertyValue");
|
2023-09-22 10:34:47 +00:00
|
|
|
d = xs_dict_append(d, "name", k2);
|
|
|
|
d = xs_dict_append(d, "value", v2);
|
2023-09-18 09:24:30 +00:00
|
|
|
|
|
|
|
attach = xs_list_append(attach, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = xs_dict_set(msg, "attachment", attach);
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:41:32 +00:00
|
|
|
/* use shared inboxes? */
|
|
|
|
if (xs_type(xs_dict_get(srv_config, "shared_inboxes")) == XSTYPE_TRUE) {
|
2023-12-06 13:46:51 +00:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
|
|
|
|
d = xs_dict_append(d, "sharedInbox", si);
|
|
|
|
msg = xs_dict_set(msg, "endpoints", d);
|
|
|
|
}
|
|
|
|
|
2024-09-18 09:05:42 +00:00
|
|
|
/* does this user have an aka? */
|
2024-09-19 17:47:07 +00:00
|
|
|
const char *aka = xs_dict_get(snac->config, "alias");
|
2024-09-18 09:05:42 +00:00
|
|
|
if (xs_type(aka) == XSTYPE_STRING && *aka) {
|
|
|
|
xs *loaka = xs_list_append(xs_list_new(), aka);
|
|
|
|
|
|
|
|
msg = xs_dict_set(msg, "alsoKnownAs", loaka);
|
|
|
|
}
|
|
|
|
|
2022-09-25 19:02:47 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 09:49:16 +00:00
|
|
|
xs_dict *msg_create(snac *snac, const xs_dict *object)
|
2022-09-27 12:07:36 +00:00
|
|
|
/* creates a 'Create' message */
|
|
|
|
{
|
2023-07-29 13:29:11 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_val *v;
|
2023-05-24 09:49:16 +00:00
|
|
|
|
2024-01-18 22:04:37 +00:00
|
|
|
if ((v = get_atto(object)))
|
2023-05-24 09:49:16 +00:00
|
|
|
msg = xs_dict_append(msg, "attributedTo", v);
|
|
|
|
|
|
|
|
if ((v = xs_dict_get(object, "cc")))
|
|
|
|
msg = xs_dict_append(msg, "cc", v);
|
2022-09-27 12:07:36 +00:00
|
|
|
|
2023-05-24 09:49:16 +00:00
|
|
|
if ((v = xs_dict_get(object, "to")))
|
|
|
|
msg = xs_dict_append(msg, "to", v);
|
|
|
|
else
|
|
|
|
msg = xs_dict_append(msg, "to", public_address);
|
2022-09-27 12:07:36 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_undo(snac *snac, const xs_val *object)
|
2022-10-01 07:12:33 +00:00
|
|
|
/* creates an 'Undo' message */
|
|
|
|
{
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
|
2023-08-31 14:11:58 +00:00
|
|
|
const char *to;
|
2022-10-01 07:12:33 +00:00
|
|
|
|
2023-08-31 14:11:58 +00:00
|
|
|
if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
|
|
|
|
msg = xs_dict_append(msg, "to", to);
|
2022-10-01 07:12:33 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_dict *msg_delete(snac *snac, const char *id)
|
2022-10-01 17:37:47 +00:00
|
|
|
/* creates a 'Delete' + 'Tombstone' for a local entry */
|
|
|
|
{
|
|
|
|
xs *tomb = xs_dict_new();
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg = NULL;
|
2022-10-01 17:37:47 +00:00
|
|
|
|
|
|
|
/* sculpt the tombstone */
|
|
|
|
tomb = xs_dict_append(tomb, "type", "Tombstone");
|
|
|
|
tomb = xs_dict_append(tomb, "id", id);
|
|
|
|
|
|
|
|
/* now create the Delete */
|
|
|
|
msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", public_address);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 12:33:53 +00:00
|
|
|
xs_dict *msg_follow(snac *snac, const char *q)
|
2022-09-27 13:28:08 +00:00
|
|
|
/* creates a 'Follow' message */
|
|
|
|
{
|
2022-11-02 19:28:40 +00:00
|
|
|
xs *actor_o = NULL;
|
|
|
|
xs *actor = NULL;
|
2023-06-04 08:34:39 +00:00
|
|
|
xs_dict *msg = NULL;
|
2022-09-27 13:28:08 +00:00
|
|
|
int status;
|
|
|
|
|
2023-04-23 12:33:53 +00:00
|
|
|
xs *url_or_uid = xs_strip_i(xs_str_new(q));
|
|
|
|
|
2024-04-18 15:13:31 +00:00
|
|
|
if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
|
2022-11-02 19:28:40 +00:00
|
|
|
actor = xs_dup(url_or_uid);
|
|
|
|
else
|
2023-05-07 05:42:47 +00:00
|
|
|
if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
|
2022-11-02 19:28:40 +00:00
|
|
|
snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-27 13:28:08 +00:00
|
|
|
/* request the actor */
|
2023-12-17 18:53:54 +00:00
|
|
|
status = actor_request(snac, actor, &actor_o);
|
2022-09-27 13:28:08 +00:00
|
|
|
|
|
|
|
if (valid_status(status)) {
|
|
|
|
/* check if the actor is an alias */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *r_actor = xs_dict_get(actor_o, "id");
|
2022-09-27 13:28:08 +00:00
|
|
|
|
|
|
|
if (r_actor && strcmp(actor, r_actor) != 0) {
|
|
|
|
snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
|
|
|
|
}
|
|
|
|
|
2022-11-02 20:36:55 +00:00
|
|
|
msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
|
2022-09-27 13:28:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-15 17:05:26 +00:00
|
|
|
xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_str *in_reply_to, const xs_list *attach, int priv)
|
2022-09-27 12:07:36 +00:00
|
|
|
/* creates a 'Note' message */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
|
2022-09-27 12:50:13 +00:00
|
|
|
xs *ctxt = NULL;
|
2022-09-27 16:33:25 +00:00
|
|
|
xs *fc2 = NULL;
|
2022-09-27 12:07:36 +00:00
|
|
|
xs *fc1 = NULL;
|
|
|
|
xs *to = NULL;
|
|
|
|
xs *cc = xs_list_new();
|
|
|
|
xs *irt = NULL;
|
2023-05-12 08:33:59 +00:00
|
|
|
xs *tag = xs_list_new();
|
2023-05-21 18:12:59 +00:00
|
|
|
xs *atls = xs_list_new();
|
2023-01-28 17:22:42 +00:00
|
|
|
xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
|
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2022-09-27 12:07:36 +00:00
|
|
|
|
|
|
|
if (rcpts == NULL)
|
|
|
|
to = xs_list_new();
|
2022-11-02 09:13:14 +00:00
|
|
|
else {
|
|
|
|
if (xs_type(rcpts) == XSTYPE_STRING) {
|
|
|
|
to = xs_list_new();
|
|
|
|
to = xs_list_append(to, rcpts);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
to = xs_dup(rcpts);
|
|
|
|
}
|
2022-09-27 12:07:36 +00:00
|
|
|
|
|
|
|
/* format the content */
|
2024-03-25 15:15:09 +00:00
|
|
|
fc2 = not_really_markdown(content, &atls, &tag);
|
2022-09-27 16:33:25 +00:00
|
|
|
|
2022-12-23 07:05:00 +00:00
|
|
|
if (in_reply_to != NULL && *in_reply_to) {
|
2022-09-27 12:50:13 +00:00
|
|
|
xs *p_msg = NULL;
|
2022-12-23 20:41:52 +00:00
|
|
|
xs *wrk = NULL;
|
2022-09-27 12:50:13 +00:00
|
|
|
|
|
|
|
/* demand this thing */
|
2023-07-13 18:19:50 +00:00
|
|
|
timeline_request(snac, &in_reply_to, &wrk, 0);
|
2022-09-27 12:50:13 +00:00
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get(in_reply_to, &p_msg))) {
|
2022-09-27 12:50:13 +00:00
|
|
|
/* add this author as recipient */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *a, *v;
|
2022-11-22 19:31:22 +00:00
|
|
|
|
2024-01-18 22:04:37 +00:00
|
|
|
if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
|
2022-11-22 19:31:22 +00:00
|
|
|
to = xs_list_append(to, a);
|
|
|
|
|
|
|
|
/* add this author to the tag list as a mention */
|
2024-01-11 05:08:52 +00:00
|
|
|
if (!xs_is_null(a)) {
|
|
|
|
xs *l = xs_split(a, "/");
|
|
|
|
xs *actor_o = NULL;
|
2022-11-22 19:31:22 +00:00
|
|
|
|
2024-01-11 05:08:52 +00:00
|
|
|
if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *uname = xs_dict_get(actor_o, "preferredUsername");
|
2022-09-27 12:50:13 +00:00
|
|
|
|
2024-01-11 05:08:52 +00:00
|
|
|
if (!xs_is_null(uname) && *uname) {
|
|
|
|
xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
|
2022-09-27 12:50:13 +00:00
|
|
|
|
2024-01-11 05:08:52 +00:00
|
|
|
xs *t = xs_dict_new();
|
|
|
|
|
|
|
|
t = xs_dict_append(t, "type", "Mention");
|
|
|
|
t = xs_dict_append(t, "href", a);
|
|
|
|
t = xs_dict_append(t, "name", handle);
|
|
|
|
|
|
|
|
tag = xs_list_append(tag, t);
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 19:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the context, if there is one */
|
2022-09-27 12:50:13 +00:00
|
|
|
if ((v = xs_dict_get(p_msg, "context")))
|
|
|
|
ctxt = xs_dup(v);
|
|
|
|
|
2023-08-22 18:12:22 +00:00
|
|
|
/* propagate the conversation field, if there is one */
|
|
|
|
if ((v = xs_dict_get(p_msg, "conversation")))
|
|
|
|
msg = xs_dict_append(msg, "conversation", v);
|
|
|
|
|
2022-09-27 12:50:13 +00:00
|
|
|
/* if this message is public, ours will also be */
|
2023-08-12 07:43:01 +00:00
|
|
|
if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
|
2022-09-27 12:50:13 +00:00
|
|
|
to = xs_list_append(to, public_address);
|
|
|
|
}
|
|
|
|
|
2022-09-27 12:07:36 +00:00
|
|
|
irt = xs_dup(in_reply_to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
irt = xs_val_new(XSTYPE_NULL);
|
|
|
|
|
2023-05-12 08:33:59 +00:00
|
|
|
/* extract the mentions and hashtags and convert the content */
|
2023-07-04 14:57:17 +00:00
|
|
|
fc1 = process_tags(snac, fc2, &tag);
|
2023-05-12 08:33:59 +00:00
|
|
|
|
2022-10-10 07:03:07 +00:00
|
|
|
/* create the attachment list, if there are any */
|
2023-01-28 17:22:42 +00:00
|
|
|
if (!xs_is_null(attach)) {
|
2024-05-21 12:12:15 +00:00
|
|
|
int c = 0;
|
|
|
|
while (xs_list_next(attach, &v, &c)) {
|
2023-08-14 16:04:27 +00:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
const char *url = xs_list_get(v, 0);
|
|
|
|
const char *alt = xs_list_get(v, 1);
|
|
|
|
const char *mime = xs_mime_by_ext(url);
|
2022-10-10 07:03:07 +00:00
|
|
|
|
|
|
|
d = xs_dict_append(d, "mediaType", mime);
|
2023-01-28 17:22:42 +00:00
|
|
|
d = xs_dict_append(d, "url", url);
|
|
|
|
d = xs_dict_append(d, "name", alt);
|
2022-10-10 07:03:07 +00:00
|
|
|
d = xs_dict_append(d, "type",
|
|
|
|
xs_startswith(mime, "image/") ? "Image" : "Document");
|
|
|
|
|
|
|
|
atls = xs_list_append(atls, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 12:50:13 +00:00
|
|
|
if (ctxt == NULL)
|
|
|
|
ctxt = xs_fmt("%s#ctxt", id);
|
|
|
|
|
2022-09-27 12:07:36 +00:00
|
|
|
/* add all mentions to the cc */
|
|
|
|
p = tag;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if (xs_type(v) == XSTYPE_DICT) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *t;
|
2022-09-27 12:07:36 +00:00
|
|
|
|
2024-01-11 18:29:09 +00:00
|
|
|
if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
|
|
|
|
if (!xs_is_null(t = xs_dict_get(v, "href")))
|
2022-09-27 12:07:36 +00:00
|
|
|
cc = xs_list_append(cc, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no recipients? must be for everybody */
|
2023-02-20 08:32:44 +00:00
|
|
|
if (!priv && xs_list_len(to) == 0)
|
2022-09-27 12:07:36 +00:00
|
|
|
to = xs_list_append(to, public_address);
|
|
|
|
|
2022-12-16 06:51:10 +00:00
|
|
|
/* delete all cc recipients that also are in the to */
|
|
|
|
p = to;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((i = xs_list_in(cc, v)) != -1)
|
|
|
|
cc = xs_list_del(cc, i);
|
|
|
|
}
|
|
|
|
|
2022-09-27 12:07:36 +00:00
|
|
|
msg = xs_dict_append(msg, "attributedTo", snac->actor);
|
|
|
|
msg = xs_dict_append(msg, "summary", "");
|
|
|
|
msg = xs_dict_append(msg, "content", fc1);
|
|
|
|
msg = xs_dict_append(msg, "context", ctxt);
|
|
|
|
msg = xs_dict_append(msg, "url", id);
|
|
|
|
msg = xs_dict_append(msg, "to", to);
|
|
|
|
msg = xs_dict_append(msg, "cc", cc);
|
|
|
|
msg = xs_dict_append(msg, "inReplyTo", irt);
|
|
|
|
msg = xs_dict_append(msg, "tag", tag);
|
|
|
|
|
2023-01-20 09:09:32 +00:00
|
|
|
msg = xs_dict_append(msg, "sourceContent", content);
|
|
|
|
|
2023-05-21 18:12:59 +00:00
|
|
|
if (xs_list_len(atls))
|
2022-10-10 07:03:07 +00:00
|
|
|
msg = xs_dict_append(msg, "attachment", atls);
|
|
|
|
|
2022-09-27 12:07:36 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-05 07:54:41 +00:00
|
|
|
xs_dict *msg_ping(snac *user, const char *rcpt)
|
|
|
|
/* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
|
|
|
|
{
|
|
|
|
xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", rcpt);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
|
|
|
|
/* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
|
|
|
|
{
|
|
|
|
xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", rcpt);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-18 16:34:17 +00:00
|
|
|
xs_dict *msg_move(snac *user, const char *new_account)
|
|
|
|
/* creates a Move message (to move the user to new_account) */
|
|
|
|
{
|
|
|
|
xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "target", new_account);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-30 03:54:45 +00:00
|
|
|
xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
|
|
|
|
const xs_list *opts, int multiple, int end_secs)
|
2023-05-24 09:07:47 +00:00
|
|
|
/* creates a Question message */
|
|
|
|
{
|
2023-05-30 03:54:45 +00:00
|
|
|
xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
|
2023-05-30 04:00:11 +00:00
|
|
|
int max = 8;
|
2023-06-01 15:14:50 +00:00
|
|
|
xs_set seen;
|
2023-05-24 09:07:47 +00:00
|
|
|
|
2023-05-30 03:54:45 +00:00
|
|
|
msg = xs_dict_set(msg, "type", "Question");
|
2023-05-24 09:07:47 +00:00
|
|
|
|
2023-05-30 08:38:17 +00:00
|
|
|
/* make it non-editable */
|
|
|
|
msg = xs_dict_del(msg, "sourceContent");
|
|
|
|
|
2023-05-24 09:07:47 +00:00
|
|
|
xs *o = xs_list_new();
|
|
|
|
xs_list *p = (xs_list *)opts;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-05-29 06:00:43 +00:00
|
|
|
xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
|
2023-05-24 09:07:47 +00:00
|
|
|
|
2023-06-01 15:14:50 +00:00
|
|
|
xs_set_init(&seen);
|
2023-05-30 04:00:11 +00:00
|
|
|
|
2023-06-01 15:14:50 +00:00
|
|
|
while (max && xs_list_iter(&p, &v)) {
|
|
|
|
if (*v) {
|
|
|
|
xs *v2 = xs_dup(v);
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
|
|
|
|
if (strlen(v2) > 60) {
|
|
|
|
v2[60] = '\0';
|
|
|
|
v2 = xs_str_cat(v2, "...");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xs_set_add(&seen, v2) == 1) {
|
2024-03-15 17:36:56 +00:00
|
|
|
d = xs_dict_append(d, "type", "Note");
|
2023-06-01 15:14:50 +00:00
|
|
|
d = xs_dict_append(d, "name", v2);
|
|
|
|
d = xs_dict_append(d, "replies", replies);
|
|
|
|
o = xs_list_append(o, d);
|
2023-05-24 09:07:47 +00:00
|
|
|
|
2023-06-01 15:14:50 +00:00
|
|
|
max--;
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 09:07:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 15:14:50 +00:00
|
|
|
xs_set_free(&seen);
|
|
|
|
|
2023-05-24 09:07:47 +00:00
|
|
|
msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
|
|
|
|
|
|
|
|
/* set the end time */
|
|
|
|
time_t t = time(NULL) + end_secs;
|
2023-05-29 07:07:27 +00:00
|
|
|
xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
|
2023-05-24 09:07:47 +00:00
|
|
|
|
2023-05-24 09:49:16 +00:00
|
|
|
msg = xs_dict_append(msg, "endTime", et);
|
2023-05-24 09:07:47 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
int update_question(snac *user, const char *id)
|
|
|
|
/* updates the poll counts */
|
|
|
|
{
|
2023-05-29 07:37:54 +00:00
|
|
|
xs *msg = NULL;
|
|
|
|
xs *rcnt = xs_dict_new();
|
2023-05-29 08:20:45 +00:00
|
|
|
xs *lopts = xs_list_new();
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *opts;
|
2023-05-29 08:20:45 +00:00
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2023-05-29 06:27:16 +00:00
|
|
|
|
|
|
|
/* get the object */
|
|
|
|
if (!valid_status(object_get(id, &msg)))
|
|
|
|
return -1;
|
|
|
|
|
2023-05-29 07:20:37 +00:00
|
|
|
/* closed? do nothing more */
|
|
|
|
if (xs_dict_get(msg, "closed"))
|
|
|
|
return -2;
|
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
/* get the options */
|
|
|
|
if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
|
|
|
|
(opts = xs_dict_get(msg, "anyOf")) == NULL)
|
2023-05-29 07:20:37 +00:00
|
|
|
return -3;
|
2023-05-29 06:27:16 +00:00
|
|
|
|
|
|
|
/* fill the initial count */
|
2024-05-21 12:12:15 +00:00
|
|
|
int c = 0;
|
|
|
|
while (xs_list_next(opts, &v, &c)) {
|
2023-05-29 06:27:16 +00:00
|
|
|
const char *name = xs_dict_get(v, "name");
|
2023-05-29 08:20:45 +00:00
|
|
|
if (name) {
|
|
|
|
lopts = xs_list_append(lopts, name);
|
2024-03-15 03:10:11 +00:00
|
|
|
rcnt = xs_dict_set(rcnt, name, xs_stock(0));
|
2023-05-29 08:20:45 +00:00
|
|
|
}
|
2023-05-29 06:27:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:56:12 +00:00
|
|
|
xs_set s;
|
|
|
|
xs_set_init(&s);
|
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
/* iterate now the children (the votes) */
|
2023-05-29 08:20:45 +00:00
|
|
|
xs *chld = object_children(id);
|
2023-05-29 06:27:16 +00:00
|
|
|
p = chld;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2023-05-29 07:03:00 +00:00
|
|
|
xs *obj = NULL;
|
|
|
|
|
|
|
|
if (!valid_status(object_get_by_md5(v, &obj)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *name = xs_dict_get(obj, "name");
|
2024-01-18 22:04:37 +00:00
|
|
|
const char *atto = get_atto(obj);
|
2023-05-29 07:37:54 +00:00
|
|
|
|
|
|
|
if (name && atto) {
|
2023-05-29 06:27:16 +00:00
|
|
|
/* get the current count */
|
2023-05-29 07:03:00 +00:00
|
|
|
const xs_number *cnt = xs_dict_get(rcnt, name);
|
2023-05-29 06:27:16 +00:00
|
|
|
|
|
|
|
if (xs_type(cnt) == XSTYPE_NUMBER) {
|
|
|
|
/* if it exists, increment */
|
|
|
|
xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
|
2023-05-29 07:03:00 +00:00
|
|
|
rcnt = xs_dict_set(rcnt, name, ucnt);
|
2023-05-29 07:37:54 +00:00
|
|
|
|
2023-08-08 11:56:12 +00:00
|
|
|
xs_set_add(&s, atto);
|
2023-05-29 06:27:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 11:56:12 +00:00
|
|
|
xs *rcpts = xs_set_result(&s);
|
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
/* create a new list of options with their new counts */
|
|
|
|
xs *nopts = xs_list_new();
|
2023-05-29 08:20:45 +00:00
|
|
|
p = lopts;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
const xs_number *cnt = xs_dict_get(rcnt, v);
|
2023-05-29 06:27:16 +00:00
|
|
|
|
2023-05-29 08:20:45 +00:00
|
|
|
if (xs_type(cnt) == XSTYPE_NUMBER) {
|
|
|
|
xs *d1 = xs_dict_new();
|
|
|
|
xs *d2 = xs_dict_new();
|
2023-05-29 06:27:16 +00:00
|
|
|
|
2023-05-29 08:20:45 +00:00
|
|
|
d2 = xs_dict_append(d2, "type", "Collection");
|
|
|
|
d2 = xs_dict_append(d2, "totalItems", cnt);
|
2023-05-29 06:27:16 +00:00
|
|
|
|
2023-05-29 08:20:45 +00:00
|
|
|
d1 = xs_dict_append(d1, "type", "Note");
|
|
|
|
d1 = xs_dict_append(d1, "name", v);
|
|
|
|
d1 = xs_dict_append(d1, "replies", d2);
|
|
|
|
|
|
|
|
nopts = xs_list_append(nopts, d1);
|
|
|
|
}
|
2023-05-29 06:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update the list */
|
|
|
|
msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
|
|
|
|
|
2023-05-29 07:12:19 +00:00
|
|
|
/* due date? */
|
2023-06-02 09:48:43 +00:00
|
|
|
int closed = 0;
|
2023-05-29 07:12:19 +00:00
|
|
|
const char *end_time = xs_dict_get(msg, "endTime");
|
|
|
|
if (!xs_is_null(end_time)) {
|
|
|
|
xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
|
|
|
|
|
2023-06-02 09:48:43 +00:00
|
|
|
/* is now greater than the endTime? */
|
2023-05-30 04:00:11 +00:00
|
|
|
if (strcmp(now, end_time) >= 0) {
|
2023-06-02 09:48:43 +00:00
|
|
|
xs *et = xs_dup(end_time);
|
|
|
|
msg = xs_dict_set(msg, "closed", et);
|
2023-05-30 12:52:18 +00:00
|
|
|
|
2023-06-02 09:48:43 +00:00
|
|
|
closed = 1;
|
2023-05-29 09:07:38 +00:00
|
|
|
}
|
2023-05-29 07:12:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 08:11:14 +00:00
|
|
|
/* update the count of voters */
|
|
|
|
xs *vcnt = xs_number_new(xs_list_len(rcpts));
|
|
|
|
msg = xs_dict_set(msg, "votersCount", vcnt);
|
2023-08-08 11:56:12 +00:00
|
|
|
msg = xs_dict_set(msg, "cc", rcpts);
|
2023-05-29 08:11:14 +00:00
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
/* store */
|
|
|
|
object_add_ow(id, msg);
|
|
|
|
|
|
|
|
snac_debug(user, 1, xs_fmt("recounted poll %s", id));
|
|
|
|
timeline_touch(user);
|
|
|
|
|
2023-05-29 07:37:54 +00:00
|
|
|
/* send an update message to all voters */
|
|
|
|
xs *u_msg = msg_update(user, msg);
|
|
|
|
u_msg = xs_dict_set(u_msg, "cc", rcpts);
|
|
|
|
|
|
|
|
enqueue_message(user, u_msg);
|
|
|
|
|
2023-06-02 09:48:43 +00:00
|
|
|
if (closed) {
|
|
|
|
xs *c_msg = msg_update(user, msg);
|
|
|
|
notify(user, "Update", "Question", user->actor, c_msg);
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:27:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-24 08:43:57 +00:00
|
|
|
/** queues **/
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
|
2022-09-24 08:43:57 +00:00
|
|
|
/* processes an ActivityPub message from the input queue */
|
2023-12-11 15:59:33 +00:00
|
|
|
/* return values: -1, fatal error; 0, transient error, retry;
|
|
|
|
1, processed and done; 2, propagate to users (only when no user is set) */
|
2022-09-23 21:49:09 +00:00
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *actor = xs_dict_get(msg, "actor");
|
|
|
|
const char *type = xs_dict_get(msg, "type");
|
2022-09-28 03:16:17 +00:00
|
|
|
xs *actor_o = NULL;
|
2022-09-28 19:09:50 +00:00
|
|
|
int a_status;
|
2022-10-21 07:41:29 +00:00
|
|
|
int do_notify = 0;
|
2022-09-25 05:58:25 +00:00
|
|
|
|
2023-12-10 09:00:17 +00:00
|
|
|
if (xs_is_null(actor) || *actor == '\0') {
|
2023-12-10 09:17:50 +00:00
|
|
|
srv_debug(0, xs_fmt("malformed message (bad actor)"));
|
2023-12-10 09:00:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-29 07:03:00 +00:00
|
|
|
/* question votes may not have a type */
|
|
|
|
if (xs_is_null(type))
|
|
|
|
type = "Note";
|
|
|
|
|
2023-08-13 14:09:20 +00:00
|
|
|
/* reject uninteresting messages right now */
|
2024-08-16 16:43:24 +00:00
|
|
|
if (xs_match(type, "Add|View|Reject|Read|Remove")) {
|
2023-12-10 09:17:50 +00:00
|
|
|
srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
|
2023-12-10 09:00:17 +00:00
|
|
|
return -1;
|
2023-03-31 17:09:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *object, *utype;
|
2022-09-25 05:58:25 +00:00
|
|
|
|
|
|
|
object = xs_dict_get(msg, "object");
|
2022-09-25 20:57:18 +00:00
|
|
|
if (object != NULL && xs_type(object) == XSTYPE_DICT)
|
2022-09-25 05:58:25 +00:00
|
|
|
utype = xs_dict_get(object, "type");
|
|
|
|
else
|
|
|
|
utype = "(null)";
|
2022-09-23 21:49:09 +00:00
|
|
|
|
2023-12-11 15:59:33 +00:00
|
|
|
/* special case for Delete messages */
|
|
|
|
if (strcmp(type, "Delete") == 0) {
|
|
|
|
/* if the actor is not here, do not even try */
|
|
|
|
if (!object_here(actor)) {
|
2023-12-14 18:58:44 +00:00
|
|
|
srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
|
2023-12-11 15:59:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2023-12-16 14:15:00 +00:00
|
|
|
|
|
|
|
/* discard crap */
|
|
|
|
if (xs_is_null(object)) {
|
|
|
|
srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* also discard if the object to be deleted is not here */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *obj_id = object;
|
2023-12-16 14:15:00 +00:00
|
|
|
if (xs_type(obj_id) == XSTYPE_DICT)
|
|
|
|
obj_id = xs_dict_get(obj_id, "id");
|
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(obj_id) || !object_here(obj_id)) {
|
2023-12-16 14:15:00 +00:00
|
|
|
srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
|
|
|
|
return -1;
|
|
|
|
}
|
2023-12-11 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 03:16:17 +00:00
|
|
|
/* bring the actor */
|
2023-12-17 18:53:54 +00:00
|
|
|
a_status = actor_request(snac, actor, &actor_o);
|
2022-09-28 19:09:50 +00:00
|
|
|
|
2023-08-09 17:36:31 +00:00
|
|
|
/* do not retry permanent failures */
|
2024-05-26 19:45:41 +00:00
|
|
|
if (a_status == HTTP_STATUS_NOT_FOUND
|
|
|
|
|| a_status == HTTP_STATUS_GONE
|
|
|
|
|| a_status < 0) {
|
2023-12-10 09:17:50 +00:00
|
|
|
srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
|
2023-12-10 09:00:17 +00:00
|
|
|
return -1;
|
2022-09-28 19:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_status(a_status)) {
|
2023-12-11 14:04:30 +00:00
|
|
|
/* do not retry 'Delete' messages */
|
|
|
|
if (strcmp(type, "Delete") == 0) {
|
2023-12-11 15:00:16 +00:00
|
|
|
srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
|
2023-12-11 14:04:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-01-11 05:34:29 +00:00
|
|
|
/* other actor download errors */
|
|
|
|
|
|
|
|
/* the actor may require a signed request; propagate if no user is set */
|
|
|
|
if (snac == NULL)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
/* may need a retry */
|
|
|
|
srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
|
2022-09-28 18:41:07 +00:00
|
|
|
return 0;
|
2022-09-28 18:24:47 +00:00
|
|
|
}
|
2022-09-28 03:16:17 +00:00
|
|
|
|
2022-09-24 08:03:27 +00:00
|
|
|
/* check the signature */
|
2023-03-01 07:25:36 +00:00
|
|
|
xs *sig_err = NULL;
|
|
|
|
|
2023-12-11 09:12:57 +00:00
|
|
|
if (!check_signature(req, &sig_err)) {
|
2023-12-10 09:17:50 +00:00
|
|
|
srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
|
2023-03-01 07:25:36 +00:00
|
|
|
|
|
|
|
srv_archive_error("check_signature", sig_err, req, msg);
|
2023-12-10 09:00:17 +00:00
|
|
|
return -1;
|
2022-09-29 12:44:24 +00:00
|
|
|
}
|
2022-09-24 08:03:27 +00:00
|
|
|
|
2023-12-11 15:59:33 +00:00
|
|
|
/* if no user is set, no further checks can be done; propagate */
|
2023-12-10 09:17:50 +00:00
|
|
|
if (snac == NULL)
|
2023-12-11 15:59:33 +00:00
|
|
|
return 2;
|
2023-12-10 09:17:50 +00:00
|
|
|
|
|
|
|
/* reject messages that are not for this user */
|
|
|
|
if (!is_msg_for_me(snac, msg)) {
|
|
|
|
snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it's a DM from someone we don't follow, reject the message */
|
|
|
|
if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
|
|
|
|
if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
|
|
|
|
!following_check(snac, actor)) {
|
|
|
|
snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 12:52:45 +00:00
|
|
|
/* check the minimum acceptable account age */
|
|
|
|
int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
|
|
|
|
|
|
|
|
if (min_account_age > 0) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *actor_date = xs_dict_get(actor_o, "published");
|
2024-02-22 12:52:45 +00:00
|
|
|
if (!xs_is_null(actor_date)) {
|
|
|
|
time_t actor_t = xs_parse_iso_date(actor_date, 0);
|
|
|
|
|
2024-02-22 13:11:05 +00:00
|
|
|
if (actor_t < 950000000) {
|
|
|
|
snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
|
|
|
|
actor, actor_date));
|
2024-02-22 12:52:45 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2024-02-22 13:11:05 +00:00
|
|
|
|
|
|
|
if (actor_t > 0) {
|
|
|
|
int td = (int)(time(NULL) - actor_t);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
|
|
|
|
|
|
|
|
if (td < min_account_age) {
|
|
|
|
snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
|
|
|
|
actor, td));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2024-02-22 12:52:45 +00:00
|
|
|
}
|
2024-02-22 13:11:05 +00:00
|
|
|
else
|
2024-03-04 08:18:07 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
|
2024-02-22 12:52:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Follow") == 0) { /** **/
|
2024-10-19 16:29:58 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
|
|
|
|
if (xs_is_null(id)) {
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
}
|
|
|
|
else
|
2022-12-15 15:37:34 +00:00
|
|
|
if (!follower_check(snac, actor)) {
|
2023-12-28 08:26:08 +00:00
|
|
|
/* ensure the actor object is here */
|
|
|
|
if (!object_here(actor)) {
|
|
|
|
xs *actor_obj = NULL;
|
|
|
|
actor_request(snac, actor, &actor_obj);
|
|
|
|
object_add(actor, actor_obj);
|
|
|
|
}
|
|
|
|
|
2022-12-15 15:37:34 +00:00
|
|
|
xs *f_msg = xs_dup(msg);
|
|
|
|
xs *reply = msg_accept(snac, f_msg, actor);
|
2022-09-27 05:54:05 +00:00
|
|
|
|
2023-07-13 16:18:23 +00:00
|
|
|
post_message(snac, actor, reply);
|
2022-09-27 05:54:05 +00:00
|
|
|
|
2022-12-15 15:37:34 +00:00
|
|
|
if (xs_is_null(xs_dict_get(f_msg, "published"))) {
|
|
|
|
/* add a date if it doesn't include one (Mastodon) */
|
2023-05-29 07:07:27 +00:00
|
|
|
xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
|
2022-12-15 15:37:34 +00:00
|
|
|
f_msg = xs_dict_set(f_msg, "published", date);
|
|
|
|
}
|
2022-09-30 16:12:32 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
timeline_add(snac, id, f_msg);
|
2022-09-27 05:54:05 +00:00
|
|
|
|
2022-12-15 15:37:34 +00:00
|
|
|
follower_add(snac, actor);
|
2022-09-27 05:54:05 +00:00
|
|
|
|
2022-12-15 15:37:34 +00:00
|
|
|
snac_log(snac, xs_fmt("new follower %s", actor));
|
|
|
|
do_notify = 1;
|
|
|
|
}
|
|
|
|
else
|
2022-12-15 15:43:01 +00:00
|
|
|
snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Undo") == 0) { /** **/
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(object, "object");
|
2024-04-11 17:29:30 +00:00
|
|
|
|
2023-12-15 10:08:10 +00:00
|
|
|
if (xs_type(object) != XSTYPE_DICT)
|
|
|
|
utype = "Follow";
|
|
|
|
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(utype, "Follow") == 0) { /** **/
|
2024-05-02 05:25:22 +00:00
|
|
|
if (id && strcmp(id, snac->actor) != 0)
|
|
|
|
snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
|
|
|
|
else {
|
|
|
|
if (valid_status(follower_del(snac, actor))) {
|
|
|
|
snac_log(snac, xs_fmt("no longer following us %s", actor));
|
|
|
|
do_notify = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("error deleting follower %s", actor));
|
2022-10-21 07:41:29 +00:00
|
|
|
}
|
2022-09-28 17:59:19 +00:00
|
|
|
}
|
2024-04-11 17:29:30 +00:00
|
|
|
else
|
2024-08-07 09:01:10 +00:00
|
|
|
if (strcmp(utype, "Like") == 0 || strcmp(utype, "EmojiReact") == 0) { /** **/
|
2024-04-11 17:29:30 +00:00
|
|
|
int status = object_unadmire(id, actor, 1);
|
|
|
|
|
2024-08-07 09:01:10 +00:00
|
|
|
snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
|
2024-04-11 17:29:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(utype, "Announce") == 0) { /** **/
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_OK;
|
2024-04-12 03:28:34 +00:00
|
|
|
|
|
|
|
/* commented out: if a followed user boosts something that
|
|
|
|
is requested and then unboosts, the post remains here,
|
|
|
|
but with no apparent reason, and that is confusing */
|
|
|
|
//status = object_unadmire(id, actor, 0);
|
2024-04-11 17:29:30 +00:00
|
|
|
|
|
|
|
snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
|
|
|
|
}
|
2022-09-28 17:59:19 +00:00
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Create") == 0) { /** **/
|
2024-01-05 16:37:36 +00:00
|
|
|
if (is_muted(snac, actor)) {
|
2023-05-24 07:43:11 +00:00
|
|
|
snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
|
2024-01-05 16:37:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2023-05-24 07:43:11 +00:00
|
|
|
|
2024-03-11 07:27:37 +00:00
|
|
|
if (xs_match(utype, "Note|Article")) { /** **/
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(object, "id");
|
2024-11-19 05:46:14 +00:00
|
|
|
const char *in_reply_to = get_in_reply_to(object);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *atto = get_atto(object);
|
2023-05-24 07:43:11 +00:00
|
|
|
xs *wrk = NULL;
|
2022-09-25 05:58:25 +00:00
|
|
|
|
2024-05-05 06:23:12 +00:00
|
|
|
if (xs_is_null(id))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
|
|
|
if (xs_is_null(atto))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
|
|
|
|
else
|
2023-10-16 17:14:44 +00:00
|
|
|
if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
|
|
|
|
snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
|
2022-09-25 05:58:25 +00:00
|
|
|
}
|
2023-10-16 17:14:44 +00:00
|
|
|
else {
|
2024-05-15 11:31:53 +00:00
|
|
|
if (content_match("filter_reject.txt", object)) {
|
2024-03-11 05:00:21 +00:00
|
|
|
snac_log(snac, xs_fmt("rejected by content %s", id));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-05-05 06:23:12 +00:00
|
|
|
if (strcmp(actor, atto) != 0)
|
|
|
|
snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
|
|
|
|
|
2023-10-16 17:14:44 +00:00
|
|
|
timeline_request(snac, &in_reply_to, &wrk, 0);
|
2023-05-29 07:03:00 +00:00
|
|
|
|
2023-10-16 17:14:44 +00:00
|
|
|
if (timeline_add(snac, id, object)) {
|
2024-03-11 05:00:21 +00:00
|
|
|
snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
|
2023-10-16 17:14:44 +00:00
|
|
|
do_notify = 1;
|
|
|
|
}
|
2023-05-29 07:03:00 +00:00
|
|
|
|
2023-10-16 17:14:44 +00:00
|
|
|
/* if it has a "name" field, it may be a vote for a question */
|
|
|
|
const char *name = xs_dict_get(object, "name");
|
|
|
|
|
|
|
|
if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
|
|
|
|
update_question(snac, in_reply_to);
|
|
|
|
}
|
2022-09-25 05:58:25 +00:00
|
|
|
}
|
2023-05-24 07:43:11 +00:00
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(utype, "Question") == 0) { /** **/
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(object, "id");
|
2023-05-24 07:43:11 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(id))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2023-05-24 07:43:11 +00:00
|
|
|
if (timeline_add(snac, id, object))
|
|
|
|
snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
|
|
|
|
}
|
2024-01-18 21:21:05 +00:00
|
|
|
else
|
|
|
|
if (strcmp(utype, "Video") == 0) { /** **/
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(object, "id");
|
2024-01-18 21:21:05 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(id))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2024-01-18 21:21:05 +00:00
|
|
|
if (timeline_add(snac, id, object))
|
|
|
|
snac_log(snac, xs_fmt("new 'Video' %s %s", actor, id));
|
|
|
|
}
|
2022-09-25 05:58:25 +00:00
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Accept") == 0) { /** **/
|
2023-07-24 18:44:00 +00:00
|
|
|
if (strcmp(utype, "(null)") == 0) {
|
|
|
|
const char *obj_id = xs_dict_get(msg, "object");
|
|
|
|
|
|
|
|
/* if the accepted object id is a string that may
|
|
|
|
be created by us, it's a follow */
|
|
|
|
if (xs_type(obj_id) == XSTYPE_STRING &&
|
|
|
|
xs_startswith(obj_id, srv_baseurl) &&
|
|
|
|
xs_endswith(obj_id, "/Follow"))
|
|
|
|
utype = "Follow";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(utype, "Follow") == 0) { /** **/
|
2022-09-27 13:38:09 +00:00
|
|
|
if (following_check(snac, actor)) {
|
|
|
|
following_add(snac, actor, msg);
|
|
|
|
snac_log(snac, xs_fmt("confirmed follow from %s", actor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
|
|
|
|
}
|
2023-08-13 13:56:11 +00:00
|
|
|
else
|
|
|
|
if (strcmp(utype, "Create") == 0) {
|
|
|
|
/* some implementations send Create confirmations, go figure */
|
|
|
|
snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
|
|
|
|
}
|
2023-07-24 18:37:31 +00:00
|
|
|
else {
|
|
|
|
srv_archive_error("accept", "ignored Accept", req, msg);
|
2022-09-27 13:38:09 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
|
2023-07-24 18:37:31 +00:00
|
|
|
}
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2024-07-30 16:34:40 +00:00
|
|
|
if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
|
2022-09-26 05:19:45 +00:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(object))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
|
2024-07-30 16:34:40 +00:00
|
|
|
snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
|
2024-02-21 08:22:32 +00:00
|
|
|
else
|
2024-10-19 16:29:58 +00:00
|
|
|
snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
|
2024-03-22 20:26:10 +00:00
|
|
|
|
|
|
|
do_notify = 1;
|
2022-09-25 15:42:39 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Announce") == 0) { /** **/
|
2023-08-07 17:21:57 +00:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(object))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2024-01-05 16:37:36 +00:00
|
|
|
if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
|
|
|
|
snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
|
|
|
|
else
|
2023-08-07 17:21:57 +00:00
|
|
|
if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
|
2023-08-07 04:35:31 +00:00
|
|
|
snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
|
|
|
|
else {
|
|
|
|
xs *a_msg = NULL;
|
|
|
|
xs *wrk = NULL;
|
2022-09-28 03:16:17 +00:00
|
|
|
|
2023-08-07 04:35:31 +00:00
|
|
|
timeline_request(snac, &object, &wrk, 0);
|
2022-09-26 05:19:45 +00:00
|
|
|
|
2023-08-07 04:35:31 +00:00
|
|
|
if (valid_status(object_get(object, &a_msg))) {
|
2024-01-18 22:04:37 +00:00
|
|
|
const char *who = get_atto(a_msg);
|
2022-09-28 03:16:17 +00:00
|
|
|
|
2023-08-07 04:35:31 +00:00
|
|
|
if (who && !is_muted(snac, who)) {
|
|
|
|
/* bring the actor */
|
|
|
|
xs *who_o = NULL;
|
2022-09-28 05:12:16 +00:00
|
|
|
|
2023-12-17 18:53:54 +00:00
|
|
|
if (valid_status(actor_request(snac, who, &who_o))) {
|
2024-05-26 19:45:41 +00:00
|
|
|
if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
|
2024-02-21 08:22:32 +00:00
|
|
|
snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
|
|
|
|
actor, object));
|
2024-03-22 20:26:10 +00:00
|
|
|
|
2024-04-30 17:41:08 +00:00
|
|
|
/* distribute the post with the actor as 'proxy' */
|
|
|
|
list_distribute(snac, actor, a_msg);
|
|
|
|
|
2024-03-22 20:26:10 +00:00
|
|
|
do_notify = 1;
|
2023-08-07 04:35:31 +00:00
|
|
|
}
|
|
|
|
else
|
2023-12-14 20:40:50 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
|
2022-09-28 05:12:16 +00:00
|
|
|
}
|
|
|
|
else
|
2023-08-07 04:35:31 +00:00
|
|
|
snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
|
2022-09-28 03:16:17 +00:00
|
|
|
}
|
|
|
|
else
|
2023-12-14 18:58:44 +00:00
|
|
|
snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
|
2022-09-28 03:16:17 +00:00
|
|
|
}
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Update") == 0) { /** **/
|
2023-09-17 01:00:31 +00:00
|
|
|
if (xs_match(utype, "Person|Service")) { /** **/
|
2023-05-04 07:19:26 +00:00
|
|
|
actor_add(actor, xs_dict_get(msg, "object"));
|
2023-05-24 08:46:54 +00:00
|
|
|
timeline_touch(snac);
|
2022-11-25 11:09:30 +00:00
|
|
|
|
2022-09-28 18:08:02 +00:00
|
|
|
snac_log(snac, xs_fmt("updated actor %s", actor));
|
|
|
|
}
|
2022-12-15 10:06:31 +00:00
|
|
|
else
|
2024-01-14 17:41:09 +00:00
|
|
|
if (xs_match(utype, "Note|Page|Article|Video")) { /** **/
|
2023-05-30 04:12:10 +00:00
|
|
|
const char *id = xs_dict_get(object, "id");
|
2022-12-15 10:06:31 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(id))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2023-10-16 17:19:41 +00:00
|
|
|
if (object_here(id)) {
|
|
|
|
object_add_ow(id, object);
|
|
|
|
timeline_touch(snac);
|
2022-12-15 10:06:31 +00:00
|
|
|
|
2024-01-14 17:41:09 +00:00
|
|
|
snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
|
2023-10-16 17:19:41 +00:00
|
|
|
}
|
|
|
|
else
|
2024-01-14 17:41:09 +00:00
|
|
|
snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
|
2022-12-15 10:06:31 +00:00
|
|
|
}
|
2023-05-24 07:43:11 +00:00
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(utype, "Question") == 0) { /** **/
|
2023-05-30 04:12:10 +00:00
|
|
|
const char *id = xs_dict_get(object, "id");
|
|
|
|
const char *closed = xs_dict_get(object, "closed");
|
2023-05-24 07:43:11 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(id))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else {
|
|
|
|
object_add_ow(id, object);
|
|
|
|
timeline_touch(snac);
|
2023-05-24 07:43:11 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
|
2023-05-30 12:52:18 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (closed != NULL)
|
|
|
|
do_notify = 1;
|
|
|
|
}
|
2023-05-24 07:43:11 +00:00
|
|
|
}
|
2024-01-14 17:41:09 +00:00
|
|
|
else {
|
|
|
|
srv_archive_error("unsupported_update", "unsupported_update", req, msg);
|
|
|
|
|
2022-09-28 18:08:02 +00:00
|
|
|
snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
|
2024-01-14 17:41:09 +00:00
|
|
|
}
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Delete") == 0) { /** **/
|
2022-10-01 17:48:13 +00:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (xs_is_null(object))
|
|
|
|
snac_log(snac, xs_fmt("malformed message: no 'id' field"));
|
|
|
|
else
|
2023-12-17 13:56:09 +00:00
|
|
|
if (object_here(object)) {
|
|
|
|
timeline_del(snac, object);
|
2022-12-08 07:19:24 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
|
2023-12-17 13:56:09 +00:00
|
|
|
}
|
2022-10-20 08:34:32 +00:00
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
2022-09-25 16:28:15 +00:00
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Pong") == 0) { /** **/
|
2023-05-05 07:54:41 +00:00
|
|
|
snac_log(snac, xs_fmt("'Pong' received from %s", actor));
|
|
|
|
}
|
|
|
|
else
|
2023-05-30 12:52:18 +00:00
|
|
|
if (strcmp(type, "Ping") == 0) { /** **/
|
2024-10-19 16:29:58 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
|
|
|
|
2023-05-05 07:54:41 +00:00
|
|
|
snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
|
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
if (!xs_is_null(id)) {
|
|
|
|
xs *rsp = msg_pong(snac, actor, id);
|
2023-05-05 07:54:41 +00:00
|
|
|
|
2024-10-19 16:29:58 +00:00
|
|
|
enqueue_output_by_actor(snac, rsp, actor, 0);
|
|
|
|
}
|
2023-05-05 07:54:41 +00:00
|
|
|
}
|
2024-07-30 16:27:09 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Block") == 0) { /** **/
|
2024-10-30 15:57:27 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("'Block' received from %s", actor));
|
2024-07-30 16:27:09 +00:00
|
|
|
|
|
|
|
/* should we MUTE the actor back? */
|
|
|
|
/* mute(snac, actor); */
|
|
|
|
|
2024-10-30 15:57:27 +00:00
|
|
|
if (!xs_is_true(xs_dict_get(srv_config, "disable_block_notifications")))
|
|
|
|
do_notify = 1;
|
2024-07-30 16:27:09 +00:00
|
|
|
}
|
2024-07-30 20:18:30 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "Move") == 0) { /** **/
|
2024-07-31 05:20:16 +00:00
|
|
|
do_notify = 1;
|
|
|
|
|
2024-07-30 20:18:30 +00:00
|
|
|
const char *old_account = xs_dict_get(msg, "object");
|
|
|
|
const char *new_account = xs_dict_get(msg, "target");
|
|
|
|
|
|
|
|
if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
|
|
|
|
if (following_check(snac, old_account)) {
|
|
|
|
xs *n_actor = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get(new_account, &n_actor))) {
|
|
|
|
const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
|
|
|
|
|
|
|
|
if (xs_type(aka) == XSTYPE_LIST) {
|
|
|
|
if (xs_list_in(aka, old_account) != -1) {
|
|
|
|
/* all conditions met! */
|
|
|
|
|
|
|
|
/* follow new account */
|
|
|
|
xs *f_msg = msg_follow(snac, new_account);
|
|
|
|
|
|
|
|
if (f_msg != NULL) {
|
|
|
|
const char *new_actor = xs_dict_get(f_msg, "object");
|
|
|
|
following_add(snac, new_actor, f_msg);
|
|
|
|
enqueue_output_by_actor(snac, f_msg, new_actor, 0);
|
|
|
|
|
|
|
|
snac_log(snac, xs_fmt("'Move': following %s", new_account));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unfollow old account */
|
|
|
|
xs *of_msg = NULL;
|
|
|
|
|
|
|
|
if (valid_status(following_get(snac, old_account, &of_msg))) {
|
|
|
|
xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
|
|
|
|
following_del(snac, old_account);
|
|
|
|
enqueue_output_by_actor(snac, uf_msg, old_account, 0);
|
|
|
|
|
|
|
|
snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
|
|
|
|
old_account, new_account));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
|
|
|
|
}
|
2024-09-16 22:26:23 +00:00
|
|
|
else {
|
2024-07-30 20:18:30 +00:00
|
|
|
snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
|
2024-09-16 22:26:23 +00:00
|
|
|
|
|
|
|
/* may be a server hiccup, retry later */
|
|
|
|
return 0;
|
|
|
|
}
|
2024-07-30 20:18:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
|
|
|
|
}
|
2024-07-30 20:21:46 +00:00
|
|
|
else {
|
2024-07-30 20:18:30 +00:00
|
|
|
snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
|
2024-07-30 20:21:46 +00:00
|
|
|
srv_archive_error("move", "move", req, msg);
|
|
|
|
}
|
2024-07-30 20:18:30 +00:00
|
|
|
}
|
2024-01-14 17:41:09 +00:00
|
|
|
else {
|
|
|
|
srv_archive_error("unsupported_type", "unsupported_type", req, msg);
|
|
|
|
|
2023-05-05 07:54:41 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
|
2024-01-14 17:41:09 +00:00
|
|
|
}
|
2022-09-28 18:41:07 +00:00
|
|
|
|
2023-04-14 17:17:16 +00:00
|
|
|
if (do_notify) {
|
2022-10-21 07:41:29 +00:00
|
|
|
notify(snac, type, utype, actor, msg);
|
|
|
|
|
2023-04-14 17:17:16 +00:00
|
|
|
timeline_touch(snac);
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:41:07 +00:00
|
|
|
return 1;
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int send_email(const char *msg)
|
2023-01-13 12:25:14 +00:00
|
|
|
/* invoke sendmail with email headers and body in msg */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int status;
|
|
|
|
int fds[2];
|
|
|
|
pid_t pid;
|
|
|
|
if (pipe(fds) == -1) return -1;
|
|
|
|
pid = vfork();
|
|
|
|
if (pid == -1) return -1;
|
|
|
|
else if (pid == 0) {
|
|
|
|
dup2(fds[0], 0);
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
if ((f = fdopen(fds[1], "w")) == NULL) {
|
|
|
|
close(fds[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fprintf(f, "%s\n", msg);
|
|
|
|
fclose(f);
|
|
|
|
if (waitpid(pid, &status, 0) == -1) return -1;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 03:55:58 +00:00
|
|
|
void process_user_queue_item(snac *snac, xs_dict *q_item)
|
2023-02-02 04:07:20 +00:00
|
|
|
/* processes an item from the user queue */
|
2022-09-23 17:37:01 +00:00
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type;
|
2022-09-23 17:37:01 +00:00
|
|
|
int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
|
|
|
|
|
2023-01-31 21:30:34 +00:00
|
|
|
if ((type = xs_dict_get(q_item, "type")) == NULL)
|
|
|
|
type = "output";
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2023-01-31 21:30:34 +00:00
|
|
|
if (strcmp(type, "message") == 0) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *msg = xs_dict_get(q_item, "message");
|
2024-06-17 15:28:10 +00:00
|
|
|
xs *rcpts = recipient_list(snac, msg, 1);
|
2023-02-20 12:19:29 +00:00
|
|
|
xs_set inboxes;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *actor;
|
2024-06-17 15:28:10 +00:00
|
|
|
int c;
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2023-02-20 12:19:29 +00:00
|
|
|
xs_set_init(&inboxes);
|
|
|
|
|
2023-03-03 05:04:40 +00:00
|
|
|
/* iterate the recipients */
|
2024-06-17 15:28:10 +00:00
|
|
|
c = 0;
|
|
|
|
while (xs_list_next(rcpts, &actor, &c)) {
|
2024-10-08 15:37:15 +00:00
|
|
|
xs *inbox = get_actor_inbox(actor, 1);
|
2023-02-20 12:19:29 +00:00
|
|
|
|
|
|
|
if (inbox != NULL) {
|
|
|
|
/* add to the set and, if it's not there, send message */
|
|
|
|
if (xs_set_add(&inboxes, inbox) == 1)
|
2023-09-29 08:34:22 +00:00
|
|
|
enqueue_output(snac, msg, inbox, 0, 0);
|
2023-02-20 12:19:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
|
2022-09-25 19:55:29 +00:00
|
|
|
}
|
2023-02-20 12:19:29 +00:00
|
|
|
|
2024-06-17 15:28:10 +00:00
|
|
|
/* if it's a public note or question, send to the collected inboxes */
|
2024-06-18 03:40:10 +00:00
|
|
|
if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
|
2024-01-24 17:50:57 +00:00
|
|
|
if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
|
|
|
|
xs *shibx = inbox_list();
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *inbox;
|
2024-01-24 17:50:57 +00:00
|
|
|
|
2024-06-17 15:28:10 +00:00
|
|
|
c = 0;
|
|
|
|
while (xs_list_next(shibx, &inbox, &c)) {
|
2024-01-24 17:50:57 +00:00
|
|
|
if (xs_set_add(&inboxes, inbox) == 1)
|
|
|
|
enqueue_output(snac, msg, inbox, 0, 0);
|
|
|
|
}
|
2023-03-03 05:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 12:19:29 +00:00
|
|
|
xs_set_free(&inboxes);
|
2023-01-31 21:30:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "input") == 0) {
|
|
|
|
/* process the message */
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *msg = xs_dict_get(q_item, "message");
|
|
|
|
const xs_dict *req = xs_dict_get(q_item, "req");
|
2023-01-31 21:30:34 +00:00
|
|
|
int retries = xs_number_get(xs_dict_get(q_item, "retries"));
|
|
|
|
|
|
|
|
if (xs_is_null(msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!process_input_message(snac, msg, req)) {
|
|
|
|
if (retries > queue_retry_max)
|
2023-02-07 12:19:27 +00:00
|
|
|
snac_log(snac, xs_fmt("input giving up"));
|
2023-01-31 21:30:34 +00:00
|
|
|
else {
|
|
|
|
/* reenqueue */
|
|
|
|
enqueue_input(snac, msg, req, retries + 1);
|
2023-02-07 12:19:27 +00:00
|
|
|
snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
|
2022-09-28 18:41:07 +00:00
|
|
|
}
|
2022-09-23 21:49:09 +00:00
|
|
|
}
|
2023-01-31 21:30:34 +00:00
|
|
|
}
|
2023-05-29 09:07:38 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "close_question") == 0) {
|
|
|
|
/* the time for this question has ended */
|
|
|
|
const char *id = xs_dict_get(q_item, "message");
|
|
|
|
|
|
|
|
if (!xs_is_null(id))
|
|
|
|
update_question(snac, id);
|
|
|
|
}
|
2023-02-07 12:19:27 +00:00
|
|
|
else
|
2024-05-04 23:38:22 +00:00
|
|
|
if (strcmp(type, "object_request") == 0) {
|
|
|
|
const char *id = xs_dict_get(q_item, "message");
|
|
|
|
|
|
|
|
if (!xs_is_null(id)) {
|
|
|
|
int status;
|
|
|
|
xs *data = NULL;
|
|
|
|
|
|
|
|
status = activitypub_request(snac, id, &data);
|
|
|
|
|
|
|
|
if (valid_status(status))
|
|
|
|
object_add_ow(id, data);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("object_request %s %d", id, status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2024-02-20 04:31:34 +00:00
|
|
|
if (strcmp(type, "verify_links") == 0) {
|
|
|
|
verify_links(snac);
|
|
|
|
}
|
2024-03-12 16:54:54 +00:00
|
|
|
else
|
2024-03-12 18:47:37 +00:00
|
|
|
if (strcmp(type, "actor_refresh") == 0) {
|
2024-03-12 16:54:54 +00:00
|
|
|
const char *actor = xs_dict_get(q_item, "actor");
|
|
|
|
double mtime = object_mtime(actor);
|
|
|
|
|
2024-03-12 18:44:08 +00:00
|
|
|
/* only refresh if it was refreshed more than an hour ago */
|
|
|
|
if (mtime + 3600.0 < (double) time(NULL)) {
|
2024-03-12 16:54:54 +00:00
|
|
|
xs *actor_o = NULL;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (valid_status((status = activitypub_request(snac, actor, &actor_o))))
|
|
|
|
actor_add(actor, actor_o);
|
2024-03-12 17:04:09 +00:00
|
|
|
else
|
|
|
|
object_touch(actor);
|
2024-03-12 16:54:54 +00:00
|
|
|
|
2024-03-12 18:47:37 +00:00
|
|
|
snac_log(snac, xs_fmt("actor_refresh %s %d", actor, status));
|
2024-03-12 16:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 10:04:59 +00:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
|
2022-09-23 17:37:01 +00:00
|
|
|
}
|
2022-09-23 18:28:23 +00:00
|
|
|
|
|
|
|
|
2023-02-22 07:39:54 +00:00
|
|
|
int process_user_queue(snac *snac)
|
2023-02-02 03:50:51 +00:00
|
|
|
/* processes a user's queue */
|
2023-01-31 21:30:34 +00:00
|
|
|
{
|
2023-02-22 07:39:54 +00:00
|
|
|
int cnt = 0;
|
2023-02-02 03:47:59 +00:00
|
|
|
xs *list = user_queue(snac);
|
2023-01-31 21:30:34 +00:00
|
|
|
|
|
|
|
xs_list *p = list;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *fn;
|
2023-01-31 21:30:34 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
2023-02-02 03:44:30 +00:00
|
|
|
xs *q_item = dequeue(fn);
|
2023-01-31 21:30:34 +00:00
|
|
|
|
2024-01-04 15:27:07 +00:00
|
|
|
if (q_item == NULL)
|
2023-01-31 21:30:34 +00:00
|
|
|
continue;
|
|
|
|
|
2023-02-02 03:55:58 +00:00
|
|
|
process_user_queue_item(snac, q_item);
|
2023-02-22 07:39:54 +00:00
|
|
|
cnt++;
|
2023-01-31 21:30:34 +00:00
|
|
|
}
|
2023-02-22 07:39:54 +00:00
|
|
|
|
|
|
|
return cnt;
|
2023-01-31 21:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 04:07:20 +00:00
|
|
|
void process_queue_item(xs_dict *q_item)
|
|
|
|
/* processes an item from the global queue */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type = xs_dict_get(q_item, "type");
|
2023-02-02 04:21:16 +00:00
|
|
|
int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
|
|
|
|
|
2023-02-07 12:31:48 +00:00
|
|
|
if (strcmp(type, "output") == 0) {
|
|
|
|
int status;
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_str *inbox = xs_dict_get(q_item, "inbox");
|
|
|
|
const xs_str *keyid = xs_dict_get(q_item, "keyid");
|
|
|
|
const xs_str *seckey = xs_dict_get(q_item, "seckey");
|
|
|
|
const xs_dict *msg = xs_dict_get(q_item, "message");
|
2023-02-07 12:31:48 +00:00
|
|
|
int retries = xs_number_get(xs_dict_get(q_item, "retries"));
|
2023-09-29 08:34:22 +00:00
|
|
|
int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
|
2023-02-07 12:31:48 +00:00
|
|
|
xs *payload = NULL;
|
|
|
|
int p_size = 0;
|
2024-04-22 05:53:40 +00:00
|
|
|
int timeout = 0;
|
2023-02-07 12:31:48 +00:00
|
|
|
|
|
|
|
if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
|
|
|
|
srv_log(xs_fmt("output message error: missing fields"));
|
|
|
|
return;
|
2023-09-02 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_instance_blocked(inbox)) {
|
|
|
|
srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
|
|
|
|
return;
|
2023-02-07 12:31:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 08:34:22 +00:00
|
|
|
/* deliver (if previous error status was a timeout, try now longer) */
|
2024-04-22 05:53:40 +00:00
|
|
|
if (p_status == 599)
|
|
|
|
timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
|
|
|
|
else
|
|
|
|
timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
|
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
timeout = 6;
|
|
|
|
|
|
|
|
status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
|
2023-02-07 12:31:48 +00:00
|
|
|
|
2023-02-14 07:26:39 +00:00
|
|
|
if (payload) {
|
2023-03-02 11:30:00 +00:00
|
|
|
if (p_size > 64) {
|
2023-02-14 07:15:38 +00:00
|
|
|
/* trim the message */
|
2023-03-02 11:30:00 +00:00
|
|
|
payload[64] = '\0';
|
2023-02-14 07:15:38 +00:00
|
|
|
payload = xs_str_cat(payload, "...");
|
|
|
|
}
|
|
|
|
|
2023-02-14 07:49:17 +00:00
|
|
|
/* strip ugly control characters */
|
|
|
|
payload = xs_replace_i(payload, "\n", "");
|
|
|
|
payload = xs_replace_i(payload, "\r", "");
|
2023-02-14 07:15:38 +00:00
|
|
|
|
2023-02-14 07:26:39 +00:00
|
|
|
if (*payload)
|
|
|
|
payload = xs_str_wrap_i(" [", payload, "]");
|
|
|
|
}
|
|
|
|
else
|
2023-02-14 07:15:38 +00:00
|
|
|
payload = xs_str_new(NULL);
|
|
|
|
|
|
|
|
srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
|
2023-02-07 12:31:48 +00:00
|
|
|
|
|
|
|
if (!valid_status(status)) {
|
|
|
|
retries++;
|
|
|
|
|
2023-09-29 08:34:22 +00:00
|
|
|
/* if it's not the first time it fails with a timeout,
|
|
|
|
penalize the server by skipping one retry */
|
2024-05-26 19:45:41 +00:00
|
|
|
if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
|
2023-09-29 08:34:22 +00:00
|
|
|
retries++;
|
|
|
|
|
2023-02-07 12:31:48 +00:00
|
|
|
/* error sending; requeue? */
|
2024-05-26 19:45:41 +00:00
|
|
|
if (status == HTTP_STATUS_BAD_REQUEST
|
|
|
|
|| status == HTTP_STATUS_NOT_FOUND
|
|
|
|
|| status == HTTP_STATUS_METHOD_NOT_ALLOWED
|
|
|
|
|| status == HTTP_STATUS_GONE
|
2024-07-30 15:53:08 +00:00
|
|
|
|| status == HTTP_STATUS_UNPROCESSABLE_CONTENT
|
2024-05-26 19:45:41 +00:00
|
|
|
|| status < 0)
|
2023-02-07 12:31:48 +00:00
|
|
|
/* explicit error: discard */
|
|
|
|
srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
|
|
|
|
else
|
|
|
|
if (retries > queue_retry_max)
|
|
|
|
srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
|
|
|
|
else {
|
|
|
|
/* requeue */
|
2023-09-29 08:34:22 +00:00
|
|
|
enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
|
2023-02-07 12:31:48 +00:00
|
|
|
srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-02-02 04:21:16 +00:00
|
|
|
if (strcmp(type, "email") == 0) {
|
|
|
|
/* send this email */
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_str *msg = xs_dict_get(q_item, "message");
|
2023-02-02 04:21:16 +00:00
|
|
|
int retries = xs_number_get(xs_dict_get(q_item, "retries"));
|
|
|
|
|
|
|
|
if (!send_email(msg))
|
|
|
|
srv_debug(1, xs_fmt("email message sent"));
|
|
|
|
else {
|
|
|
|
retries++;
|
|
|
|
|
|
|
|
if (retries > queue_retry_max)
|
2023-02-07 06:37:23 +00:00
|
|
|
srv_log(xs_fmt("email giving up (errno: %d)", errno));
|
2023-02-02 04:21:16 +00:00
|
|
|
else {
|
|
|
|
/* requeue */
|
|
|
|
srv_log(xs_fmt(
|
2023-02-07 06:37:23 +00:00
|
|
|
"email requeue #%d (errno: %d)", retries, errno));
|
2023-02-02 04:21:16 +00:00
|
|
|
|
|
|
|
enqueue_email(msg, retries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 19:07:29 +00:00
|
|
|
else
|
2023-02-07 06:37:23 +00:00
|
|
|
if (strcmp(type, "telegram") == 0) {
|
|
|
|
/* send this via telegram */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *bot = xs_dict_get(q_item, "bot");
|
|
|
|
const char *msg = xs_dict_get(q_item, "message");
|
2023-02-07 06:37:23 +00:00
|
|
|
xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
/* chat_id must start with a - */
|
|
|
|
if (!xs_startswith(chat_id, "-"))
|
|
|
|
chat_id = xs_str_wrap_i("-", chat_id, NULL);
|
|
|
|
|
|
|
|
xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
|
|
|
|
xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
|
|
|
|
|
|
|
|
xs *headers = xs_dict_new();
|
|
|
|
headers = xs_dict_append(headers, "content-type", "application/json");
|
|
|
|
|
2023-05-04 07:34:33 +00:00
|
|
|
xs *rsp = xs_http_request("POST", url, headers,
|
2023-02-07 06:37:23 +00:00
|
|
|
body, strlen(body), &status, NULL, NULL, 0);
|
2023-05-04 07:34:33 +00:00
|
|
|
rsp = xs_free(rsp);
|
2023-02-07 06:37:23 +00:00
|
|
|
|
|
|
|
srv_debug(0, xs_fmt("telegram post %d", status));
|
|
|
|
}
|
|
|
|
else
|
2024-01-12 08:54:14 +00:00
|
|
|
if (strcmp(type, "ntfy") == 0) {
|
|
|
|
/* send this via ntfy */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
|
|
|
|
const char *msg = xs_dict_get(q_item, "message");
|
|
|
|
const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
|
2024-01-12 08:54:14 +00:00
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
xs *url = xs_fmt("%s", ntfy_server);
|
|
|
|
xs *body = xs_fmt("%s", msg);
|
|
|
|
|
|
|
|
xs *headers = xs_dict_new();
|
|
|
|
headers = xs_dict_append(headers, "content-type", "text/plain");
|
|
|
|
// Append the Authorization header only if ntfy_token is not NULL
|
|
|
|
if (ntfy_token != NULL) {
|
|
|
|
headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
|
|
|
|
}
|
|
|
|
|
|
|
|
xs *rsp = xs_http_request("POST", url, headers,
|
|
|
|
body, strlen(body), &status, NULL, NULL, 0);
|
|
|
|
rsp = xs_free(rsp);
|
|
|
|
|
|
|
|
srv_debug(0, xs_fmt("ntfy post %d", status));
|
|
|
|
}
|
|
|
|
else
|
2023-02-06 19:07:29 +00:00
|
|
|
if (strcmp(type, "purge") == 0) {
|
|
|
|
srv_log(xs_dup("purge start"));
|
|
|
|
|
|
|
|
purge_all();
|
|
|
|
|
|
|
|
srv_log(xs_dup("purge end"));
|
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
else
|
|
|
|
if (strcmp(type, "input") == 0) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *msg = xs_dict_get(q_item, "message");
|
|
|
|
const xs_dict *req = xs_dict_get(q_item, "req");
|
2023-12-11 16:59:48 +00:00
|
|
|
int retries = xs_number_get(xs_dict_get(q_item, "retries"));
|
|
|
|
|
|
|
|
/* do some instance-level checks */
|
|
|
|
int r = process_input_message(NULL, msg, req);
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
if (r == 0) {
|
|
|
|
/* transient error? retry */
|
|
|
|
if (retries > queue_retry_max)
|
|
|
|
srv_log(xs_fmt("shared input giving up"));
|
|
|
|
else {
|
|
|
|
/* reenqueue */
|
|
|
|
enqueue_shared_input(msg, req, retries + 1);
|
|
|
|
srv_log(xs_fmt("shared input requeue #%d", retries + 1));
|
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
}
|
2023-12-11 16:59:48 +00:00
|
|
|
else
|
|
|
|
if (r == 2) {
|
|
|
|
/* redistribute the input message to all users */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(q_item, "ntid");
|
2023-12-11 16:59:48 +00:00
|
|
|
xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(tmpfn, "w")) != NULL) {
|
|
|
|
xs_json_dump(q_item, 4, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
xs *users = user_list();
|
|
|
|
xs_list *p = users;
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2023-12-11 16:59:48 +00:00
|
|
|
int cnt = 0;
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
snac user;
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
if (user_open(&user, v)) {
|
|
|
|
if (is_msg_for_me(&user, msg)) {
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2024-03-04 08:22:06 +00:00
|
|
|
snac_debug(&user, 1,
|
|
|
|
xs_fmt("enqueue_input (from shared inbox) %s", xs_dict_get(msg, "id")));
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
if (link(tmpfn, fn) < 0)
|
|
|
|
srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
|
2023-12-11 06:37:53 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
user_free(&user);
|
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 16:59:48 +00:00
|
|
|
unlink(tmpfn);
|
2023-12-11 06:37:53 +00:00
|
|
|
|
2023-12-17 14:21:16 +00:00
|
|
|
if (cnt == 0) {
|
2024-03-04 08:51:11 +00:00
|
|
|
srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
|
2023-12-17 14:21:16 +00:00
|
|
|
}
|
2023-12-11 16:59:48 +00:00
|
|
|
}
|
2023-12-06 13:46:51 +00:00
|
|
|
}
|
2023-02-07 12:19:27 +00:00
|
|
|
else
|
|
|
|
srv_log(xs_fmt("unexpected q_item type '%s'", type));
|
2023-02-02 04:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-22 07:39:54 +00:00
|
|
|
int process_queue(void)
|
2023-02-02 04:07:20 +00:00
|
|
|
/* processes the global queue */
|
|
|
|
{
|
2023-02-22 07:39:54 +00:00
|
|
|
int cnt = 0;
|
2023-02-02 04:07:20 +00:00
|
|
|
xs *list = queue();
|
|
|
|
|
|
|
|
xs_list *p = list;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *fn;
|
2023-02-02 04:07:20 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
|
|
|
xs *q_item = dequeue(fn);
|
|
|
|
|
2023-02-22 07:39:54 +00:00
|
|
|
if (q_item != NULL) {
|
2023-03-02 11:38:02 +00:00
|
|
|
job_post(q_item, 0);
|
2023-02-22 07:39:54 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
2023-02-02 04:07:20 +00:00
|
|
|
}
|
2023-02-22 07:39:54 +00:00
|
|
|
|
|
|
|
return cnt;
|
2023-02-02 04:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-18 16:34:17 +00:00
|
|
|
/** account migration **/
|
|
|
|
|
|
|
|
int migrate_account(snac *user)
|
2024-09-19 18:13:18 +00:00
|
|
|
/* migrates this account to a new one (stored in the 'alias' user field) */
|
2024-09-18 16:34:17 +00:00
|
|
|
{
|
2024-09-19 17:47:07 +00:00
|
|
|
const char *new_account = xs_dict_get(user->config, "alias");
|
2024-09-18 16:34:17 +00:00
|
|
|
|
|
|
|
if (xs_type(new_account) != XSTYPE_STRING) {
|
2024-09-19 17:47:07 +00:00
|
|
|
snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
|
2024-09-18 16:34:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:13:18 +00:00
|
|
|
xs *new_actor = NULL;
|
|
|
|
int status;
|
|
|
|
|
2024-10-09 09:03:28 +00:00
|
|
|
if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
|
2024-09-19 18:13:18 +00:00
|
|
|
snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-10-09 09:03:28 +00:00
|
|
|
actor_add(new_account, new_actor);
|
|
|
|
|
2024-09-19 18:13:18 +00:00
|
|
|
const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
|
|
|
|
|
|
|
|
if (xs_type(loaka) != XSTYPE_LIST) {
|
|
|
|
snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xs_list_in(loaka, user->actor) == -1) {
|
|
|
|
snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-20 08:47:14 +00:00
|
|
|
xs *move = msg_move(user, new_account);
|
|
|
|
xs *fwers = follower_list(user);
|
|
|
|
|
2024-10-08 15:37:15 +00:00
|
|
|
const char *actor;
|
|
|
|
xs_list_foreach(fwers, actor) {
|
|
|
|
/* get the actor inbox, excluding the shared one */
|
|
|
|
xs *inbox = get_actor_inbox(actor, 0);
|
|
|
|
|
|
|
|
if (xs_is_null(inbox))
|
|
|
|
snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
|
|
|
|
else
|
|
|
|
enqueue_output(user, move, inbox, 0, 0);
|
|
|
|
}
|
2024-09-20 08:47:14 +00:00
|
|
|
|
2024-09-18 16:34:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-26 07:22:05 +00:00
|
|
|
/** HTTP handlers */
|
|
|
|
|
2023-05-04 07:25:09 +00:00
|
|
|
int activitypub_get_handler(const xs_dict *req, const char *q_path,
|
2022-09-23 18:59:19 +00:00
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_OK;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *accept = xs_dict_get(req, "accept");
|
2022-09-23 18:59:19 +00:00
|
|
|
snac snac;
|
2022-09-24 09:04:35 +00:00
|
|
|
xs *msg = NULL;
|
2022-09-23 18:59:19 +00:00
|
|
|
|
2022-09-23 21:09:09 +00:00
|
|
|
if (accept == NULL)
|
2022-10-16 17:00:17 +00:00
|
|
|
return 0;
|
2022-09-23 21:09:09 +00:00
|
|
|
|
2022-09-23 18:59:19 +00:00
|
|
|
if (xs_str_in(accept, "application/activity+json") == -1 &&
|
|
|
|
xs_str_in(accept, "application/ld+json") == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
xs *l = xs_split_n(q_path, "/", 2);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *uid;
|
|
|
|
const char *p_path;
|
2022-09-23 18:59:19 +00:00
|
|
|
|
|
|
|
uid = xs_list_get(l, 1);
|
|
|
|
if (!user_open(&snac, uid)) {
|
|
|
|
/* invalid user */
|
2023-02-03 15:51:59 +00:00
|
|
|
srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NOT_FOUND;
|
2022-09-23 18:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_path = xs_list_get(l, 2);
|
|
|
|
|
2022-09-26 10:29:26 +00:00
|
|
|
*ctype = "application/activity+json";
|
|
|
|
|
2022-09-23 18:59:19 +00:00
|
|
|
if (p_path == NULL) {
|
|
|
|
/* if there was no component after the user, it's an actor request */
|
2022-09-25 19:02:47 +00:00
|
|
|
msg = msg_actor(&snac);
|
2022-12-01 18:38:11 +00:00
|
|
|
*ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
|
2022-12-16 07:35:52 +00:00
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ua = xs_dict_get(req, "user-agent");
|
2023-03-02 14:34:04 +00:00
|
|
|
|
|
|
|
snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
|
2022-09-23 18:59:19 +00:00
|
|
|
}
|
|
|
|
else
|
2024-08-26 09:26:34 +00:00
|
|
|
if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
|
|
|
|
xs *id = xs_fmt("%s/%s", snac.actor, p_path);
|
2022-09-28 02:48:23 +00:00
|
|
|
xs *list = xs_list_new();
|
2022-09-24 08:43:57 +00:00
|
|
|
msg = msg_collection(&snac, id);
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2024-08-26 09:26:34 +00:00
|
|
|
int tc = 0;
|
2022-09-28 02:48:23 +00:00
|
|
|
|
2024-08-26 09:26:34 +00:00
|
|
|
/* get the public outbox or the pinned list */
|
|
|
|
xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, 20) : pinned_list(&snac);
|
|
|
|
|
|
|
|
while (xs_list_next(elems, &v, &tc)) {
|
2022-12-02 18:30:59 +00:00
|
|
|
xs *i = NULL;
|
2022-09-28 02:48:23 +00:00
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get_by_md5(v, &i))) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type = xs_dict_get(i, "type");
|
|
|
|
const char *id = xs_dict_get(i, "id");
|
2022-12-02 18:30:59 +00:00
|
|
|
|
|
|
|
if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
|
2023-07-29 13:29:11 +00:00
|
|
|
xs *c_msg = msg_create(&snac, i);
|
|
|
|
list = xs_list_append(list, c_msg);
|
2022-12-02 18:30:59 +00:00
|
|
|
}
|
2022-09-28 02:52:17 +00:00
|
|
|
}
|
2022-09-28 02:48:23 +00:00
|
|
|
}
|
2022-09-24 09:04:35 +00:00
|
|
|
|
|
|
|
/* replace the 'orderedItems' with the latest posts */
|
2022-10-25 08:19:42 +00:00
|
|
|
xs *items = xs_number_new(xs_list_len(list));
|
2022-09-28 02:48:23 +00:00
|
|
|
msg = xs_dict_set(msg, "orderedItems", list);
|
2022-10-25 08:19:42 +00:00
|
|
|
msg = xs_dict_set(msg, "totalItems", items);
|
2022-09-23 18:59:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
|
|
|
|
xs *id = xs_fmt("%s/%s", snac.actor, p_path);
|
2022-09-24 08:43:57 +00:00
|
|
|
msg = msg_collection(&snac, id);
|
2022-09-23 18:59:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (xs_startswith(p_path, "p/")) {
|
2022-09-27 17:00:24 +00:00
|
|
|
xs *id = xs_fmt("%s/%s", snac.actor, p_path);
|
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
status = object_get(id, &msg);
|
2023-08-18 16:21:52 +00:00
|
|
|
|
|
|
|
/* don't return non-public objects */
|
|
|
|
if (valid_status(status) && !is_msg_public(msg))
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_NOT_FOUND;
|
2022-09-23 18:59:19 +00:00
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_NOT_FOUND;
|
2022-09-23 18:59:19 +00:00
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
if (status == HTTP_STATUS_OK && msg != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
*body = xs_json_dumps(msg, 4);
|
2022-09-23 18:59:19 +00:00
|
|
|
*b_size = strlen(*body);
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:00:24 +00:00
|
|
|
snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
|
|
|
|
|
2022-09-23 18:59:19 +00:00
|
|
|
user_free(&snac);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-04 07:25:09 +00:00
|
|
|
int activitypub_post_handler(const xs_dict *req, const char *q_path,
|
|
|
|
char *payload, int p_size,
|
2022-09-23 18:28:23 +00:00
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
/* processes an input message */
|
|
|
|
{
|
2023-05-04 07:28:36 +00:00
|
|
|
(void)b_size;
|
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_ACCEPTED;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *i_ctype = xs_dict_get(req, "content-type");
|
2022-09-23 18:28:23 +00:00
|
|
|
snac snac;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v;
|
2022-09-23 18:28:23 +00:00
|
|
|
|
2023-02-14 07:15:38 +00:00
|
|
|
if (i_ctype == NULL) {
|
|
|
|
*body = xs_str_new("no content-type");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_BAD_REQUEST;
|
2023-02-14 07:15:38 +00:00
|
|
|
}
|
2022-09-23 21:09:09 +00:00
|
|
|
|
2023-12-18 12:24:07 +00:00
|
|
|
if (xs_is_null(payload)) {
|
|
|
|
*body = xs_str_new("no payload");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_BAD_REQUEST;
|
2023-12-18 12:24:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:28:23 +00:00
|
|
|
if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
|
|
|
|
xs_str_in(i_ctype, "application/ld+json") == -1)
|
|
|
|
return 0;
|
|
|
|
|
2022-09-24 09:54:35 +00:00
|
|
|
/* decode the message */
|
|
|
|
xs *msg = xs_json_loads(payload);
|
2023-08-13 14:09:20 +00:00
|
|
|
const char *id = xs_dict_get(msg, "id");
|
2022-09-24 09:54:35 +00:00
|
|
|
|
2023-08-13 14:09:20 +00:00
|
|
|
if (msg == NULL) {
|
2022-09-24 09:54:35 +00:00
|
|
|
srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
|
2023-02-14 07:15:38 +00:00
|
|
|
|
2023-08-11 20:04:23 +00:00
|
|
|
srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
|
|
|
|
|
2023-02-14 07:15:38 +00:00
|
|
|
*body = xs_str_new("JSON error");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_BAD_REQUEST;
|
2023-06-29 06:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 14:09:20 +00:00
|
|
|
if (id && is_instance_blocked(id)) {
|
2023-06-29 06:26:44 +00:00
|
|
|
srv_debug(1, xs_fmt("full instance block for %s", id));
|
|
|
|
|
|
|
|
*body = xs_str_new("blocked");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_FORBIDDEN;
|
2022-09-24 09:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the user and path */
|
2022-09-23 18:28:23 +00:00
|
|
|
xs *l = xs_split_n(q_path, "/", 2);
|
|
|
|
|
2023-12-06 13:46:51 +00:00
|
|
|
if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
|
|
|
|
enqueue_shared_input(msg, req, 0);
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_ACCEPTED;
|
2023-12-06 13:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:28:23 +00:00
|
|
|
if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
|
|
|
|
/* strange q_path */
|
2022-09-24 09:54:35 +00:00
|
|
|
srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NOT_FOUND;
|
2022-09-23 18:28:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 14:09:20 +00:00
|
|
|
const char *uid = xs_list_get(l, 1);
|
2022-09-23 18:28:23 +00:00
|
|
|
if (!user_open(&snac, uid)) {
|
|
|
|
/* invalid user */
|
2022-09-24 09:54:35 +00:00
|
|
|
srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NOT_FOUND;
|
2022-09-23 18:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 05:16:46 +00:00
|
|
|
/* if it has a digest, check it now, because
|
|
|
|
later the payload won't be exactly the same */
|
|
|
|
if ((v = xs_dict_get(req, "digest")) != NULL) {
|
|
|
|
xs *s1 = xs_sha256_base64(payload, p_size);
|
|
|
|
xs *s2 = xs_fmt("SHA-256=%s", s1);
|
|
|
|
|
2022-09-27 17:00:24 +00:00
|
|
|
if (strcmp(s2, v) != 0) {
|
2022-09-27 05:16:46 +00:00
|
|
|
srv_log(xs_fmt("digest check FAILED"));
|
2023-02-14 07:15:38 +00:00
|
|
|
|
|
|
|
*body = xs_str_new("bad digest");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_BAD_REQUEST;
|
2022-09-27 17:00:24 +00:00
|
|
|
}
|
2022-09-27 05:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 23:26:50 +00:00
|
|
|
/* if the message is from a muted actor, reject it right now */
|
|
|
|
if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
|
|
|
|
if (is_muted(&snac, v)) {
|
2023-05-24 11:49:34 +00:00
|
|
|
snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
|
2023-03-03 23:26:50 +00:00
|
|
|
|
|
|
|
*body = xs_str_new("rejected");
|
|
|
|
*ctype = "text/plain";
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_FORBIDDEN;
|
2023-03-03 23:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:00:24 +00:00
|
|
|
if (valid_status(status)) {
|
2022-09-28 18:41:07 +00:00
|
|
|
enqueue_input(&snac, msg, req, 0);
|
2022-09-27 17:00:24 +00:00
|
|
|
*ctype = "application/activity+json";
|
|
|
|
}
|
2022-09-23 21:09:09 +00:00
|
|
|
|
2022-09-23 18:28:23 +00:00
|
|
|
user_free(&snac);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|