-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivitypub.c
More file actions
4193 lines (3321 loc) · 132 KB
/
Copy pathactivitypub.c
File metadata and controls
4193 lines (3321 loc) · 132 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
#include "xs.h"
#include "xs_json.h"
#include "xs_curl.h"
#include "xs_url.h"
#include "xs_mime.h"
#include "xs_openssl.h"
#include "xs_regex.h"
#include "xs_time.h"
#include "xs_set.h"
#include "xs_match.h"
#include "xs_unicode.h"
#include "xs_webmention.h"
#include "xs_http.h"
#include "snac.h"
#include <stddef.h>
#include <sys/wait.h>
const char * const public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
/* susie.png */
const char * const susie =
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
"CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
"vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
"eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
"AYTtEsDU9F34AAAAAElFTkSuQmCC";
const char * const susie_cool =
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
"CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
"qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
"+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
"ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
const char * const susie_muertos =
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
"CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
"ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
"xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
"QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
const char *default_avatar_base64(void)
/* returns the default avatar in base64 */
{
time_t t = time(NULL);
struct tm tm;
const char *p = susie;
gmtime_r(&t, &tm);
if (tm.tm_mon == 10 && tm.tm_mday == 2)
p = susie_muertos;
else
if (tm.tm_wday == 0 || tm.tm_wday == 6)
p = susie_cool;
return p;
}
int activitypub_request(snac *user, const char *url, xs_dict **data)
/* request an object */
{
int status = 0;
xs *response = NULL;
xs *payload = NULL;
int p_size;
const char *ctype;
*data = NULL;
if (user != NULL) {
/* get from the net */
response = http_signed_request(user, "GET", url,
NULL, NULL, 0, &status, &payload, &p_size, 0);
}
if (status == 0 || (status >= 500 && status <= 599)) {
/* I found an instance running Misskey that returned
500 on signed messages but returned the object
perfectly without signing (?), so why not try */
xs_free(response);
xs *hdrs = xs_dict_new();
hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
response = xs_http_request("GET", url, hdrs,
NULL, 0, &status, &payload, &p_size, 0);
}
if (valid_status(status)) {
/* ensure it's ActivityPub data */
ctype = xs_dict_get(response, "content-type");
if (xs_is_null(ctype))
status = HTTP_STATUS_BAD_REQUEST;
else
if (xs_str_in(ctype, "application/activity+json") != -1 ||
xs_str_in(ctype, "application/ld+json") != -1) {
/* if there is no payload, fail */
if (xs_is_null(payload))
status = HTTP_STATUS_BAD_REQUEST;
else
*data = xs_json_loads(payload);
}
else
status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
}
return status;
}
static xs_dict *actor_get_collections(snac *user, xs_dict *actor, int throttle)
/* fetches follower/following/statuses counts from an actor's collections and adds them to the actor object */
{
/* only fetch if counts are not already present in the actor object */
const xs_number *existing_followers = xs_dict_get(actor, "followers_count");
const xs_number *existing_following = xs_dict_get(actor, "following_count");
const xs_number *existing_statuses = xs_dict_get(actor, "statuses_count");
/* skip if we already have all counts */
if (xs_type(existing_followers) == XSTYPE_NUMBER &&
xs_type(existing_following) == XSTYPE_NUMBER &&
xs_type(existing_statuses) == XSTYPE_NUMBER) {
return actor;
}
/* CRITICAL: duplicate URLs BEFORE any xs_dict_set calls, as xs_dict_set can reallocate the dict */
xs *followers_url = xs_dup(xs_dict_get(actor, "followers"));
xs *following_url = xs_dup(xs_dict_get(actor, "following"));
xs *outbox_url = xs_dup(xs_dict_get(actor, "outbox"));
/* only fetch followers count if not already present */
if (xs_type(existing_followers) != XSTYPE_NUMBER && !xs_is_null(followers_url)) {
xs *followers_coll = NULL;
if (valid_status(activitypub_request(user, followers_url, &followers_coll))) {
const xs_number *total = xs_dict_get(followers_coll, "totalItems");
if (xs_type(total) == XSTYPE_NUMBER) {
xs *total_copy = xs_dup(total);
actor = xs_dict_set(actor, "followers_count", total_copy);
}
}
/* throttle to prevent resource exhaustion on low-power devices */
if (throttle)
usleep(100000); /* 100ms delay between requests */
}
/* only fetch following count if not already present */
if (xs_type(existing_following) != XSTYPE_NUMBER && !xs_is_null(following_url)) {
xs *following_coll = NULL;
if (valid_status(activitypub_request(user, following_url, &following_coll))) {
const xs_number *total = xs_dict_get(following_coll, "totalItems");
if (xs_type(total) == XSTYPE_NUMBER) {
xs *total_copy = xs_dup(total);
actor = xs_dict_set(actor, "following_count", total_copy);
}
}
/* throttle to prevent resource exhaustion on low-power devices */
if (throttle)
usleep(100000); /* 100ms delay between requests */
}
/* only fetch statuses count if not already present */
if (xs_type(existing_statuses) != XSTYPE_NUMBER && !xs_is_null(outbox_url)) {
xs *outbox_coll = NULL;
if (valid_status(activitypub_request(user, outbox_url, &outbox_coll))) {
const xs_number *total = xs_dict_get(outbox_coll, "totalItems");
if (xs_type(total) == XSTYPE_NUMBER) {
xs *total_copy = xs_dup(total);
actor = xs_dict_set(actor, "statuses_count", total_copy);
}
}
/* throttle to prevent resource exhaustion on low-power devices */
if (throttle)
usleep(100000); /* 100ms delay between requests */
}
return actor;
}
int actor_request(snac *user, const char *actor, xs_dict **data)
/* request an actor */
{
int status;
xs *payload = NULL;
if (data)
*data = NULL;
/* get from disk first */
status = actor_get_refresh(user, actor, data);
if (!valid_status(status)) {
/* actor data non-existent: get from the net */
status = activitypub_request(user, actor, &payload);
if (valid_status(status)) {
/* fetch collection counts when initially fetching an actor (no throttle) */
payload = actor_get_collections(user, payload, 0);
/* renew data */
status = actor_add(actor, payload);
if (data != NULL) {
*data = payload;
payload = NULL;
}
}
else
srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
}
/* collect the (presumed) shared inbox in this actor */
if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
if (valid_status(status) && data && *data)
inbox_add_by_actor(*data);
}
return status;
}
const char *get_atto(const xs_dict *msg)
/* gets the attributedTo field (an actor) */
{
const xs_val *actor = xs_dict_get(msg, "attributedTo");
/* if the actor is a list of objects (like on Peertube videos), pick the Person */
if (xs_type(actor) == XSTYPE_LIST) {
const xs_list *p = actor;
int c = 0;
const xs_dict *v;
actor = NULL;
while (actor == NULL && xs_list_next(p, &v, &c)) {
if (xs_type(v) == XSTYPE_DICT) {
const char *type = xs_dict_get(v, "type");
if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
actor = xs_dict_get(v, "id");
if (xs_type(actor) != XSTYPE_STRING)
actor = NULL;
}
}
}
}
else
if (xs_type(actor) == XSTYPE_DICT) {
/* bandwagon.fm returns this */
actor = xs_dict_get(actor, "id");
}
return actor;
}
const char *get_in_reply_to(const xs_dict *msg)
/* gets the inReplyTo id */
{
const xs_val *in_reply_to = xs_dict_get(msg, "inReplyTo");
if (xs_type(in_reply_to) == XSTYPE_DICT)
in_reply_to = xs_dict_get(in_reply_to, "id");
return in_reply_to;
}
xs_list *get_attachments(const xs_dict *msg)
/* unify the garbage fire that are the attachments */
{
xs_list *l = xs_list_new();
const xs_list *p;
/* try first the attachments list */
if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
xs *attach = NULL;
const xs_val *v;
/* ensure it's a list */
if (xs_type(p) == XSTYPE_DICT) {
attach = xs_list_new();
attach = xs_list_append(attach, p);
}
else
attach = xs_dup(p);
if (xs_type(attach) == XSTYPE_LIST && xs_list_len(attach) == 0) {
/* does the message have an image? */
const xs_dict *d = xs_dict_get(msg, "image");
if (xs_type(d) == XSTYPE_DICT) {
/* add it to the attachment list */
attach = xs_list_append(attach, d);
}
}
/* now iterate the list */
int c = 0;
while (xs_list_next(attach, &v, &c)) {
const char *type = xs_dict_get(v, "mediaType");
if (xs_is_null(type))
type = xs_dict_get(v, "type");
if (xs_is_null(type))
continue;
const char *href = xs_dict_get(v, "url");
if (xs_is_null(href))
href = xs_dict_get(v, "href");
if (xs_is_null(href))
continue;
/* infer MIME type from non-specific attachments */
if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
char *mt = (char *)xs_mime_by_ext(href);
if (xs_match(mt, "image/*|audio/*|video/*")) /* */
type = mt;
}
const char *name = xs_dict_get(v, "name");
if (xs_is_null(name))
name = xs_dict_get(msg, "name");
if (xs_is_null(name))
name = "";
xs *d = xs_dict_new();
d = xs_dict_append(d, "type", type);
d = xs_dict_append(d, "href", href);
d = xs_dict_append(d, "name", name);
const xs_dict *icon = xs_dict_get(v, "icon");
if (xs_type(icon) == XSTYPE_DICT)
d = xs_dict_append(d, "icon", icon);
l = xs_list_append(l, d);
}
}
/** urls (attachments from Peertube) **/
p = xs_dict_get(msg, "url");
if (xs_type(p) == XSTYPE_LIST) {
const char *href = NULL;
const char *type = NULL;
int c = 0;
const xs_val *v;
while (href == NULL && xs_list_next(p, &v, &c)) {
if (xs_type(v) == XSTYPE_DICT) {
const char *mtype = xs_dict_get(v, "type");
if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
mtype = xs_dict_get(v, "mediaType");
const xs_list *tag = xs_dict_get(v, "tag");
if (xs_type(mtype) == XSTYPE_STRING &&
strcmp(mtype, "application/x-mpegURL") == 0 &&
xs_type(tag) == XSTYPE_LIST) {
/* now iterate the tag list, looking for a video URL */
const xs_dict *d;
int c = 0;
while (href == NULL && xs_list_next(tag, &d, &c)) {
if (xs_type(d) == XSTYPE_DICT) {
if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
xs_startswith(mtype, "video/")) {
const char *h = xs_dict_get(d, "href");
/* this is probably it */
if (xs_type(h) == XSTYPE_STRING) {
href = h;
type = mtype;
}
}
}
}
}
}
}
}
if (href && type) {
xs *d = xs_dict_new();
d = xs_dict_append(d, "href", href);
d = xs_dict_append(d, "type", type);
d = xs_dict_append(d, "name", "---");
l = xs_list_append(l, d);
}
}
return l;
}
int hashtag_in_msg(const xs_list *hashtags, const xs_dict *msg)
/* returns 1 if the message contains any of the list of hashtags */
{
if (xs_is_list(hashtags) && xs_is_dict(msg)) {
const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
if (xs_is_list(tags_in_msg)) {
const xs_dict *te;
/* iterate the tags in the message */
xs_list_foreach(tags_in_msg, te) {
if (xs_is_dict(te)) {
const char *type = xs_dict_get(te, "type");
const char *name = xs_dict_get(te, "name");
if (xs_is_string(type) && xs_is_string(name)) {
if (strcmp(type, "Hashtag") == 0) {
xs *lc_name = xs_utf8_to_lower(name);
if (xs_list_in(hashtags, lc_name) != -1)
return 1;
}
}
}
}
}
}
return 0;
}
int followed_hashtag_check(snac *user, const xs_dict *msg)
/* returns 1 if this message contains a hashtag followed by me */
{
return hashtag_in_msg(xs_dict_get(user->config, "followed_hashtags"), msg);
}
int blocked_hashtag_check(snac *user, const xs_dict *msg)
/* returns 1 if this message contains a hashtag blocked by me */
{
return hashtag_in_msg(xs_dict_get(user->config, "blocked_hashtags"), msg);
}
int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
/* ensures that an entry and its ancestors are in the timeline */
{
int status = 0;
if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
xs *msg = NULL;
/* from a blocked instance? discard and break */
if (is_instance_blocked(*id)) {
snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
return status;
}
/* is the object already there? */
if (!valid_status((status = object_get(*id, &msg)))) {
/* no; download it */
status = activitypub_request(snac, *id, &msg);
}
if (valid_status(status)) {
const xs_dict *object = msg;
const char *type = xs_dict_get(object, "type");
/* get the id again from the object, as it may be different */
const char *nid = xs_dict_get(object, "id");
if (xs_type(nid) != XSTYPE_STRING)
return 0;
if (wrk && strcmp(nid, *id) != 0) {
snac_debug(snac, 1,
xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
*wrk = xs_dup(nid);
*id = *wrk;
}
if (xs_is_null(type))
type = "(null)";
srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
if (strcmp(type, "Create") == 0) {
/* some software like lemmy nest Announce + Create + Note */
if (!xs_is_null(object = xs_dict_get(object, "object"))) {
type = xs_dict_get(object, "type");
nid = xs_dict_get(object, "id");
}
else
type = "(null)";
}
if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
if (content_match("filter_reject.txt", object))
snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
else
if (blocked_hashtag_check(snac, object))
snac_log(snac, xs_fmt("timeline_request rejected by hashtag %s", nid));
else {
const char *actor = get_atto(object);
if (!xs_is_null(actor)) {
/* request (and drop) the actor for this entry */
if (!valid_status(actor_request(snac, actor, NULL))) {
/* failed? retry later */
enqueue_actor_refresh(snac, actor, 60);
}
/* does it have an ancestor? */
const char *in_reply_to = get_in_reply_to(object);
/* store */
timeline_add(snac, nid, object);
/* redistribute to lists for this user */
list_distribute(snac, actor, object);
/* recurse! */
timeline_request(snac, &in_reply_to, NULL, level + 1);
}
}
}
}
}
return status;
}
int send_to_inbox_raw(const char *keyid, const char *seckey,
const xs_str *inbox, const xs_dict *msg,
xs_val **payload, int *p_size, int timeout)
/* sends a message to an Inbox */
{
int status;
xs_dict *response;
xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
response = http_signed_request_raw(keyid, seckey, "POST", inbox,
NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
xs_free(response);
return status;
}
int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
xs_val **payload, int *p_size, int timeout)
/* sends a message to an Inbox */
{
const char *seckey = xs_dict_get(snac->key, "secret");
return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
}
xs_str *get_actor_inbox(const char *actor, int shared)
/* gets an actor's inbox */
{
xs *data = NULL;
const char *v = NULL;
if (valid_status(actor_request(NULL, actor, &data))) {
/* try first endpoints/sharedInbox */
if (shared && (v = xs_dict_get(data, "endpoints")))
v = xs_dict_get(v, "sharedInbox");
/* try then the regular inbox */
if (xs_is_null(v))
v = xs_dict_get(data, "inbox");
}
return xs_is_null(v) ? NULL : xs_dup(v);
}
int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
xs_val **payload, int *p_size, int timeout)
/* sends a message to an actor */
{
int status = HTTP_STATUS_BAD_REQUEST;
xs *inbox = get_actor_inbox(actor, 1);
if (!xs_is_null(inbox))
status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
return status;
}
void post_message(snac *snac, const char *actor, const xs_dict *msg)
/* posts a message immediately (bypassing the output queues) */
{
xs *payload = NULL;
int p_size;
int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
srv_log(xs_fmt("post_message to actor %s %d", actor, status));
if (!valid_status(status))
/* cannot send right now, enqueue */
enqueue_message(snac, msg);
}
xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
/* returns the list of recipients for a message */
{
const xs_val *to = xs_dict_get(msg, "to");
const xs_val *cc = xs_dict_get(msg, "cc");
xs_set rcpts;
int n;
xs_set_init(&rcpts);
const xs_list *lists[] = { to, cc, NULL };
for (n = 0; lists[n]; n++) {
xs_list *l = (xs_list *)lists[n];
const char *v;
xs *tl = NULL;
/* if it's a string, create a list with only one element */
if (xs_type(l) == XSTYPE_STRING) {
tl = xs_list_new();
tl = xs_list_append(tl, l);
l = tl;
}
while (xs_list_iter(&l, &v)) {
if (expand_public) {
xs *followers_url = xs_fmt("%s/followers", snac->actor);
if (strcmp(v, public_address) == 0 ||
/* check if it's a followers collection URL */
(xs_type(v) == XSTYPE_STRING &&
strcmp(v, followers_url) == 0) ||
(xs_type(v) == XSTYPE_LIST &&
xs_list_in(v, followers_url) != -1)) {
/* iterate the followers and add them */
xs *fwers = follower_list(snac);
const char *actor;
char *p = fwers;
while (xs_list_iter(&p, &actor))
xs_set_add(&rcpts, actor);
}
else
xs_set_add(&rcpts, v);
}
else
xs_set_add(&rcpts, v);
}
}
return xs_set_result(&rcpts);
}
int is_msg_public(const xs_dict *msg)
/* checks if a message is public */
{
const char *to = xs_dict_get(msg, "to");
const char *cc = xs_dict_get(msg, "cc");
int n;
const char *lists[] = { to, cc, NULL };
for (n = 0; lists[n]; n++) {
const xs_val *l = lists[n];
if (xs_type(l) == XSTYPE_STRING) {
if (strcmp(l, public_address) == 0)
return 1;
}
else
if (xs_type(l) == XSTYPE_LIST) {
if (xs_list_in(l, public_address) != -1)
return 1;
}
}
return 0;
}
int is_msg_from_private_user(const xs_dict *msg)
/* checks if a message is from a local, private user */
{
int ret = 0;
/* is this message from a local user? */
if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
const char *atto = get_atto(msg);
xs *l = xs_split(atto, "/");
const char *uid = xs_list_get(l, -1);
snac user;
if (uid && user_open(&user, uid)) {
if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
ret = 1;
user_free(&user);
}
}
return ret;
}
void followed_hashtag_distribute(const xs_dict *msg)
/* distribute this post to all users following the included hashtags */
{
const char *id = xs_dict_get(msg, "id");
const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
if (!xs_is_string(id) || !xs_is_list(tags_in_msg) || xs_list_len(tags_in_msg) == 0)
return;
srv_debug(1, xs_fmt("followed_hashtag_distribute check for %s", id));
xs *users = user_list();
const char *uid;
xs_list_foreach(users, uid) {
snac user;
if (user_open(&user, uid)) {
if (followed_hashtag_check(&user, msg)) {
timeline_add(&user, id, msg);
snac_log(&user, xs_fmt("followed hashtag in %s", id));
}
user_free(&user);
}
}
}
int is_msg_for_me(snac *snac, const xs_dict *c_msg)
/* checks if this message is for me */
{
const char *type = xs_dict_get(c_msg, "type");
const char *actor = xs_dict_get(c_msg, "actor");
if (strcmp(actor, snac->actor) == 0) {
/* message by myself? (most probably via the shared-inbox) reject */
snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
return 0;
}
if (xs_match(type, "Like|Announce|EmojiReact")) {
const char *object = xs_dict_get(c_msg, "object");
xs *obj = NULL;
if (xs_is_dict(object)) {
obj = xs_dup(object);
object = xs_dict_get(object, "id");
}
/* bad object id? reject */
if (!xs_is_string(object))
return 0;
/* try to get the object */
if (!xs_is_dict(obj))
object_get(object, &obj);
/* if it's about one of our posts, accept it */
if (is_msg_mine(snac, object))
return 2;
/* blocked by hashtag? */
if (blocked_hashtag_check(snac, obj))
return 0;
/* if it's by someone we follow, accept it */
if (following_check(snac, actor))
return 1;
/* do we follow any hashtag? */
if (followed_hashtag_check(snac, obj))
return 7;
return 0;
}
/* if it's an Undo, it must be from someone related to us */
if (xs_match(type, "Undo")) {
return follower_check(snac, actor) || following_check(snac, actor);
}
/* if it's an Accept + Follow, it must be for a Follow we created */
if (xs_match(type, "Accept")) {
return following_check(snac, actor);
}
/* if it's a Follow, it must be explicitly for us */
if (xs_match(type, "Follow")) {
const char *object = xs_dict_get(c_msg, "object");
return !xs_is_null(object) && (strcmp(snac->actor, object) == 0 || strcmp(snac->actor_alt, object) == 0);
}
/* only accept Ping directed to us */
if (xs_match(type, "Ping")) {
const char *dest = xs_dict_get(c_msg, "to");
return !xs_is_null(dest) && (strcmp(snac->actor, dest) == 0 || strcmp(snac->actor_alt, dest) == 0);
}
/* if it's not a Create or Update, allow as is */
if (!xs_match(type, "Create|Update")) {
return 1;
}
const xs_dict *msg = xs_dict_get(c_msg, "object");
/* any blocked hashtag? reject */
if (blocked_hashtag_check(snac, msg)) {
snac_debug(snac, 1, xs_fmt("blocked by hashtag %s", xs_dict_get(msg, "id")));
return 0;
}
int pub_msg = is_msg_public(c_msg);
/* if this message is public and we follow the actor of this post, allow */
if (pub_msg && following_check(snac, actor))
return 1;
xs *rcpts = recipient_list(snac, msg, 0);
xs_list *p = rcpts;
const xs_str *v;
xs *actor_followers = NULL;
if (!pub_msg) {
/* not a public message; get the actor and its followers list */
xs *actor_obj = NULL;
if (valid_status(object_get(actor, &actor_obj))) {
const xs_val *fw = xs_dict_get(actor_obj, "followers");
if (fw)
actor_followers = xs_dup(fw);
}
}
while(xs_list_iter(&p, &v)) {
/* explicitly for me? accept */
if (strcmp(v, snac->actor) == 0 || strcmp(v, snac->actor_alt) == 0)
return 2;
if (pub_msg) {
/* a public message for someone we follow? (probably cc'ed) accept */
if (strcmp(v, public_address) != 0 && following_check(snac, v))
return 5;
}
else
if (actor_followers && strcmp(v, actor_followers) == 0) {
/* if this message is for this actor's followers, are we one of them? */
if (following_check(snac, actor))
return 6;
}
}
/* accept if it's by someone we follow */
const char *atto = get_atto(msg);
if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
return 3;
/* is this message a reply to another? */
const char *irt = get_in_reply_to(msg);
if (!xs_is_null(irt)) {
xs *r_msg = NULL;
/* try to get the replied message */
if (valid_status(object_get(irt, &r_msg))) {
atto = get_atto(r_msg);
/* accept if the replied message is from someone we follow */
if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
return 4;
}
}
/* does this message contain a tag we are following? */
if (pub_msg && followed_hashtag_check(snac, msg))
return 7;
return 0;
}
xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
/* parses mentions and tags from content */
{
xs_str *nc = xs_str_new(NULL);
xs_list *tl = *tag;
xs *split;
xs_list *p;
const xs_val *v;
int n = 0;
/* create a default server for incomplete mentions */
xs *def_srv = NULL;
if (xs_list_len(tl)) {
/* if there are any mentions, get the server from
the first one, which is the inReplyTo author */
p = tl;
while (xs_list_iter(&p, &v)) {
const char *type = xs_dict_get(v, "type");
const char *name = xs_dict_get(v, "name");
if (type && name && strcmp(type, "Mention") == 0) {
xs *l = xs_split(name, "@");
def_srv = xs_dup(xs_list_get(l, -1));
break;
}
}
}
if (xs_is_null(def_srv))
/* use this same server */
def_srv = xs_dup(xs_dict_get(srv_config, "host"));
split = xs_regex_split(content, "(@[A-Za-z0-9_-]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#(_|[^[:punct:][:space:]])+)");
p = split;
while (xs_list_iter(&p, &v)) {
if ((n & 0x1)) {
if (*v == '@') {
xs *link = NULL;
xs *wuid = NULL;
if (strchr(v + 1, '@') == NULL) {
/* only one @? it's a dumb Mastodon-like mention
without server; add the default one */
wuid = xs_fmt("%s@%s", v, def_srv);
snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
}
else
wuid = xs_dup(v);
/* query the webfinger about this fellow */
xs *actor = NULL;
xs *uid = NULL;
int status;
status = webfinger_request(wuid, &actor, &uid);
if (valid_status(status) && actor && uid) {
xs *d = xs_dict_new();
xs *n = xs_fmt("@%s", uid);
d = xs_dict_append(d, "type", "Mention");
d = xs_dict_append(d, "href", actor);
d = xs_dict_append(d, "name", n);
tl = xs_list_append(tl, d);
link = xs_fmt("<span class=\"h-card\"><a href=\"%s\" class=\"u-url mention\">%s</a></span>", actor, n);
}
if (!xs_is_null(link))
nc = xs_str_cat(nc, link);
else
nc = xs_str_cat(nc, v);
}
else
if (*v == '#') {
/* hashtag */
xs *d = xs_dict_new();
xs *n = xs_utf8_to_lower(v);
xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
d = xs_dict_append(d, "type", "Hashtag");
d = xs_dict_append(d, "href", h);
d = xs_dict_append(d, "name", n);
tl = xs_list_append(tl, d);