2.90
activitypub.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_json.h"
6#include "xs_curl.h"
7#include "xs_url.h"
8#include "xs_mime.h"
9#include "xs_openssl.h"
10#include "xs_regex.h"
11#include "xs_time.h"
12#include "xs_set.h"
13#include "xs_match.h"
14#include "xs_unicode.h"
15#include "xs_webmention.h"
16#include "xs_http.h"
17
18#include "snac.h"
19
20#include <stddef.h>
21#include <sys/wait.h>
22
23const char * const public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
24
25/* susie.png */
26
27const char * const susie =
28 "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
29 "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
30 "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
31 "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
32 "AYTtEsDU9F34AAAAAElFTkSuQmCC";
33
34const char * const susie_cool =
35 "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
36 "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
37 "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
38 "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
39 "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
40
41const char * const susie_muertos =
42 "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
43 "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
44 "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
45 "xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
46 "QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
47
48
49const char *default_avatar_base64(void)
50/* returns the default avatar in base64 */
51{
52 time_t t = time(NULL);
53 struct tm tm;
54 const char *p = susie;
55
56 gmtime_r(&t, &tm);
57
58 if (tm.tm_mon == 10 && tm.tm_mday == 2)
59 p = susie_muertos;
60 else
61 if (tm.tm_wday == 0 || tm.tm_wday == 6)
62 p = susie_cool;
63
64 return p;
65}
66
67
68int activitypub_request(snac *user, const char *url, xs_dict **data)
69/* request an object */
70{
71 int status = 0;
72 xs *response = NULL;
73 xs *payload = NULL;
74 int p_size;
75 const char *ctype;
76
77 *data = NULL;
78
79 if (user != NULL) {
80 /* get from the net */
81 response = http_signed_request(user, "GET", url,
82 NULL, NULL, 0, &status, &payload, &p_size, 0);
83 }
84
85 if (status == 0 || (status >= 500 && status <= 599)) {
86 /* I found an instance running Misskey that returned
87 500 on signed messages but returned the object
88 perfectly without signing (?), so why not try */
89 xs_free(response);
90
91 xs *hdrs = xs_dict_new();
92 hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
93 hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
94
95 response = xs_http_request("GET", url, hdrs,
96 NULL, 0, &status, &payload, &p_size, 0);
97 }
98
99 if (valid_status(status)) {
100 /* ensure it's ActivityPub data */
101 ctype = xs_dict_get(response, "content-type");
102
103 if (xs_is_null(ctype))
104 status = HTTP_STATUS_BAD_REQUEST;
105 else
106 if (xs_str_in(ctype, "application/activity+json") != -1 ||
107 xs_str_in(ctype, "application/ld+json") != -1) {
108
109 /* if there is no payload, fail */
110 if (xs_is_null(payload))
111 status = HTTP_STATUS_BAD_REQUEST;
112 else
113 *data = xs_json_loads(payload);
114 }
115 else
116 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
117 }
118
119 return status;
120}
121
122
123static xs_dict *actor_get_collections(snac *user, xs_dict *actor, int throttle)
124/* fetches follower/following/statuses counts from an actor's collections and adds them to the actor object */
125{
126 /* only fetch if counts are not already present in the actor object */
127 const xs_number *existing_followers = xs_dict_get(actor, "followers_count");
128 const xs_number *existing_following = xs_dict_get(actor, "following_count");
129 const xs_number *existing_statuses = xs_dict_get(actor, "statuses_count");
130
131 /* skip if we already have all counts */
132 if (xs_type(existing_followers) == XSTYPE_NUMBER &&
133 xs_type(existing_following) == XSTYPE_NUMBER &&
134 xs_type(existing_statuses) == XSTYPE_NUMBER) {
135 return actor;
136 }
137
138 /* CRITICAL: duplicate URLs BEFORE any xs_dict_set calls, as xs_dict_set can reallocate the dict */
139 xs *followers_url = xs_dup(xs_dict_get(actor, "followers"));
140 xs *following_url = xs_dup(xs_dict_get(actor, "following"));
141 xs *outbox_url = xs_dup(xs_dict_get(actor, "outbox"));
142
143 /* only fetch followers count if not already present */
144 if (xs_type(existing_followers) != XSTYPE_NUMBER && !xs_is_null(followers_url)) {
145 xs *followers_coll = NULL;
146 if (valid_status(activitypub_request(user, followers_url, &followers_coll))) {
147 const xs_number *total = xs_dict_get(followers_coll, "totalItems");
148 if (xs_type(total) == XSTYPE_NUMBER) {
149 xs *total_copy = xs_dup(total);
150 actor = xs_dict_set(actor, "followers_count", total_copy);
151 }
152 }
153 /* throttle to prevent resource exhaustion on low-power devices */
154 if (throttle)
155 usleep(100000); /* 100ms delay between requests */
156 }
157
158 /* only fetch following count if not already present */
159 if (xs_type(existing_following) != XSTYPE_NUMBER && !xs_is_null(following_url)) {
160 xs *following_coll = NULL;
161 if (valid_status(activitypub_request(user, following_url, &following_coll))) {
162 const xs_number *total = xs_dict_get(following_coll, "totalItems");
163 if (xs_type(total) == XSTYPE_NUMBER) {
164 xs *total_copy = xs_dup(total);
165 actor = xs_dict_set(actor, "following_count", total_copy);
166 }
167 }
168 /* throttle to prevent resource exhaustion on low-power devices */
169 if (throttle)
170 usleep(100000); /* 100ms delay between requests */
171 }
172
173 /* only fetch statuses count if not already present */
174 if (xs_type(existing_statuses) != XSTYPE_NUMBER && !xs_is_null(outbox_url)) {
175 xs *outbox_coll = NULL;
176 if (valid_status(activitypub_request(user, outbox_url, &outbox_coll))) {
177 const xs_number *total = xs_dict_get(outbox_coll, "totalItems");
178 if (xs_type(total) == XSTYPE_NUMBER) {
179 xs *total_copy = xs_dup(total);
180 actor = xs_dict_set(actor, "statuses_count", total_copy);
181 }
182 }
183 /* throttle to prevent resource exhaustion on low-power devices */
184 if (throttle)
185 usleep(100000); /* 100ms delay between requests */
186 }
187
188 return actor;
189}
190
191
192int actor_request(snac *user, const char *actor, xs_dict **data)
193/* request an actor */
194{
195 int status;
196 xs *payload = NULL;
197
198 if (data)
199 *data = NULL;
200
201 /* get from disk first */
202 status = actor_get_refresh(user, actor, data);
203
204 if (!valid_status(status)) {
205 /* actor data non-existent: get from the net */
206 status = activitypub_request(user, actor, &payload);
207
208 if (valid_status(status)) {
209 /* fetch collection counts when initially fetching an actor (no throttle) */
210 payload = actor_get_collections(user, payload, 0);
211
212 /* renew data */
213 status = actor_add(actor, payload);
214
215 if (data != NULL) {
216 *data = payload;
217 payload = NULL;
218 }
219 }
220 else
221 srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
222 }
223
224 /* collect the (presumed) shared inbox in this actor */
225 if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
226 if (valid_status(status) && data && *data)
227 inbox_add_by_actor(*data);
228 }
229
230 return status;
231}
232
233
234const char *get_atto(const xs_dict *msg)
235/* gets the attributedTo field (an actor) */
236{
237 const xs_val *actor = xs_dict_get(msg, "attributedTo");
238
239 /* if the actor is a list of objects (like on Peertube videos), pick the Person */
240 if (xs_type(actor) == XSTYPE_LIST) {
241 const xs_list *p = actor;
242 int c = 0;
243 const xs_dict *v;
244 actor = NULL;
245
246 while (actor == NULL && xs_list_next(p, &v, &c)) {
247 if (xs_type(v) == XSTYPE_DICT) {
248 const char *type = xs_dict_get(v, "type");
249 if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
250 actor = xs_dict_get(v, "id");
251
252 if (xs_type(actor) != XSTYPE_STRING)
253 actor = NULL;
254 }
255 }
256 }
257 }
258 else
259 if (xs_type(actor) == XSTYPE_DICT) {
260 /* bandwagon.fm returns this */
261 actor = xs_dict_get(actor, "id");
262 }
263
264 return actor;
265}
266
267
268const char *get_in_reply_to(const xs_dict *msg)
269/* gets the inReplyTo id */
270{
271 const xs_val *in_reply_to = xs_dict_get(msg, "inReplyTo");
272
273 if (xs_type(in_reply_to) == XSTYPE_DICT)
274 in_reply_to = xs_dict_get(in_reply_to, "id");
275
276 return in_reply_to;
277}
278
279
280xs_list *get_attachments(const xs_dict *msg)
281/* unify the garbage fire that are the attachments */
282{
283 xs_list *l = xs_list_new();
284 const xs_list *p;
285
286 /* try first the attachments list */
287 if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
288 xs *attach = NULL;
289 const xs_val *v;
290
291 /* ensure it's a list */
292 if (xs_type(p) == XSTYPE_DICT) {
293 attach = xs_list_new();
294 attach = xs_list_append(attach, p);
295 }
296 else
297 attach = xs_dup(p);
298
299 if (xs_type(attach) == XSTYPE_LIST && xs_list_len(attach) == 0) {
300 /* does the message have an image? */
301 const xs_dict *d = xs_dict_get(msg, "image");
302 if (xs_type(d) == XSTYPE_DICT) {
303 /* add it to the attachment list */
304 attach = xs_list_append(attach, d);
305 }
306 }
307
308 /* now iterate the list */
309 int c = 0;
310 while (xs_list_next(attach, &v, &c)) {
311 const char *type = xs_dict_get(v, "mediaType");
312 if (xs_is_null(type))
313 type = xs_dict_get(v, "type");
314
315 if (xs_is_null(type))
316 continue;
317
318 const char *href = xs_dict_get(v, "url");
319 if (xs_is_null(href))
320 href = xs_dict_get(v, "href");
321 if (xs_is_null(href))
322 continue;
323
324 /* infer MIME type from non-specific attachments */
325 if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
326 char *mt = (char *)xs_mime_by_ext(href);
327
328 if (xs_match(mt, "image/*|audio/*|video/*")) /* */
329 type = mt;
330 }
331
332 const char *name = xs_dict_get(v, "name");
333 if (xs_is_null(name))
334 name = xs_dict_get(msg, "name");
335 if (xs_is_null(name))
336 name = "";
337
338 xs *d = xs_dict_new();
339 d = xs_dict_append(d, "type", type);
340 d = xs_dict_append(d, "href", href);
341 d = xs_dict_append(d, "name", name);
342
343 const xs_dict *icon = xs_dict_get(v, "icon");
344 if (xs_type(icon) == XSTYPE_DICT)
345 d = xs_dict_append(d, "icon", icon);
346
347 l = xs_list_append(l, d);
348 }
349 }
350
351 /** urls (attachments from Peertube) **/
352 p = xs_dict_get(msg, "url");
353
354 if (xs_type(p) == XSTYPE_LIST) {
355 const char *href = NULL;
356 const char *type = NULL;
357 int c = 0;
358 const xs_val *v;
359
360 while (href == NULL && xs_list_next(p, &v, &c)) {
361 if (xs_type(v) == XSTYPE_DICT) {
362 const char *mtype = xs_dict_get(v, "type");
363
364 if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
365 mtype = xs_dict_get(v, "mediaType");
366 const xs_list *tag = xs_dict_get(v, "tag");
367
368 if (xs_type(mtype) == XSTYPE_STRING &&
369 strcmp(mtype, "application/x-mpegURL") == 0 &&
370 xs_type(tag) == XSTYPE_LIST) {
371 /* now iterate the tag list, looking for a video URL */
372 const xs_dict *d;
373 int c = 0;
374
375 while (href == NULL && xs_list_next(tag, &d, &c)) {
376 if (xs_type(d) == XSTYPE_DICT) {
377 if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
378 xs_startswith(mtype, "video/")) {
379 const char *h = xs_dict_get(d, "href");
380
381 /* this is probably it */
382 if (xs_type(h) == XSTYPE_STRING) {
383 href = h;
384 type = mtype;
385 }
386 }
387 }
388 }
389 }
390 }
391 }
392 }
393
394 if (href && type) {
395 xs *d = xs_dict_new();
396 d = xs_dict_append(d, "href", href);
397 d = xs_dict_append(d, "type", type);
398 d = xs_dict_append(d, "name", "---");
399
400 l = xs_list_append(l, d);
401 }
402 }
403
404 return l;
405}
406
407
408int hashtag_in_msg(const xs_list *hashtags, const xs_dict *msg)
409/* returns 1 if the message contains any of the list of hashtags */
410{
411 if (xs_is_list(hashtags) && xs_is_dict(msg)) {
412 const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
413
414 if (xs_is_list(tags_in_msg)) {
415 const xs_dict *te;
416
417 /* iterate the tags in the message */
418 xs_list_foreach(tags_in_msg, te) {
419 if (xs_is_dict(te)) {
420 const char *type = xs_dict_get(te, "type");
421 const char *name = xs_dict_get(te, "name");
422
423 if (xs_is_string(type) && xs_is_string(name)) {
424 if (strcmp(type, "Hashtag") == 0) {
425 xs *lc_name = xs_utf8_to_lower(name);
426
427 if (xs_list_in(hashtags, lc_name) != -1)
428 return 1;
429 }
430 }
431 }
432 }
433 }
434 }
435
436 return 0;
437}
438
439
440int followed_hashtag_check(snac *user, const xs_dict *msg)
441/* returns 1 if this message contains a hashtag followed by me */
442{
443 return hashtag_in_msg(xs_dict_get(user->config, "followed_hashtags"), msg);
444}
445
446
447int blocked_hashtag_check(snac *user, const xs_dict *msg)
448/* returns 1 if this message contains a hashtag blocked by me */
449{
450 return hashtag_in_msg(xs_dict_get(user->config, "blocked_hashtags"), msg);
451}
452
453
454int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
455/* ensures that an entry and its ancestors are in the timeline */
456{
457 int status = 0;
458
459 if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
460 xs *msg = NULL;
461
462 /* from a blocked instance? discard and break */
463 if (is_instance_blocked(*id)) {
464 snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
465 return status;
466 }
467
468 /* is the object already there? */
469 if (!valid_status((status = object_get(*id, &msg)))) {
470 /* no; download it */
471 status = activitypub_request(snac, *id, &msg);
472 }
473
474 if (valid_status(status)) {
475 const xs_dict *object = msg;
476 const char *type = xs_dict_get(object, "type");
477
478 /* get the id again from the object, as it may be different */
479 const char *nid = xs_dict_get(object, "id");
480
481 if (xs_type(nid) != XSTYPE_STRING)
482 return 0;
483
484 if (wrk && strcmp(nid, *id) != 0) {
485 snac_debug(snac, 1,
486 xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
487
488 *wrk = xs_dup(nid);
489 *id = *wrk;
490 }
491
492 if (xs_is_null(type))
493 type = "(null)";
494
495 srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
496
497 if (strcmp(type, "Create") == 0) {
498 /* some software like lemmy nest Announce + Create + Note */
499 if (!xs_is_null(object = xs_dict_get(object, "object"))) {
500 type = xs_dict_get(object, "type");
501 nid = xs_dict_get(object, "id");
502 }
503 else
504 type = "(null)";
505 }
506
507 if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
508 if (content_match("filter_reject.txt", object))
509 snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
510 else
511 if (blocked_hashtag_check(snac, object))
512 snac_log(snac, xs_fmt("timeline_request rejected by hashtag %s", nid));
513 else {
514 const char *actor = get_atto(object);
515
516 if (!xs_is_null(actor)) {
517 /* request (and drop) the actor for this entry */
518 if (!valid_status(actor_request(snac, actor, NULL))) {
519 /* failed? retry later */
520 enqueue_actor_refresh(snac, actor, 60);
521 }
522
523 /* does it have an ancestor? */
524 const char *in_reply_to = get_in_reply_to(object);
525
526 /* store */
527 timeline_add(snac, nid, object);
528
529 /* redistribute to lists for this user */
530 list_distribute(snac, actor, object);
531
532 /* recurse! */
533 timeline_request(snac, &in_reply_to, NULL, level + 1);
534 }
535 }
536 }
537 }
538 }
539
540 return status;
541}
542
543
544int send_to_inbox_raw(const char *keyid, const char *seckey,
545 const xs_str *inbox, const xs_dict *msg,
546 xs_val **payload, int *p_size, int timeout)
547/* sends a message to an Inbox */
548{
549 int status;
550 xs_dict *response;
551 xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
552
553 response = http_signed_request_raw(keyid, seckey, "POST", inbox,
554 NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
555
556 xs_free(response);
557
558 return status;
559}
560
561
562int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
563 xs_val **payload, int *p_size, int timeout)
564/* sends a message to an Inbox */
565{
566 const char *seckey = xs_dict_get(snac->key, "secret");
567
568 return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
569}
570
571
572xs_str *get_actor_inbox(const char *actor, int shared)
573/* gets an actor's inbox */
574{
575 xs *data = NULL;
576 const char *v = NULL;
577
578 if (valid_status(actor_request(NULL, actor, &data))) {
579 /* try first endpoints/sharedInbox */
580 if (shared && (v = xs_dict_get(data, "endpoints")))
581 v = xs_dict_get(v, "sharedInbox");
582
583 /* try then the regular inbox */
584 if (xs_is_null(v))
585 v = xs_dict_get(data, "inbox");
586 }
587
588 return xs_is_null(v) ? NULL : xs_dup(v);
589}
590
591
592int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
593 xs_val **payload, int *p_size, int timeout)
594/* sends a message to an actor */
595{
596 int status = HTTP_STATUS_BAD_REQUEST;
597 xs *inbox = get_actor_inbox(actor, 1);
598
599 if (!xs_is_null(inbox))
600 status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
601
602 return status;
603}
604
605
606void post_message(snac *snac, const char *actor, const xs_dict *msg)
607/* posts a message immediately (bypassing the output queues) */
608{
609 xs *payload = NULL;
610 int p_size;
611
612 int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
613
614 srv_log(xs_fmt("post_message to actor %s %d", actor, status));
615
616 if (!valid_status(status))
617 /* cannot send right now, enqueue */
618 enqueue_message(snac, msg);
619}
620
621
622xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
623/* returns the list of recipients for a message */
624{
625 const xs_val *to = xs_dict_get(msg, "to");
626 const xs_val *cc = xs_dict_get(msg, "cc");
627 xs_set rcpts;
628 int n;
629
630 xs_set_init(&rcpts);
631
632 const xs_list *lists[] = { to, cc, NULL };
633 for (n = 0; lists[n]; n++) {
634 xs_list *l = (xs_list *)lists[n];
635 const char *v;
636 xs *tl = NULL;
637
638 /* if it's a string, create a list with only one element */
639 if (xs_type(l) == XSTYPE_STRING) {
640 tl = xs_list_new();
641 tl = xs_list_append(tl, l);
642
643 l = tl;
644 }
645
646 while (xs_list_iter(&l, &v)) {
647 if (expand_public) {
648 if (strcmp(v, public_address) == 0 ||
649 /* check if it's a followers collection URL */
650 (xs_type(v) == XSTYPE_STRING &&
651 strcmp(v, xs_fmt("%s/followers", snac->actor)) == 0) ||
652 (xs_type(v) == XSTYPE_LIST &&
653 xs_list_in(v, xs_fmt("%s/followers", snac->actor)) != -1)) {
654 /* iterate the followers and add them */
655 xs *fwers = follower_list(snac);
656 const char *actor;
657
658 char *p = fwers;
659 while (xs_list_iter(&p, &actor))
660 xs_set_add(&rcpts, actor);
661 }
662 else
663 xs_set_add(&rcpts, v);
664 }
665 else
666 xs_set_add(&rcpts, v);
667 }
668 }
669
670 return xs_set_result(&rcpts);
671}
672
673
674int is_msg_public(const xs_dict *msg)
675/* checks if a message is public */
676{
677 const char *to = xs_dict_get(msg, "to");
678 const char *cc = xs_dict_get(msg, "cc");
679 int n;
680
681 const char *lists[] = { to, cc, NULL };
682 for (n = 0; lists[n]; n++) {
683 const xs_val *l = lists[n];
684
685 if (xs_type(l) == XSTYPE_STRING) {
686 if (strcmp(l, public_address) == 0)
687 return 1;
688 }
689 else
690 if (xs_type(l) == XSTYPE_LIST) {
691 if (xs_list_in(l, public_address) != -1)
692 return 1;
693 }
694 }
695
696 return 0;
697}
698
699
700int is_msg_from_private_user(const xs_dict *msg)
701/* checks if a message is from a local, private user */
702{
703 int ret = 0;
704
705 /* is this message from a local user? */
706 if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
707 const char *atto = get_atto(msg);
708 xs *l = xs_split(atto, "/");
709 const char *uid = xs_list_get(l, -1);
710 snac user;
711
712 if (uid && user_open(&user, uid)) {
713 if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
714 ret = 1;
715
716 user_free(&user);
717 }
718 }
719
720 return ret;
721}
722
723
724void followed_hashtag_distribute(const xs_dict *msg)
725/* distribute this post to all users following the included hashtags */
726{
727 const char *id = xs_dict_get(msg, "id");
728 const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
729
730 if (!xs_is_string(id) || !xs_is_list(tags_in_msg) || xs_list_len(tags_in_msg) == 0)
731 return;
732
733 srv_debug(1, xs_fmt("followed_hashtag_distribute check for %s", id));
734
735 xs *users = user_list();
736 const char *uid;
737
738 xs_list_foreach(users, uid) {
739 snac user;
740
741 if (user_open(&user, uid)) {
742 if (followed_hashtag_check(&user, msg)) {
743 timeline_add(&user, id, msg);
744
745 snac_log(&user, xs_fmt("followed hashtag in %s", id));
746 }
747
748 user_free(&user);
749 }
750 }
751}
752
753
754int is_msg_for_me(snac *snac, const xs_dict *c_msg)
755/* checks if this message is for me */
756{
757 const char *type = xs_dict_get(c_msg, "type");
758 const char *actor = xs_dict_get(c_msg, "actor");
759
760 if (strcmp(actor, snac->actor) == 0) {
761 /* message by myself? (most probably via the shared-inbox) reject */
762 snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
763 return 0;
764 }
765
766 if (xs_match(type, "Like|Announce|EmojiReact")) {
767 const char *object = xs_dict_get(c_msg, "object");
768 xs *obj = NULL;
769
770 if (xs_is_dict(object)) {
771 obj = xs_dup(object);
772 object = xs_dict_get(object, "id");
773 }
774
775 /* bad object id? reject */
776 if (!xs_is_string(object))
777 return 0;
778
779 /* try to get the object */
780 if (!xs_is_dict(obj))
781 object_get(object, &obj);
782
783 /* if it's about one of our posts, accept it */
784 if (is_msg_mine(snac, object))
785 return 2;
786
787 /* blocked by hashtag? */
788 if (blocked_hashtag_check(snac, obj))
789 return 0;
790
791 /* if it's by someone we follow, accept it */
792 if (following_check(snac, actor))
793 return 1;
794
795 /* do we follow any hashtag? */
796 if (followed_hashtag_check(snac, obj))
797 return 7;
798
799 return 0;
800 }
801
802 /* if it's an Undo, it must be from someone related to us */
803 if (xs_match(type, "Undo")) {
804 return follower_check(snac, actor) || following_check(snac, actor);
805 }
806
807 /* if it's an Accept + Follow, it must be for a Follow we created */
808 if (xs_match(type, "Accept")) {
809 return following_check(snac, actor);
810 }
811
812 /* if it's a Follow, it must be explicitly for us */
813 if (xs_match(type, "Follow")) {
814 const char *object = xs_dict_get(c_msg, "object");
815 return !xs_is_null(object) && strcmp(snac->actor, object) == 0;
816 }
817
818 /* only accept Ping directed to us */
819 if (xs_match(type, "Ping")) {
820 const char *dest = xs_dict_get(c_msg, "to");
821 return !xs_is_null(dest) && strcmp(snac->actor, dest) == 0;
822 }
823
824 /* if it's not a Create or Update, allow as is */
825 if (!xs_match(type, "Create|Update")) {
826 return 1;
827 }
828
829 const xs_dict *msg = xs_dict_get(c_msg, "object");
830
831 /* any blocked hashtag? reject */
832 if (blocked_hashtag_check(snac, msg)) {
833 snac_debug(snac, 1, xs_fmt("blocked by hashtag %s", xs_dict_get(msg, "id")));
834 return 0;
835 }
836
837 int pub_msg = is_msg_public(c_msg);
838
839 /* if this message is public and we follow the actor of this post, allow */
840 if (pub_msg && following_check(snac, actor))
841 return 1;
842
843 xs *rcpts = recipient_list(snac, msg, 0);
844 xs_list *p = rcpts;
845 const xs_str *v;
846
847 xs *actor_followers = NULL;
848
849 if (!pub_msg) {
850 /* not a public message; get the actor and its followers list */
851 xs *actor_obj = NULL;
852
853 if (valid_status(object_get(actor, &actor_obj))) {
854 const xs_val *fw = xs_dict_get(actor_obj, "followers");
855 if (fw)
856 actor_followers = xs_dup(fw);
857 }
858 }
859
860 while(xs_list_iter(&p, &v)) {
861 /* explicitly for me? accept */
862 if (strcmp(v, snac->actor) == 0)
863 return 2;
864
865 if (pub_msg) {
866 /* a public message for someone we follow? (probably cc'ed) accept */
867 if (strcmp(v, public_address) != 0 && following_check(snac, v))
868 return 5;
869 }
870 else
871 if (actor_followers && strcmp(v, actor_followers) == 0) {
872 /* if this message is for this actor's followers, are we one of them? */
873 if (following_check(snac, actor))
874 return 6;
875 }
876 }
877
878 /* accept if it's by someone we follow */
879 const char *atto = get_atto(msg);
880
881 if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
882 return 3;
883
884 /* is this message a reply to another? */
885 const char *irt = get_in_reply_to(msg);
886 if (!xs_is_null(irt)) {
887 xs *r_msg = NULL;
888
889 /* try to get the replied message */
890 if (valid_status(object_get(irt, &r_msg))) {
891 atto = get_atto(r_msg);
892
893 /* accept if the replied message is from someone we follow */
894 if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
895 return 4;
896 }
897 }
898
899 /* does this message contain a tag we are following? */
900 if (pub_msg && followed_hashtag_check(snac, msg))
901 return 7;
902
903 return 0;
904}
905
906
907xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
908/* parses mentions and tags from content */
909{
910 xs_str *nc = xs_str_new(NULL);
911 xs_list *tl = *tag;
912 xs *split;
913 xs_list *p;
914 const xs_val *v;
915 int n = 0;
916
917 /* create a default server for incomplete mentions */
918 xs *def_srv = NULL;
919
920 if (xs_list_len(tl)) {
921 /* if there are any mentions, get the server from
922 the first one, which is the inReplyTo author */
923 p = tl;
924 while (xs_list_iter(&p, &v)) {
925 const char *type = xs_dict_get(v, "type");
926 const char *name = xs_dict_get(v, "name");
927
928 if (type && name && strcmp(type, "Mention") == 0) {
929 xs *l = xs_split(name, "@");
930
931 def_srv = xs_dup(xs_list_get(l, -1));
932
933 break;
934 }
935 }
936 }
937
938 if (xs_is_null(def_srv))
939 /* use this same server */
940 def_srv = xs_dup(xs_dict_get(srv_config, "host"));
941
942 split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#(_|[^[:punct:][:space:]])+)");
943
944 p = split;
945 while (xs_list_iter(&p, &v)) {
946 if ((n & 0x1)) {
947 if (*v == '@') {
948 xs *link = NULL;
949 xs *wuid = NULL;
950
951 if (strchr(v + 1, '@') == NULL) {
952 /* only one @? it's a dumb Mastodon-like mention
953 without server; add the default one */
954 wuid = xs_fmt("%s@%s", v, def_srv);
955
956 snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
957 }
958 else
959 wuid = xs_dup(v);
960
961 /* query the webfinger about this fellow */
962 xs *actor = NULL;
963 xs *uid = NULL;
964 int status;
965
966 status = webfinger_request(wuid, &actor, &uid);
967
968 if (valid_status(status) && actor && uid) {
969 xs *d = xs_dict_new();
970 xs *n = xs_fmt("@%s", uid);
971
972 d = xs_dict_append(d, "type", "Mention");
973 d = xs_dict_append(d, "href", actor);
974 d = xs_dict_append(d, "name", n);
975
976 tl = xs_list_append(tl, d);
977
978 link = xs_fmt("<span class=\"h-card\"><a href=\"%s\" class=\"u-url mention\">%s</a></span>", actor, n);
979 }
980
981 if (!xs_is_null(link))
982 nc = xs_str_cat(nc, link);
983 else
984 nc = xs_str_cat(nc, v);
985 }
986 else
987 if (*v == '#') {
988 /* hashtag */
989 xs *d = xs_dict_new();
990 xs *n = xs_utf8_to_lower(v);
991 xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
992 xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
993
994 d = xs_dict_append(d, "type", "Hashtag");
995 d = xs_dict_append(d, "href", h);
996 d = xs_dict_append(d, "name", n);
997
998 tl = xs_list_append(tl, d);
999
1000 /* add the code */
1001 nc = xs_str_cat(nc, l);
1002 }
1003 else
1004 if (*v == '&') {
1005 /* HTML Unicode entity, probably part of an emoji */
1006
1007 /* write as is */
1008 nc = xs_str_cat(nc, v);
1009 }
1010 }
1011 else
1012 nc = xs_str_cat(nc, v);
1013
1014 n++;
1015 }
1016
1017 *tag = tl;
1018
1019 return nc;
1020}
1021
1022
1023void collect_replies(snac *user, const char *id)
1024/* collects all replies for a post */
1025{
1026 xs *obj = NULL;
1027
1028 if (!valid_status(object_get(id, &obj))) {
1029 snac_debug(user, 1, xs_fmt("collect_replies: object '%s' is not here", id));
1030 return;
1031 }
1032
1033 const xs_dict *replies_first = xs_dict_get_path(obj, "replies.first");
1034 if (!xs_is_dict(replies_first)) {
1035 snac_debug(user, 1, xs_fmt("collect_replies: object '%s' does not have replies.first", id));
1036 return;
1037 }
1038
1039 const xs_list *level0_replies = xs_dict_get(replies_first, "items");
1040 const xs_list *level1_replies = NULL;
1041
1042 const char *next = xs_dict_get(replies_first, "next");
1043 xs *reply_obj = NULL;
1044
1045 if (xs_is_string(next) && !valid_status(activitypub_request(user, next, &reply_obj))) {
1046 snac_debug(user, 1, xs_fmt("collect_replies: error getting next replies object '%s'", next));
1047 return;
1048 }
1049
1050 if (xs_is_dict(reply_obj))
1051 level1_replies = xs_dict_get(reply_obj, "items");
1052
1053 xs *items = xs_list_new();
1054
1055 if (xs_is_list(level0_replies))
1056 items = xs_list_cat(items, level0_replies);
1057
1058 if (xs_is_list(level1_replies))
1059 items = xs_list_cat(items, level1_replies);
1060
1061 const xs_val *v;
1062
1063 xs_list_foreach(items, v) {
1064 xs *reply = NULL;
1065
1066 if (xs_is_string(v)) {
1067 /* request the object */
1068 if (!valid_status(object_get(v, &reply))) {
1069 if (!valid_status(activitypub_request(user, v, &reply))) {
1070 snac_debug(user, 1, xs_fmt("collect_replies: error requesting object '%s'", v));
1071 continue;
1072 }
1073 }
1074 }
1075 else
1076 if (xs_is_dict(v)) {
1077 /* actually the post object */
1078 reply = xs_dup(v);
1079 }
1080
1081 if (!xs_is_dict(reply))
1082 continue;
1083
1084 const char *id = xs_dict_get(reply, "id");
1085 const char *type = xs_dict_get(reply, "type");
1086 const char *attr_to = get_atto(reply);
1087
1088 if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
1089 continue;
1090
1091 if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
1092 continue;
1093
1094 if (timeline_here(user, id)) {
1095 snac_debug(user, 1, xs_fmt("collect_replies: item already in timeline %s", id));
1096 continue;
1097 }
1098
1099 enqueue_actor_refresh(user, attr_to, 0);
1100
1101 timeline_add(user, id, reply);
1102
1103 snac_log(user, xs_fmt("new '%s' (collect_replies) %s %s", type, attr_to, id));
1104 }
1105}
1106
1107
1108void collect_outbox(snac *user, const char *actor_id)
1109/* gets an actor's outbox and inserts a bunch of posts in a user's timeline */
1110{
1111 int status;
1112 xs *actor = NULL;
1113
1114 if (!valid_status(status = actor_request(user, actor_id, &actor))) {
1115 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor object '%s' %d", actor_id, status));
1116 return;
1117 }
1118
1119 xs *outbox = NULL;
1120 const char *outbox_url = xs_dict_get(actor, "outbox");
1121
1122 if (!xs_is_string(outbox_url))
1123 return;
1124
1125 if (!valid_status(status = activitypub_request(user, outbox_url, &outbox))) {
1126 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor outbox '%s' %d", outbox_url, status));
1127 return;
1128 }
1129
1130 const xs_list *ordered_items = xs_dict_get(outbox, "orderedItems");
1131
1132 if (!xs_is_list(ordered_items)) {
1133 /* the list is not here; does it have a 'first'? */
1134 const char *first = xs_dict_get(outbox, "first");
1135
1136 if (xs_is_string(first)) {
1137 /* download this instead */
1138 xs *first2 = xs_dup(first);
1139 xs_free(outbox);
1140
1141 if (!valid_status(status = activitypub_request(user, first2, &outbox))) {
1142 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get first page of outbox '%s' %d", first2, status));
1143 return;
1144 }
1145
1146 /* last chance */
1147 ordered_items = xs_dict_get(outbox, "orderedItems");
1148 }
1149 }
1150
1151 if (!xs_is_list(ordered_items)) {
1152 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get list of posts for actor '%s' outbox", actor_id));
1153 return;
1154 }
1155
1156 /* well, ok, then */
1157 int max = 4;
1158 const xs_val *v;
1159
1160 xs_list_foreach(ordered_items, v) {
1161 if (max == 0)
1162 break;
1163
1164 xs *post = NULL;
1165
1166 if (xs_is_string(v)) {
1167 /* it's probably the post url */
1168 if (!valid_status(activitypub_request(user, v, &post)))
1169 continue;
1170 }
1171 else
1172 if (xs_is_dict(v))
1173 post = xs_dup(v);
1174
1175 if (post == NULL)
1176 continue;
1177
1178 const char *type = xs_dict_get(post, "type");
1179
1180 if (!xs_is_string(type) || strcmp(type, "Create")) {
1181 /* not a post */
1182 continue;
1183 }
1184 if (is_msg_for_me(user, post) == 0) {
1185 /* not for us */
1186 continue;
1187 }
1188
1189 const xs_dict *object = xs_dict_get(post, "object");
1190
1191 if (!xs_is_dict(object))
1192 continue;
1193
1194 type = xs_dict_get(object, "type");
1195 const char *id = xs_dict_get(object, "id");
1196 const char *attr_to = get_atto(object);
1197
1198 if (!xs_is_string(type) || !xs_is_string(id) || !xs_is_string(attr_to))
1199 continue;
1200
1201 if (!timeline_here(user, id)) {
1202 timeline_add(user, id, object);
1203 snac_log(user, xs_fmt("new '%s' (collect_outbox) %s %s", type, attr_to, id));
1204 }
1205 else
1206 snac_debug(user, 1, xs_fmt("collect_outbox: post '%s' already here", id));
1207
1208 max--;
1209 }
1210}
1211
1212
1213void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
1214/* notifies the user of relevant events */
1215{
1216 /* skip our own notifications */
1217 if (strcmp(snac->actor, actor) == 0)
1218 return;
1219
1220 const char *id = xs_dict_get(msg, "id");
1221
1222 if (strcmp(type, "Create") == 0) {
1223 /* only notify of notes specifically for us */
1224 xs *rcpts = recipient_list(snac, msg, 0);
1225
1226 if (xs_list_in(rcpts, snac->actor) == -1)
1227 return;
1228
1229 /* discard votes */
1230 const xs_dict *note = xs_dict_get(msg, "object");
1231
1232 if (note && !xs_is_null(xs_dict_get(note, "name")))
1233 return;
1234 }
1235
1236 if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
1237 return;
1238
1239 /* get the object id */
1240 const char *objid = xs_dict_get(msg, "object");
1241
1242 if (xs_type(objid) == XSTYPE_DICT)
1243 objid = xs_dict_get(objid, "id");
1244
1245 if (xs_match(type, "Like|Announce|EmojiReact")) {
1246 /* if it's not an admiration about something by us, done */
1247 if (xs_is_null(objid) || !is_msg_mine(snac, objid))
1248 return;
1249
1250 /* if it's an announce by our own relay, done */
1251 xs *relay_id = xs_fmt("%s/relay", srv_baseurl);
1252 if (xs_startswith(id, relay_id))
1253 return;
1254 }
1255
1256 /* updated poll? */
1257 if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
1258 const xs_dict *poll;
1259 const char *poll_id;
1260
1261 if ((poll = xs_dict_get(msg, "object")) == NULL)
1262 return;
1263
1264 /* if it's not closed, discard */
1265 if (xs_is_null(xs_dict_get(poll, "closed")))
1266 return;
1267
1268 if ((poll_id = xs_dict_get(poll, "id")) == NULL)
1269 return;
1270
1271 /* if it's not ours and we didn't vote, discard */
1272 if (!is_msg_mine(snac, poll_id) && !was_question_voted(snac, poll_id))
1273 return;
1274 }
1275
1276 /* user will love to know about this! */
1277
1278 /* prepare message body */
1279 xs *body = xs_fmt("User : @%s@%s\n",
1280 xs_dict_get(snac->config, "uid"),
1281 xs_dict_get(srv_config, "host")
1282 );
1283
1284 if (strcmp(utype, "(null)") != 0) {
1285 xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
1286 body = xs_str_cat(body, s1);
1287 }
1288 else {
1289 xs *s1 = xs_fmt("Type : %s\n", type);
1290 body = xs_str_cat(body, s1);
1291 }
1292
1293 {
1294 xs *s1 = xs_fmt("Actor : %s\n", actor);
1295 body = xs_str_cat(body, s1);
1296 }
1297
1298 if (objid != NULL) {
1299 xs *s1 = xs_fmt("Object: %s\n", objid);
1300 body = xs_str_cat(body, s1);
1301 }
1302
1303 /* email */
1304
1305 const char *email = "[disabled by admin]";
1306
1307 if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
1308 email = xs_dict_get(snac->config_o, "email");
1309 if (xs_is_null(email)) {
1310 email = xs_dict_get(snac->config, "email");
1311
1312 if (xs_is_null(email))
1313 email = "[empty]";
1314 }
1315 }
1316
1317 if (*email != '\0' && *email != '[') {
1318 snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
1319
1320 xs *subject = xs_fmt("snac notify for @%s@%s",
1321 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
1322 xs *from = xs_fmt("<snac-daemon@%s>", xs_dict_get(srv_config, "host"));
1323 xs *header = xs_fmt(
1324 "From: snac-daemon %s\n"
1325 "To: %s\n"
1326 "Subject: %s\n"
1327 "\n",
1328 from, email, subject);
1329
1330 xs *mailinfo = xs_dict_new();
1331 xs *bd = xs_fmt("%s%s", header, body);
1332 mailinfo = xs_dict_append(mailinfo, "from", from);
1333 mailinfo = xs_dict_append(mailinfo, "to", email);
1334 mailinfo = xs_dict_append(mailinfo, "body", bd);
1335
1336 enqueue_email(mailinfo, 0);
1337 }
1338
1339 /* telegram */
1340
1341 const char *bot = xs_dict_get(snac->config, "telegram_bot");
1342 const char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
1343
1344 if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
1345 enqueue_telegram(body, bot, chat_id);
1346
1347 /* ntfy */
1348 const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
1349 const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
1350
1351 if (!xs_is_null(ntfy_server) && *ntfy_server)
1352 enqueue_ntfy(body, ntfy_server, ntfy_token);
1353
1354 /* auto boost */
1355 if (xs_match(type, "Create") && xs_is_true(xs_dict_get(snac->config, "auto_boost"))) {
1356 xs *msg = msg_admiration(snac, objid, "Announce");
1357 enqueue_message(snac, msg);
1358
1359 snac_debug(snac, 1, xs_fmt("auto boosted %s", objid));
1360 }
1361
1362 /* finally, store it in the notification folder */
1363 if (strcmp(type, "Follow") == 0)
1364 objid = id;
1365 else
1366 if (strcmp(utype, "Follow") == 0)
1367 objid = actor;
1368
1369 notify_add(snac, type, utype, actor, objid != NULL ? objid : id, msg);
1370}
1371
1372/** messages **/
1373
1374xs_dict *msg_base(snac *snac, const char *type, const char *id,
1375 const char *actor, const char *date, const char *object)
1376/* creates a base ActivityPub message */
1377{
1378 xs *did = NULL;
1379 xs *published = NULL;
1380 xs *ntid = tid(0);
1381 const char *obj_id;
1382
1383 if (xs_type(object) == XSTYPE_DICT)
1384 obj_id = xs_dict_get(object, "id");
1385 else
1386 obj_id = object;
1387
1388 /* generated values */
1389 if (date && strcmp(date, "@now") == 0) {
1390 published = xs_str_utctime(0, ISO_DATE_SPEC);
1391 date = published;
1392 }
1393
1394 if (id != NULL) {
1395 if (strcmp(id, "@dummy") == 0) {
1396 did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
1397
1398 id = did;
1399 }
1400 else
1401 if (strcmp(id, "@object") == 0) {
1402 if (obj_id != NULL) {
1403 did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
1404 id = did;
1405 }
1406 else
1407 id = NULL;
1408 }
1409 else
1410 if (strcmp(id, "@wrapper") == 0) {
1411 /* like @object, but always generate the same id */
1412 if (object != NULL) {
1413 date = xs_dict_get(object, "published");
1414 did = xs_fmt("%s/%s", obj_id, type);
1415 id = did;
1416 }
1417 else
1418 id = NULL;
1419 }
1420 }
1421
1422 xs_dict *msg = xs_dict_new();
1423
1424 msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
1425 msg = xs_dict_append(msg, "type", type);
1426
1427 if (id != NULL)
1428 msg = xs_dict_append(msg, "id", id);
1429
1430 if (actor != NULL)
1431 msg = xs_dict_append(msg, "actor", actor);
1432
1433 if (date != NULL)
1434 msg = xs_dict_append(msg, "published", date);
1435
1436 if (object != NULL)
1437 msg = xs_dict_append(msg, "object", object);
1438
1439 return msg;
1440}
1441
1442
1443xs_dict *msg_collection(snac *snac, const char *id, int items)
1444/* creates an empty OrderedCollection message */
1445{
1446 xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
1447 xs *n = xs_number_new(items);
1448
1449 msg = xs_dict_append(msg, "attributedTo", snac->actor);
1450 msg = xs_dict_append(msg, "totalItems", n);
1451
1452 return msg;
1453}
1454
1455
1456xs_dict *msg_replies(snac *user, const char *id, int fill)
1457/* creates a CollectionPage with replies of id */
1458{
1459 xs *r_id = xs_replace(id, "/p/", "/r/");
1460 xs *r_idp = xs_fmt("%s?page=true", r_id);
1461 xs *r_idh = xs_fmt("%s", r_id);
1462
1463 xs_dict *msg = msg_base(user, "CollectionPage", r_idp, NULL, NULL, NULL);
1464
1465 msg = xs_dict_set(msg, "partOf", r_idh);
1466
1467 xs *items = xs_list_new();
1468 if (fill) {
1469 xs *children = object_children(id);
1470 const char *md5;
1471
1472 xs_list_foreach(children, md5) {
1473 xs *obj = NULL;
1474
1475 if (valid_status(object_get_by_md5(md5, &obj)) && is_msg_public(obj)) {
1476 const char *c_id = xs_dict_get(obj, "id");
1477
1478 if (xs_is_string(c_id))
1479 items = xs_list_append(items, c_id);
1480 }
1481 }
1482 }
1483 else {
1484 msg = xs_dict_del(msg, "@context");
1485 msg = xs_dict_del(msg, "id");
1486 msg = xs_dict_set(msg, "next", r_idp);
1487 }
1488
1489 msg = xs_dict_set(msg, "items", items);
1490
1491 return msg;
1492}
1493
1494
1495xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
1496/* creates an Accept message (as a response to a Follow) */
1497{
1498 xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
1499
1500 msg = xs_dict_append(msg, "to", to);
1501
1502 return msg;
1503}
1504
1505
1506xs_dict *msg_update(snac *snac, const xs_dict *object)
1507/* creates an Update message */
1508{
1509 xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
1510
1511 const char *type = xs_dict_get(object, "type");
1512
1513 if (strcmp(type, "Note") == 0) {
1514 msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
1515 msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
1516 }
1517 else
1518 if (strcmp(type, "Person") == 0) {
1519 msg = xs_dict_append(msg, "to", public_address);
1520
1521 /* also spam the people being followed, so that
1522 they have the newest information about who we are */
1523 xs *cc = following_list(snac);
1524
1525 msg = xs_dict_append(msg, "cc", cc);
1526 }
1527 else
1528 msg = xs_dict_append(msg, "to", public_address);
1529
1530 return msg;
1531}
1532
1533
1534xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
1535/* creates a Like, Announce or EmojiReact message */
1536{
1537 xs *a_msg = NULL;
1538 xs_dict *msg = NULL;
1539 xs *wrk = NULL;
1540 char t = 0;
1541
1542 switch (*type) {
1543 case 'L':
1544 t = 'l';
1545 break;
1546 case 'A':
1547 t = 'a';
1548 break;
1549 case 'E':
1550 t = 'e';
1551 break;
1552 }
1553
1554 /* call the object */
1555 timeline_request(snac, &object, &wrk, 0);
1556
1557 if (valid_status(object_get(object, &a_msg))) {
1558 xs *rcpts = xs_list_new();
1559 xs *o_md5 = xs_md5_hex(object, strlen(object));
1560 xs *id = xs_fmt("%s/%c/%s", snac->actor, t, o_md5);
1561
1562 msg = msg_base(snac, type, id, snac->actor, "@now", object);
1563
1564 if (is_msg_public(a_msg))
1565 rcpts = xs_list_append(rcpts, public_address);
1566
1567 rcpts = xs_list_append(rcpts, get_atto(a_msg));
1568
1569 msg = xs_dict_append(msg, "to", rcpts);
1570 }
1571 else
1572 snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
1573
1574 return msg;
1575}
1576
1577
1578xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
1579/* creates an Undo + admiration message */
1580{
1581 xs *a_msg = NULL;
1582 xs_dict *msg = NULL;
1583
1584 if (valid_status(object_get(id, &a_msg))) {
1585 /* create a clone of the original admiration message */
1586 xs *object = msg_admiration(user, id, type);
1587
1588 /* delete the published date */
1589 object = xs_dict_del(object, "published");
1590
1591 /* create an undo message for this object */
1592 msg = msg_undo(user, object);
1593
1594 /* copy the 'to' field */
1595 msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
1596 }
1597
1598 /* now we despise this */
1599 object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
1600
1601 return msg;
1602}
1603
1604xs_dict *msg_emoji_init(snac *snac, const char *mid, const char *eid_o)
1605/* creates an emoji reaction from a local user */
1606{
1607 xs_dict *n_msg = msg_admiration(snac, mid, "EmojiReact");
1608
1609 xs *eid = xs_strip_chars_i(xs_dup(eid_o), ":");
1610 xs *content = NULL;
1611 xs *tag = xs_list_new();
1612 xs *dict = xs_dict_new();
1613 xs *icon = xs_dict_new();
1614 xs *accounts = xs_list_new();
1615 xs *emjs = emojis_rm_categories();
1616
1617 /* may be a default emoji */
1618 xs *eidd = xs_dup(eid);
1619 const char *eidda = eid;
1620
1621 if (xs_is_emoji(xs_utf8_dec(&eidda)))
1622 content = xs_dup(eid);
1623
1624 else if (*eid == '%') {
1625 content = xs_url_dec_emoji(xs_dup(eid));
1626 if (content == NULL) {
1627 return NULL;
1628 }
1629 }
1630 else {
1631 content = xs_fmt(":%s:", eid);
1632 const char *emo = xs_dict_get(emjs, content);
1633
1634 if (emo == NULL)
1635 return NULL;
1636
1637 if (xs_match(emo, "https://*|http://*")) { /* emoji is an URL to an image */
1638 icon = xs_dict_set(icon, "type", "Image");
1639 icon = xs_dict_set(icon, "url", emo);
1640 dict = xs_dict_set(dict, "icon", icon);
1641
1642 dict = xs_dict_set(dict, "id", emo);
1643 dict = xs_dict_set(dict, "name", content);
1644 dict = xs_dict_set(dict, "type", "Emoji");
1645 tag = xs_list_append(tag, dict);
1646 }
1647 else
1648 if (xs_startswith(emo, "&#")) {
1649 /* snac default emoji as an HTML entity: decode */
1650 content = xs_free(content);
1651
1652 xs *s1 = xs_strip_chars_i(xs_dup(emo), "&#");
1653 unsigned int cpoint = 0;
1654 sscanf(s1, "%u;", &cpoint);
1655
1656 if (cpoint)
1657 content = xs_utf8_cat(xs_str_new(NULL), cpoint);
1658 else
1659 content = xs_dup(emo);
1660 }
1661 else {
1662 /* use as it is and hope for the best */
1663 xs_free(content);
1664 content = xs_dup(emo);
1665 }
1666 }
1667
1668 accounts = xs_list_append(accounts, snac->actor);
1669
1670 n_msg = xs_dict_set(n_msg, "content", content);
1671 n_msg = xs_dict_set(n_msg, "accounts", accounts);
1672 n_msg = xs_dict_set(n_msg, "attributedTo", xs_list_get(xs_dict_get(n_msg, "to"), 1));
1673 n_msg = xs_dict_set(n_msg, "accountId", snac->uid);
1674 n_msg = xs_dict_set(n_msg, "tag", tag);
1675
1676 int ret = timeline_admire(snac, xs_dict_get(n_msg, "object"), snac->actor, 1, n_msg);
1677 if (ret == 200 || ret == 201) {
1678 enqueue_message(snac, n_msg);
1679 return n_msg;
1680 }
1681
1682 return NULL;
1683}
1684
1685xs_dict *msg_emoji_unreact(snac *user, const char *mid, const char *eid)
1686/* creates an Undo + emoji reaction message */
1687{
1688 xs *a_msg = NULL;
1689 xs_dict *msg = NULL;
1690
1691 if (valid_status(object_get(mid, &a_msg))) {
1692 /* create a clone of the original admiration message */
1693 xs *object = msg_admiration(user, mid, "EmojiReact");
1694
1695 /* delete the published date */
1696 object = xs_dict_del(object, "published");
1697
1698 /* create an undo message for this object */
1699 msg = msg_undo(user, object);
1700
1701 /* copy the 'to' field */
1702 msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
1703 }
1704
1705 xs *emotes = object_get_emoji_reacts(mid);
1706 const char *v;
1707 int c = 0;
1708
1709 /* may be a default emoji */
1710 if (strlen(eid) == 12 && *eid == '%') {
1711 eid = xs_url_dec(eid);
1712 if (eid == NULL) {
1713 return NULL;
1714 }
1715 }
1716
1717 /* lets get all emotes for this msg, and compare it to our content */
1718 while (xs_list_next(emotes, &v, &c)) {
1719 xs_dict *e = NULL;
1720 if (valid_status(object_get_by_md5(v, &e))) {
1721 const char *content = xs_dict_get(e, "content");
1722 const char *id = xs_dict_get(e, "id");
1723 const char *actor = xs_dict_get(e, "actor");
1724 /* maybe formated as :{emoteName}: too */
1725 if (xs_str_in(eid, content) != -1)
1726 if (strcmp(user->actor, actor) == 0) {
1727 object_rm_emoji_react(mid, id);
1728 return msg;
1729 }
1730 }
1731 }
1732
1733 return NULL;
1734}
1735
1736
1737xs_dict *msg_actor_place(snac *user, const char *label)
1738/* creates a Place object, if the user has a location defined */
1739{
1740 xs_dict *place = NULL;
1741 const char *latitude = xs_dict_get_def(user->config, "latitude", "");
1742 const char *longitude = xs_dict_get_def(user->config, "longitude", "");
1743
1744 if (*latitude && *longitude) {
1745 xs *d_la = xs_number_new(atof(latitude));
1746 xs *d_lo = xs_number_new(atof(longitude));
1747
1748 place = msg_base(user, "Place", NULL, user->actor, NULL, NULL);
1749
1750 place = xs_dict_set(place, "name", label);
1751 place = xs_dict_set(place, "latitude", d_la);
1752 place = xs_dict_set(place, "longitude", d_lo);
1753 }
1754
1755 return place;
1756}
1757
1758
1759xs_dict *msg_actor(snac *snac)
1760/* create a Person message for this actor */
1761{
1762 xs *ctxt = xs_list_new();
1763 xs *icon = xs_dict_new();
1764 xs *keys = xs_dict_new();
1765 xs *tags = xs_list_new();
1766 xs *avtr = NULL;
1767 xs *kid = NULL;
1768 xs *f_bio = NULL;
1769 xs_dict *msg = NULL;
1770 const char *p;
1771 int n;
1772
1773 /* everybody loves some caching */
1774 if (time(NULL) - object_mtime(snac->actor) < 3 * 3600 &&
1775 valid_status(object_get(snac->actor, &msg))) {
1776 snac_debug(snac, 2, xs_fmt("Returning cached actor %s", snac->actor));
1777
1778 return msg;
1779 }
1780
1781 msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
1782
1783 /* change the @context (is this really necessary?) */
1784 ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
1785 ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
1786 msg = xs_dict_set(msg, "@context", ctxt);
1787
1788 msg = xs_dict_set(msg, "url", snac->actor);
1789 msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
1790 msg = xs_dict_set(msg, "preferredUsername", snac->uid);
1791 msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
1792
1793 // this exists so we get the emoji tags from our name too.
1794 // and then we just throw away the result, because it's kinda useless to have markdown in the display name.
1795 xs *name_dummy = not_really_markdown(xs_dict_get(snac->config, "name"), NULL, &tags);
1796
1797 xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
1798 f_bio = process_tags(snac, f_bio_2, &tags);
1799 msg = xs_dict_set(msg, "summary", f_bio);
1800 msg = xs_dict_set(msg, "tag", tags);
1801
1802 char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
1803 for (n = 0; folders[n]; n++) {
1804 xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
1805 msg = xs_dict_set(msg, folders[n], f);
1806 }
1807
1808 p = xs_dict_get(snac->config, "avatar");
1809
1810 if (*p == '\0')
1811 avtr = xs_fmt("%s/susie.png", srv_baseurl);
1812 else
1813 avtr = xs_dup(p);
1814
1815 icon = xs_dict_append(icon, "type", "Image");
1816 icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
1817 icon = xs_dict_append(icon, "url", avtr);
1818 msg = xs_dict_set(msg, "icon", icon);
1819
1820 kid = xs_fmt("%s#main-key", snac->actor);
1821
1822 keys = xs_dict_append(keys, "id", kid);
1823 keys = xs_dict_append(keys, "owner", snac->actor);
1824 keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
1825 msg = xs_dict_set(msg, "publicKey", keys);
1826
1827 /* if the "bot" config field is set to true, change type to "Service" */
1828 if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
1829 msg = xs_dict_set(msg, "type", "Service");
1830
1831 /* if it's named "relay", then identify as an "Application" */
1832 if (strcmp(snac->uid, "relay") == 0)
1833 msg = xs_dict_set(msg, "type", "Application");
1834
1835 /* add the header image, if there is one defined */
1836 const char *header = xs_dict_get(snac->config, "header");
1837 if (!xs_is_null(header)) {
1838 xs *d = xs_dict_new();
1839 d = xs_dict_append(d, "type", "Image");
1840 d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
1841 d = xs_dict_append(d, "url", header);
1842 msg = xs_dict_set(msg, "image", d);
1843 }
1844
1845 /* add the metadata as attachments of PropertyValue */
1846 xs *metadata = NULL;
1847 const xs_dict *md = xs_dict_get(snac->config, "metadata");
1848
1849 if (xs_type(md) == XSTYPE_DICT)
1850 metadata = xs_dup(md);
1851 else
1852 if (xs_type(md) == XSTYPE_STRING) {
1853 metadata = xs_dict_new();
1854 xs *l = xs_split(md, "\n");
1855 const char *ll;
1856
1857 xs_list_foreach(l, ll) {
1858 xs *kv = xs_split_n(ll, "=", 1);
1859 const char *k = xs_list_get(kv, 0);
1860 const char *v = xs_list_get(kv, 1);
1861
1862 if (k && v) {
1863 xs *kk = xs_strip_i(xs_dup(k));
1864 xs *vv = xs_strip_i(xs_dup(v));
1865 metadata = xs_dict_set(metadata, kk, vv);
1866 }
1867 }
1868 }
1869
1870 if (xs_type(metadata) == XSTYPE_DICT) {
1871 xs *attach = xs_list_new();
1872 const xs_str *k;
1873 const xs_str *v;
1874
1875 int c = 0;
1876 while (xs_dict_next(metadata, &k, &v, &c)) {
1877 xs *d = xs_dict_new();
1878
1879 xs *k2 = encode_html(k);
1880 xs *v2 = NULL;
1881
1882 if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
1883 xs *t = encode_html(v);
1884 v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
1885 }
1886 else
1887 v2 = encode_html(v);
1888
1889 d = xs_dict_append(d, "type", "PropertyValue");
1890 d = xs_dict_append(d, "name", k2);
1891 d = xs_dict_append(d, "value", v2);
1892
1893 attach = xs_list_append(attach, d);
1894 }
1895
1896 msg = xs_dict_set(msg, "attachment", attach);
1897 }
1898
1899 /* use shared inboxes? */
1900 if (xs_is_true(xs_dict_get(srv_config, "shared_inboxes")) || strcmp(snac->uid, "relay") == 0) {
1901 xs *d = xs_dict_new();
1902 xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
1903 d = xs_dict_append(d, "sharedInbox", si);
1904 msg = xs_dict_set(msg, "endpoints", d);
1905 }
1906
1907 /* does this user have an aka? */
1908 const char *aka = xs_dict_get(snac->config, "alias");
1909 if (xs_type(aka) == XSTYPE_STRING && *aka) {
1910 xs *loaka = xs_list_append(xs_list_new(), aka);
1911
1912 msg = xs_dict_set(msg, "alsoKnownAs", loaka);
1913 }
1914
1915 const xs_val *manually = xs_dict_get(snac->config, "approve_followers");
1916 msg = xs_dict_set(msg, "manuallyApprovesFollowers",
1917 xs_stock(xs_is_true(manually) ? XSTYPE_TRUE : XSTYPE_FALSE));
1918
1919 /* if there are location coords, create a Place object */
1920 xs *location = msg_actor_place(snac, "Home");
1921 if (xs_type(location) == XSTYPE_DICT)
1922 msg = xs_dict_set(msg, "location", location);
1923
1924 xs *webfinger = xs_fmt("%s@%s", snac->uid, xs_dict_get(srv_config, "host"));
1925 msg = xs_dict_set(msg, "webfinger", webfinger);
1926
1927 /* cache it */
1928 snac_debug(snac, 1, xs_fmt("Caching actor %s", snac->actor));
1929 object_add_ow(snac->actor, msg);
1930
1931 return msg;
1932}
1933
1934
1935xs_dict *msg_create(snac *snac, const xs_dict *object)
1936/* creates a 'Create' message */
1937{
1938 xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
1939 const xs_val *v;
1940
1941 if ((v = get_atto(object)))
1942 msg = xs_dict_append(msg, "attributedTo", v);
1943
1944 if ((v = xs_dict_get(object, "cc")))
1945 msg = xs_dict_append(msg, "cc", v);
1946
1947 if ((v = xs_dict_get(object, "to")))
1948 msg = xs_dict_append(msg, "to", v);
1949 else
1950 msg = xs_dict_append(msg, "to", public_address);
1951
1952 return msg;
1953}
1954
1955
1956xs_dict *msg_undo(snac *snac, const xs_val *object)
1957/* creates an 'Undo' message */
1958{
1959 xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
1960 const char *to;
1961
1962 if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
1963 msg = xs_dict_append(msg, "to", to);
1964
1965 return msg;
1966}
1967
1968
1969xs_dict *msg_delete(snac *snac, const char *id)
1970/* creates a 'Delete' + 'Tombstone' for a local entry */
1971{
1972 xs *tomb = xs_dict_new();
1973 xs_dict *msg = NULL;
1974
1975 /* sculpt the tombstone */
1976 tomb = xs_dict_append(tomb, "type", "Tombstone");
1977 tomb = xs_dict_append(tomb, "id", id);
1978
1979 /* now create the Delete */
1980 msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
1981
1982 xs *to = xs_list_new();
1983 xs *involved = object_likes(id);
1984 xs *boosts = object_announces(id);
1985 xs *children = object_children(id);
1986 const char *md5;
1987
1988 involved = xs_list_cat(involved, boosts);
1989 involved = xs_list_cat(involved, children);
1990
1991 /* add everybody */
1992 to = xs_list_append(to, public_address);
1993
1994 /* add actors that liked, boosted or replied to this */
1995 xs_list_foreach(involved, md5) {
1996 xs *actor = NULL;
1997
1998 if (valid_status(object_get_by_md5(md5, &actor))) {
1999 const char *id = xs_dict_get(actor, "id");
2000 const char *atto = get_atto(actor);
2001
2002 if (xs_is_string(atto))
2003 to = xs_list_append(to, atto);
2004 else
2005 if (xs_is_string(id))
2006 to = xs_list_append(to, id);
2007 }
2008 }
2009
2010 msg = xs_dict_append(msg, "to", to);
2011
2012 return msg;
2013}
2014
2015
2016xs_dict *msg_follow(snac *snac, const char *q)
2017/* creates a 'Follow' message */
2018{
2019 xs *actor_o = NULL;
2020 xs *actor = NULL;
2021 xs_dict *msg = NULL;
2022 int status;
2023
2024 xs *url_or_uid = xs_strip_i(xs_str_new(q));
2025
2026 if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
2027 actor = xs_dup(url_or_uid);
2028 else
2029 if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
2030 snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
2031 return NULL;
2032 }
2033
2034 /* request the actor */
2035 status = actor_request(snac, actor, &actor_o);
2036
2037 if (valid_status(status)) {
2038 /* check if the actor is an alias */
2039 const char *r_actor = xs_dict_get(actor_o, "id");
2040
2041 if (r_actor && strcmp(actor, r_actor) != 0) {
2042 snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
2043 }
2044
2045 msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
2046 }
2047 else
2048 snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
2049
2050 return msg;
2051}
2052
2053
2054xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
2055 const xs_str *in_reply_to, const xs_list *attach,
2056 int scope, const char *lang_str, const char *msg_date)
2057/* creates a 'Note' message */
2058/* scope: 0, public; 1, private (mentioned only); 2, "quiet public"; 3, followers only */
2059{
2060 xs *ntid = tid(0);
2061 xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
2062 xs *ctxt = NULL;
2063 xs *fc2 = NULL;
2064 xs *fc1 = NULL;
2065 xs *to = NULL;
2066 xs *cc = xs_list_new();
2067 xs *irt = NULL;
2068 xs *tag = xs_list_new();
2069 xs *atls = xs_list_new();
2070
2071 /* discard non-parseable dates */
2072 if (!xs_is_string(msg_date) || xs_parse_iso_date(msg_date, 0) == 0)
2073 msg_date = NULL;
2074
2075 xs_dict *msg = msg_base(snac, "Note", id, NULL, xs_or(msg_date, "@now"), NULL);
2076 xs_list *p;
2077 const xs_val *v;
2078
2079 const int priv = (scope == SCOPE_MENTIONED || scope == SCOPE_FOLLOWERS);
2080
2081 if (rcpts == NULL)
2082 to = xs_list_new();
2083 else {
2084 if (xs_type(rcpts) == XSTYPE_STRING) {
2085 to = xs_list_new();
2086 to = xs_list_append(to, rcpts);
2087 }
2088 else
2089 to = xs_dup(rcpts);
2090 }
2091
2092 /* format the content */
2093 fc2 = not_really_markdown(content, &atls, &tag);
2094
2095 if (in_reply_to != NULL && *in_reply_to) {
2096 xs *p_msg = NULL;
2097 xs *wrk = NULL;
2098
2099 /* demand this thing */
2100 timeline_request(snac, &in_reply_to, &wrk, 0);
2101
2102 if (valid_status(object_get(in_reply_to, &p_msg))) {
2103 /* add this author as recipient */
2104 const char *a, *v;
2105
2106 if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
2107 to = xs_list_append(to, a);
2108
2109 /* add this author to the tag list as a mention */
2110 if (!xs_is_null(a)) {
2111 xs *l = xs_split(a, "/");
2112 xs *actor_o = NULL;
2113
2114 if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
2115 const char *uname = xs_dict_get(actor_o, "preferredUsername");
2116
2117 if (!xs_is_null(uname) && *uname) {
2118 xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
2119
2120 xs *t = xs_dict_new();
2121
2122 t = xs_dict_append(t, "type", "Mention");
2123 t = xs_dict_append(t, "href", a);
2124 t = xs_dict_append(t, "name", handle);
2125
2126 tag = xs_list_append(tag, t);
2127 }
2128 }
2129 }
2130
2131 /* get the context, if there is one */
2132 if ((v = xs_dict_get(p_msg, "context")))
2133 ctxt = xs_dup(v);
2134
2135 /* propagate the conversation field, if there is one */
2136 if ((v = xs_dict_get(p_msg, "conversation")))
2137 msg = xs_dict_append(msg, "conversation", v);
2138
2139 /* if this message is public or unlisted, ours will also be */
2140 const int orig_scope = get_msg_visibility(p_msg);
2141 if (!priv && orig_scope == SCOPE_PUBLIC && xs_list_in(to, public_address) == -1)
2142 to = xs_list_append(to, public_address);
2143 else
2144 if (!priv && orig_scope == SCOPE_UNLISTED && xs_list_in(cc, public_address) == -1)
2145 cc = xs_list_append(cc, public_address);
2146 }
2147
2148 irt = xs_dup(in_reply_to);
2149 }
2150 else
2151 irt = xs_val_new(XSTYPE_NULL);
2152
2153 /* extract the mentions and hashtags and convert the content */
2154 fc1 = process_tags(snac, fc2, &tag);
2155
2156 /* create the attachment list, if there are any */
2157 if (!xs_is_null(attach)) {
2158 xs_list_foreach(attach, v) {
2159 const char *url = xs_list_get(v, 0);
2160 const char *alt = xs_list_get(v, 1);
2161 const char *mime = xs_mime_by_ext(url);
2162 int add = 1;
2163
2164 /* check if it's already here */
2165 const xs_dict *ad;
2166 xs_list_foreach(atls, ad) {
2167 if (strcmp(xs_dict_get_def(ad, "url", ""), url) == 0) {
2168 add = 0;
2169 break;
2170 }
2171 }
2172
2173 if (add) {
2174 xs *d = xs_dict_new();
2175 d = xs_dict_append(d, "mediaType", mime);
2176 d = xs_dict_append(d, "url", url);
2177 d = xs_dict_append(d, "name", alt);
2178 d = xs_dict_append(d, "type",
2179 xs_startswith(mime, "image/") ? "Image" : "Document");
2180
2181 atls = xs_list_append(atls, d);
2182 }
2183 }
2184 }
2185
2186 if (ctxt == NULL)
2187 ctxt = xs_fmt("%s#ctxt", id);
2188
2189 /* add all mentions to the appropriate field */
2190 p = tag;
2191 while (xs_list_iter(&p, &v)) {
2192 if (xs_type(v) == XSTYPE_DICT) {
2193 const char *t;
2194
2195 if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
2196 if (!xs_is_null(t = xs_dict_get(v, "href"))) {
2197 if (scope == SCOPE_MENTIONED) {
2198 /* for DMs, mentions go to 'to' */
2199 to = xs_list_append(to, t);
2200 } else {
2201 /* for other visibility levels, mentions go to 'cc' */
2202 cc = xs_list_append(cc, t);
2203 }
2204 }
2205 }
2206 }
2207 }
2208
2209 if (scope == SCOPE_UNLISTED) {
2210 /* Mastodon's "quiet public": remove from to, add public address to cc */
2211 int idx;
2212 if ((idx = xs_list_in(to, public_address)) != -1)
2213 to = xs_list_del(to, idx);
2214 if (xs_list_in(cc, public_address) == -1)
2215 cc = xs_list_append(cc, public_address);
2216 }
2217 else
2218 if (scope == SCOPE_FOLLOWERS) {
2219 /* followers-only: add followers collection to to */
2220 xs *followers_url = xs_fmt("%s/followers", snac->actor);
2221 if (xs_list_in(to, followers_url) == -1)
2222 to = xs_list_append(to, followers_url);
2223 }
2224 else
2225 if (scope == SCOPE_PUBLIC) {
2226 /* public: ensure public address is in to and not in cc */
2227 int idx;
2228 if ((idx = xs_list_in(cc, public_address)) != -1)
2229 cc = xs_list_del(cc, idx);
2230 if (xs_list_in(to, public_address) == -1)
2231 to = xs_list_append(to, public_address);
2232 }
2233
2234 /* delete all cc recipients that also are in the to */
2235 p = to;
2236 while (xs_list_iter(&p, &v)) {
2237 int i;
2238
2239 if ((i = xs_list_in(cc, v)) != -1)
2240 cc = xs_list_del(cc, i);
2241 }
2242
2243 msg = xs_dict_append(msg, "attributedTo", snac->actor);
2244 msg = xs_dict_append(msg, "summary", "");
2245 msg = xs_dict_append(msg, "content", fc1);
2246 msg = xs_dict_append(msg, "context", ctxt);
2247 msg = xs_dict_append(msg, "url", id);
2248 msg = xs_dict_append(msg, "to", to);
2249 msg = xs_dict_append(msg, "cc", cc);
2250 msg = xs_dict_append(msg, "inReplyTo", irt);
2251 msg = xs_dict_append(msg, "tag", tag);
2252
2253 msg = xs_dict_append(msg, "sourceContent", content);
2254
2255 if (xs_list_len(atls))
2256 msg = xs_dict_append(msg, "attachment", atls);
2257
2258 /* set language content map */
2259 if (xs_type(lang_str) == XSTYPE_STRING) {
2260 /* split at the first _ */
2261 xs *l0 = xs_split(lang_str, "_");
2262 const char *lang = xs_list_get(l0, 0);
2263
2264 if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
2265 /* a valid ISO language id */
2266 xs *cmap = xs_dict_new();
2267 cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
2268 msg = xs_dict_set(msg, "contentMap", cmap);
2269 }
2270 }
2271
2272 if (!priv) {
2273 /* create the replies object */
2274 xs *replies = xs_dict_new();
2275 xs *r_id = xs_replace(id, "/p/", "/r/");
2276 xs *h_id = xs_fmt("%s", r_id);
2277 xs *rp = msg_replies(snac, id, 0);
2278
2279 replies = xs_dict_set(replies, "id", h_id);
2280 replies = xs_dict_set(replies, "type", "Collection");
2281 replies = xs_dict_set(replies, "first", rp);
2282
2283 msg = xs_dict_set(msg, "replies", replies);
2284 }
2285
2286 return msg;
2287}
2288
2289
2290xs_dict *msg_ping(snac *user, const char *rcpt)
2291/* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
2292{
2293 xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
2294
2295 msg = xs_dict_append(msg, "to", rcpt);
2296
2297 return msg;
2298}
2299
2300
2301xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
2302/* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
2303{
2304 xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
2305
2306 msg = xs_dict_append(msg, "to", rcpt);
2307
2308 return msg;
2309}
2310
2311
2312xs_dict *msg_move(snac *user, const char *new_account)
2313/* creates a Move message (to move the user to new_account) */
2314{
2315 xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
2316
2317 msg = xs_dict_append(msg, "target", new_account);
2318
2319 return msg;
2320}
2321
2322
2323xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
2324 const xs_list *opts, int multiple, int end_secs)
2325/* creates a Question message */
2326{
2327 xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0, NULL, NULL);
2328 const xs_number *max_options = xs_dict_get(srv_config, "max_poll_options");
2329 const xs_number *max_length = xs_dict_get(srv_config, "max_poll_option_length");
2330 xs_set seen;
2331
2332 size_t max_line = 60;
2333 int max = 8;
2334 if (xs_type(max_options) == XSTYPE_NUMBER)
2335 max = xs_number_get(max_options);
2336 if (xs_type(max_length) == XSTYPE_NUMBER)
2337 max_line = xs_number_get(max_length);
2338
2339 msg = xs_dict_set(msg, "type", "Question");
2340
2341 /* make it non-editable */
2342 msg = xs_dict_del(msg, "sourceContent");
2343
2344 xs *o = xs_list_new();
2345 xs_list *p = (xs_list *)opts;
2346 const xs_str *v;
2347 xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
2348
2349 xs_set_init(&seen);
2350
2351 while (max && xs_list_iter(&p, &v)) {
2352 if (*v) {
2353 xs *v2 = xs_dup(v);
2354 xs *d = xs_dict_new();
2355
2356 if (strlen(v2) > max_line) {
2357 v2[max_line] = '\0';
2358 v2 = xs_str_cat(v2, "...");
2359 }
2360
2361 if (xs_set_add(&seen, v2) == 1) {
2362 d = xs_dict_append(d, "type", "Note");
2363 d = xs_dict_append(d, "name", v2);
2364 d = xs_dict_append(d, "replies", replies);
2365 o = xs_list_append(o, d);
2366
2367 max--;
2368 }
2369 }
2370 }
2371
2372 xs_set_free(&seen);
2373
2374 msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
2375
2376 /* set the end time */
2377 time_t t = time(NULL) + end_secs;
2378 xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
2379
2380 msg = xs_dict_append(msg, "endTime", et);
2381
2382 return msg;
2383}
2384
2385int get_msg_visibility(const xs_dict *msg)
2386/* determine visibility from message based on CC, TO and /followers mark */
2387{
2388 const xs_val *to = xs_dict_get(msg, "to");
2389 const xs_val *cc = xs_dict_get(msg, "cc");
2390
2391 /* check if it's unlisted (public in cc but not in to) */
2392 int pub_in_to = 0;
2393 int pub_in_cc = 0;
2394
2395 if ((xs_type(to) == XSTYPE_STRING && strcmp(to, public_address) == 0) ||
2396 (xs_type(to) == XSTYPE_LIST && xs_list_in(to, public_address) != -1))
2397 pub_in_to = 1;
2398
2399 if ((xs_type(cc) == XSTYPE_STRING && strcmp(cc, public_address) == 0) ||
2400 (xs_type(cc) == XSTYPE_LIST && xs_list_in(cc, public_address) != -1))
2401 pub_in_cc = 1;
2402
2403 if (!pub_in_to && pub_in_cc) {
2404 return SCOPE_UNLISTED;
2405 }
2406 if (pub_in_to && !pub_in_cc) {
2407 return SCOPE_PUBLIC;
2408 }
2409
2410 xs *followers_url = xs_fmt("%s/followers", xs_dict_get(msg, "attributedTo"));
2411
2412 if ((xs_type(to) == XSTYPE_STRING && strcmp(to, followers_url) == 0) ||
2413 (xs_type(to) == XSTYPE_LIST && xs_list_in(to, followers_url) != -1))
2414 return SCOPE_FOLLOWERS;
2415 else
2416 return SCOPE_MENTIONED;
2417 /* should be unreachable, someone violated the protocol. */
2418 /* better treat it as followers-only to make sure we don't leak information.*/
2419 return SCOPE_FOLLOWERS;
2420}
2421
2422int update_question(snac *user, const char *id)
2423/* updates the poll counts */
2424{
2425 xs *msg = NULL;
2426 xs *rcnt = xs_dict_new();
2427 xs *lopts = xs_list_new();
2428 const xs_list *opts;
2429 xs_list *p;
2430 const xs_val *v;
2431
2432 /* get the object */
2433 if (!valid_status(object_get(id, &msg)))
2434 return -1;
2435
2436 /* closed? do nothing more */
2437 if (xs_dict_get(msg, "closed"))
2438 return -2;
2439
2440 /* get the options */
2441 if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
2442 (opts = xs_dict_get(msg, "anyOf")) == NULL)
2443 return -3;
2444
2445 /* fill the initial count */
2446 int c = 0;
2447 while (xs_list_next(opts, &v, &c)) {
2448 const char *name = xs_dict_get(v, "name");
2449 if (name) {
2450 lopts = xs_list_append(lopts, name);
2451 rcnt = xs_dict_set(rcnt, name, xs_stock(0));
2452 }
2453 }
2454
2455 xs_set s;
2456 xs_set_init(&s);
2457
2458 /* iterate now the children (the votes) */
2459 xs *chld = object_children(id);
2460 p = chld;
2461 while (xs_list_iter(&p, &v)) {
2462 xs *obj = NULL;
2463
2464 if (!valid_status(object_get_by_md5(v, &obj)))
2465 continue;
2466
2467 const char *name = xs_dict_get(obj, "name");
2468 const char *atto = get_atto(obj);
2469
2470 if (name && atto) {
2471 /* get the current count */
2472 const xs_number *cnt = xs_dict_get(rcnt, name);
2473
2474 if (xs_type(cnt) == XSTYPE_NUMBER) {
2475 /* if it exists, increment */
2476 xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
2477 rcnt = xs_dict_set(rcnt, name, ucnt);
2478
2479 xs_set_add(&s, atto);
2480 }
2481 }
2482 }
2483
2484 xs *rcpts = xs_set_result(&s);
2485
2486 /* create a new list of options with their new counts */
2487 xs *nopts = xs_list_new();
2488 p = lopts;
2489 while (xs_list_iter(&p, &v)) {
2490 const xs_number *cnt = xs_dict_get(rcnt, v);
2491
2492 if (xs_type(cnt) == XSTYPE_NUMBER) {
2493 xs *d1 = xs_dict_new();
2494 xs *d2 = xs_dict_new();
2495
2496 d2 = xs_dict_append(d2, "type", "Collection");
2497 d2 = xs_dict_append(d2, "totalItems", cnt);
2498
2499 d1 = xs_dict_append(d1, "type", "Note");
2500 d1 = xs_dict_append(d1, "name", v);
2501 d1 = xs_dict_append(d1, "replies", d2);
2502
2503 nopts = xs_list_append(nopts, d1);
2504 }
2505 }
2506
2507 /* update the list */
2508 msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
2509
2510 /* due date? */
2511 int closed = 0;
2512 const char *end_time = xs_dict_get(msg, "endTime");
2513 if (!xs_is_null(end_time)) {
2514 xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
2515
2516 /* is now greater than the endTime? */
2517 if (strcmp(now, end_time) >= 0) {
2518 xs *et = xs_dup(end_time);
2519 msg = xs_dict_set(msg, "closed", et);
2520
2521 closed = 1;
2522 }
2523 }
2524
2525 /* update the count of voters */
2526 xs *vcnt = xs_number_new(xs_list_len(rcpts));
2527 msg = xs_dict_set(msg, "votersCount", vcnt);
2528 msg = xs_dict_set(msg, "cc", rcpts);
2529
2530 /* store */
2531 object_add_ow(id, msg);
2532
2533 snac_debug(user, 1, xs_fmt("recounted poll %s", id));
2534 timeline_touch(user);
2535
2536 /* send an update message to all voters */
2537 xs *u_msg = msg_update(user, msg);
2538 u_msg = xs_dict_set(u_msg, "cc", rcpts);
2539
2540 enqueue_message(user, u_msg);
2541
2542 if (closed) {
2543 xs *c_msg = msg_update(user, msg);
2544 notify(user, "Update", "Question", user->actor, c_msg);
2545 }
2546
2547 return 0;
2548}
2549
2550
2551/** queues **/
2552
2553int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
2554/* processes an ActivityPub message from the input queue */
2555/* return values: -1, fatal error; 0, transient error, retry;
2556 1, processed and done; 2, propagate to users (only when no user is set) */
2557{
2558 const char *actor = xs_dict_get(msg, "actor");
2559 const char *type = xs_dict_get(msg, "type");
2560 xs *actor_o = NULL;
2561 int a_status;
2562 int do_notify = 0;
2563
2564 if (xs_is_null(actor) || *actor == '\0') {
2565 srv_debug(0, xs_fmt("malformed message (bad actor)"));
2566 return -1;
2567 }
2568
2569 /* this instance is alive */
2570 instance_failure(actor, 2);
2571
2572 /* question votes may not have a type */
2573 if (xs_is_null(type))
2574 type = "Note";
2575
2576 /* reject uninteresting messages right now */
2577 if (xs_match(type, "Add|View|Reject|Read|Remove")) {
2578 srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
2579
2580 /* archive the ignored activity */
2581 xs *ntid = tid(0);
2582 xs *fn = xs_fmt("%s/ignored/%s.json", srv_basedir, ntid);
2583 FILE *f;
2584
2585 if ((f = fopen(fn, "w")) != NULL) {
2586 xs_json_dump(msg, 4, f);
2587 fclose(f);
2588 }
2589
2590 return -1;
2591 }
2592
2593 if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) {
2594 srv_log(xs_fmt("Dropping EmojiReact from %s due to admin configuration", actor));
2595 return -1;
2596 }
2597
2598 const char *object, *utype;
2599
2600 object = xs_dict_get(msg, "object");
2601 if (object != NULL && xs_type(object) == XSTYPE_DICT)
2602 utype = xs_dict_get(object, "type");
2603 else
2604 utype = "(null)";
2605
2606 /* special case for Delete messages */
2607 if (strcmp(type, "Delete") == 0) {
2608 /* if the actor is not here, do not even try */
2609 if (!object_here(actor)) {
2610 srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
2611 return -1;
2612 }
2613
2614 /* discard crap */
2615 if (xs_is_null(object)) {
2616 srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
2617 return -1;
2618 }
2619
2620 /* also discard if the object to be deleted is not here */
2621 const char *obj_id = object;
2622 if (xs_type(obj_id) == XSTYPE_DICT)
2623 obj_id = xs_dict_get(obj_id, "id");
2624
2625 if (xs_is_null(obj_id) || !object_here(obj_id)) {
2626 srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
2627 return -1;
2628 }
2629 }
2630
2631 /* bring the actor */
2632 a_status = actor_request(snac, actor, &actor_o);
2633
2634 /* do not retry permanent failures */
2635 if (a_status == HTTP_STATUS_NOT_FOUND
2636 || a_status == HTTP_STATUS_GONE
2637 || a_status < 0) {
2638 srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
2639 return -1;
2640 }
2641
2642 if (!valid_status(a_status)) {
2643 /* do not retry 'Delete' messages */
2644 if (strcmp(type, "Delete") == 0) {
2645 srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
2646 return -1;
2647 }
2648
2649 /* other actor download errors */
2650
2651 /* the actor may require a signed request; propagate if no user is set */
2652 if (snac == NULL)
2653 return 2;
2654
2655 /* may need a retry */
2656 srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
2657 return 0;
2658 }
2659
2660 /* check the signature */
2661 xs *sig_err = NULL;
2662
2663 if (!check_signature(req, &sig_err)) {
2664 srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
2665
2666 srv_archive_error("check_signature", sig_err, req, msg);
2667 return -1;
2668 }
2669
2670 /* if no user is set, no further checks can be done; propagate */
2671 if (snac == NULL)
2672 return 2;
2673
2674 /* reject messages that are not for this user */
2675 if (!is_msg_for_me(snac, msg)) {
2676 snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
2677
2678 return 1;
2679 }
2680
2681 /* if it's a DM from someone we don't follow, reject the message */
2682 if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
2683 if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
2684 !following_check(snac, actor)) {
2685 snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
2686
2687 return 1;
2688 }
2689 }
2690
2691 /* check the minimum acceptable account age */
2692 int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
2693
2694 if (min_account_age > 0) {
2695 const char *actor_date = xs_dict_get(actor_o, "published");
2696 if (!xs_is_null(actor_date)) {
2697 time_t actor_t = xs_parse_iso_date(actor_date, 0);
2698
2699 if (actor_t < 950000000) {
2700 snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
2701 actor, actor_date));
2702
2703 return 1;
2704 }
2705
2706 if (actor_t > 0) {
2707 int td = (int)(time(NULL) - actor_t);
2708
2709 snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
2710
2711 if (td < min_account_age) {
2712 snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
2713 actor, td));
2714
2715 return 1;
2716 }
2717 }
2718 }
2719 else
2720 snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
2721 }
2722
2723 if (strcmp(type, "Follow") == 0) { /** **/
2724 const char *id = xs_dict_get(msg, "id");
2725
2726 if (xs_is_null(id)) {
2727 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2728 }
2729 else
2730 if (!follower_check(snac, actor)) {
2731 /* ensure the actor object is here */
2732 if (!object_here(actor)) {
2733 xs *actor_obj = NULL;
2734 actor_request(snac, actor, &actor_obj);
2735 object_add(actor, actor_obj);
2736 }
2737
2738 if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) {
2739 pending_add(snac, actor, msg);
2740
2741 snac_log(snac, xs_fmt("new pending follower approval %s", actor));
2742 }
2743 else {
2744 /* automatic following */
2745 xs *f_msg = xs_dup(msg);
2746 xs *reply = msg_accept(snac, f_msg, actor);
2747
2748 post_message(snac, actor, reply);
2749
2750 if (xs_is_null(xs_dict_get(f_msg, "published"))) {
2751 /* add a date if it doesn't include one (Mastodon) */
2752 xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
2753 f_msg = xs_dict_set(f_msg, "published", date);
2754 }
2755
2756 timeline_add(snac, id, f_msg);
2757
2758 follower_add(snac, actor);
2759
2760 snac_log(snac, xs_fmt("new follower %s", actor));
2761 }
2762
2763 do_notify = 1;
2764 }
2765 else
2766 snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
2767 }
2768 else
2769 if (strcmp(type, "Undo") == 0) { /** **/
2770 const char *id = xs_dict_get(object, "object");
2771 const char *content = xs_dict_get(object, "content");
2772 /* misskey sends emojis as like + tag */
2773 xs *cd = xs_dup(content);
2774 const char *sna = cd;
2775 const xs_dict *tag = xs_dict_get(object, "tag");
2776 unsigned int utf = xs_utf8_dec((const char **)&sna);
2777
2778 int isEmoji = 0;
2779 if (xs_is_emoji(utf) || (tag && xs_list_len(tag) > 0))
2780 isEmoji = 1;
2781
2782 if (xs_type(object) != XSTYPE_DICT) {
2783 snac_debug(snac, 1, xs_fmt("undo: overriding utype %s | %s | %s",
2784 utype, id, actor));
2785 utype = "Follow";
2786 }
2787
2788 if (strcmp(utype, "Follow") == 0) { /** **/
2789 if (!id) {
2790 snac_log(snac, xs_fmt("no id (msg.object.object) when "
2791 "handling follow: %s", actor));
2792 return 1;
2793 }
2794 if (strcmp(id, snac->actor) != 0)
2795 snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
2796 else
2797 if (valid_status(follower_del(snac, actor))) {
2798 snac_log(snac, xs_fmt("no longer following us %s", actor));
2799 do_notify = 1;
2800 }
2801 else
2802 if (pending_check(snac, actor)) {
2803 pending_del(snac, actor);
2804 snac_log(snac, xs_fmt("cancelled pending follow from %s", actor));
2805 }
2806 else
2807 snac_log(snac, xs_fmt("error deleting follower %s", actor));
2808 }
2809 /* *key emojis are like w/ Emoji tag */
2810 else
2811 if ((isEmoji || strcmp(utype, "EmojiReact") == 0) &&
2812 (content && strcmp(content, "♥") != 0)) {
2813 const xs_val *mid = xs_dict_get(object, "id");
2814 int status = object_rm_emoji_react((char *)id, mid);
2815 /* ensure *key notifications type */
2816 utype = "EmojiReact";
2817
2818 snac_log(snac, xs_fmt("Undo 'EmojiReact' for %s %d", id, status));
2819 }
2820 else
2821 if (strcmp(utype, "Like") == 0) { /** **/
2822 int status = object_unadmire(id, actor, 1);
2823
2824 snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
2825 }
2826 else
2827 if (strcmp(utype, "Announce") == 0) { /** **/
2828 int status = HTTP_STATUS_OK;
2829
2830 /* commented out: if a followed user boosts something that
2831 is requested and then unboosts, the post remains here,
2832 but with no apparent reason, and that is confusing */
2833 //status = object_unadmire(id, actor, 0);
2834
2835 snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
2836 }
2837 else
2838 snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
2839 }
2840 else
2841 if (strcmp(type, "Create") == 0) { /** **/
2842 if (is_muted(snac, actor)) {
2843 snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
2844 return 1;
2845 }
2846
2847 if (xs_match(utype, "Note|Article")) { /** **/
2848 const char *id = xs_dict_get(object, "id");
2849 const char *in_reply_to = get_in_reply_to(object);
2850 const char *atto = get_atto(object);
2851 xs *wrk = NULL;
2852
2853 if (xs_is_null(id))
2854 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2855 else
2856 if (xs_is_null(atto))
2857 snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
2858 else
2859 if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
2860 snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
2861 }
2862 else {
2863 if (content_match("filter_reject.txt", object)) {
2864 snac_log(snac, xs_fmt("rejected by content %s", id));
2865 return 1;
2866 }
2867
2868 if (strcmp(actor, atto) != 0)
2869 snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
2870
2871 timeline_request(snac, &in_reply_to, &wrk, 0);
2872
2873 const char *quoted_id = xs_or(xs_dict_get(object, "quoteUri"), xs_dict_get(object, "quoteUrl"));
2874
2875 if (xs_is_string(quoted_id) && xs_match(quoted_id, "https://*|http://*")) { /** **/
2876 xs *quoted_post = NULL;
2877 int status;
2878
2879 if (valid_status(status = activitypub_request(snac, quoted_id, "ed_post))) {
2880 /* got quoted post */
2881 object_add(quoted_id, quoted_post);
2882 }
2883
2884 snac_debug(snac, 1, xs_fmt("retrieving quoted post %s %d", quoted_id, status));
2885 }
2886
2887 if (timeline_add(snac, id, object)) {
2888 snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
2889 do_notify = 1;
2890 }
2891
2892 /* if it has a "name" field, it may be a vote for a question */
2893 const char *name = xs_dict_get(object, "name");
2894
2895 if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
2896 update_question(snac, in_reply_to);
2897 }
2898 }
2899 else
2900 if (strcmp(utype, "Question") == 0) { /** **/
2901 const char *id = xs_dict_get(object, "id");
2902
2903 if (xs_is_null(id))
2904 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2905 else
2906 if (timeline_add(snac, id, object))
2907 snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
2908 }
2909 else
2910 if (xs_match(utype, "Audio|Video|Event")) { /** **/
2911 const char *id = xs_dict_get(object, "id");
2912
2913 if (xs_is_null(id))
2914 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2915 else
2916 if (timeline_add(snac, id, object))
2917 snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
2918 }
2919 else
2920 snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
2921 }
2922 else
2923 if (strcmp(type, "Accept") == 0) { /** **/
2924 if (strcmp(utype, "(null)") == 0) {
2925 const char *obj_id = xs_dict_get(msg, "object");
2926
2927 /* if the accepted object id is a string that may
2928 be created by us, it's a follow */
2929 if (xs_type(obj_id) == XSTYPE_STRING &&
2930 xs_startswith(obj_id, srv_baseurl) &&
2931 xs_endswith(obj_id, "/Follow"))
2932 utype = "Follow";
2933 }
2934
2935 if (strcmp(utype, "Follow") == 0) { /** **/
2936 if (following_check(snac, actor)) {
2937 following_add(snac, actor, msg);
2938 snac_log(snac, xs_fmt("confirmed follow from %s", actor));
2939
2940 /* request a bit of this fellow's outbox */
2941 enqueue_collect_outbox(snac, actor);
2942 }
2943 else
2944 snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
2945 }
2946 else
2947 if (strcmp(utype, "Create") == 0) {
2948 /* some implementations send Create confirmations, go figure */
2949 snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
2950 }
2951 else {
2952 srv_archive_error("accept", "ignored Accept", req, msg);
2953 snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
2954 }
2955 }
2956 else
2957 if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
2958 /* misskey sends emojis as Like + tag.
2959 * It is easier to handle them both at the same time. */
2960 const char *sna = xs_dict_get(msg, "content");
2961 const xs_dict *tag = xs_dict_get(msg, "tag");
2962 unsigned int utf = xs_utf8_dec((const char **)&sna);
2963
2964 if (xs_is_emoji(utf) || (tag && xs_list_len(tag) > 0))
2965 type = "EmojiReact";
2966
2967 if (xs_type(object) == XSTYPE_DICT)
2968 object = xs_dict_get(object, "id");
2969
2970 if (xs_is_null(object))
2971 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2972 else
2973 if (timeline_admire(snac, object, actor, 1, msg) == HTTP_STATUS_CREATED)
2974 snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
2975 else
2976 snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
2977
2978 do_notify = 1;
2979 }
2980 else
2981 if (strcmp(type, "Announce") == 0) { /** **/
2982 if (xs_type(object) == XSTYPE_DICT)
2983 object = xs_dict_get(object, "id");
2984
2985 if (xs_is_null(object))
2986 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2987 else
2988 if (is_muted(snac, actor) && !is_msg_mine(snac, object))
2989 snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
2990 else
2991 if (is_limited(snac, actor) && !is_msg_mine(snac, object))
2992 snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
2993 else {
2994 xs *a_msg = NULL;
2995 xs *wrk = NULL;
2996
2997 timeline_request(snac, &object, &wrk, 0);
2998
2999 if (valid_status(object_get(object, &a_msg))) {
3000 const char *who = get_atto(a_msg);
3001
3002 /* got the admired object: instance is [back] online */
3003 instance_failure(object, 2);
3004
3005 if (who && !is_muted(snac, who)) {
3006 /* bring the actor */
3007 xs *who_o = NULL;
3008
3009 if (valid_status(actor_request(snac, who, &who_o))) {
3010 /* don't account as such announces by our own relay */
3011 xs *this_relay = xs_fmt("%s/relay", srv_baseurl);
3012
3013 if (strcmp(actor, this_relay) != 0) {
3014 if (valid_status(timeline_admire(snac, object, actor, 0, a_msg)))
3015 snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
3016 else
3017 snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
3018 actor, object));
3019 }
3020
3021 /* distribute the post with the actor as 'proxy' */
3022 list_distribute(snac, actor, a_msg);
3023
3024 /* distribute the post to users following these hashtags */
3025 followed_hashtag_distribute(a_msg);
3026
3027 do_notify = 1;
3028 }
3029 else
3030 snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
3031 }
3032 else
3033 snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
3034 }
3035 else
3036 snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
3037 }
3038 }
3039 else
3040 if (strcmp(type, "Update") == 0) { /** **/
3041 if (xs_match(utype, "Person|Service|Application")) { /** **/
3042 actor_add(actor, xs_dict_get(msg, "object"));
3043 timeline_touch(snac);
3044
3045 snac_log(snac, xs_fmt("updated actor %s", actor));
3046 }
3047 else
3048 if (xs_match(utype, "Note|Page|Article|Video|Audio|Event")) { /** **/
3049 const char *id = xs_dict_get(object, "id");
3050
3051 if (xs_is_null(id))
3052 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
3053 else
3054 if (object_here(id)) {
3055 if (xs_startswith(id, srv_baseurl) && !xs_startswith(id, actor))
3056 snac_log(snac, xs_fmt("ignored incorrect 'Update' %s %s", actor, id));
3057 else {
3058 object_add_ow(id, object);
3059 timeline_touch(snac);
3060
3061 snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
3062 }
3063 }
3064 else
3065 snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
3066 }
3067 else
3068 if (strcmp(utype, "Question") == 0) { /** **/
3069 const char *id = xs_dict_get(object, "id");
3070 const char *closed = xs_dict_get(object, "closed");
3071
3072 if (xs_is_null(id))
3073 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
3074 else {
3075 object_add_ow(id, object);
3076 timeline_touch(snac);
3077
3078 snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
3079
3080 if (closed != NULL)
3081 do_notify = 1;
3082 }
3083 }
3084 else {
3085 srv_archive_error("unsupported_update", "unsupported_update", req, msg);
3086
3087 snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
3088 }
3089 }
3090 else
3091 if (strcmp(type, "Delete") == 0) { /** **/
3092 if (xs_type(object) == XSTYPE_DICT)
3093 object = xs_dict_get(object, "id");
3094
3095 if (xs_is_null(object))
3096 snac_log(snac, xs_fmt("malformed message: no 'id' field"));
3097 else
3098 if (object_here(object)) {
3099 if (xs_startswith(object, srv_baseurl) && !is_msg_mine(snac, object))
3100 snac_log(snac, xs_fmt("ignored incorrect 'Delete' %s %s", actor, object));
3101 else {
3102 timeline_del(snac, object);
3103 snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
3104 }
3105 }
3106 else
3107 snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
3108 }
3109 else
3110 if (strcmp(type, "Pong") == 0) { /** **/
3111 snac_log(snac, xs_fmt("'Pong' received from %s", actor));
3112 }
3113 else
3114 if (strcmp(type, "Ping") == 0) { /** **/
3115 const char *id = xs_dict_get(msg, "id");
3116
3117 snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
3118
3119 if (!xs_is_null(id)) {
3120 xs *rsp = msg_pong(snac, actor, id);
3121
3122 enqueue_output_by_actor(snac, rsp, actor, 0);
3123 }
3124 }
3125 else
3126 if (strcmp(type, "Block") == 0) { /** **/
3127 snac_debug(snac, 1, xs_fmt("'Block' received from %s", actor));
3128
3129 /* should we MUTE the actor back? */
3130 /* mute(snac, actor); */
3131
3132 if (!xs_is_true(xs_dict_get(srv_config, "disable_block_notifications")))
3133 do_notify = 1;
3134 }
3135 else
3136 if (strcmp(type, "Move") == 0) { /** **/
3137 do_notify = 1;
3138
3139 const char *old_account = xs_dict_get(msg, "object");
3140 const char *new_account = xs_dict_get(msg, "target");
3141
3142 if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
3143 if (following_check(snac, old_account)) {
3144 xs *n_actor = NULL;
3145
3146 if (valid_status(object_get(new_account, &n_actor))) {
3147 const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
3148
3149 if (xs_type(aka) == XSTYPE_LIST) {
3150 if (xs_list_in(aka, old_account) != -1) {
3151 /* all conditions met! */
3152
3153 /* follow new account */
3154 xs *f_msg = msg_follow(snac, new_account);
3155
3156 if (f_msg != NULL) {
3157 const char *new_actor = xs_dict_get(f_msg, "object");
3158 following_add(snac, new_actor, f_msg);
3159 enqueue_output_by_actor(snac, f_msg, new_actor, 0);
3160
3161 snac_log(snac, xs_fmt("'Move': following %s", new_account));
3162 }
3163
3164 /* unfollow old account */
3165 xs *of_msg = NULL;
3166
3167 if (valid_status(following_get(snac, old_account, &of_msg))) {
3168 xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
3169 following_del(snac, old_account);
3170 enqueue_output_by_actor(snac, uf_msg, old_account, 0);
3171
3172 snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
3173 }
3174 }
3175 else
3176 snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
3177 old_account, new_account));
3178 }
3179 else
3180 snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
3181 }
3182 else {
3183 snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
3184
3185 /* may be a server hiccup, retry later */
3186 return 0;
3187 }
3188 }
3189 else
3190 snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
3191 }
3192 else {
3193 snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
3194 srv_archive_error("move", "move", req, msg);
3195 }
3196 }
3197 else {
3198 srv_archive_error("unsupported_type", "unsupported_type", req, msg);
3199
3200 snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
3201 }
3202
3203 if (do_notify) {
3204 notify(snac, type, utype, actor, msg);
3205
3206 timeline_touch(snac);
3207 }
3208
3209 return 1;
3210}
3211
3212
3213int send_email(const xs_dict *mailinfo)
3214/* invoke curl */
3215{
3216 const char *url = xs_dict_get(srv_config, "smtp_url");
3217
3218 if (!xs_is_string(url) || *url == '\0') {
3219 /* revert back to old sendmail pipe behaviour */
3220 const char *msg = xs_dict_get(mailinfo, "body");
3221 FILE *f;
3222 int status;
3223 int fds[2];
3224 pid_t pid;
3225
3226 if (pipe(fds) == -1) return -1;
3227 pid = vfork();
3228 if (pid == -1) return -1;
3229 else if (pid == 0) {
3230 dup2(fds[0], 0);
3231 close(fds[0]);
3232 close(fds[1]);
3233 execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
3234 _exit(1);
3235 }
3236 close(fds[0]);
3237 if ((f = fdopen(fds[1], "w")) == NULL) {
3238 close(fds[1]);
3239 return -1;
3240 }
3241 fprintf(f, "%s\n", msg);
3242 fclose(f);
3243 if (waitpid(pid, &status, 0) == -1) return -1;
3244 return status;
3245 }
3246
3247 const char
3248 *user = xs_dict_get(srv_config, "smtp_username"),
3249 *pass = xs_dict_get(srv_config, "smtp_password"),
3250 *from = xs_dict_get(mailinfo, "from"),
3251 *to = xs_dict_get(mailinfo, "to"),
3252 *body = xs_dict_get(mailinfo, "body");
3253
3254 if (url == NULL || *url == '\0')
3255 url = "smtp://localhost";
3256
3257 int smtp_port = parse_port(url, NULL);
3258
3259 return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587);
3260}
3261
3262
3263void process_user_queue_item(snac *user, xs_dict *q_item)
3264/* processes an item from the user queue */
3265{
3266 const char *type;
3267 int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
3268
3269 if ((type = xs_dict_get(q_item, "type")) == NULL)
3270 type = "output";
3271
3272 if (strcmp(type, "message") == 0) {
3273 const xs_dict *msg = xs_dict_get(q_item, "message");
3274 xs *rcpts = recipient_list(user, msg, 1);
3275 xs_set inboxes;
3276 const xs_str *actor;
3277
3278 xs_set_init(&inboxes);
3279
3280 /* add this shared inbox first */
3281 xs *this_shared_inbox = xs_fmt("%s/shared-inbox", srv_baseurl);
3282 xs_set_add(&inboxes, this_shared_inbox);
3283 enqueue_output(user, msg, this_shared_inbox, 0, 0);
3284
3285 /* iterate the recipients */
3286 xs_list_foreach(rcpts, actor) {
3287 /* local users were served by this_shared_inbox */
3288 if (!xs_startswith(actor, srv_baseurl)) {
3289 xs *inbox = get_actor_inbox(actor, 1);
3290
3291 if (inbox != NULL) {
3292 /* add to the set and, if it's not there, send message */
3293 if (xs_set_add(&inboxes, inbox) == 1)
3294 enqueue_output(user, msg, inbox, 0, 0);
3295 }
3296 else
3297 snac_log(user, xs_fmt("cannot find inbox for %s", actor));
3298 }
3299 }
3300
3301 /* if it's a public note or question, send to the collected inboxes */
3302 if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
3303 if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
3304 xs *shibx = inbox_list();
3305 const xs_str *inbox;
3306
3307 xs_list_foreach(shibx, inbox) {
3308 if (xs_set_add(&inboxes, inbox) == 1)
3309 enqueue_output(user, msg, inbox, 0, 0);
3310 }
3311 }
3312 }
3313
3314 xs_set_free(&inboxes);
3315
3316 /* relay this note */
3317 if (is_msg_public(msg) && strcmp(user->uid, "relay") != 0) { /* avoid loops */
3318 snac relay;
3319 if (user_open(&relay, "relay")) {
3320 /* a 'relay' user exists */
3321 const char *type = xs_dict_get(msg, "type");
3322
3323 if (xs_is_string(type) && strcmp(type, "Create") == 0) {
3324 const xs_val *object = xs_dict_get(msg, "object");
3325
3326 if (xs_is_dict(object)) {
3327 object = xs_dict_get(object, "id");
3328
3329 snac_debug(&relay, 1, xs_fmt("relaying message %s", object));
3330
3331 xs *boost = msg_admiration(&relay, object, "Announce");
3332 enqueue_message(&relay, boost);
3333 }
3334 }
3335
3336 user_free(&relay);
3337 }
3338 }
3339 }
3340 else
3341 if (strcmp(type, "input") == 0) {
3342 /* process the message */
3343 const xs_dict *msg = xs_dict_get(q_item, "message");
3344 const xs_dict *req = xs_dict_get(q_item, "req");
3345 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
3346
3347 if (xs_is_null(msg))
3348 return;
3349
3350 if (!process_input_message(user, msg, req)) {
3351 if (retries > queue_retry_max)
3352 snac_log(user, xs_fmt("input giving up"));
3353 else {
3354 /* reenqueue */
3355 enqueue_input(user, msg, req, retries + 1);
3356 snac_log(user, xs_fmt("input requeue #%d", retries + 1));
3357 }
3358 }
3359 }
3360 else
3361 if (strcmp(type, "close_question") == 0) {
3362 /* the time for this question has ended */
3363 const char *id = xs_dict_get(q_item, "message");
3364
3365 if (!xs_is_null(id))
3366 update_question(user, id);
3367 }
3368 else
3369 if (strcmp(type, "object_request") == 0) {
3370 const char *id = xs_dict_get(q_item, "message");
3371
3372 if (!xs_is_null(id)) {
3373 int status;
3374 xs *data = NULL;
3375
3376 status = activitypub_request(user, id, &data);
3377
3378 if (valid_status(status))
3379 object_add_ow(id, data);
3380
3381 snac_debug(user, 1, xs_fmt("object_request %s %d", id, status));
3382 }
3383 }
3384 else
3385 if (strcmp(type, "verify_links") == 0) {
3386 verify_links(user);
3387 }
3388 else
3389 if (strcmp(type, "actor_refresh") == 0) {
3390 const char *actor = xs_dict_get(q_item, "actor");
3391 double mtime = object_mtime(actor);
3392
3393 if (actor_failure(actor, 0) == -1) {
3394 /* actor is broken beyond repair */
3395 snac_debug(user, 1, xs_fmt("actor_refresh skipped broken actor %s", actor));
3396 }
3397 else
3398 /* only refresh if it was refreshed more than an hour ago */
3399 if (mtime + 3600.0 < (double) time(NULL)) {
3400 xs *actor_o = NULL;
3401 int status;
3402
3403 if (valid_status((status = activitypub_request(user, actor, &actor_o)))) {
3404 /* refresh collection counts with throttling to prevent resource exhaustion */
3405 actor_o = actor_get_collections(user, actor_o, 1);
3406
3407 actor_add(actor, actor_o);
3408 }
3409 else {
3410 if (status == HTTP_STATUS_GONE || status == HTTP_STATUS_NOT_FOUND) {
3411 actor_failure(actor, 1);
3412 snac_log(user, xs_fmt("actor_refresh marking actor %s as broken %d", actor, status));
3413 }
3414 else {
3415 actor_failure(actor, 2);
3416 object_touch(actor);
3417 }
3418 }
3419
3420 snac_log(user, xs_fmt("actor_refresh %s %d", actor, status));
3421 }
3422 }
3423 else
3424 if (strcmp(type, "notify_webhook") == 0) {
3425 const char *webhook = xs_dict_get(user->config, "notify_webhook");
3426
3427 if (xs_is_string(webhook) && xs_match(webhook, "https://*|http://*")) { /** **/
3428 const xs_dict *msg = xs_dict_get(q_item, "message");
3429 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
3430
3431 xs *hdrs = xs_dict_new();
3432
3433 hdrs = xs_dict_set(hdrs, "content-type", "application/json");
3434 hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
3435
3436 xs *body = xs_json_dumps(msg, 4);
3437
3438 int status;
3439 xs *rsp = xs_http_request("POST", webhook, hdrs, body, strlen(body), &status, NULL, NULL, 0);
3440
3441 snac_debug(user, 0, xs_fmt("webhook post %s %d", webhook, status));
3442
3443 if (!valid_status(status)) {
3444 retries++;
3445
3446 if (retries > queue_retry_max)
3447 snac_debug(user, 0, xs_fmt("webhook post giving up %s", webhook));
3448 else {
3449 snac_debug(user, 0, xs_fmt("webhook post requeue %s %d", webhook, retries));
3450
3451 enqueue_notify_webhook(user, msg, retries);
3452 }
3453 }
3454 }
3455 }
3456 else
3457 if (strcmp(type, "collect_replies") == 0) {
3458 const char *post = xs_dict_get(q_item, "message");
3459
3460 collect_replies(user, post);
3461 }
3462 else
3463 if (strcmp(type, "collect_outbox") == 0) {
3464 const char *actor_id = xs_dict_get(q_item, "message");
3465
3466 collect_outbox(user, actor_id);
3467 }
3468 else
3469 snac_log(user, xs_fmt("unexpected user q_item type '%s'", type));
3470}
3471
3472
3473int process_user_queue(snac *snac)
3474/* processes a user's queue */
3475{
3476 int cnt = 0;
3477 xs *list = user_queue(snac);
3478
3479 xs_list *p = list;
3480 const xs_str *fn;
3481
3482 while (xs_list_iter(&p, &fn)) {
3483 xs *q_item = dequeue(fn);
3484
3485 if (q_item == NULL)
3486 continue;
3487
3488 process_user_queue_item(snac, q_item);
3489 cnt++;
3490 }
3491
3492 scheduled_process(snac);
3493
3494 return cnt;
3495}
3496
3497
3498xs_str *str_status(int status)
3499{
3500 return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : xs_http_status_text(status));
3501}
3502
3503
3504void process_queue_item(xs_dict *q_item)
3505/* processes an item from the global queue */
3506{
3507 const char *type = xs_dict_get(q_item, "type");
3508 int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
3509
3510 if (strcmp(type, "output") == 0) {
3511 int status;
3512 const xs_str *inbox = xs_dict_get(q_item, "inbox");
3513 const xs_str *keyid = xs_dict_get(q_item, "keyid");
3514 const xs_str *seckey = xs_dict_get(q_item, "seckey");
3515 const xs_dict *msg = xs_dict_get(q_item, "message");
3516 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
3517 int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
3518 xs *payload = NULL;
3519 int p_size = 0;
3520 int timeout = 0;
3521
3522 if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
3523 srv_log(xs_fmt("output message error: missing fields"));
3524 return;
3525 }
3526
3527 if (is_instance_blocked(inbox)) {
3528 srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
3529 return;
3530 }
3531
3532 if (instance_failure(inbox, 0)) {
3533 srv_debug(1, xs_fmt("output message error: too many failures for instance %s", inbox));
3534 return;
3535 }
3536
3537 /* deliver (if previous error status was a timeout, try now longer) */
3538 if (p_status == 599)
3539 timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
3540 else
3541 timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
3542
3543 if (timeout == 0)
3544 timeout = 6;
3545
3546 status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
3547
3548 /* register or clear a value for this instance */
3549 instance_failure(inbox, valid_status(status) ? 2 : 1);
3550
3551 if (payload) {
3552 if (p_size > 64) {
3553 /* trim the message */
3554 payload[64] = '\0';
3555 payload = xs_str_cat(payload, "...");
3556 }
3557
3558 /* strip ugly control characters */
3559 payload = xs_replace_i(payload, "\n", "");
3560 payload = xs_replace_i(payload, "\r", "");
3561
3562 if (*payload)
3563 payload = xs_str_wrap_i(" [", payload, "]");
3564 }
3565 else
3566 payload = xs_str_new(NULL);
3567
3568 xs *s_status = str_status(status);
3569
3570 srv_log(xs_fmt("output message: sent to inbox %s (%s)%s", inbox, s_status, payload));
3571
3572 if (!valid_status(status)) {
3573 retries++;
3574
3575 /* if it's not the first time it fails with a timeout,
3576 penalize the server by skipping one retry */
3577 if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
3578 retries++;
3579
3580 /* error sending; requeue? */
3581 if (status == HTTP_STATUS_BAD_REQUEST
3582 || status == HTTP_STATUS_NOT_FOUND
3583 || status == HTTP_STATUS_METHOD_NOT_ALLOWED
3584 || status == HTTP_STATUS_GONE
3585 || status == HTTP_STATUS_UNPROCESSABLE_CONTENT
3586 || status < 0)
3587 /* explicit error: discard */
3588 srv_log(xs_fmt("output message: error %s (%s)", inbox, s_status));
3589 else
3590 if (retries > queue_retry_max)
3591 srv_log(xs_fmt("output message: giving up %s (%s)", inbox, s_status));
3592 else {
3593 /* requeue */
3594 enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
3595 srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
3596 }
3597 }
3598 }
3599 else
3600 if (strcmp(type, "email") == 0) {
3601 /* send this email */
3602 const xs_dict *msg = xs_dict_get(q_item, "message");
3603 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
3604
3605 if (!send_email(msg))
3606 srv_debug(1, xs_fmt("email message sent"));
3607 else {
3608 retries++;
3609
3610 if (retries > queue_retry_max)
3611 srv_log(xs_fmt("email giving up (errno: %d)", errno));
3612 else {
3613 /* requeue */
3614 srv_log(xs_fmt(
3615 "email requeue #%d (errno: %d)", retries, errno));
3616
3617 enqueue_email(msg, retries);
3618 }
3619 }
3620 }
3621 else
3622 if (strcmp(type, "telegram") == 0) {
3623 /* send this via telegram */
3624 const char *bot = xs_dict_get(q_item, "bot");
3625 const char *msg = xs_dict_get(q_item, "message");
3626 xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
3627 int status = 0;
3628
3629 /* chat_id must start with a - */
3630 if (!xs_startswith(chat_id, "-"))
3631 chat_id = xs_str_wrap_i("-", chat_id, NULL);
3632
3633 xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
3634 xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
3635
3636 xs *headers = xs_dict_new();
3637 headers = xs_dict_append(headers, "content-type", "application/json");
3638
3639 xs *rsp = xs_http_request("POST", url, headers,
3640 body, strlen(body), &status, NULL, NULL, 0);
3641 rsp = xs_free(rsp);
3642
3643 srv_debug(0, xs_fmt("telegram post %d", status));
3644 }
3645 else
3646 if (strcmp(type, "ntfy") == 0) {
3647 /* send this via ntfy */
3648 const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
3649 const char *msg = xs_dict_get(q_item, "message");
3650 const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
3651 int status = 0;
3652
3653 xs *url = xs_fmt("%s", ntfy_server);
3654 xs *body = xs_fmt("%s", msg);
3655
3656 xs *headers = xs_dict_new();
3657 headers = xs_dict_append(headers, "content-type", "text/plain");
3658 // Append the Authorization header only if ntfy_token is not NULL
3659 if (ntfy_token != NULL) {
3660 headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
3661 }
3662
3663 xs *rsp = xs_http_request("POST", url, headers,
3664 body, strlen(body), &status, NULL, NULL, 0);
3665 rsp = xs_free(rsp);
3666
3667 srv_debug(0, xs_fmt("ntfy post %d", status));
3668 }
3669 else
3670 if (strcmp(type, "purge") == 0) {
3671 srv_log(xs_dup("purge start"));
3672
3673 purge_all();
3674
3675 srv_log(xs_dup("purge end"));
3676 }
3677 else
3678 if (strcmp(type, "input") == 0) {
3679 const xs_dict *msg = xs_dict_get(q_item, "message");
3680 const xs_dict *req = xs_dict_get(q_item, "req");
3681 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
3682
3683 /* do some instance-level checks */
3684 int r = process_input_message(NULL, msg, req);
3685
3686 if (r == 0) {
3687 /* transient error? retry */
3688 if (retries > queue_retry_max)
3689 srv_log(xs_fmt("shared input giving up"));
3690 else {
3691 /* reenqueue */
3692 enqueue_shared_input(msg, req, retries + 1);
3693 srv_log(xs_fmt("shared input requeue #%d", retries + 1));
3694 }
3695 }
3696 else
3697 if (r == 2) {
3698 /* redistribute the input message to all users */
3699 const char *ntid = xs_dict_get(q_item, "ntid");
3700 xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
3701 FILE *f;
3702
3703 if ((f = fopen(tmpfn, "w")) != NULL) {
3704 xs_json_dump(q_item, 4, f);
3705 fclose(f);
3706 }
3707
3708 xs *users = user_list();
3709 xs_list *p = users;
3710 const char *v;
3711 int cnt = 0;
3712
3713 while (xs_list_iter(&p, &v)) {
3714 snac user;
3715
3716 if (user_open(&user, v)) {
3717 int rsn = is_msg_for_me(&user, msg);
3718 if (rsn) {
3719 xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
3720
3721 snac_debug(&user, 1,
3722 xs_fmt("enqueue_input (from shared inbox) %s [%d]", xs_dict_get(msg, "id"), rsn));
3723
3724 if (link(tmpfn, fn) < 0)
3725 srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
3726
3727 cnt++;
3728 }
3729
3730 user_free(&user);
3731 }
3732 }
3733
3734 unlink(tmpfn);
3735
3736 if (cnt == 0) {
3737 srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
3738 }
3739 }
3740 }
3741 else
3742 if (strcmp(type, "webmention") == 0) {
3743 const xs_dict *msg = xs_dict_get(q_item, "message");
3744 const char *source = xs_dict_get(msg, "id");
3745 const char *content = xs_dict_get(msg, "content");
3746
3747 if (xs_is_string(source) && xs_is_string(content)) {
3748 xs *links = xs_regex_select(content, "\"https?:/" "/[^\"]+");
3749 const char *link;
3750
3751 xs_list_foreach(links, link) {
3752 xs *target = xs_strip_chars_i(xs_dup(link), "\"");
3753
3754 int r = xs_webmention_send(source, target, USER_AGENT);
3755
3756 srv_debug(1, xs_fmt("webmention source=%s target=%s %d", source, target, r));
3757 }
3758 }
3759 }
3760 else
3761 if (strcmp(type, "rss_hashtag_poll") == 0) {
3762 rss_poll_hashtags();
3763 }
3764 else
3765 if (strcmp(type, "fsck") == 0) {
3766 srv_log(xs_fmt("started deferred data integrity check"));
3767 data_fsck();
3768 srv_log(xs_fmt("finished deferred data integrity check"));
3769 }
3770 else
3771 srv_log(xs_fmt("unexpected q_item type '%s'", type));
3772}
3773
3774
3775int process_queue(void)
3776/* processes the global queue */
3777{
3778 int cnt = 0;
3779 xs *list = queue();
3780
3781 xs_list *p = list;
3782 const xs_str *fn;
3783
3784 while (xs_list_iter(&p, &fn)) {
3785 xs *q_item = dequeue(fn);
3786
3787 if (q_item != NULL) {
3788 job_post(q_item, 0);
3789 cnt++;
3790 }
3791 }
3792
3793 return cnt;
3794}
3795
3796
3797/** account migration **/
3798
3799int migrate_account(snac *user)
3800/* migrates this account to a new one (stored in the 'alias' user field) */
3801{
3802 const char *new_account = xs_dict_get(user->config, "alias");
3803
3804 if (xs_type(new_account) != XSTYPE_STRING) {
3805 snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
3806 return 1;
3807 }
3808
3809 xs *new_actor = NULL;
3810 int status;
3811
3812 if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
3813 snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
3814 return 1;
3815 }
3816
3817 actor_add(new_account, new_actor);
3818
3819 const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
3820
3821 if (xs_type(loaka) != XSTYPE_LIST) {
3822 snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
3823 return 1;
3824 }
3825
3826 if (xs_list_in(loaka, user->actor) == -1) {
3827 snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
3828 return 1;
3829 }
3830
3831 xs *move = msg_move(user, new_account);
3832 xs *fwers = follower_list(user);
3833
3834 const char *actor;
3835 xs_list_foreach(fwers, actor) {
3836 /* get the actor inbox, excluding the shared one */
3837 xs *inbox = get_actor_inbox(actor, 0);
3838
3839 if (xs_is_null(inbox))
3840 snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
3841 else
3842 enqueue_output(user, move, inbox, 0, 0);
3843 }
3844
3845 return 0;
3846}
3847
3848
3849/** HTTP handlers */
3850
3851int activitypub_get_handler(const xs_dict *req, const char *q_path,
3852 char **body, int *b_size, char **ctype)
3853{
3854 int status = HTTP_STATUS_OK;
3855 const char *accept = xs_dict_get(req, "accept");
3856 snac snac;
3857 xs *msg = NULL;
3858
3859 if (accept == NULL)
3860 return 0;
3861
3862 if (xs_str_in(accept, "application/activity+json") == -1 &&
3863 xs_str_in(accept, "application/ld+json") == -1)
3864 return 0;
3865
3866 xs *l = xs_split_n(q_path, "/", 2);
3867 const char *uid;
3868 const char *p_path;
3869
3870 uid = xs_list_get(l, 1);
3871 if (!user_open(&snac, uid)) {
3872 /* invalid user */
3873 status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
3874 srv_debug(1, xs_fmt("activitypub_get_handler bad user %s %d", uid, status));
3875 return status;
3876 }
3877
3878 p_path = xs_list_get(l, 2);
3879
3880 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
3881
3882 *ctype = "application/activity+json";
3883
3884 int show_contact_metrics = xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"));
3885
3886 if (p_path == NULL) {
3887 /* if there was no component after the user, it's an actor request */
3888 msg = msg_actor(&snac);
3889 *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
3890
3891 const char *ua = xs_dict_get(req, "user-agent");
3892
3893 snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
3894 }
3895 else
3896 if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
3897 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
3898 xs *list = xs_list_new();
3899 const char *v;
3900 int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
3901
3902 /* get the public outbox or the pinned list */
3903 xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, cnt, NULL) : pinned_list(&snac);
3904
3905 xs_list_foreach(elems, v) {
3906 xs *i = NULL;
3907
3908 if (valid_status(object_get_by_md5(v, &i))) {
3909 const char *type = xs_dict_get(i, "type");
3910 const char *id = xs_dict_get(i, "id");
3911
3912 if (type && id && strcmp(type, "Note") == 0 && is_msg_mine(&snac, id)) {
3913 if (is_msg_public(i)) {
3914 xs *c_msg = msg_create(&snac, i);
3915 list = xs_list_append(list, c_msg);
3916 }
3917 }
3918 }
3919 }
3920
3921 /* replace the 'orderedItems' with the latest posts */
3922 msg = msg_collection(&snac, id, xs_list_len(list));
3923 msg = xs_dict_set(msg, "orderedItems", list);
3924 }
3925 else
3926 if (strcmp(p_path, "followers") == 0) {
3927 int total = 0;
3928
3929 if (show_contact_metrics) {
3930 total = follower_list_len(&snac);
3931 }
3932
3933 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
3934 msg = msg_collection(&snac, id, total);
3935 }
3936 else
3937 if (strcmp(p_path, "following") == 0) {
3938 int total = 0;
3939
3940 if (show_contact_metrics) {
3941 total = following_list_len(&snac);
3942 }
3943
3944 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
3945 msg = msg_collection(&snac, id, total);
3946 }
3947 else
3948 if (xs_startswith(p_path, "p/")) {
3949 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
3950
3951 status = object_get(id, &msg);
3952
3953 /* don't return non-public objects */
3954 if (valid_status(status) && !is_msg_public(msg))
3955 status = HTTP_STATUS_NOT_FOUND;
3956 }
3957 else
3958 if (xs_startswith(p_path, "r/")) {
3959 /* replies to a post */
3960 xs *s = xs_dup(p_path);
3961 s[0] = 'p';
3962
3963 xs *id = xs_fmt("%s/%s", snac.actor, s);
3964
3965 xs *obj = NULL;
3966 status = object_get(id, &obj);
3967
3968 /* don't return non-public objects */
3969 if (!valid_status(status))
3970 status = HTTP_STATUS_NOT_FOUND;
3971 else
3972 if (!is_msg_public(obj))
3973 status = HTTP_STATUS_NOT_FOUND;
3974 else
3975 if (xs_dict_get(q_vars, "page"))
3976 msg = msg_replies(&snac, id, 1);
3977 else {
3978 const xs_dict *replies = xs_dict_get(obj, "replies");
3979 if (xs_is_dict(replies)) {
3980 msg = xs_dup(replies);
3981 msg = xs_dict_set(msg, "@context", "https:/""/www.w3.org/ns/activitystreams");
3982 }
3983 }
3984 }
3985 else
3986 status = HTTP_STATUS_NOT_FOUND;
3987
3988 if (status == HTTP_STATUS_OK && msg != NULL) {
3989 *body = xs_json_dumps(msg, 4);
3990 *b_size = strlen(*body);
3991 }
3992
3993 snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
3994
3995 user_free(&snac);
3996
3997 return status;
3998}
3999
4000
4001int activitypub_post_handler(const xs_dict *req, const char *q_path,
4002 char *payload, int p_size,
4003 char **body, int *b_size, char **ctype)
4004/* processes an input message */
4005{
4006 (void)b_size;
4007
4008 int status = HTTP_STATUS_ACCEPTED;
4009 const char *i_ctype = xs_dict_get(req, "content-type");
4010 snac snac;
4011 const char *v;
4012
4013 if (i_ctype == NULL) {
4014 *body = xs_str_new("no content-type");
4015 *ctype = "text/plain";
4016 return HTTP_STATUS_BAD_REQUEST;
4017 }
4018
4019 if (xs_is_null(payload)) {
4020 *body = xs_str_new("no payload");
4021 *ctype = "text/plain";
4022 return HTTP_STATUS_BAD_REQUEST;
4023 }
4024
4025 if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
4026 xs_str_in(i_ctype, "application/ld+json") == -1)
4027 return 0;
4028
4029 /* decode the message */
4030 xs *msg = xs_json_loads(payload);
4031 const char *id = xs_dict_get(msg, "id");
4032
4033 if (msg == NULL) {
4034 srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
4035
4036 srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
4037
4038 *body = xs_str_new("JSON error");
4039 *ctype = "text/plain";
4040 return HTTP_STATUS_BAD_REQUEST;
4041 }
4042
4043 if (id && is_instance_blocked(id)) {
4044 srv_debug(1, xs_fmt("full instance block for %s", id));
4045
4046 *body = xs_str_new("blocked");
4047 *ctype = "text/plain";
4048 return HTTP_STATUS_FORBIDDEN;
4049 }
4050
4051 /* get the user and path */
4052 xs *l = xs_split_n(q_path, "/", 2);
4053
4054 if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
4055 enqueue_shared_input(msg, req, 0);
4056 return HTTP_STATUS_ACCEPTED;
4057 }
4058
4059 if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
4060 /* strange q_path */
4061 srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
4062 return HTTP_STATUS_NOT_FOUND;
4063 }
4064
4065 const char *uid = xs_list_get(l, 1);
4066 if (!user_open(&snac, uid)) {
4067 /* invalid user */
4068 status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
4069 srv_debug(1, xs_fmt("activitypub_post_handler bad user %s %d", uid, status));
4070 return status;
4071 }
4072
4073 /* if it has a digest, check it now, because
4074 later the payload won't be exactly the same */
4075 if ((v = xs_dict_get(req, "digest")) != NULL) {
4076 xs *s1 = xs_sha256_base64(payload, p_size);
4077 xs *s2 = xs_fmt("SHA-256=%s", s1);
4078
4079 if (strcmp(s2, v) != 0) {
4080 srv_log(xs_fmt("digest check FAILED"));
4081
4082 *body = xs_str_new("bad digest");
4083 *ctype = "text/plain";
4084 status = HTTP_STATUS_BAD_REQUEST;
4085 }
4086 }
4087
4088 /* if the message is from a muted actor, reject it right now */
4089 if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
4090 if (is_muted(&snac, v)) {
4091 snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
4092
4093 *body = xs_str_new("rejected");
4094 *ctype = "text/plain";
4095 status = HTTP_STATUS_FORBIDDEN;
4096 }
4097 }
4098
4099 if (valid_status(status)) {
4100 enqueue_input(&snac, msg, req, 0);
4101 *ctype = "application/activity+json";
4102 }
4103
4104 user_free(&snac);
4105
4106 return status;
4107}