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