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