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