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