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