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