2022-09-23 18:33:33 +03:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2023-01-17 11:50:16 +03:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink / MIT license */
|
2022-09-23 18:33:33 +03:00
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#include "xs_json.h"
|
|
|
|
#include "xs_curl.h"
|
2022-09-25 22:02:47 +03:00
|
|
|
#include "xs_mime.h"
|
2022-09-27 08:16:46 +03:00
|
|
|
#include "xs_openssl.h"
|
2022-09-27 19:20:25 +03:00
|
|
|
#include "xs_regex.h"
|
2022-10-02 10:27:17 +03:00
|
|
|
#include "xs_time.h"
|
2022-11-28 17:49:56 +03:00
|
|
|
#include "xs_set.h"
|
2022-09-23 18:33:33 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2023-01-13 15:25:14 +03:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2022-09-23 18:33:33 +03:00
|
|
|
const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
|
|
|
|
|
2023-01-27 20:17:11 +03:00
|
|
|
/* susie.png */
|
2023-01-27 20:21:54 +03:00
|
|
|
|
2023-01-27 20:17:11 +03:00
|
|
|
const char *susie =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
|
|
|
"CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
|
|
|
|
"vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
|
|
|
|
"eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
|
|
|
|
"AYTtEsDU9F34AAAAAElFTkSuQmCC";
|
|
|
|
|
2023-01-27 20:21:54 +03:00
|
|
|
const char *susie_cool =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
|
|
|
"CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
|
|
|
|
"qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
|
|
|
|
"+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
|
|
|
|
"ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
|
|
|
|
|
2023-05-20 20:25:52 +03:00
|
|
|
const char *susie_muertos =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
|
2023-05-20 20:33:57 +03:00
|
|
|
"CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
|
|
|
|
"ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
|
|
|
|
"xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
|
|
|
|
"QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
|
2023-05-20 20:25:52 +03:00
|
|
|
|
2023-01-27 20:17:11 +03:00
|
|
|
|
|
|
|
const char *default_avatar_base64(void)
|
|
|
|
/* returns the default avatar in base64 */
|
|
|
|
{
|
2023-01-27 20:28:55 +03:00
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm tm;
|
2023-05-20 20:25:52 +03:00
|
|
|
const char *p = susie;
|
2023-01-27 20:28:55 +03:00
|
|
|
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
|
2023-05-20 20:25:52 +03: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 20:17:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 06:33:54 +03:00
|
|
|
int activitypub_request(snac *snac, const char *url, xs_dict **data)
|
2022-09-23 18:33:33 +03:00
|
|
|
/* request an object */
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
xs *response = NULL;
|
2022-09-23 19:15:59 +03:00
|
|
|
xs *payload = NULL;
|
2022-09-23 18:33:33 +03:00
|
|
|
int p_size;
|
2022-09-23 19:15:59 +03:00
|
|
|
char *ctype;
|
2022-09-23 18:33:33 +03:00
|
|
|
|
|
|
|
/* get from the net */
|
|
|
|
response = http_signed_request(snac, "GET", url,
|
2023-01-24 17:06:58 +03:00
|
|
|
NULL, NULL, 0, &status, &payload, &p_size, 0);
|
2022-09-23 18:33:33 +03:00
|
|
|
|
2023-02-24 11:00:03 +03: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 15:30:29 +03:00
|
|
|
hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
|
|
|
|
hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
|
2023-02-24 11:00:03 +03:00
|
|
|
|
|
|
|
response = xs_http_request("GET", url, hdrs,
|
|
|
|
NULL, 0, &status, &payload, &p_size, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:33:33 +03:00
|
|
|
if (valid_status(status)) {
|
2022-09-23 19:15:59 +03:00
|
|
|
/* ensure it's ActivityPub data */
|
|
|
|
ctype = xs_dict_get(response, "content-type");
|
|
|
|
|
2022-09-27 10:38:46 +03:00
|
|
|
if (xs_str_in(ctype, "application/activity+json") != -1 ||
|
|
|
|
xs_str_in(ctype, "application/ld+json") != -1)
|
2022-09-23 19:15:59 +03:00
|
|
|
*data = xs_json_loads(payload);
|
|
|
|
else
|
|
|
|
status = 500;
|
2022-09-23 18:33:33 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 19:15:59 +03:00
|
|
|
if (!valid_status(status))
|
|
|
|
*data = NULL;
|
|
|
|
|
2022-09-23 18:33:33 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 06:33:54 +03:00
|
|
|
int actor_request(snac *snac, const char *actor, xs_dict **data)
|
2022-09-23 18:33:33 +03:00
|
|
|
/* request an actor */
|
|
|
|
{
|
2022-09-23 19:15:59 +03:00
|
|
|
int status, status2;
|
|
|
|
xs *payload = NULL;
|
2022-09-23 18:33:33 +03:00
|
|
|
|
2023-03-01 11:22:32 +03:00
|
|
|
if (data)
|
|
|
|
*data = NULL;
|
|
|
|
|
2022-09-23 18:33:33 +03:00
|
|
|
/* get from disk first */
|
|
|
|
status = actor_get(snac, actor, data);
|
|
|
|
|
2023-03-01 11:22:32 +03:00
|
|
|
if (status != 200) {
|
|
|
|
/* actor data non-existent or stale: get from the net */
|
|
|
|
status2 = activitypub_request(snac, actor, &payload);
|
|
|
|
|
|
|
|
if (valid_status(status2)) {
|
|
|
|
/* renew data */
|
2023-05-04 10:19:26 +03:00
|
|
|
status = actor_add(actor, payload);
|
2023-03-01 11:22:32 +03:00
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
*data = payload;
|
|
|
|
payload = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 18:33:33 +03:00
|
|
|
|
2023-03-08 12:14:40 +03: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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("NOT collected"));
|
2022-09-23 18:33:33 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2022-09-23 20:07:45 +03:00
|
|
|
|
|
|
|
|
2023-01-11 22:51:15 +03:00
|
|
|
int timeline_request(snac *snac, char **id, d_char **wrk)
|
2022-09-25 19:28:15 +03:00
|
|
|
/* ensures that an entry and its ancestors are in the timeline */
|
|
|
|
{
|
2022-09-27 15:50:13 +03:00
|
|
|
int status = 0;
|
|
|
|
|
2022-12-23 12:01:10 +03:00
|
|
|
if (!xs_is_null(*id)) {
|
2022-09-25 19:28:15 +03:00
|
|
|
/* is the admired object already there? */
|
2022-12-23 12:01:10 +03:00
|
|
|
if (!object_here(*id)) {
|
2022-09-25 19:28:15 +03:00
|
|
|
xs *object = NULL;
|
|
|
|
|
|
|
|
/* no; download it */
|
2022-12-23 12:01:10 +03:00
|
|
|
status = activitypub_request(snac, *id, &object);
|
2022-09-25 19:28:15 +03:00
|
|
|
|
|
|
|
if (valid_status(status)) {
|
2022-11-01 22:13:23 +03:00
|
|
|
char *type = xs_dict_get(object, "type");
|
2022-10-01 19:48:05 +03:00
|
|
|
|
2022-12-23 12:01:10 +03:00
|
|
|
/* get the id again from the object, as it may be different */
|
2022-12-23 23:30:18 +03:00
|
|
|
char *nid = xs_dict_get(object, "id");
|
2022-12-23 12:01:10 +03:00
|
|
|
|
2022-12-23 23:41:52 +03:00
|
|
|
if (wrk && strcmp(nid, *id) != 0) {
|
2022-12-23 14:32:59 +03:00
|
|
|
snac_debug(snac, 1,
|
2022-12-23 23:30:18 +03:00
|
|
|
xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
|
|
|
|
|
2022-12-23 23:41:52 +03:00
|
|
|
*wrk = xs_dup(nid);
|
|
|
|
*id = *wrk;
|
2022-12-23 23:30:18 +03:00
|
|
|
}
|
2022-12-23 12:01:10 +03:00
|
|
|
|
2022-11-01 22:13:23 +03:00
|
|
|
if (!xs_is_null(type) && strcmp(type, "Note") == 0) {
|
|
|
|
char *actor = xs_dict_get(object, "attributedTo");
|
2022-10-01 19:48:05 +03:00
|
|
|
|
2022-11-01 22:13:23 +03:00
|
|
|
/* request (and drop) the actor for this entry */
|
|
|
|
if (!xs_is_null(actor))
|
|
|
|
actor_request(snac, actor, NULL);
|
2022-09-25 19:28:15 +03:00
|
|
|
|
2022-11-01 22:13:23 +03:00
|
|
|
/* does it have an ancestor? */
|
|
|
|
char *in_reply_to = xs_dict_get(object, "inReplyTo");
|
2022-09-25 19:28:15 +03:00
|
|
|
|
2022-11-01 22:13:23 +03:00
|
|
|
/* recurse! */
|
2023-01-11 22:51:15 +03:00
|
|
|
timeline_request(snac, &in_reply_to, NULL);
|
2022-11-01 22:13:23 +03:00
|
|
|
|
|
|
|
/* finally store */
|
2023-01-11 22:40:13 +03:00
|
|
|
timeline_add(snac, *id, object);
|
2022-11-01 22:13:23 +03:00
|
|
|
}
|
2022-09-25 19:28:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 15:50:13 +03:00
|
|
|
|
|
|
|
return status;
|
2022-09-25 19:28:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 12:29:06 +03: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 20:07:45 +03:00
|
|
|
/* sends a message to an Inbox */
|
|
|
|
{
|
|
|
|
int status;
|
2023-02-07 12:29:06 +03:00
|
|
|
xs_dict *response;
|
|
|
|
xs *j_msg = xs_json_dumps_pp((xs_dict *)msg, 4);
|
2022-09-23 20:07:45 +03:00
|
|
|
|
2023-02-07 12:29:06 +03:00
|
|
|
response = http_signed_request_raw(keyid, seckey, "POST", inbox,
|
2023-01-24 17:06:58 +03:00
|
|
|
NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
|
2022-09-23 20:07:45 +03:00
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
xs_free(response);
|
2022-09-23 20:07:45 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 12:29:06 +03: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 */
|
|
|
|
{
|
|
|
|
char *seckey = xs_dict_get(snac->key, "secret");
|
|
|
|
|
|
|
|
return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 09:44:26 +03:00
|
|
|
d_char *get_actor_inbox(snac *snac, const char *actor)
|
2022-11-17 11:04:24 +03:00
|
|
|
/* gets an actor's inbox */
|
2022-09-23 20:07:45 +03:00
|
|
|
{
|
|
|
|
xs *data = NULL;
|
2022-11-17 11:04:24 +03:00
|
|
|
char *v = NULL;
|
2022-09-23 20:07:45 +03:00
|
|
|
|
2023-02-20 12:02:21 +03:00
|
|
|
if (valid_status(actor_request(snac, actor, &data))) {
|
2022-11-17 11:04:24 +03:00
|
|
|
/* try first endpoints/sharedInbox */
|
|
|
|
if ((v = xs_dict_get(data, "endpoints")))
|
|
|
|
v = xs_dict_get(v, "sharedInbox");
|
2022-09-23 20:07:45 +03:00
|
|
|
|
2022-11-17 11:04:24 +03:00
|
|
|
/* try then the regular inbox */
|
|
|
|
if (xs_is_null(v))
|
|
|
|
v = xs_dict_get(data, "inbox");
|
2022-09-23 20:07:45 +03:00
|
|
|
}
|
|
|
|
|
2022-11-17 11:04:24 +03:00
|
|
|
return xs_is_null(v) ? NULL : xs_dup(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-24 17:06:58 +03:00
|
|
|
int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout)
|
2022-11-17 11:04:24 +03:00
|
|
|
/* sends a message to an actor */
|
|
|
|
{
|
|
|
|
int status = 400;
|
|
|
|
xs *inbox = get_actor_inbox(snac, actor);
|
|
|
|
|
|
|
|
if (!xs_is_null(inbox))
|
2023-01-24 17:06:58 +03:00
|
|
|
status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
|
2022-11-17 11:04:24 +03:00
|
|
|
|
2022-09-23 20:07:45 +03:00
|
|
|
return status;
|
|
|
|
}
|
2022-09-23 20:37:01 +03:00
|
|
|
|
|
|
|
|
2023-04-11 10:50:12 +03:00
|
|
|
xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
|
2022-09-27 15:50:13 +03:00
|
|
|
/* returns the list of recipients for a message */
|
|
|
|
{
|
|
|
|
char *to = xs_dict_get(msg, "to");
|
|
|
|
char *cc = xs_dict_get(msg, "cc");
|
2022-11-28 17:49:56 +03:00
|
|
|
xs_set rcpts;
|
2022-09-27 15:50:13 +03:00
|
|
|
int n;
|
|
|
|
|
2022-11-28 17:49:56 +03:00
|
|
|
xs_set_init(&rcpts);
|
|
|
|
|
2022-09-27 15:50:13 +03:00
|
|
|
char *lists[] = { to, cc, NULL };
|
|
|
|
for (n = 0; lists[n]; n++) {
|
|
|
|
char *l = lists[n];
|
|
|
|
char *v;
|
2022-10-01 20:37:47 +03:00
|
|
|
xs *tl = NULL;
|
2022-09-27 15:50:13 +03:00
|
|
|
|
2022-10-01 20:37:47 +03:00
|
|
|
/* if it's a string, create a list with only one element */
|
2022-09-27 15:50:13 +03:00
|
|
|
if (xs_type(l) == XSTYPE_STRING) {
|
2022-10-01 20:37:47 +03:00
|
|
|
tl = xs_list_new();
|
|
|
|
tl = xs_list_append(tl, l);
|
|
|
|
|
|
|
|
l = tl;
|
2022-09-27 15:50:13 +03:00
|
|
|
}
|
2022-10-01 20:37:47 +03:00
|
|
|
|
2022-09-27 15:50:13 +03: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);
|
2022-11-28 12:46:42 +03:00
|
|
|
char *actor;
|
2022-09-27 15:50:13 +03:00
|
|
|
|
|
|
|
char *p = fwers;
|
2022-11-28 17:49:56 +03:00
|
|
|
while (xs_list_iter(&p, &actor))
|
|
|
|
xs_set_add(&rcpts, actor);
|
2022-09-27 15:50:13 +03:00
|
|
|
}
|
|
|
|
else
|
2022-11-28 17:49:56 +03:00
|
|
|
xs_set_add(&rcpts, v);
|
2022-09-27 15:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:49:56 +03:00
|
|
|
return xs_set_result(&rcpts);
|
2022-09-27 15:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 10:50:12 +03:00
|
|
|
int is_msg_public(snac *snac, const xs_dict *msg)
|
2022-09-27 15:50:13 +03:00
|
|
|
/* checks if a message is public */
|
|
|
|
{
|
|
|
|
xs *rcpts = recipient_list(snac, msg, 0);
|
|
|
|
|
2023-03-02 11:15:40 +03:00
|
|
|
return xs_list_in(rcpts, public_address) != -1;
|
2022-09-27 15:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 10:50:12 +03:00
|
|
|
int is_msg_for_me(snac *snac, const xs_dict *c_msg)
|
2023-03-06 16:28:53 +03:00
|
|
|
/* checks if this message is for me */
|
|
|
|
{
|
2023-03-31 20:09:59 +03:00
|
|
|
const char *type = xs_dict_get(c_msg, "type");
|
|
|
|
|
|
|
|
if (strcmp(type, "Announce") == 0) {
|
2023-04-06 00:23:19 +03:00
|
|
|
const char *object = xs_dict_get(c_msg, "object");
|
|
|
|
|
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
|
|
|
/* 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-03-31 20:09:59 +03:00
|
|
|
if (!following_check(snac, xs_dict_get(c_msg, "actor")))
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-06 16:28:53 +03:00
|
|
|
|
2023-03-06 22:07:44 +03:00
|
|
|
/* if it's not a Create, allow */
|
2023-03-31 20:09:59 +03:00
|
|
|
if (strcmp(type, "Create") != 0)
|
2023-03-06 22:07:44 +03:00
|
|
|
return 1;
|
2023-03-06 16:28:53 +03:00
|
|
|
|
2023-03-06 22:07:44 +03:00
|
|
|
xs_dict *msg = xs_dict_get(c_msg, "object");
|
|
|
|
xs *rcpts = recipient_list(snac, msg, 0);
|
|
|
|
xs_list *p = rcpts;
|
|
|
|
xs_str *v;
|
2023-03-06 16:28:53 +03:00
|
|
|
|
2023-03-06 22:07:44 +03:00
|
|
|
while(xs_list_iter(&p, &v)) {
|
2023-03-07 11:40:55 +03:00
|
|
|
/* explicitly for me? accept */
|
2023-03-06 22:07:44 +03:00
|
|
|
if (strcmp(v, snac->actor) == 0)
|
|
|
|
return 2;
|
2023-03-07 11:40:55 +03:00
|
|
|
|
|
|
|
/* for someone we follow? (probably cc'ed) accept */
|
|
|
|
if (following_check(snac, v))
|
|
|
|
return 5;
|
2023-03-06 16:28:53 +03:00
|
|
|
}
|
|
|
|
|
2023-03-07 11:40:55 +03:00
|
|
|
/* accept if it's by someone we follow */
|
2023-03-06 22:07:44 +03:00
|
|
|
char *atto = xs_dict_get(msg, "attributedTo");
|
|
|
|
|
|
|
|
if (!xs_is_null(atto) && following_check(snac, atto))
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
/* is this message a reply to another? */
|
|
|
|
char *irt = xs_dict_get(msg, "inReplyTo");
|
|
|
|
if (!xs_is_null(irt)) {
|
|
|
|
xs *r_msg = NULL;
|
|
|
|
|
2023-03-07 11:40:55 +03:00
|
|
|
/* try to get the replied message */
|
2023-03-06 22:07:44 +03:00
|
|
|
if (valid_status(object_get(irt, &r_msg))) {
|
|
|
|
atto = xs_dict_get(r_msg, "attributedTo");
|
|
|
|
|
|
|
|
/* accept if the replied message is from someone we follow */
|
|
|
|
if (!xs_is_null(atto) && following_check(snac, atto))
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-03-06 16:28:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-12 11:15:44 +03:00
|
|
|
void process_tags(snac *snac, const char *content, xs_str **n_content, xs_list **tag)
|
2022-09-27 19:20:25 +03:00
|
|
|
/* parses mentions and tags from content */
|
|
|
|
{
|
2023-05-12 11:15:44 +03:00
|
|
|
xs_str *nc = xs_str_new(NULL);
|
2023-05-12 11:33:59 +03:00
|
|
|
xs_list *tl = *tag;
|
2022-09-27 19:20:25 +03:00
|
|
|
xs *split;
|
2023-05-12 11:15:44 +03:00
|
|
|
xs_list *p;
|
|
|
|
xs_val *v;
|
2022-09-27 19:20:25 +03:00
|
|
|
int n = 0;
|
|
|
|
|
2023-05-12 20:01:53 +03: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"));
|
|
|
|
|
2023-05-12 12:56:17 +03:00
|
|
|
split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#[^ ,\\.:;<]+)");
|
2022-12-14 06:55:47 +03:00
|
|
|
|
|
|
|
p = split;
|
2022-09-27 19:20:25 +03:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if ((n & 0x1)) {
|
|
|
|
if (*v == '@') {
|
2023-05-12 12:56:17 +03:00
|
|
|
xs *link = NULL;
|
2023-05-12 20:01:53 +03:00
|
|
|
xs *wuid = NULL;
|
2023-05-12 12:56:17 +03:00
|
|
|
|
|
|
|
if (strchr(v + 1, '@') == NULL) {
|
|
|
|
/* only one @? it's a dumb Mastodon-like mention
|
2023-05-12 20:01:53 +03: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 12:56:17 +03:00
|
|
|
}
|
2023-05-12 20:01:53 +03:00
|
|
|
else
|
|
|
|
wuid = xs_dup(v);
|
|
|
|
|
|
|
|
/* query the webfinger about this fellow */
|
|
|
|
xs *actor = NULL;
|
|
|
|
xs *uid = NULL;
|
|
|
|
int status;
|
2022-09-27 19:20:25 +03:00
|
|
|
|
2023-05-12 20:01:53 +03:00
|
|
|
status = webfinger_request(wuid, &actor, &uid);
|
2022-09-27 19:20:25 +03:00
|
|
|
|
2023-05-12 20:01:53 +03:00
|
|
|
if (valid_status(status)) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
xs *n = xs_fmt("@%s", uid);
|
2022-09-27 19:20:25 +03:00
|
|
|
|
2023-05-12 20:01:53 +03: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 19:20:25 +03:00
|
|
|
|
2023-05-12 20:01:53 +03:00
|
|
|
tl = xs_list_append(tl, d);
|
2022-09-27 19:33:25 +03:00
|
|
|
|
2023-05-12 20:01:53 +03:00
|
|
|
link = xs_fmt("<a href=\"%s\" class=\"u-url mention\">%s</a>", actor, n);
|
2022-09-27 19:20:25 +03:00
|
|
|
}
|
2023-05-12 12:56:17 +03:00
|
|
|
|
|
|
|
if (!xs_is_null(link))
|
|
|
|
nc = xs_str_cat(nc, link);
|
2022-09-27 19:20:25 +03:00
|
|
|
else
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*v == '#') {
|
|
|
|
/* hashtag */
|
2023-01-12 15:49:37 +03:00
|
|
|
xs *d = xs_dict_new();
|
|
|
|
xs *n = xs_tolower_i(xs_dup(v));
|
|
|
|
xs *h = xs_fmt("%s%s", snac->actor, n);
|
|
|
|
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 19:20:25 +03:00
|
|
|
}
|
2023-01-17 11:37:41 +03:00
|
|
|
else
|
|
|
|
if (*v == '&') {
|
|
|
|
/* HTML Unicode entity, probably part of an emoji */
|
|
|
|
|
|
|
|
/* write as is */
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
}
|
2022-09-27 19:20:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nc = xs_str_cat(nc, v);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*n_content = nc;
|
|
|
|
*tag = tl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-24 11:43:57 +03:00
|
|
|
/** messages **/
|
|
|
|
|
2023-05-05 10:54:41 +03: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 11:43:57 +03:00
|
|
|
/* creates a base ActivityPub message */
|
|
|
|
{
|
2022-09-27 08:54:05 +03:00
|
|
|
xs *did = NULL;
|
|
|
|
xs *published = NULL;
|
2023-01-22 22:31:16 +03:00
|
|
|
xs *ntid = tid(0);
|
2022-09-27 08:54:05 +03:00
|
|
|
|
|
|
|
/* generated values */
|
2022-12-14 06:55:47 +03:00
|
|
|
if (date && strcmp(date, "@now") == 0) {
|
|
|
|
published = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
|
|
|
|
date = published;
|
|
|
|
}
|
2022-09-27 08:54:05 +03:00
|
|
|
|
|
|
|
if (id != NULL) {
|
|
|
|
if (strcmp(id, "@dummy") == 0) {
|
2022-12-14 06:55:47 +03:00
|
|
|
did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
|
|
|
|
|
|
|
|
id = did;
|
2022-09-27 08:54:05 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(id, "@object") == 0) {
|
2022-12-14 06:55:47 +03:00
|
|
|
if (object != NULL) {
|
2023-01-22 22:31:16 +03:00
|
|
|
did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
|
2022-12-14 06:55:47 +03:00
|
|
|
id = did;
|
|
|
|
}
|
2022-09-27 08:54:05 +03:00
|
|
|
else
|
|
|
|
id = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:54:41 +03:00
|
|
|
xs_dict *msg = xs_dict_new();
|
2022-09-24 11:43:57 +03: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 12:04:35 +03:00
|
|
|
if (actor != NULL)
|
2022-09-24 11:43:57 +03:00
|
|
|
msg = xs_dict_append(msg, "actor", actor);
|
2022-09-24 12:04:35 +03:00
|
|
|
|
2022-09-27 08:54:05 +03:00
|
|
|
if (date != NULL)
|
|
|
|
msg = xs_dict_append(msg, "published", date);
|
|
|
|
|
|
|
|
if (object != NULL)
|
|
|
|
msg = xs_dict_append(msg, "object", object);
|
2022-09-24 11:43:57 +03:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *msg_collection(snac *snac, char *id)
|
|
|
|
/* creates an empty OrderedCollection message */
|
|
|
|
{
|
2022-09-27 08:54:05 +03:00
|
|
|
d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
|
2022-09-24 11:43:57 +03:00
|
|
|
xs *ol = xs_list_new();
|
|
|
|
xs *nz = xs_number_new(0);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "attributedTo", snac->actor);
|
|
|
|
msg = xs_dict_append(msg, "orderedItems", ol);
|
|
|
|
msg = xs_dict_append(msg, "totalItems", nz);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-27 08:54:05 +03:00
|
|
|
d_char *msg_accept(snac *snac, char *object, char *to)
|
|
|
|
/* creates an Accept message (as a response to a Follow) */
|
|
|
|
{
|
|
|
|
d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", to);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-06 13:26:43 +03:00
|
|
|
xs_dict *msg_update(snac *snac, xs_dict *object)
|
2022-09-24 12:54:35 +03:00
|
|
|
/* creates an Update message */
|
2022-09-24 12:04:35 +03:00
|
|
|
{
|
2023-05-17 11:49:46 +03:00
|
|
|
xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
|
2022-09-24 12:04:35 +03:00
|
|
|
|
2023-03-06 13:26:43 +03:00
|
|
|
char *type = xs_dict_get(object, "type");
|
|
|
|
|
|
|
|
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 11:49:46 +03: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 13:26:43 +03:00
|
|
|
else
|
|
|
|
msg = xs_dict_append(msg, "to", public_address);
|
2022-09-24 12:04:35 +03:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-26 11:08:14 +03:00
|
|
|
d_char *msg_admiration(snac *snac, char *object, char *type)
|
|
|
|
/* creates a Like or Announce message */
|
|
|
|
{
|
2022-09-26 12:19:45 +03:00
|
|
|
xs *a_msg = NULL;
|
|
|
|
d_char *msg = NULL;
|
2022-12-23 23:41:52 +03:00
|
|
|
xs *wrk = NULL;
|
2022-09-26 11:08:14 +03:00
|
|
|
|
|
|
|
/* call the object */
|
2023-01-11 22:51:15 +03:00
|
|
|
timeline_request(snac, &object, &wrk);
|
2022-09-26 11:08:14 +03:00
|
|
|
|
2023-02-05 19:45:00 +03:00
|
|
|
if (valid_status(object_get(object, &a_msg))) {
|
2022-09-26 12:19:45 +03:00
|
|
|
xs *rcpts = xs_list_new();
|
2022-09-26 11:08:14 +03:00
|
|
|
|
2022-09-27 08:54:05 +03:00
|
|
|
msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
|
2022-09-26 12:19:45 +03:00
|
|
|
|
2023-03-06 13:16:15 +03:00
|
|
|
if (is_msg_public(snac, a_msg))
|
|
|
|
rcpts = xs_list_append(rcpts, public_address);
|
|
|
|
|
2022-09-26 12:19:45 +03:00
|
|
|
rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
|
|
|
|
|
2022-12-23 12:01:10 +03:00
|
|
|
msg = xs_dict_append(msg, "to", rcpts);
|
2022-09-26 12:19:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
|
2022-09-26 11:08:14 +03:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 22:02:47 +03:00
|
|
|
d_char *msg_actor(snac *snac)
|
|
|
|
/* create a Person message for this actor */
|
|
|
|
{
|
2022-09-27 15:07:36 +03:00
|
|
|
xs *ctxt = xs_list_new();
|
|
|
|
xs *icon = xs_dict_new();
|
|
|
|
xs *keys = xs_dict_new();
|
|
|
|
xs *avtr = NULL;
|
|
|
|
xs *kid = NULL;
|
|
|
|
xs *f_bio = NULL;
|
2022-09-27 08:54:05 +03:00
|
|
|
d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
|
2022-09-25 22:02:47 +03:00
|
|
|
char *p;
|
|
|
|
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"));
|
|
|
|
|
2023-05-21 21:32:23 +03:00
|
|
|
f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
|
2022-09-27 15:07:36 +03:00
|
|
|
msg = xs_dict_set(msg, "summary", f_bio);
|
2022-09-25 22:02:47 +03:00
|
|
|
|
2022-09-27 15:07:36 +03:00
|
|
|
char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
|
2022-09-25 22:02:47 +03: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);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-27 15:07:36 +03:00
|
|
|
d_char *msg_create(snac *snac, char *object)
|
|
|
|
/* creates a 'Create' message */
|
|
|
|
{
|
|
|
|
d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
|
|
|
|
msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
|
|
|
|
msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-01 10:12:33 +03:00
|
|
|
d_char *msg_undo(snac *snac, char *object)
|
|
|
|
/* creates an 'Undo' message */
|
|
|
|
{
|
|
|
|
d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-01 20:37:47 +03:00
|
|
|
d_char *msg_delete(snac *snac, char *id)
|
|
|
|
/* creates a 'Delete' + 'Tombstone' for a local entry */
|
|
|
|
{
|
|
|
|
xs *tomb = xs_dict_new();
|
|
|
|
d_char *msg = NULL;
|
|
|
|
|
|
|
|
/* 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 15:33:53 +03:00
|
|
|
xs_dict *msg_follow(snac *snac, const char *q)
|
2022-09-27 16:28:08 +03:00
|
|
|
/* creates a 'Follow' message */
|
|
|
|
{
|
2022-11-02 22:28:40 +03:00
|
|
|
xs *actor_o = NULL;
|
|
|
|
xs *actor = NULL;
|
2022-09-27 16:28:08 +03:00
|
|
|
d_char *msg = NULL;
|
|
|
|
int status;
|
|
|
|
|
2023-04-23 15:33:53 +03:00
|
|
|
xs *url_or_uid = xs_strip_i(xs_str_new(q));
|
|
|
|
|
2022-11-02 22:28:40 +03:00
|
|
|
if (xs_startswith(url_or_uid, "https:/"))
|
|
|
|
actor = xs_dup(url_or_uid);
|
|
|
|
else
|
2023-05-07 08:42:47 +03:00
|
|
|
if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
|
2022-11-02 22:28:40 +03:00
|
|
|
snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:28:08 +03:00
|
|
|
/* request the actor */
|
|
|
|
status = actor_request(snac, actor, &actor_o);
|
|
|
|
|
|
|
|
if (valid_status(status)) {
|
|
|
|
/* check if the actor is an alias */
|
|
|
|
char *r_actor = xs_dict_get(actor_o, "id");
|
|
|
|
|
|
|
|
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 23:36:55 +03:00
|
|
|
msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
|
2022-09-27 16:28:08 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-15 20:05:26 +03:00
|
|
|
xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
|
2023-02-20 11:32:44 +03:00
|
|
|
xs_str *in_reply_to, xs_list *attach, int priv)
|
2022-09-27 15:07:36 +03:00
|
|
|
/* creates a 'Note' message */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
|
2022-09-27 15:50:13 +03:00
|
|
|
xs *ctxt = NULL;
|
2022-09-27 19:33:25 +03:00
|
|
|
xs *fc2 = NULL;
|
2022-09-27 15:07:36 +03:00
|
|
|
xs *fc1 = NULL;
|
|
|
|
xs *to = NULL;
|
|
|
|
xs *cc = xs_list_new();
|
|
|
|
xs *irt = NULL;
|
2023-05-12 11:33:59 +03:00
|
|
|
xs *tag = xs_list_new();
|
2023-05-21 21:12:59 +03:00
|
|
|
xs *atls = xs_list_new();
|
2023-01-28 20:22:42 +03:00
|
|
|
xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
|
|
|
|
xs_list *p;
|
|
|
|
xs_val *v;
|
2022-09-27 15:07:36 +03:00
|
|
|
|
|
|
|
if (rcpts == NULL)
|
|
|
|
to = xs_list_new();
|
2022-11-02 12:13:14 +03: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 15:07:36 +03:00
|
|
|
|
|
|
|
/* format the content */
|
2023-05-21 21:32:23 +03:00
|
|
|
fc2 = not_really_markdown(content, &atls);
|
2022-09-27 19:33:25 +03:00
|
|
|
|
2022-12-23 10:05:00 +03:00
|
|
|
if (in_reply_to != NULL && *in_reply_to) {
|
2022-09-27 15:50:13 +03:00
|
|
|
xs *p_msg = NULL;
|
2022-12-23 23:41:52 +03:00
|
|
|
xs *wrk = NULL;
|
2022-09-27 15:50:13 +03:00
|
|
|
|
|
|
|
/* demand this thing */
|
2023-01-11 22:51:15 +03:00
|
|
|
timeline_request(snac, &in_reply_to, &wrk);
|
2022-09-27 15:50:13 +03:00
|
|
|
|
2023-02-05 19:45:00 +03:00
|
|
|
if (valid_status(object_get(in_reply_to, &p_msg))) {
|
2022-09-27 15:50:13 +03:00
|
|
|
/* add this author as recipient */
|
2022-11-22 22:31:22 +03:00
|
|
|
char *a, *v;
|
|
|
|
|
|
|
|
if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
|
|
|
|
to = xs_list_append(to, a);
|
|
|
|
|
|
|
|
/* add this author to the tag list as a mention */
|
2022-12-23 14:20:54 +03:00
|
|
|
xs *t_href = NULL;
|
|
|
|
xs *t_name = NULL;
|
2022-11-22 22:31:22 +03:00
|
|
|
|
|
|
|
if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
|
|
|
|
xs *t = xs_dict_new();
|
2022-09-27 15:50:13 +03:00
|
|
|
|
2022-11-22 22:31:22 +03:00
|
|
|
t = xs_dict_append(t, "type", "Mention");
|
|
|
|
t = xs_dict_append(t, "href", t_href);
|
|
|
|
t = xs_dict_append(t, "name", t_name);
|
2022-09-27 15:50:13 +03:00
|
|
|
|
2022-11-22 22:31:22 +03:00
|
|
|
tag = xs_list_append(tag, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the context, if there is one */
|
2022-09-27 15:50:13 +03:00
|
|
|
if ((v = xs_dict_get(p_msg, "context")))
|
|
|
|
ctxt = xs_dup(v);
|
|
|
|
|
|
|
|
/* if this message is public, ours will also be */
|
2023-02-20 11:32:44 +03:00
|
|
|
if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1)
|
2022-09-27 15:50:13 +03:00
|
|
|
to = xs_list_append(to, public_address);
|
|
|
|
}
|
|
|
|
|
2022-09-27 15:07:36 +03:00
|
|
|
irt = xs_dup(in_reply_to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
irt = xs_val_new(XSTYPE_NULL);
|
|
|
|
|
2023-05-12 11:33:59 +03:00
|
|
|
/* extract the mentions and hashtags and convert the content */
|
|
|
|
process_tags(snac, fc2, &fc1, &tag);
|
|
|
|
|
2022-10-10 10:03:07 +03:00
|
|
|
/* create the attachment list, if there are any */
|
2023-01-28 20:22:42 +03:00
|
|
|
if (!xs_is_null(attach)) {
|
2022-10-10 10:03:07 +03:00
|
|
|
while (xs_list_iter(&attach, &v)) {
|
2023-05-21 21:12:59 +03:00
|
|
|
xs *d = xs_dict_new();
|
2023-01-28 20:22:42 +03:00
|
|
|
char *url = xs_list_get(v, 0);
|
|
|
|
char *alt = xs_list_get(v, 1);
|
|
|
|
char *mime = xs_mime_by_ext(url);
|
2022-10-10 10:03:07 +03:00
|
|
|
|
|
|
|
d = xs_dict_append(d, "mediaType", mime);
|
2023-01-28 20:22:42 +03:00
|
|
|
d = xs_dict_append(d, "url", url);
|
|
|
|
d = xs_dict_append(d, "name", alt);
|
2022-10-10 10:03:07 +03:00
|
|
|
d = xs_dict_append(d, "type",
|
|
|
|
xs_startswith(mime, "image/") ? "Image" : "Document");
|
|
|
|
|
|
|
|
atls = xs_list_append(atls, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 15:50:13 +03:00
|
|
|
if (ctxt == NULL)
|
|
|
|
ctxt = xs_fmt("%s#ctxt", id);
|
|
|
|
|
2022-09-27 15:07:36 +03:00
|
|
|
/* add all mentions to the cc */
|
|
|
|
p = tag;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if (xs_type(v) == XSTYPE_DICT) {
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
|
|
|
|
if ((t = xs_dict_get(v, "href")) != NULL)
|
|
|
|
cc = xs_list_append(cc, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no recipients? must be for everybody */
|
2023-02-20 11:32:44 +03:00
|
|
|
if (!priv && xs_list_len(to) == 0)
|
2022-09-27 15:07:36 +03:00
|
|
|
to = xs_list_append(to, public_address);
|
|
|
|
|
2022-12-16 09:51:10 +03: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 15:07:36 +03: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 12:09:32 +03:00
|
|
|
msg = xs_dict_append(msg, "sourceContent", content);
|
|
|
|
|
2023-05-21 21:12:59 +03:00
|
|
|
if (xs_list_len(atls))
|
2022-10-10 10:03:07 +03:00
|
|
|
msg = xs_dict_append(msg, "attachment", atls);
|
|
|
|
|
2022-09-27 15:07:36 +03:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-05 10:54:41 +03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 12:07:47 +03:00
|
|
|
xs_dict *msg_question(snac *user, const char *content, const xs_list *opts, int multiple, int end_secs)
|
|
|
|
/* creates a Question message */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *id = xs_fmt("%s/q/%s", user->actor, ntid);
|
|
|
|
xs_dict *msg = msg_base(user, "Question", id, NULL, "@now", NULL);
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "content", content);
|
|
|
|
msg = xs_dict_append(msg, "attributedTo", user->actor);
|
|
|
|
|
|
|
|
xs *o = xs_list_new();
|
|
|
|
xs_list *p = (xs_list *)opts;
|
|
|
|
xs_str *v;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *d = xs_dict_new();
|
|
|
|
|
|
|
|
d = xs_dict_append(d, "name", v);
|
|
|
|
o = xs_list_append(o, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
|
|
|
|
|
|
|
|
/* set the end time */
|
|
|
|
time_t t = time(NULL) + end_secs;
|
|
|
|
xs *et = xs_str_utctime(t, "%Y-%m-%dT%H:%M:%SZ");
|
|
|
|
|
|
|
|
msg = xs_dict_append(msg, "endTime", msg);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-08 16:47:23 +03:00
|
|
|
void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg)
|
2022-10-21 10:41:29 +03:00
|
|
|
/* notifies the user of relevant events */
|
|
|
|
{
|
|
|
|
if (strcmp(type, "Create") == 0) {
|
|
|
|
/* only notify of notes specifically for us */
|
2022-11-07 21:37:05 +03:00
|
|
|
xs *rcpts = recipient_list(snac, msg, 0);
|
2022-10-21 10:41:29 +03:00
|
|
|
|
|
|
|
if (xs_list_in(rcpts, snac->actor) == -1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
|
|
|
|
return;
|
|
|
|
|
2023-04-13 16:32:13 +03:00
|
|
|
/* get the object id */
|
|
|
|
const char *objid = xs_dict_get(msg, "object");
|
2022-10-21 11:36:09 +03:00
|
|
|
|
2023-04-13 16:32:13 +03:00
|
|
|
if (xs_type(objid) == XSTYPE_DICT)
|
|
|
|
objid = xs_dict_get(objid, "id");
|
2022-10-21 11:36:09 +03:00
|
|
|
|
2023-04-13 16:32:13 +03:00
|
|
|
if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
|
|
|
|
/* if it's not an admiration about something by us, done */
|
|
|
|
if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
|
|
|
|
return;
|
2022-10-21 11:36:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:32:13 +03:00
|
|
|
/* user will love to know about this! */
|
|
|
|
|
2023-02-07 09:37:23 +03:00
|
|
|
/* prepare message body */
|
2023-02-08 16:47:23 +03:00
|
|
|
xs *body = xs_fmt("User : @%s@%s\n",
|
|
|
|
xs_dict_get(snac->config, "uid"),
|
|
|
|
xs_dict_get(srv_config, "host")
|
|
|
|
);
|
2022-10-21 11:26:45 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-10-21 10:41:29 +03:00
|
|
|
|
2022-10-21 11:26:45 +03:00
|
|
|
{
|
|
|
|
xs *s1 = xs_fmt("Actor : %s\n", actor);
|
|
|
|
body = xs_str_cat(body, s1);
|
|
|
|
}
|
2022-10-21 10:41:29 +03:00
|
|
|
|
2023-04-13 16:32:13 +03:00
|
|
|
if (objid != NULL) {
|
|
|
|
xs *s1 = xs_fmt("Object: %s\n", objid);
|
2022-10-21 11:36:09 +03:00
|
|
|
body = xs_str_cat(body, s1);
|
2022-10-21 10:41:29 +03:00
|
|
|
}
|
|
|
|
|
2023-02-07 09:37:23 +03:00
|
|
|
/* email */
|
|
|
|
|
2023-02-20 08:00:54 +03:00
|
|
|
const char *email = "[disabled by admin]";
|
2023-02-07 09:37:23 +03:00
|
|
|
|
2023-02-20 08:00:54 +03:00
|
|
|
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]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 08:12:23 +03:00
|
|
|
if (*email != '\0' && *email != '[') {
|
2023-02-07 09:37:23 +03:00
|
|
|
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 */
|
|
|
|
|
|
|
|
char *bot = xs_dict_get(snac->config, "telegram_bot");
|
|
|
|
char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
|
|
|
|
|
|
|
|
if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
|
|
|
|
enqueue_telegram(body, bot, chat_id);
|
2023-04-13 17:59:17 +03:00
|
|
|
|
|
|
|
/* finally, store it in the notification folder */
|
2023-04-14 14:05:36 +03:00
|
|
|
if (strcmp(type, "Follow") == 0)
|
|
|
|
objid = xs_dict_get(msg, "id");
|
|
|
|
|
2023-04-13 17:59:17 +03:00
|
|
|
notify_add(snac, type, utype, actor, objid);
|
2022-10-21 10:41:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-24 11:43:57 +03:00
|
|
|
/** queues **/
|
|
|
|
|
2023-03-06 16:28:53 +03:00
|
|
|
int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
|
2022-09-24 11:43:57 +03:00
|
|
|
/* processes an ActivityPub message from the input queue */
|
2022-09-24 00:49:09 +03:00
|
|
|
{
|
2022-09-25 08:58:25 +03:00
|
|
|
/* actor and type exist, were checked previously */
|
|
|
|
char *actor = xs_dict_get(msg, "actor");
|
|
|
|
char *type = xs_dict_get(msg, "type");
|
2022-09-28 06:16:17 +03:00
|
|
|
xs *actor_o = NULL;
|
2022-09-28 22:09:50 +03:00
|
|
|
int a_status;
|
2022-10-21 10:41:29 +03:00
|
|
|
int do_notify = 0;
|
2022-09-25 08:58:25 +03:00
|
|
|
|
2023-03-31 20:09:59 +03:00
|
|
|
if (xs_is_null(actor) || xs_is_null(type)) {
|
|
|
|
snac_debug(snac, 0, xs_fmt("malformed message"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-25 08:58:25 +03:00
|
|
|
char *object, *utype;
|
|
|
|
|
|
|
|
object = xs_dict_get(msg, "object");
|
2022-09-25 23:57:18 +03:00
|
|
|
if (object != NULL && xs_type(object) == XSTYPE_DICT)
|
2022-09-25 08:58:25 +03:00
|
|
|
utype = xs_dict_get(object, "type");
|
|
|
|
else
|
|
|
|
utype = "(null)";
|
2022-09-24 00:49:09 +03:00
|
|
|
|
2023-03-06 16:28:53 +03:00
|
|
|
/* reject messages that are not for this user */
|
2023-03-07 12:04:13 +03:00
|
|
|
if (!is_msg_for_me(snac, msg)) {
|
2023-04-02 12:17:51 +03:00
|
|
|
snac_debug(snac, 0, xs_fmt("message from %s of type '%s' not for us", actor, type));
|
2023-03-07 12:04:13 +03:00
|
|
|
|
|
|
|
return 1;
|
2023-03-06 16:28:53 +03:00
|
|
|
}
|
|
|
|
|
2023-05-17 12:24:47 +03:00
|
|
|
/* 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(snac, msg) &&
|
|
|
|
!following_check(snac, actor)) {
|
|
|
|
snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:16:17 +03:00
|
|
|
/* bring the actor */
|
2022-09-28 22:09:50 +03:00
|
|
|
a_status = actor_request(snac, actor, &actor_o);
|
|
|
|
|
2022-09-29 10:11:43 +03:00
|
|
|
/* if the actor does not explicitly exist, discard */
|
|
|
|
if (a_status == 404 || a_status == 410) {
|
|
|
|
snac_debug(snac, 1,
|
|
|
|
xs_fmt("dropping message due to actor error %s %d", actor, a_status));
|
|
|
|
|
2022-09-28 22:09:50 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_status(a_status)) {
|
2022-09-29 10:11:43 +03:00
|
|
|
/* other actor download errors may need a retry */
|
|
|
|
snac_debug(snac, 1,
|
2022-09-28 22:09:50 +03:00
|
|
|
xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
|
|
|
|
|
2022-09-28 21:41:07 +03:00
|
|
|
return 0;
|
2022-09-28 21:24:47 +03:00
|
|
|
}
|
2022-09-28 06:16:17 +03:00
|
|
|
|
2022-09-24 11:03:27 +03:00
|
|
|
/* check the signature */
|
2023-03-01 10:25:36 +03:00
|
|
|
xs *sig_err = NULL;
|
|
|
|
|
|
|
|
if (!check_signature(snac, req, &sig_err)) {
|
|
|
|
snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
|
|
|
|
|
|
|
|
srv_archive_error("check_signature", sig_err, req, msg);
|
|
|
|
|
2022-09-29 15:44:24 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2022-09-24 11:03:27 +03:00
|
|
|
|
2022-09-24 00:49:09 +03:00
|
|
|
if (strcmp(type, "Follow") == 0) {
|
2022-12-15 18:37:34 +03:00
|
|
|
if (!follower_check(snac, actor)) {
|
|
|
|
xs *f_msg = xs_dup(msg);
|
|
|
|
xs *reply = msg_accept(snac, f_msg, actor);
|
2022-09-27 08:54:05 +03:00
|
|
|
|
2022-12-16 09:16:00 +03:00
|
|
|
enqueue_message(snac, reply);
|
2022-09-27 08:54:05 +03:00
|
|
|
|
2022-12-15 18:37:34 +03:00
|
|
|
if (xs_is_null(xs_dict_get(f_msg, "published"))) {
|
|
|
|
/* add a date if it doesn't include one (Mastodon) */
|
|
|
|
xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
|
|
|
|
f_msg = xs_dict_set(f_msg, "published", date);
|
|
|
|
}
|
2022-09-30 19:12:32 +03:00
|
|
|
|
2023-01-11 22:40:13 +03:00
|
|
|
timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
|
2022-09-27 08:54:05 +03:00
|
|
|
|
2022-12-15 18:37:34 +03:00
|
|
|
follower_add(snac, actor);
|
2022-09-27 08:54:05 +03:00
|
|
|
|
2022-12-15 18:37:34 +03:00
|
|
|
snac_log(snac, xs_fmt("new follower %s", actor));
|
|
|
|
do_notify = 1;
|
|
|
|
}
|
|
|
|
else
|
2022-12-15 18:43:01 +03:00
|
|
|
snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Undo") == 0) {
|
2022-09-28 20:59:19 +03:00
|
|
|
if (strcmp(utype, "Follow") == 0) {
|
2022-10-21 10:41:29 +03:00
|
|
|
if (valid_status(follower_del(snac, actor))) {
|
2022-09-28 20:59:19 +03:00
|
|
|
snac_log(snac, xs_fmt("no longer following us %s", actor));
|
2022-10-21 10:41:29 +03:00
|
|
|
do_notify = 1;
|
|
|
|
}
|
2022-09-28 20:59:19 +03:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("error deleting follower %s", actor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Create") == 0) {
|
2023-05-24 10:43:11 +03:00
|
|
|
if (is_muted(snac, actor))
|
|
|
|
snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
|
|
|
|
|
2022-09-25 08:58:25 +03:00
|
|
|
if (strcmp(utype, "Note") == 0) {
|
2023-05-24 10:43:11 +03:00
|
|
|
char *id = xs_dict_get(object, "id");
|
|
|
|
char *in_reply_to = xs_dict_get(object, "inReplyTo");
|
|
|
|
xs *wrk = NULL;
|
2022-09-25 08:58:25 +03:00
|
|
|
|
2023-05-24 10:43:11 +03:00
|
|
|
timeline_request(snac, &in_reply_to, &wrk);
|
2022-09-25 08:58:25 +03:00
|
|
|
|
2023-05-24 10:43:11 +03:00
|
|
|
if (timeline_add(snac, id, object)) {
|
|
|
|
snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
|
|
|
|
do_notify = 1;
|
2022-09-25 08:58:25 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 10:43:11 +03:00
|
|
|
else
|
|
|
|
if (strcmp(utype, "Question") == 0) {
|
|
|
|
char *id = xs_dict_get(object, "id");
|
|
|
|
|
|
|
|
if (timeline_add(snac, id, object))
|
|
|
|
snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
|
|
|
|
}
|
2022-09-25 08:58:25 +03:00
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Accept") == 0) {
|
2022-09-27 16:38:09 +03:00
|
|
|
if (strcmp(utype, "Follow") == 0) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
2022-09-25 18:42:39 +03:00
|
|
|
if (strcmp(type, "Like") == 0) {
|
2022-09-26 08:19:45 +03:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2023-01-11 22:47:36 +03:00
|
|
|
timeline_admire(snac, object, actor, 1);
|
2022-09-26 08:19:45 +03:00
|
|
|
snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
|
2022-10-21 10:41:29 +03:00
|
|
|
do_notify = 1;
|
2022-09-25 18:42:39 +03:00
|
|
|
}
|
|
|
|
else
|
2022-09-25 19:28:15 +03:00
|
|
|
if (strcmp(type, "Announce") == 0) {
|
2022-09-28 06:16:17 +03:00
|
|
|
xs *a_msg = NULL;
|
2022-12-23 23:41:52 +03:00
|
|
|
xs *wrk = NULL;
|
2022-09-28 06:16:17 +03:00
|
|
|
|
2022-09-26 08:19:45 +03:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
2022-09-25 19:28:15 +03:00
|
|
|
|
2023-01-11 22:51:15 +03:00
|
|
|
timeline_request(snac, &object, &wrk);
|
2022-09-26 08:19:45 +03:00
|
|
|
|
2023-02-05 19:45:00 +03:00
|
|
|
if (valid_status(object_get(object, &a_msg))) {
|
2022-09-28 06:16:17 +03:00
|
|
|
char *who = xs_dict_get(a_msg, "attributedTo");
|
|
|
|
|
|
|
|
if (who && !is_muted(snac, who)) {
|
2022-09-28 08:12:16 +03:00
|
|
|
/* bring the actor */
|
|
|
|
xs *who_o = NULL;
|
|
|
|
|
|
|
|
if (valid_status(actor_request(snac, who, &who_o))) {
|
2023-01-11 22:47:36 +03:00
|
|
|
timeline_admire(snac, object, actor, 0);
|
2022-09-28 08:12:16 +03:00
|
|
|
snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
|
2022-10-21 10:41:29 +03:00
|
|
|
do_notify = 1;
|
2022-09-28 08:12:16 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
|
2022-09-28 06:16:17 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Update") == 0) {
|
2022-09-28 21:08:02 +03:00
|
|
|
if (strcmp(utype, "Person") == 0) {
|
2023-05-04 10:19:26 +03:00
|
|
|
actor_add(actor, xs_dict_get(msg, "object"));
|
2023-05-24 11:46:54 +03:00
|
|
|
timeline_touch(snac);
|
2022-11-25 14:09:30 +03:00
|
|
|
|
2022-09-28 21:08:02 +03:00
|
|
|
snac_log(snac, xs_fmt("updated actor %s", actor));
|
|
|
|
}
|
2022-12-15 13:06:31 +03:00
|
|
|
else
|
|
|
|
if (strcmp(utype, "Note") == 0) {
|
|
|
|
char *id = xs_dict_get(object, "id");
|
|
|
|
|
|
|
|
object_add_ow(id, object);
|
2023-05-24 11:46:54 +03:00
|
|
|
timeline_touch(snac);
|
2022-12-15 13:06:31 +03:00
|
|
|
|
|
|
|
snac_log(snac, xs_fmt("updated post %s", id));
|
|
|
|
}
|
2023-05-24 10:43:11 +03:00
|
|
|
else
|
|
|
|
if (strcmp(utype, "Question") == 0) {
|
|
|
|
char *id = xs_dict_get(object, "id");
|
|
|
|
|
|
|
|
object_add_ow(id, object);
|
2023-05-24 11:46:54 +03:00
|
|
|
timeline_touch(snac);
|
2023-05-24 10:43:11 +03:00
|
|
|
|
|
|
|
snac_log(snac, xs_fmt("updated poll %s", id));
|
|
|
|
}
|
2022-09-28 21:08:02 +03:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Delete") == 0) {
|
2022-10-01 20:48:13 +03:00
|
|
|
if (xs_type(object) == XSTYPE_DICT)
|
|
|
|
object = xs_dict_get(object, "id");
|
|
|
|
|
2022-10-20 11:34:32 +03:00
|
|
|
if (valid_status(timeline_del(snac, object)))
|
2022-12-08 10:19:24 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
|
2022-10-20 11:34:32 +03:00
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
2022-09-25 19:28:15 +03:00
|
|
|
else
|
2023-05-05 10:54:41 +03:00
|
|
|
if (strcmp(type, "Pong") == 0) {
|
|
|
|
snac_log(snac, xs_fmt("'Pong' received from %s", actor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "Ping") == 0) {
|
|
|
|
snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
|
|
|
|
|
|
|
|
xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
|
|
|
|
|
|
|
|
enqueue_output_by_actor(snac, rsp, actor, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
|
2022-09-28 21:41:07 +03:00
|
|
|
|
2023-04-14 20:17:16 +03:00
|
|
|
if (do_notify) {
|
2022-10-21 10:41:29 +03:00
|
|
|
notify(snac, type, utype, actor, msg);
|
|
|
|
|
2023-04-14 20:17:16 +03:00
|
|
|
timeline_touch(snac);
|
|
|
|
}
|
|
|
|
|
2022-09-28 21:41:07 +03:00
|
|
|
return 1;
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-13 15:25:14 +03:00
|
|
|
int send_email(char *msg)
|
|
|
|
/* 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 06:55:58 +03:00
|
|
|
void process_user_queue_item(snac *snac, xs_dict *q_item)
|
2023-02-02 07:07:20 +03:00
|
|
|
/* processes an item from the user queue */
|
2022-09-23 20:37:01 +03:00
|
|
|
{
|
2023-02-01 00:30:34 +03:00
|
|
|
char *type;
|
2022-09-23 20:37:01 +03:00
|
|
|
int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
|
|
|
|
|
2023-02-01 00:30:34 +03:00
|
|
|
if ((type = xs_dict_get(q_item, "type")) == NULL)
|
|
|
|
type = "output";
|
2022-09-23 20:37:01 +03:00
|
|
|
|
2023-02-01 00:30:34 +03:00
|
|
|
if (strcmp(type, "message") == 0) {
|
|
|
|
xs_dict *msg = xs_dict_get(q_item, "message");
|
2023-02-20 15:19:29 +03:00
|
|
|
xs *rcpts = recipient_list(snac, msg, 1);
|
|
|
|
xs_set inboxes;
|
2023-02-01 00:30:34 +03:00
|
|
|
xs_list *p;
|
2023-02-20 15:19:29 +03:00
|
|
|
xs_str *actor;
|
2022-09-23 20:37:01 +03:00
|
|
|
|
2023-02-20 15:19:29 +03:00
|
|
|
xs_set_init(&inboxes);
|
|
|
|
|
2023-03-03 08:04:40 +03:00
|
|
|
/* iterate the recipients */
|
2023-02-20 15:19:29 +03:00
|
|
|
p = rcpts;
|
|
|
|
while (xs_list_iter(&p, &actor)) {
|
|
|
|
xs *inbox = get_actor_inbox(snac, actor);
|
|
|
|
|
|
|
|
if (inbox != NULL) {
|
|
|
|
/* add to the set and, if it's not there, send message */
|
|
|
|
if (xs_set_add(&inboxes, inbox) == 1)
|
|
|
|
enqueue_output(snac, msg, inbox, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
|
2022-09-25 22:55:29 +03:00
|
|
|
}
|
2023-02-20 15:19:29 +03:00
|
|
|
|
2023-03-03 08:04:40 +03:00
|
|
|
/* if it's public, send to the collected inboxes */
|
|
|
|
if (is_msg_public(snac, msg)) {
|
|
|
|
xs *shibx = inbox_list();
|
|
|
|
xs_str *inbox;
|
|
|
|
|
|
|
|
p = shibx;
|
|
|
|
while (xs_list_iter(&p, &inbox)) {
|
|
|
|
if (xs_set_add(&inboxes, inbox) == 1)
|
|
|
|
enqueue_output(snac, msg, inbox, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:19:29 +03:00
|
|
|
xs_set_free(&inboxes);
|
2023-02-01 00:30:34 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(type, "input") == 0) {
|
|
|
|
/* process the message */
|
|
|
|
xs_dict *msg = xs_dict_get(q_item, "message");
|
|
|
|
xs_dict *req = xs_dict_get(q_item, "req");
|
|
|
|
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 15:19:27 +03:00
|
|
|
snac_log(snac, xs_fmt("input giving up"));
|
2023-02-01 00:30:34 +03:00
|
|
|
else {
|
|
|
|
/* reenqueue */
|
|
|
|
enqueue_input(snac, msg, req, retries + 1);
|
2023-02-07 15:19:27 +03:00
|
|
|
snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
|
2022-09-28 21:41:07 +03:00
|
|
|
}
|
2022-09-24 00:49:09 +03:00
|
|
|
}
|
2023-02-01 00:30:34 +03:00
|
|
|
}
|
2023-02-07 15:19:27 +03:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("unexpected q_item type '%s'", type));
|
2022-09-23 20:37:01 +03:00
|
|
|
}
|
2022-09-23 21:28:23 +03:00
|
|
|
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
int process_user_queue(snac *snac)
|
2023-02-02 06:50:51 +03:00
|
|
|
/* processes a user's queue */
|
2023-02-01 00:30:34 +03:00
|
|
|
{
|
2023-02-22 10:39:54 +03:00
|
|
|
int cnt = 0;
|
2023-02-02 06:47:59 +03:00
|
|
|
xs *list = user_queue(snac);
|
2023-02-01 00:30:34 +03:00
|
|
|
|
|
|
|
xs_list *p = list;
|
|
|
|
xs_str *fn;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
2023-02-02 06:44:30 +03:00
|
|
|
xs *q_item = dequeue(fn);
|
2023-02-01 00:30:34 +03:00
|
|
|
|
|
|
|
if (q_item == NULL) {
|
2023-02-02 07:07:20 +03:00
|
|
|
snac_log(snac, xs_fmt("process_user_queue q_item error"));
|
2023-02-01 00:30:34 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-02 06:55:58 +03:00
|
|
|
process_user_queue_item(snac, q_item);
|
2023-02-22 10:39:54 +03:00
|
|
|
cnt++;
|
2023-02-01 00:30:34 +03:00
|
|
|
}
|
2023-02-22 10:39:54 +03:00
|
|
|
|
|
|
|
return cnt;
|
2023-02-01 00:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 07:07:20 +03:00
|
|
|
void process_queue_item(xs_dict *q_item)
|
|
|
|
/* processes an item from the global queue */
|
|
|
|
{
|
2023-02-02 07:21:16 +03:00
|
|
|
char *type = xs_dict_get(q_item, "type");
|
|
|
|
int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
|
|
|
|
|
2023-02-07 15:31:48 +03:00
|
|
|
if (strcmp(type, "output") == 0) {
|
|
|
|
int status;
|
|
|
|
xs_str *inbox = xs_dict_get(q_item, "inbox");
|
|
|
|
xs_str *keyid = xs_dict_get(q_item, "keyid");
|
|
|
|
xs_str *seckey = xs_dict_get(q_item, "seckey");
|
|
|
|
xs_dict *msg = xs_dict_get(q_item, "message");
|
|
|
|
int retries = xs_number_get(xs_dict_get(q_item, "retries"));
|
|
|
|
xs *payload = NULL;
|
|
|
|
int p_size = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deliver */
|
|
|
|
status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
|
|
|
|
|
2023-02-14 10:26:39 +03:00
|
|
|
if (payload) {
|
2023-03-02 14:30:00 +03:00
|
|
|
if (p_size > 64) {
|
2023-02-14 10:15:38 +03:00
|
|
|
/* trim the message */
|
2023-03-02 14:30:00 +03:00
|
|
|
payload[64] = '\0';
|
2023-02-14 10:15:38 +03:00
|
|
|
payload = xs_str_cat(payload, "...");
|
|
|
|
}
|
|
|
|
|
2023-02-14 10:49:17 +03:00
|
|
|
/* strip ugly control characters */
|
|
|
|
payload = xs_replace_i(payload, "\n", "");
|
|
|
|
payload = xs_replace_i(payload, "\r", "");
|
2023-02-14 10:15:38 +03:00
|
|
|
|
2023-02-14 10:26:39 +03:00
|
|
|
if (*payload)
|
|
|
|
payload = xs_str_wrap_i(" [", payload, "]");
|
|
|
|
}
|
|
|
|
else
|
2023-02-14 10:15:38 +03:00
|
|
|
payload = xs_str_new(NULL);
|
|
|
|
|
|
|
|
srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
|
2023-02-07 15:31:48 +03:00
|
|
|
|
|
|
|
if (!valid_status(status)) {
|
|
|
|
retries++;
|
|
|
|
|
|
|
|
/* error sending; requeue? */
|
|
|
|
if (status == 404 || status == 410)
|
|
|
|
/* 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 */
|
|
|
|
enqueue_output_raw(keyid, seckey, msg, inbox, retries);
|
|
|
|
srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-02-02 07:21:16 +03:00
|
|
|
if (strcmp(type, "email") == 0) {
|
|
|
|
/* send this email */
|
|
|
|
xs_str *msg = xs_dict_get(q_item, "message");
|
|
|
|
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 09:37:23 +03:00
|
|
|
srv_log(xs_fmt("email giving up (errno: %d)", errno));
|
2023-02-02 07:21:16 +03:00
|
|
|
else {
|
|
|
|
/* requeue */
|
|
|
|
srv_log(xs_fmt(
|
2023-02-07 09:37:23 +03:00
|
|
|
"email requeue #%d (errno: %d)", retries, errno));
|
2023-02-02 07:21:16 +03:00
|
|
|
|
|
|
|
enqueue_email(msg, retries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 22:07:29 +03:00
|
|
|
else
|
2023-02-07 09:37:23 +03:00
|
|
|
if (strcmp(type, "telegram") == 0) {
|
|
|
|
/* send this via telegram */
|
|
|
|
char *bot = xs_dict_get(q_item, "bot");
|
|
|
|
char *msg = xs_dict_get(q_item, "message");
|
|
|
|
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 10:34:33 +03:00
|
|
|
xs *rsp = xs_http_request("POST", url, headers,
|
2023-02-07 09:37:23 +03:00
|
|
|
body, strlen(body), &status, NULL, NULL, 0);
|
2023-05-04 10:34:33 +03:00
|
|
|
rsp = xs_free(rsp);
|
2023-02-07 09:37:23 +03:00
|
|
|
|
|
|
|
srv_debug(0, xs_fmt("telegram post %d", status));
|
|
|
|
}
|
|
|
|
else
|
2023-02-06 22:07:29 +03:00
|
|
|
if (strcmp(type, "purge") == 0) {
|
|
|
|
srv_log(xs_dup("purge start"));
|
|
|
|
|
|
|
|
purge_all();
|
|
|
|
|
|
|
|
srv_log(xs_dup("purge end"));
|
|
|
|
}
|
2023-02-07 15:19:27 +03:00
|
|
|
else
|
|
|
|
srv_log(xs_fmt("unexpected q_item type '%s'", type));
|
2023-02-02 07:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
int process_queue(void)
|
2023-02-02 07:07:20 +03:00
|
|
|
/* processes the global queue */
|
|
|
|
{
|
2023-02-22 10:39:54 +03:00
|
|
|
int cnt = 0;
|
2023-02-02 07:07:20 +03:00
|
|
|
xs *list = queue();
|
|
|
|
|
|
|
|
xs_list *p = list;
|
|
|
|
xs_str *fn;
|
|
|
|
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
|
|
|
xs *q_item = dequeue(fn);
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
if (q_item != NULL) {
|
2023-03-02 14:38:02 +03:00
|
|
|
job_post(q_item, 0);
|
2023-02-22 10:39:54 +03:00
|
|
|
cnt++;
|
|
|
|
}
|
2023-02-02 07:07:20 +03:00
|
|
|
}
|
2023-02-22 10:39:54 +03:00
|
|
|
|
|
|
|
return cnt;
|
2023-02-02 07:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-26 10:22:05 +03:00
|
|
|
/** HTTP handlers */
|
|
|
|
|
2023-05-04 10:25:09 +03:00
|
|
|
int activitypub_get_handler(const xs_dict *req, const char *q_path,
|
2022-09-23 21:59:19 +03:00
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
{
|
|
|
|
int status = 200;
|
2022-09-25 08:42:57 +03:00
|
|
|
char *accept = xs_dict_get(req, "accept");
|
2022-09-23 21:59:19 +03:00
|
|
|
snac snac;
|
2022-09-24 12:04:35 +03:00
|
|
|
xs *msg = NULL;
|
2022-09-23 21:59:19 +03:00
|
|
|
|
2022-09-24 00:09:09 +03:00
|
|
|
if (accept == NULL)
|
2022-10-16 20:00:17 +03:00
|
|
|
return 0;
|
2022-09-24 00:09:09 +03:00
|
|
|
|
2022-09-23 21:59:19 +03: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);
|
|
|
|
char *uid, *p_path;
|
|
|
|
|
|
|
|
uid = xs_list_get(l, 1);
|
|
|
|
if (!user_open(&snac, uid)) {
|
|
|
|
/* invalid user */
|
2023-02-03 18:51:59 +03:00
|
|
|
srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
|
2022-09-23 21:59:19 +03:00
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_path = xs_list_get(l, 2);
|
|
|
|
|
2022-09-26 13:29:26 +03:00
|
|
|
*ctype = "application/activity+json";
|
|
|
|
|
2022-09-23 21:59:19 +03:00
|
|
|
if (p_path == NULL) {
|
|
|
|
/* if there was no component after the user, it's an actor request */
|
2022-09-25 22:02:47 +03:00
|
|
|
msg = msg_actor(&snac);
|
2022-12-01 21:38:11 +03:00
|
|
|
*ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
|
2022-12-16 10:35:52 +03:00
|
|
|
|
2023-03-02 17:34:04 +03:00
|
|
|
char *ua = xs_dict_get(req, "user-agent");
|
|
|
|
|
|
|
|
snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
|
2022-09-23 21:59:19 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(p_path, "outbox") == 0) {
|
2022-09-24 11:43:57 +03:00
|
|
|
xs *id = xs_fmt("%s/outbox", snac.actor);
|
2022-12-04 23:28:29 +03:00
|
|
|
xs *elems = timeline_simple_list(&snac, "public", 0, 20);
|
2022-09-28 05:48:23 +03:00
|
|
|
xs *list = xs_list_new();
|
2022-09-24 11:43:57 +03:00
|
|
|
msg = msg_collection(&snac, id);
|
2022-09-28 05:48:23 +03:00
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = elems;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-12-02 21:30:59 +03:00
|
|
|
xs *i = NULL;
|
2022-09-28 05:48:23 +03:00
|
|
|
|
2023-02-05 19:45:00 +03:00
|
|
|
if (valid_status(object_get_by_md5(v, &i))) {
|
2022-12-02 21:30:59 +03:00
|
|
|
char *type = xs_dict_get(i, "type");
|
|
|
|
char *id = xs_dict_get(i, "id");
|
|
|
|
|
|
|
|
if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
|
|
|
|
i = xs_dict_del(i, "_snac");
|
|
|
|
list = xs_list_append(list, i);
|
|
|
|
}
|
2022-09-28 05:52:17 +03:00
|
|
|
}
|
2022-09-28 05:48:23 +03:00
|
|
|
}
|
2022-09-24 12:04:35 +03:00
|
|
|
|
|
|
|
/* replace the 'orderedItems' with the latest posts */
|
2022-10-25 11:19:42 +03:00
|
|
|
xs *items = xs_number_new(xs_list_len(list));
|
2022-09-28 05:48:23 +03:00
|
|
|
msg = xs_dict_set(msg, "orderedItems", list);
|
2022-10-25 11:19:42 +03:00
|
|
|
msg = xs_dict_set(msg, "totalItems", items);
|
2022-09-23 21:59:19 +03: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 11:43:57 +03:00
|
|
|
msg = msg_collection(&snac, id);
|
2022-09-23 21:59:19 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (xs_startswith(p_path, "p/")) {
|
2022-09-27 20:00:24 +03:00
|
|
|
xs *id = xs_fmt("%s/%s", snac.actor, p_path);
|
|
|
|
|
2023-02-05 19:45:00 +03:00
|
|
|
status = object_get(id, &msg);
|
2022-09-23 21:59:19 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 404;
|
|
|
|
|
2022-09-24 12:04:35 +03:00
|
|
|
if (status == 200 && msg != NULL) {
|
2022-09-23 21:59:19 +03:00
|
|
|
*body = xs_json_dumps_pp(msg, 4);
|
|
|
|
*b_size = strlen(*body);
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:00:24 +03:00
|
|
|
snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
|
|
|
|
|
2022-09-23 21:59:19 +03:00
|
|
|
user_free(&snac);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-04 10:25:09 +03:00
|
|
|
int activitypub_post_handler(const xs_dict *req, const char *q_path,
|
|
|
|
char *payload, int p_size,
|
2022-09-23 21:28:23 +03:00
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
/* processes an input message */
|
|
|
|
{
|
2023-05-04 10:28:36 +03:00
|
|
|
(void)b_size;
|
|
|
|
|
2022-09-24 00:09:09 +03:00
|
|
|
int status = 202; /* accepted */
|
2022-09-25 08:42:57 +03:00
|
|
|
char *i_ctype = xs_dict_get(req, "content-type");
|
2022-09-23 21:28:23 +03:00
|
|
|
snac snac;
|
2022-09-27 08:16:46 +03:00
|
|
|
char *v;
|
2022-09-23 21:28:23 +03:00
|
|
|
|
2023-02-14 10:15:38 +03:00
|
|
|
if (i_ctype == NULL) {
|
|
|
|
*body = xs_str_new("no content-type");
|
|
|
|
*ctype = "text/plain";
|
2022-09-24 00:09:09 +03:00
|
|
|
return 400;
|
2023-02-14 10:15:38 +03:00
|
|
|
}
|
2022-09-24 00:09:09 +03:00
|
|
|
|
2022-09-23 21:28:23 +03: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 12:54:35 +03:00
|
|
|
/* decode the message */
|
|
|
|
xs *msg = xs_json_loads(payload);
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
|
2023-02-14 10:15:38 +03:00
|
|
|
|
|
|
|
*body = xs_str_new("JSON error");
|
|
|
|
*ctype = "text/plain";
|
2022-09-24 12:54:35 +03:00
|
|
|
status = 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the user and path */
|
2022-09-23 21:28:23 +03:00
|
|
|
xs *l = xs_split_n(q_path, "/", 2);
|
|
|
|
char *uid;
|
|
|
|
|
|
|
|
if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
|
|
|
|
/* strange q_path */
|
2022-09-24 12:54:35 +03:00
|
|
|
srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
|
2022-09-23 21:28:23 +03:00
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid = xs_list_get(l, 1);
|
|
|
|
if (!user_open(&snac, uid)) {
|
|
|
|
/* invalid user */
|
2022-09-24 12:54:35 +03:00
|
|
|
srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
|
2022-09-23 21:28:23 +03:00
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
2022-09-27 08:16:46 +03: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 20:00:24 +03:00
|
|
|
if (strcmp(s2, v) != 0) {
|
2022-09-27 08:16:46 +03:00
|
|
|
srv_log(xs_fmt("digest check FAILED"));
|
2023-02-14 10:15:38 +03:00
|
|
|
|
|
|
|
*body = xs_str_new("bad digest");
|
|
|
|
*ctype = "text/plain";
|
2022-09-27 20:00:24 +03:00
|
|
|
status = 400;
|
|
|
|
}
|
2022-09-27 08:16:46 +03:00
|
|
|
}
|
|
|
|
|
2023-03-04 02:26:50 +03: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)) {
|
|
|
|
srv_log(xs_fmt("rejected message from MUTEd actor %s", v));
|
|
|
|
|
|
|
|
*body = xs_str_new("rejected");
|
|
|
|
*ctype = "text/plain";
|
|
|
|
status = 403;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:00:24 +03:00
|
|
|
if (valid_status(status)) {
|
2022-09-28 21:41:07 +03:00
|
|
|
enqueue_input(&snac, msg, req, 0);
|
2022-09-27 20:00:24 +03:00
|
|
|
*ctype = "application/activity+json";
|
|
|
|
}
|
2022-09-24 00:09:09 +03:00
|
|
|
|
2022-09-23 21:28:23 +03:00
|
|
|
user_free(&snac);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|