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