2.65 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)
1042/* creates an empty OrderedCollection message */
1043{
1044    xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
1045    xs *ol = xs_list_new();
1046
1047    msg = xs_dict_append(msg, "attributedTo", snac->actor);
1048    msg = xs_dict_append(msg, "orderedItems", ol);
1049    msg = xs_dict_append(msg, "totalItems",   xs_stock(0));
1050
1051    return msg;
1052}
1053
1054
1055xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
1056/* creates an Accept message (as a response to a Follow) */
1057{
1058    xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
1059
1060    msg = xs_dict_append(msg, "to", to);
1061
1062    return msg;
1063}
1064
1065
1066xs_dict *msg_update(snac *snac, const xs_dict *object)
1067/* creates an Update message */
1068{
1069    xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
1070
1071    const char *type = xs_dict_get(object, "type");
1072
1073    if (strcmp(type, "Note") == 0) {
1074        msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
1075        msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
1076    }
1077    else
1078    if (strcmp(type, "Person") == 0) {
1079        msg = xs_dict_append(msg, "to", public_address);
1080
1081        /* also spam the people being followed, so that
1082           they have the newest information about who we are */
1083        xs *cc = following_list(snac);
1084
1085        msg = xs_dict_append(msg, "cc", cc);
1086    }
1087    else
1088        msg = xs_dict_append(msg, "to", public_address);
1089
1090    return msg;
1091}
1092
1093
1094xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
1095/* creates a Like or Announce message */
1096{
1097    xs *a_msg    = NULL;
1098    xs_dict *msg = NULL;
1099    xs *wrk      = NULL;
1100
1101    /* call the object */
1102    timeline_request(snac, &object, &wrk, 0);
1103
1104    if (valid_status(object_get(object, &a_msg))) {
1105        xs *rcpts = xs_list_new();
1106        xs *o_md5 = xs_md5_hex(object, strlen(object));
1107        xs *id    = xs_fmt("%s/%s/%s", snac->actor, *type == 'L' ? "l" : "a", o_md5);
1108
1109        msg = msg_base(snac, type, id, snac->actor, "@now", object);
1110
1111        if (is_msg_public(a_msg))
1112            rcpts = xs_list_append(rcpts, public_address);
1113
1114        rcpts = xs_list_append(rcpts, get_atto(a_msg));
1115
1116        msg = xs_dict_append(msg, "to", rcpts);
1117    }
1118    else
1119        snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
1120
1121    return msg;
1122}
1123
1124
1125xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
1126/* creates an Undo + admiration message */
1127{
1128    xs *a_msg    = NULL;
1129    xs_dict *msg = NULL;
1130
1131    if (valid_status(object_get(id, &a_msg))) {
1132        /* create a clone of the original admiration message */
1133        xs *object = msg_admiration(user, id, type);
1134
1135        /* delete the published date */
1136        object = xs_dict_del(object, "published");
1137
1138        /* create an undo message for this object */
1139        msg = msg_undo(user, object);
1140
1141        /* copy the 'to' field */
1142        msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
1143    }
1144
1145    /* now we despise this */
1146    object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
1147
1148    return msg;
1149}
1150
1151
1152xs_dict *msg_actor(snac *snac)
1153/* create a Person message for this actor */
1154{
1155    xs *ctxt     = xs_list_new();
1156    xs *icon     = xs_dict_new();
1157    xs *keys     = xs_dict_new();
1158    xs *tags     = xs_list_new();
1159    xs *avtr     = NULL;
1160    xs *kid      = NULL;
1161    xs *f_bio    = NULL;
1162    xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
1163    const char *p;
1164    int n;
1165
1166    /* change the @context (is this really necessary?) */
1167    ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
1168    ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
1169    msg = xs_dict_set(msg, "@context",          ctxt);
1170
1171    msg = xs_dict_set(msg, "url",               snac->actor);
1172    msg = xs_dict_set(msg, "name",              xs_dict_get(snac->config, "name"));
1173    msg = xs_dict_set(msg, "preferredUsername", snac->uid);
1174    msg = xs_dict_set(msg, "published",         xs_dict_get(snac->config, "published"));
1175
1176    xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
1177    f_bio = process_tags(snac, f_bio_2, &tags);
1178    msg = xs_dict_set(msg, "summary", f_bio);
1179    msg = xs_dict_set(msg, "tag", tags);
1180
1181    char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
1182    for (n = 0; folders[n]; n++) {
1183        xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
1184        msg = xs_dict_set(msg, folders[n], f);
1185    }
1186
1187    p = xs_dict_get(snac->config, "avatar");
1188
1189    if (*p == '\0')
1190        avtr = xs_fmt("%s/susie.png", srv_baseurl);
1191    else
1192        avtr = xs_dup(p);
1193
1194    icon = xs_dict_append(icon, "type",         "Image");
1195    icon = xs_dict_append(icon, "mediaType",    xs_mime_by_ext(avtr));
1196    icon = xs_dict_append(icon, "url",          avtr);
1197    msg = xs_dict_set(msg, "icon", icon);
1198
1199    kid = xs_fmt("%s#main-key", snac->actor);
1200
1201    keys = xs_dict_append(keys, "id",           kid);
1202    keys = xs_dict_append(keys, "owner",        snac->actor);
1203    keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
1204    msg = xs_dict_set(msg, "publicKey", keys);
1205
1206    /* if the "bot" config field is set to true, change type to "Service" */
1207    if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
1208        msg = xs_dict_set(msg, "type", "Service");
1209
1210    /* add the header image, if there is one defined */
1211    const char *header = xs_dict_get(snac->config, "header");
1212    if (!xs_is_null(header)) {
1213        xs *d = xs_dict_new();
1214        d = xs_dict_append(d, "type",       "Image");
1215        d = xs_dict_append(d, "mediaType",  xs_mime_by_ext(header));
1216        d = xs_dict_append(d, "url",        header);
1217        msg = xs_dict_set(msg, "image", d);
1218    }
1219
1220    /* add the metadata as attachments of PropertyValue */
1221    xs *metadata = NULL;
1222    const xs_dict *md = xs_dict_get(snac->config, "metadata");
1223
1224    if (xs_type(md) == XSTYPE_DICT)
1225        metadata = xs_dup(md);
1226    else
1227    if (xs_type(md) == XSTYPE_STRING) {
1228        metadata = xs_dict_new();
1229        xs *l = xs_split(md, "\n");
1230        const char *ll;
1231
1232        xs_list_foreach(l, ll) {
1233            xs *kv = xs_split_n(ll, "=", 1);
1234            const char *k = xs_list_get(kv, 0);
1235            const char *v = xs_list_get(kv, 1);
1236
1237            if (k && v) {
1238                xs *kk = xs_strip_i(xs_dup(k));
1239                xs *vv = xs_strip_i(xs_dup(v));
1240                metadata = xs_dict_set(metadata, kk, vv);
1241            }
1242        }
1243    }
1244
1245    if (xs_type(metadata) == XSTYPE_DICT) {
1246        xs *attach = xs_list_new();
1247        const xs_str *k;
1248        const xs_str *v;
1249
1250        int c = 0;
1251        while (xs_dict_next(metadata, &k, &v, &c)) {
1252            xs *d = xs_dict_new();
1253
1254            xs *k2 = encode_html(k);
1255            xs *v2 = NULL;
1256
1257            if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
1258                xs *t = encode_html(v);
1259                v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
1260            }
1261            else
1262                v2 = encode_html(v);
1263
1264            d = xs_dict_append(d, "type",  "PropertyValue");
1265            d = xs_dict_append(d, "name",  k2);
1266            d = xs_dict_append(d, "value", v2);
1267
1268            attach = xs_list_append(attach, d);
1269        }
1270
1271        msg = xs_dict_set(msg, "attachment", attach);
1272    }
1273
1274    /* use shared inboxes? */
1275    if (xs_type(xs_dict_get(srv_config, "shared_inboxes")) == XSTYPE_TRUE) {
1276        xs *d = xs_dict_new();
1277        xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
1278        d = xs_dict_append(d, "sharedInbox", si);
1279        msg = xs_dict_set(msg, "endpoints", d);
1280    }
1281
1282    /* does this user have an aka? */
1283    const char *aka = xs_dict_get(snac->config, "alias");
1284    if (xs_type(aka) == XSTYPE_STRING && *aka) {
1285        xs *loaka = xs_list_append(xs_list_new(), aka);
1286
1287        msg = xs_dict_set(msg, "alsoKnownAs", loaka);
1288    }
1289
1290    const xs_val *manually = xs_dict_get(snac->config, "approve_followers");
1291    msg = xs_dict_set(msg, "manuallyApprovesFollowers",
1292        xs_stock(xs_is_true(manually) ? XSTYPE_TRUE : XSTYPE_FALSE));
1293
1294    return msg;
1295}
1296
1297
1298xs_dict *msg_create(snac *snac, const xs_dict *object)
1299/* creates a 'Create' message */
1300{
1301    xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
1302    const xs_val *v;
1303
1304    if ((v = get_atto(object)))
1305        msg = xs_dict_append(msg, "attributedTo", v);
1306
1307    if ((v = xs_dict_get(object, "cc")))
1308        msg = xs_dict_append(msg, "cc", v);
1309
1310    if ((v = xs_dict_get(object, "to")))
1311        msg = xs_dict_append(msg, "to", v);
1312    else
1313        msg = xs_dict_append(msg, "to", public_address);
1314
1315    return msg;
1316}
1317
1318
1319xs_dict *msg_undo(snac *snac, const xs_val *object)
1320/* creates an 'Undo' message */
1321{
1322    xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
1323    const char *to;
1324
1325    if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
1326        msg = xs_dict_append(msg, "to", to);
1327
1328    return msg;
1329}
1330
1331
1332xs_dict *msg_delete(snac *snac, const char *id)
1333/* creates a 'Delete' + 'Tombstone' for a local entry */
1334{
1335    xs *tomb = xs_dict_new();
1336    xs_dict *msg = NULL;
1337
1338    /* sculpt the tombstone */
1339    tomb = xs_dict_append(tomb, "type", "Tombstone");
1340    tomb = xs_dict_append(tomb, "id",   id);
1341
1342    /* now create the Delete */
1343    msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
1344
1345    msg = xs_dict_append(msg, "to", public_address);
1346
1347    return msg;
1348}
1349
1350
1351xs_dict *msg_follow(snac *snac, const char *q)
1352/* creates a 'Follow' message */
1353{
1354    xs *actor_o = NULL;
1355    xs *actor   = NULL;
1356    xs_dict *msg = NULL;
1357    int status;
1358
1359    xs *url_or_uid = xs_strip_i(xs_str_new(q));
1360
1361    if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
1362        actor = xs_dup(url_or_uid);
1363    else
1364    if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
1365        snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
1366        return NULL;
1367    }
1368
1369    /* request the actor */
1370    status = actor_request(snac, actor, &actor_o);
1371
1372    if (valid_status(status)) {
1373        /* check if the actor is an alias */
1374        const char *r_actor = xs_dict_get(actor_o, "id");
1375
1376        if (r_actor && strcmp(actor, r_actor) != 0) {
1377            snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
1378        }
1379
1380        msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
1381    }
1382    else
1383        snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
1384
1385    return msg;
1386}
1387
1388
1389xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
1390                  const xs_str *in_reply_to, const xs_list *attach, int priv)
1391/* creates a 'Note' message */
1392{
1393    xs *ntid = tid(0);
1394    xs *id   = xs_fmt("%s/p/%s", snac->actor, ntid);
1395    xs *ctxt = NULL;
1396    xs *fc2  = NULL;
1397    xs *fc1  = NULL;
1398    xs *to   = NULL;
1399    xs *cc   = xs_list_new();
1400    xs *irt  = NULL;
1401    xs *tag  = xs_list_new();
1402    xs *atls = xs_list_new();
1403    xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
1404    xs_list *p;
1405    const xs_val *v;
1406
1407    if (rcpts == NULL)
1408        to = xs_list_new();
1409    else {
1410        if (xs_type(rcpts) == XSTYPE_STRING) {
1411            to = xs_list_new();
1412            to = xs_list_append(to, rcpts);
1413        }
1414        else
1415            to = xs_dup(rcpts);
1416    }
1417
1418    /* format the content */
1419    fc2 = not_really_markdown(content, &atls, &tag);
1420
1421    if (in_reply_to != NULL && *in_reply_to) {
1422        xs *p_msg = NULL;
1423        xs *wrk   = NULL;
1424
1425        /* demand this thing */
1426        timeline_request(snac, &in_reply_to, &wrk, 0);
1427
1428        if (valid_status(object_get(in_reply_to, &p_msg))) {
1429            /* add this author as recipient */
1430            const char *a, *v;
1431
1432            if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
1433                to = xs_list_append(to, a);
1434
1435            /* add this author to the tag list as a mention */
1436            if (!xs_is_null(a)) {
1437                xs *l = xs_split(a, "/");
1438                xs *actor_o = NULL;
1439
1440                if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
1441                    const char *uname = xs_dict_get(actor_o, "preferredUsername");
1442
1443                    if (!xs_is_null(uname) && *uname) {
1444                        xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
1445
1446                        xs *t = xs_dict_new();
1447
1448                        t = xs_dict_append(t, "type", "Mention");
1449                        t = xs_dict_append(t, "href", a);
1450                        t = xs_dict_append(t, "name", handle);
1451
1452                        tag = xs_list_append(tag, t);
1453                    }
1454                }
1455            }
1456
1457            /* get the context, if there is one */
1458            if ((v = xs_dict_get(p_msg, "context")))
1459                ctxt = xs_dup(v);
1460
1461            /* propagate the conversation field, if there is one */
1462            if ((v = xs_dict_get(p_msg, "conversation")))
1463                msg = xs_dict_append(msg, "conversation", v);
1464
1465            /* if this message is public, ours will also be */
1466            if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
1467                to = xs_list_append(to, public_address);
1468        }
1469
1470        irt = xs_dup(in_reply_to);
1471    }
1472    else
1473        irt = xs_val_new(XSTYPE_NULL);
1474
1475    /* extract the mentions and hashtags and convert the content */
1476    fc1 = process_tags(snac, fc2, &tag);
1477
1478    /* create the attachment list, if there are any */
1479    if (!xs_is_null(attach)) {
1480        int c = 0;
1481        while (xs_list_next(attach, &v, &c)) {
1482            xs *d            = xs_dict_new();
1483            const char *url  = xs_list_get(v, 0);
1484            const char *alt  = xs_list_get(v, 1);
1485            const char *mime = xs_mime_by_ext(url);
1486
1487            d = xs_dict_append(d, "mediaType", mime);
1488            d = xs_dict_append(d, "url",       url);
1489            d = xs_dict_append(d, "name",      alt);
1490            d = xs_dict_append(d, "type",
1491                xs_startswith(mime, "image/") ? "Image" : "Document");
1492
1493            atls = xs_list_append(atls, d);
1494        }
1495    }
1496
1497    if (ctxt == NULL)
1498        ctxt = xs_fmt("%s#ctxt", id);
1499
1500    /* add all mentions to the cc */
1501    p = tag;
1502    while (xs_list_iter(&p, &v)) {
1503        if (xs_type(v) == XSTYPE_DICT) {
1504            const char *t;
1505
1506            if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
1507                if (!xs_is_null(t = xs_dict_get(v, "href")))
1508                    cc = xs_list_append(cc, t);
1509            }
1510        }
1511    }
1512
1513    /* no recipients? must be for everybody */
1514    if (!priv && xs_list_len(to) == 0)
1515        to = xs_list_append(to, public_address);
1516
1517    /* delete all cc recipients that also are in the to */
1518    p = to;
1519    while (xs_list_iter(&p, &v)) {
1520        int i;
1521
1522        if ((i = xs_list_in(cc, v)) != -1)
1523            cc = xs_list_del(cc, i);
1524    }
1525
1526    msg = xs_dict_append(msg, "attributedTo", snac->actor);
1527    msg = xs_dict_append(msg, "summary",      "");
1528    msg = xs_dict_append(msg, "content",      fc1);
1529    msg = xs_dict_append(msg, "context",      ctxt);
1530    msg = xs_dict_append(msg, "url",          id);
1531    msg = xs_dict_append(msg, "to",           to);
1532    msg = xs_dict_append(msg, "cc",           cc);
1533    msg = xs_dict_append(msg, "inReplyTo",    irt);
1534    msg = xs_dict_append(msg, "tag",          tag);
1535
1536    msg = xs_dict_append(msg, "sourceContent", content);
1537
1538    if (xs_list_len(atls))
1539        msg = xs_dict_append(msg, "attachment", atls);
1540
1541    return msg;
1542}
1543
1544
1545xs_dict *msg_ping(snac *user, const char *rcpt)
1546/* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
1547{
1548    xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
1549
1550    msg = xs_dict_append(msg, "to", rcpt);
1551
1552    return msg;
1553}
1554
1555
1556xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
1557/* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
1558{
1559    xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
1560
1561    msg = xs_dict_append(msg, "to", rcpt);
1562
1563    return msg;
1564}
1565
1566
1567xs_dict *msg_move(snac *user, const char *new_account)
1568/* creates a Move message (to move the user to new_account) */
1569{
1570    xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
1571
1572    msg = xs_dict_append(msg, "target", new_account);
1573
1574    return msg;
1575}
1576
1577
1578xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
1579                      const xs_list *opts, int multiple, int end_secs)
1580/* creates a Question message */
1581{
1582    xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
1583    int max      = 8;
1584    xs_set seen;
1585
1586    msg = xs_dict_set(msg, "type", "Question");
1587
1588    /* make it non-editable */
1589    msg = xs_dict_del(msg, "sourceContent");
1590
1591    xs *o = xs_list_new();
1592    xs_list *p = (xs_list *)opts;
1593    const xs_str *v;
1594    xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
1595
1596    xs_set_init(&seen);
1597
1598    while (max && xs_list_iter(&p, &v)) {
1599        if (*v) {
1600            xs *v2 = xs_dup(v);
1601            xs *d  = xs_dict_new();
1602
1603            if (strlen(v2) > 60) {
1604                v2[60] = '\0';
1605                v2 = xs_str_cat(v2, "...");
1606            }
1607
1608            if (xs_set_add(&seen, v2) == 1) {
1609                d = xs_dict_append(d, "type",    "Note");
1610                d = xs_dict_append(d, "name",    v2);
1611                d = xs_dict_append(d, "replies", replies);
1612                o = xs_list_append(o, d);
1613
1614                max--;
1615            }
1616        }
1617    }
1618
1619    xs_set_free(&seen);
1620
1621    msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
1622
1623    /* set the end time */
1624    time_t t = time(NULL) + end_secs;
1625    xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
1626
1627    msg = xs_dict_append(msg, "endTime", et);
1628
1629    return msg;
1630}
1631
1632
1633int update_question(snac *user, const char *id)
1634/* updates the poll counts */
1635{
1636    xs *msg   = NULL;
1637    xs *rcnt  = xs_dict_new();
1638    xs *lopts = xs_list_new();
1639    const xs_list *opts;
1640    xs_list *p;
1641    const xs_val *v;
1642
1643    /* get the object */
1644    if (!valid_status(object_get(id, &msg)))
1645        return -1;
1646
1647    /* closed? do nothing more */
1648    if (xs_dict_get(msg, "closed"))
1649        return -2;
1650
1651    /* get the options */
1652    if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
1653        (opts = xs_dict_get(msg, "anyOf")) == NULL)
1654        return -3;
1655
1656    /* fill the initial count */
1657    int c = 0;
1658    while (xs_list_next(opts, &v, &c)) {
1659        const char *name = xs_dict_get(v, "name");
1660        if (name) {
1661            lopts = xs_list_append(lopts, name);
1662            rcnt  = xs_dict_set(rcnt, name, xs_stock(0));
1663        }
1664    }
1665
1666    xs_set s;
1667    xs_set_init(&s);
1668
1669    /* iterate now the children (the votes) */
1670    xs *chld = object_children(id);
1671    p = chld;
1672    while (xs_list_iter(&p, &v)) {
1673        xs *obj = NULL;
1674
1675        if (!valid_status(object_get_by_md5(v, &obj)))
1676            continue;
1677
1678        const char *name = xs_dict_get(obj, "name");
1679        const char *atto = get_atto(obj);
1680
1681        if (name && atto) {
1682            /* get the current count */
1683            const xs_number *cnt = xs_dict_get(rcnt, name);
1684
1685            if (xs_type(cnt) == XSTYPE_NUMBER) {
1686                /* if it exists, increment */
1687                xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
1688                rcnt = xs_dict_set(rcnt, name, ucnt);
1689
1690                xs_set_add(&s, atto);
1691            }
1692        }
1693    }
1694
1695    xs *rcpts = xs_set_result(&s);
1696
1697    /* create a new list of options with their new counts */
1698    xs *nopts = xs_list_new();
1699    p = lopts;
1700    while (xs_list_iter(&p, &v)) {
1701        const xs_number *cnt = xs_dict_get(rcnt, v);
1702
1703        if (xs_type(cnt) == XSTYPE_NUMBER) {
1704            xs *d1 = xs_dict_new();
1705            xs *d2 = xs_dict_new();
1706
1707            d2 = xs_dict_append(d2, "type",       "Collection");
1708            d2 = xs_dict_append(d2, "totalItems", cnt);
1709
1710            d1 = xs_dict_append(d1, "type",    "Note");
1711            d1 = xs_dict_append(d1, "name",    v);
1712            d1 = xs_dict_append(d1, "replies", d2);
1713
1714            nopts = xs_list_append(nopts, d1);
1715        }
1716    }
1717
1718    /* update the list */
1719    msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
1720
1721    /* due date? */
1722    int closed = 0;
1723    const char *end_time = xs_dict_get(msg, "endTime");
1724    if (!xs_is_null(end_time)) {
1725        xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
1726
1727        /* is now greater than the endTime? */
1728        if (strcmp(now, end_time) >= 0) {
1729            xs *et    = xs_dup(end_time);
1730            msg       = xs_dict_set(msg, "closed", et);
1731
1732            closed = 1;
1733        }
1734    }
1735
1736    /* update the count of voters */
1737    xs *vcnt = xs_number_new(xs_list_len(rcpts));
1738    msg = xs_dict_set(msg, "votersCount", vcnt);
1739    msg = xs_dict_set(msg, "cc", rcpts);
1740
1741    /* store */
1742    object_add_ow(id, msg);
1743
1744    snac_debug(user, 1, xs_fmt("recounted poll %s", id));
1745    timeline_touch(user);
1746
1747    /* send an update message to all voters */
1748    xs *u_msg = msg_update(user, msg);
1749    u_msg = xs_dict_set(u_msg, "cc", rcpts);
1750
1751    enqueue_message(user, u_msg);
1752
1753    if (closed) {
1754        xs *c_msg = msg_update(user, msg);
1755        notify(user, "Update", "Question", user->actor, c_msg);
1756    }
1757
1758    return 0;
1759}
1760
1761
1762/** queues **/
1763
1764int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
1765/* processes an ActivityPub message from the input queue */
1766/* return values: -1, fatal error; 0, transient error, retry;
1767   1, processed and done; 2, propagate to users (only when no user is set) */
1768{
1769    const char *actor = xs_dict_get(msg, "actor");
1770    const char *type  = xs_dict_get(msg, "type");
1771    xs *actor_o = NULL;
1772    int a_status;
1773    int do_notify = 0;
1774
1775    if (xs_is_null(actor) || *actor == '\0') {
1776        srv_debug(0, xs_fmt("malformed message (bad actor)"));
1777        return -1;
1778    }
1779
1780    /* question votes may not have a type */
1781    if (xs_is_null(type))
1782        type = "Note";
1783
1784    /* reject uninteresting messages right now */
1785    if (xs_match(type, "Add|View|Reject|Read|Remove")) {
1786        srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
1787        return -1;
1788    }
1789
1790    const char *object, *utype;
1791
1792    object = xs_dict_get(msg, "object");
1793    if (object != NULL && xs_type(object) == XSTYPE_DICT)
1794        utype = xs_dict_get(object, "type");
1795    else
1796        utype = "(null)";
1797
1798    /* special case for Delete messages */
1799    if (strcmp(type, "Delete") == 0) {
1800        /* if the actor is not here, do not even try */
1801        if (!object_here(actor)) {
1802            srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
1803            return -1;
1804        }
1805
1806        /* discard crap */
1807        if (xs_is_null(object)) {
1808            srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
1809            return -1;
1810        }
1811
1812        /* also discard if the object to be deleted is not here */
1813        const char *obj_id = object;
1814        if (xs_type(obj_id) == XSTYPE_DICT)
1815            obj_id = xs_dict_get(obj_id, "id");
1816
1817        if (xs_is_null(obj_id) || !object_here(obj_id)) {
1818            srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
1819            return -1;
1820        }
1821    }
1822
1823    /* bring the actor */
1824    a_status = actor_request(snac, actor, &actor_o);
1825
1826    /* do not retry permanent failures */
1827    if (a_status == HTTP_STATUS_NOT_FOUND
1828        || a_status == HTTP_STATUS_GONE
1829        || a_status < 0) {
1830        srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
1831        return -1;
1832    }
1833
1834    if (!valid_status(a_status)) {
1835        /* do not retry 'Delete' messages */
1836        if (strcmp(type, "Delete") == 0) {
1837            srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
1838            return -1;
1839        }
1840
1841        /* other actor download errors */
1842
1843        /* the actor may require a signed request; propagate if no user is set */
1844        if (snac == NULL)
1845            return 2;
1846
1847        /* may need a retry */
1848        srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
1849        return 0;
1850    }
1851
1852    /* check the signature */
1853    xs *sig_err = NULL;
1854
1855    if (!check_signature(req, &sig_err)) {
1856        srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
1857
1858        srv_archive_error("check_signature", sig_err, req, msg);
1859        return -1;
1860    }
1861
1862    /* if no user is set, no further checks can be done; propagate */
1863    if (snac == NULL)
1864        return 2;
1865
1866    /* reject messages that are not for this user */
1867    if (!is_msg_for_me(snac, msg)) {
1868        snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
1869
1870        return 1;
1871    }
1872
1873    /* if it's a DM from someone we don't follow, reject the message */
1874    if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
1875        if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
1876            !following_check(snac, actor)) {
1877            snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
1878
1879            return 1;
1880        }
1881    }
1882
1883    /* check the minimum acceptable account age */
1884    int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
1885
1886    if (min_account_age > 0) {
1887        const char *actor_date = xs_dict_get(actor_o, "published");
1888        if (!xs_is_null(actor_date)) {
1889            time_t actor_t = xs_parse_iso_date(actor_date, 0);
1890
1891            if (actor_t < 950000000) {
1892                snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
1893                    actor, actor_date));
1894
1895                return 1;
1896            }
1897
1898            if (actor_t > 0) {
1899                int td = (int)(time(NULL) - actor_t);
1900
1901                snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
1902
1903                if (td < min_account_age) {
1904                    snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
1905                        actor, td));
1906
1907                    return 1;
1908                }
1909            }
1910        }
1911        else
1912            snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
1913    }
1914
1915    if (strcmp(type, "Follow") == 0) { /** **/
1916        const char *id = xs_dict_get(msg, "id");
1917
1918        if (xs_is_null(id)) {
1919            snac_log(snac, xs_fmt("malformed message: no 'id' field"));
1920        }
1921        else
1922        if (!follower_check(snac, actor)) {
1923            /* ensure the actor object is here */
1924            if (!object_here(actor)) {
1925                xs *actor_obj = NULL;
1926                actor_request(snac, actor, &actor_obj);
1927                object_add(actor, actor_obj);
1928            }
1929
1930            if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) {
1931                pending_add(snac, actor, msg);
1932
1933                snac_log(snac, xs_fmt("new pending follower approval %s", actor));
1934            }
1935            else {
1936                /* automatic following */
1937                xs *f_msg = xs_dup(msg);
1938                xs *reply = msg_accept(snac, f_msg, actor);
1939
1940                post_message(snac, actor, reply);
1941
1942                if (xs_is_null(xs_dict_get(f_msg, "published"))) {
1943                    /* add a date if it doesn't include one (Mastodon) */
1944                    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
1945                    f_msg = xs_dict_set(f_msg, "published", date);
1946                }
1947
1948                timeline_add(snac, id, f_msg);
1949
1950                follower_add(snac, actor);
1951
1952                snac_log(snac, xs_fmt("new follower %s", actor));
1953            }
1954
1955            do_notify = 1;
1956        }
1957        else
1958            snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
1959    }
1960    else
1961    if (strcmp(type, "Undo") == 0) { /** **/
1962        const char *id = xs_dict_get(object, "object");
1963
1964        if (xs_type(object) != XSTYPE_DICT)
1965            utype = "Follow";
1966
1967        if (strcmp(utype, "Follow") == 0) { /** **/
1968            if (id && strcmp(id, snac->actor) != 0)
1969                snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
1970            else {
1971                if (valid_status(follower_del(snac, actor))) {
1972                    snac_log(snac, xs_fmt("no longer following us %s", actor));
1973                    do_notify = 1;
1974                }
1975                else
1976                if (pending_check(snac, actor)) {
1977                    pending_del(snac, actor);
1978                    snac_log(snac, xs_fmt("cancelled pending follow from %s", actor));
1979                }
1980                else
1981                    snac_log(snac, xs_fmt("error deleting follower %s", actor));
1982            }
1983        }
1984        else
1985        if (strcmp(utype, "Like") == 0 || strcmp(utype, "EmojiReact") == 0) { /** **/
1986            int status = object_unadmire(id, actor, 1);
1987
1988            snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
1989        }
1990        else
1991        if (strcmp(utype, "Announce") == 0) { /** **/
1992            int status = HTTP_STATUS_OK;
1993
1994            /* commented out: if a followed user boosts something that
1995               is requested and then unboosts, the post remains here,
1996               but with no apparent reason, and that is confusing */
1997            //status = object_unadmire(id, actor, 0);
1998
1999            snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
2000        }
2001        else
2002            snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
2003    }
2004    else
2005    if (strcmp(type, "Create") == 0) { /** **/
2006        if (is_muted(snac, actor)) {
2007            snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
2008            return 1;
2009        }
2010
2011        if (xs_match(utype, "Note|Article")) { /** **/
2012            const char *id          = xs_dict_get(object, "id");
2013            const char *in_reply_to = get_in_reply_to(object);
2014            const char *atto        = get_atto(object);
2015            xs *wrk           = NULL;
2016
2017            if (xs_is_null(id))
2018                snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2019            else
2020            if (xs_is_null(atto))
2021                snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
2022            else
2023            if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
2024                snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
2025            }
2026            else {
2027                if (content_match("filter_reject.txt", object)) {
2028                    snac_log(snac, xs_fmt("rejected by content %s", id));
2029                    return 1;
2030                }
2031
2032                if (strcmp(actor, atto) != 0)
2033                    snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
2034
2035                timeline_request(snac, &in_reply_to, &wrk, 0);
2036
2037                if (timeline_add(snac, id, object)) {
2038                    snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
2039                    do_notify = 1;
2040                }
2041
2042                /* if it has a "name" field, it may be a vote for a question */
2043                const char *name = xs_dict_get(object, "name");
2044
2045                if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
2046                    update_question(snac, in_reply_to);
2047            }
2048        }
2049        else
2050        if (strcmp(utype, "Question") == 0) { /**  **/
2051            const char *id = xs_dict_get(object, "id");
2052
2053            if (xs_is_null(id))
2054                snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2055            else
2056            if (timeline_add(snac, id, object))
2057                snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
2058        }
2059        else
2060        if (strcmp(utype, "Video") == 0) { /** **/
2061            const char *id = xs_dict_get(object, "id");
2062
2063            if (xs_is_null(id))
2064                snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2065            else
2066            if (timeline_add(snac, id, object))
2067                snac_log(snac, xs_fmt("new 'Video' %s %s", actor, id));
2068        }
2069        else
2070            snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
2071    }
2072    else
2073    if (strcmp(type, "Accept") == 0) { /** **/
2074        if (strcmp(utype, "(null)") == 0) {
2075            const char *obj_id = xs_dict_get(msg, "object");
2076
2077            /* if the accepted object id is a string that may
2078               be created by us, it's a follow */
2079            if (xs_type(obj_id) == XSTYPE_STRING &&
2080                xs_startswith(obj_id, srv_baseurl) &&
2081                xs_endswith(obj_id, "/Follow"))
2082                utype = "Follow";
2083        }
2084
2085        if (strcmp(utype, "Follow") == 0) { /** **/
2086            if (following_check(snac, actor)) {
2087                following_add(snac, actor, msg);
2088                snac_log(snac, xs_fmt("confirmed follow from %s", actor));
2089            }
2090            else
2091                snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
2092        }
2093        else
2094        if (strcmp(utype, "Create") == 0) {
2095            /* some implementations send Create confirmations, go figure */
2096            snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
2097        }
2098        else {
2099            srv_archive_error("accept", "ignored Accept", req, msg);
2100            snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
2101        }
2102    }
2103    else
2104    if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
2105        if (xs_type(object) == XSTYPE_DICT)
2106            object = xs_dict_get(object, "id");
2107
2108        if (xs_is_null(object))
2109            snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2110        else
2111        if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
2112            snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
2113        else
2114            snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
2115
2116        do_notify = 1;
2117    }
2118    else
2119    if (strcmp(type, "Announce") == 0) { /** **/
2120        if (xs_type(object) == XSTYPE_DICT)
2121            object = xs_dict_get(object, "id");
2122
2123        if (xs_is_null(object))
2124            snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2125        else
2126        if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
2127            snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
2128        else
2129        if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
2130            snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
2131        else {
2132            xs *a_msg = NULL;
2133            xs *wrk   = NULL;
2134
2135            timeline_request(snac, &object, &wrk, 0);
2136
2137            if (valid_status(object_get(object, &a_msg))) {
2138                const char *who = get_atto(a_msg);
2139
2140                if (who && !is_muted(snac, who)) {
2141                    /* bring the actor */
2142                    xs *who_o = NULL;
2143
2144                    if (valid_status(actor_request(snac, who, &who_o))) {
2145                        if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
2146                            snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
2147                        else
2148                            snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
2149                                actor, object));
2150
2151                        /* distribute the post with the actor as 'proxy' */
2152                        list_distribute(snac, actor, a_msg);
2153
2154                        do_notify = 1;
2155                    }
2156                    else
2157                        snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
2158                }
2159                else
2160                    snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
2161            }
2162            else
2163                snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
2164        }
2165    }
2166    else
2167    if (strcmp(type, "Update") == 0) { /** **/
2168        if (xs_match(utype, "Person|Service")) { /** **/
2169            actor_add(actor, xs_dict_get(msg, "object"));
2170            timeline_touch(snac);
2171
2172            snac_log(snac, xs_fmt("updated actor %s", actor));
2173        }
2174        else
2175        if (xs_match(utype, "Note|Page|Article|Video")) { /** **/
2176            const char *id = xs_dict_get(object, "id");
2177
2178            if (xs_is_null(id))
2179                snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2180            else
2181            if (object_here(id)) {
2182                object_add_ow(id, object);
2183                timeline_touch(snac);
2184
2185                snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
2186            }
2187            else
2188                snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
2189        }
2190        else
2191        if (strcmp(utype, "Question") == 0) { /** **/
2192            const char *id     = xs_dict_get(object, "id");
2193            const char *closed = xs_dict_get(object, "closed");
2194
2195            if (xs_is_null(id))
2196                snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2197            else {
2198                object_add_ow(id, object);
2199                timeline_touch(snac);
2200
2201                snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
2202
2203                if (closed != NULL)
2204                    do_notify = 1;
2205            }
2206        }
2207        else {
2208            srv_archive_error("unsupported_update", "unsupported_update", req, msg);
2209
2210            snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
2211        }
2212    }
2213    else
2214    if (strcmp(type, "Delete") == 0) { /** **/
2215        if (xs_type(object) == XSTYPE_DICT)
2216            object = xs_dict_get(object, "id");
2217
2218        if (xs_is_null(object))
2219            snac_log(snac, xs_fmt("malformed message: no 'id' field"));
2220        else
2221        if (object_here(object)) {
2222            timeline_del(snac, object);
2223            snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
2224        }
2225        else
2226            snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
2227    }
2228    else
2229    if (strcmp(type, "Pong") == 0) { /** **/
2230        snac_log(snac, xs_fmt("'Pong' received from %s", actor));
2231    }
2232    else
2233    if (strcmp(type, "Ping") == 0) { /** **/
2234        const char *id = xs_dict_get(msg, "id");
2235
2236        snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
2237
2238        if (!xs_is_null(id)) {
2239            xs *rsp = msg_pong(snac, actor, id);
2240
2241            enqueue_output_by_actor(snac, rsp, actor, 0);
2242        }
2243    }
2244    else
2245    if (strcmp(type, "Block") == 0) { /** **/
2246        snac_debug(snac, 1, xs_fmt("'Block' received from %s", actor));
2247
2248        /* should we MUTE the actor back? */
2249        /* mute(snac, actor); */
2250
2251        if (!xs_is_true(xs_dict_get(srv_config, "disable_block_notifications")))
2252            do_notify = 1;
2253    }
2254    else
2255    if (strcmp(type, "Move") == 0) { /** **/
2256        do_notify = 1;
2257
2258        const char *old_account = xs_dict_get(msg, "object");
2259        const char *new_account = xs_dict_get(msg, "target");
2260
2261        if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
2262            if (following_check(snac, old_account)) {
2263                xs *n_actor = NULL;
2264
2265                if (valid_status(object_get(new_account, &n_actor))) {
2266                    const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
2267
2268                    if (xs_type(aka) == XSTYPE_LIST) {
2269                        if (xs_list_in(aka, old_account) != -1) {
2270                            /* all conditions met! */
2271
2272                            /* follow new account */
2273                            xs *f_msg = msg_follow(snac, new_account);
2274
2275                            if (f_msg != NULL) {
2276                                const char *new_actor = xs_dict_get(f_msg, "object");
2277                                following_add(snac, new_actor, f_msg);
2278                                enqueue_output_by_actor(snac, f_msg, new_actor, 0);
2279
2280                                snac_log(snac, xs_fmt("'Move': following %s", new_account));
2281                            }
2282
2283                            /* unfollow old account */
2284                            xs *of_msg = NULL;
2285
2286                            if (valid_status(following_get(snac, old_account, &of_msg))) {
2287                                xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
2288                                following_del(snac, old_account);
2289                                enqueue_output_by_actor(snac, uf_msg, old_account, 0);
2290
2291                                snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
2292                            }
2293                        }
2294                        else
2295                            snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
2296                                old_account, new_account));
2297                    }
2298                    else
2299                        snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
2300                }
2301                else {
2302                    snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
2303
2304                    /* may be a server hiccup, retry later */
2305                    return 0;
2306                }
2307            }
2308            else
2309                snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
2310        }
2311        else {
2312            snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
2313            srv_archive_error("move", "move", req, msg);
2314        }
2315    }
2316    else {
2317        srv_archive_error("unsupported_type", "unsupported_type", req, msg);
2318
2319        snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
2320    }
2321
2322    if (do_notify) {
2323        notify(snac, type, utype, actor, msg);
2324
2325        timeline_touch(snac);
2326    }
2327
2328    return 1;
2329}
2330
2331
2332int send_email(const char *msg)
2333/* invoke sendmail with email headers and body in msg */
2334{
2335    FILE *f;
2336    int status;
2337    int fds[2];
2338    pid_t pid;
2339    if (pipe(fds) == -1) return -1;
2340    pid = vfork();
2341    if (pid == -1) return -1;
2342    else if (pid == 0) {
2343        dup2(fds[0], 0);
2344        close(fds[0]);
2345        close(fds[1]);
2346        execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
2347        _exit(1);
2348    }
2349    close(fds[0]);
2350    if ((f = fdopen(fds[1], "w")) == NULL) {
2351        close(fds[1]);
2352        return -1;
2353    }
2354    fprintf(f, "%s\n", msg);
2355    fclose(f);
2356    if (waitpid(pid, &status, 0) == -1) return -1;
2357    return status;
2358}
2359
2360
2361void process_user_queue_item(snac *snac, xs_dict *q_item)
2362/* processes an item from the user queue */
2363{
2364    const char *type;
2365    int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
2366
2367    if ((type = xs_dict_get(q_item, "type")) == NULL)
2368        type = "output";
2369
2370    if (strcmp(type, "message") == 0) {
2371        const xs_dict *msg = xs_dict_get(q_item, "message");
2372        xs *rcpts = recipient_list(snac, msg, 1);
2373        xs_set inboxes;
2374        const xs_str *actor;
2375        int c;
2376
2377        xs_set_init(&inboxes);
2378
2379        /* iterate the recipients */
2380        c = 0;
2381        while (xs_list_next(rcpts, &actor, &c)) {
2382            xs *inbox = get_actor_inbox(actor, 1);
2383
2384            if (inbox != NULL) {
2385                /* add to the set and, if it's not there, send message */
2386                if (xs_set_add(&inboxes, inbox) == 1)
2387                    enqueue_output(snac, msg, inbox, 0, 0);
2388            }
2389            else
2390                snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
2391        }
2392
2393        /* if it's a public note or question, send to the collected inboxes */
2394        if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
2395            if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
2396                xs *shibx = inbox_list();
2397                const xs_str *inbox;
2398
2399                c = 0;
2400                while (xs_list_next(shibx, &inbox, &c)) {
2401                    if (xs_set_add(&inboxes, inbox) == 1)
2402                        enqueue_output(snac, msg, inbox, 0, 0);
2403                }
2404            }
2405        }
2406
2407        xs_set_free(&inboxes);
2408    }
2409    else
2410    if (strcmp(type, "input") == 0) {
2411        /* process the message */
2412        const xs_dict *msg = xs_dict_get(q_item, "message");
2413        const xs_dict *req = xs_dict_get(q_item, "req");
2414        int retries  = xs_number_get(xs_dict_get(q_item, "retries"));
2415
2416        if (xs_is_null(msg))
2417            return;
2418
2419        if (!process_input_message(snac, msg, req)) {
2420            if (retries > queue_retry_max)
2421                snac_log(snac, xs_fmt("input giving up"));
2422            else {
2423                /* reenqueue */
2424                enqueue_input(snac, msg, req, retries + 1);
2425                snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
2426            }
2427        }
2428    }
2429    else
2430    if (strcmp(type, "close_question") == 0) {
2431        /* the time for this question has ended */
2432        const char *id = xs_dict_get(q_item, "message");
2433
2434        if (!xs_is_null(id))
2435            update_question(snac, id);
2436    }
2437    else
2438    if (strcmp(type, "object_request") == 0) {
2439        const char *id = xs_dict_get(q_item, "message");
2440
2441        if (!xs_is_null(id)) {
2442            int status;
2443            xs *data = NULL;
2444
2445            status = activitypub_request(snac, id, &data);
2446
2447            if (valid_status(status))
2448                object_add_ow(id, data);
2449
2450            snac_debug(snac, 1, xs_fmt("object_request %s %d", id, status));
2451        }
2452    }
2453    else
2454    if (strcmp(type, "verify_links") == 0) {
2455        verify_links(snac);
2456    }
2457    else
2458    if (strcmp(type, "actor_refresh") == 0) {
2459        const char *actor = xs_dict_get(q_item, "actor");
2460        double mtime = object_mtime(actor);
2461
2462        /* only refresh if it was refreshed more than an hour ago */
2463        if (mtime + 3600.0 < (double) time(NULL)) {
2464            xs *actor_o = NULL;
2465            int status;
2466
2467            if (valid_status((status = activitypub_request(snac, actor, &actor_o))))
2468                actor_add(actor, actor_o);
2469            else
2470                object_touch(actor);
2471
2472            snac_log(snac, xs_fmt("actor_refresh %s %d", actor, status));
2473        }
2474    }
2475    else
2476        snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
2477}
2478
2479
2480int process_user_queue(snac *snac)
2481/* processes a user's queue */
2482{
2483    int cnt = 0;
2484    xs *list = user_queue(snac);
2485
2486    xs_list *p = list;
2487    const xs_str *fn;
2488
2489    while (xs_list_iter(&p, &fn)) {
2490        xs *q_item = dequeue(fn);
2491
2492        if (q_item == NULL)
2493            continue;
2494
2495        process_user_queue_item(snac, q_item);
2496        cnt++;
2497    }
2498
2499    return cnt;
2500}
2501
2502
2503void process_queue_item(xs_dict *q_item)
2504/* processes an item from the global queue */
2505{
2506    const char *type = xs_dict_get(q_item, "type");
2507    int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
2508
2509    if (strcmp(type, "output") == 0) {
2510        int status;
2511        const xs_str *inbox  = xs_dict_get(q_item, "inbox");
2512        const xs_str *keyid  = xs_dict_get(q_item, "keyid");
2513        const xs_str *seckey = xs_dict_get(q_item, "seckey");
2514        const xs_dict *msg   = xs_dict_get(q_item, "message");
2515        int retries    = xs_number_get(xs_dict_get(q_item, "retries"));
2516        int p_status   = xs_number_get(xs_dict_get(q_item, "p_status"));
2517        xs *payload    = NULL;
2518        int p_size     = 0;
2519        int timeout    = 0;
2520
2521        if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
2522            srv_log(xs_fmt("output message error: missing fields"));
2523            return;
2524        }
2525
2526        if (is_instance_blocked(inbox)) {
2527            srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
2528            return;
2529        }
2530
2531        /* deliver (if previous error status was a timeout, try now longer) */
2532        if (p_status == 599)
2533            timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
2534        else
2535            timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
2536
2537        if (timeout == 0)
2538            timeout = 6;
2539
2540        status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
2541
2542        if (payload) {
2543            if (p_size > 64) {
2544                /* trim the message */
2545                payload[64] = '\0';
2546                payload = xs_str_cat(payload, "...");
2547            }
2548
2549            /* strip ugly control characters */
2550            payload = xs_replace_i(payload, "\n", "");
2551            payload = xs_replace_i(payload, "\r", "");
2552
2553            if (*payload)
2554                payload = xs_str_wrap_i(" [", payload, "]");
2555        }
2556        else
2557            payload = xs_str_new(NULL);
2558
2559        srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
2560
2561        if (!valid_status(status)) {
2562            retries++;
2563
2564            /* if it's not the first time it fails with a timeout,
2565               penalize the server by skipping one retry */
2566            if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
2567                retries++;
2568
2569            /* error sending; requeue? */
2570            if (status == HTTP_STATUS_BAD_REQUEST
2571                || status == HTTP_STATUS_NOT_FOUND
2572                || status == HTTP_STATUS_METHOD_NOT_ALLOWED
2573                || status == HTTP_STATUS_GONE
2574                || status == HTTP_STATUS_UNPROCESSABLE_CONTENT
2575                || status < 0)
2576                /* explicit error: discard */
2577                srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
2578            else
2579            if (retries > queue_retry_max)
2580                srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
2581            else {
2582                /* requeue */
2583                enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
2584                srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
2585            }
2586        }
2587    }
2588    else
2589    if (strcmp(type, "email") == 0) {
2590        /* send this email */
2591        const xs_str *msg = xs_dict_get(q_item, "message");
2592        int retries = xs_number_get(xs_dict_get(q_item, "retries"));
2593
2594        if (!send_email(msg))
2595            srv_debug(1, xs_fmt("email message sent"));
2596        else {
2597            retries++;
2598
2599            if (retries > queue_retry_max)
2600                srv_log(xs_fmt("email giving up (errno: %d)", errno));
2601            else {
2602                /* requeue */
2603                srv_log(xs_fmt(
2604                    "email requeue #%d (errno: %d)", retries, errno));
2605
2606                enqueue_email(msg, retries);
2607            }
2608        }
2609    }
2610    else
2611    if (strcmp(type, "telegram") == 0) {
2612        /* send this via telegram */
2613        const char *bot   = xs_dict_get(q_item, "bot");
2614        const char *msg   = xs_dict_get(q_item, "message");
2615        xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
2616        int status  = 0;
2617
2618        /* chat_id must start with a - */
2619        if (!xs_startswith(chat_id, "-"))
2620            chat_id = xs_str_wrap_i("-", chat_id, NULL);
2621
2622        xs *url  = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
2623        xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
2624
2625        xs *headers = xs_dict_new();
2626        headers = xs_dict_append(headers, "content-type", "application/json");
2627
2628        xs *rsp = xs_http_request("POST", url, headers,
2629                                   body, strlen(body), &status, NULL, NULL, 0);
2630        rsp = xs_free(rsp);
2631
2632        srv_debug(0, xs_fmt("telegram post %d", status));
2633    }
2634    else
2635    if (strcmp(type, "ntfy") == 0) {
2636        /* send this via ntfy */
2637        const char *ntfy_server   = xs_dict_get(q_item, "ntfy_server");
2638        const char *msg   = xs_dict_get(q_item, "message");
2639        const char *ntfy_token   = xs_dict_get(q_item, "ntfy_token");
2640        int status  = 0;
2641
2642        xs *url  = xs_fmt("%s", ntfy_server);
2643        xs *body = xs_fmt("%s", msg);
2644
2645        xs *headers = xs_dict_new();
2646        headers = xs_dict_append(headers, "content-type", "text/plain");
2647        // Append the Authorization header only if ntfy_token is not NULL
2648         if (ntfy_token != NULL) {
2649             headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
2650             }
2651
2652        xs *rsp = xs_http_request("POST", url, headers,
2653                                   body, strlen(body), &status, NULL, NULL, 0);
2654        rsp = xs_free(rsp);
2655
2656        srv_debug(0, xs_fmt("ntfy post %d", status));
2657    }
2658    else
2659    if (strcmp(type, "purge") == 0) {
2660        srv_log(xs_dup("purge start"));
2661
2662        purge_all();
2663
2664        srv_log(xs_dup("purge end"));
2665    }
2666    else
2667    if (strcmp(type, "input") == 0) {
2668        const xs_dict *msg = xs_dict_get(q_item, "message");
2669        const xs_dict *req = xs_dict_get(q_item, "req");
2670        int retries  = xs_number_get(xs_dict_get(q_item, "retries"));
2671
2672        /* do some instance-level checks */
2673        int r = process_input_message(NULL, msg, req);
2674
2675        if (r == 0) {
2676            /* transient error? retry */
2677            if (retries > queue_retry_max)
2678                srv_log(xs_fmt("shared input giving up"));
2679            else {
2680                /* reenqueue */
2681                enqueue_shared_input(msg, req, retries + 1);
2682                srv_log(xs_fmt("shared input requeue #%d", retries + 1));
2683            }
2684        }
2685        else
2686        if (r == 2) {
2687            /* redistribute the input message to all users */
2688            const char *ntid = xs_dict_get(q_item, "ntid");
2689            xs *tmpfn  = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
2690            FILE *f;
2691
2692            if ((f = fopen(tmpfn, "w")) != NULL) {
2693                xs_json_dump(q_item, 4, f);
2694                fclose(f);
2695            }
2696
2697            xs *users = user_list();
2698            xs_list *p = users;
2699            const char *v;
2700            int cnt = 0;
2701
2702            while (xs_list_iter(&p, &v)) {
2703                snac user;
2704
2705                if (user_open(&user, v)) {
2706                    if (is_msg_for_me(&user, msg)) {
2707                        xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
2708
2709                        snac_debug(&user, 1,
2710                            xs_fmt("enqueue_input (from shared inbox) %s", xs_dict_get(msg, "id")));
2711
2712                        if (link(tmpfn, fn) < 0)
2713                            srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
2714
2715                        cnt++;
2716                    }
2717
2718                    user_free(&user);
2719                }
2720            }
2721
2722            unlink(tmpfn);
2723
2724            if (cnt == 0) {
2725                srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
2726            }
2727        }
2728    }
2729    else
2730        srv_log(xs_fmt("unexpected q_item type '%s'", type));
2731}
2732
2733
2734int process_queue(void)
2735/* processes the global queue */
2736{
2737    int cnt = 0;
2738    xs *list = queue();
2739
2740    xs_list *p = list;
2741    const xs_str *fn;
2742
2743    while (xs_list_iter(&p, &fn)) {
2744        xs *q_item = dequeue(fn);
2745
2746        if (q_item != NULL) {
2747            job_post(q_item, 0);
2748            cnt++;
2749        }
2750    }
2751
2752    return cnt;
2753}
2754
2755
2756/** account migration **/
2757
2758int migrate_account(snac *user)
2759/* migrates this account to a new one (stored in the 'alias' user field) */
2760{
2761    const char *new_account = xs_dict_get(user->config, "alias");
2762
2763    if (xs_type(new_account) != XSTYPE_STRING) {
2764        snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
2765        return 1;
2766    }
2767
2768    xs *new_actor = NULL;
2769    int status;
2770
2771    if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
2772        snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
2773        return 1;
2774    }
2775
2776    actor_add(new_account, new_actor);
2777
2778    const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
2779
2780    if (xs_type(loaka) != XSTYPE_LIST) {
2781        snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
2782        return 1;
2783    }
2784
2785    if (xs_list_in(loaka, user->actor) == -1) {
2786        snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
2787        return 1;
2788    }
2789
2790    xs *move = msg_move(user, new_account);
2791    xs *fwers = follower_list(user);
2792
2793    const char *actor;
2794    xs_list_foreach(fwers, actor) {
2795        /* get the actor inbox, excluding the shared one */
2796        xs *inbox = get_actor_inbox(actor, 0);
2797
2798        if (xs_is_null(inbox))
2799            snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
2800        else
2801            enqueue_output(user, move, inbox, 0, 0);
2802    }
2803
2804    return 0;
2805}
2806
2807
2808/** HTTP handlers */
2809
2810int activitypub_get_handler(const xs_dict *req, const char *q_path,
2811                            char **body, int *b_size, char **ctype)
2812{
2813    int status = HTTP_STATUS_OK;
2814    const char *accept = xs_dict_get(req, "accept");
2815    snac snac;
2816    xs *msg = NULL;
2817
2818    if (accept == NULL)
2819        return 0;
2820
2821    if (xs_str_in(accept, "application/activity+json") == -1 &&
2822        xs_str_in(accept, "application/ld+json") == -1)
2823        return 0;
2824
2825    xs *l = xs_split_n(q_path, "/", 2);
2826    const char *uid;
2827    const char *p_path;
2828
2829    uid = xs_list_get(l, 1);
2830    if (!user_open(&snac, uid)) {
2831        /* invalid user */
2832        srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
2833        return HTTP_STATUS_NOT_FOUND;
2834    }
2835
2836    p_path = xs_list_get(l, 2);
2837
2838    *ctype  = "application/activity+json";
2839
2840    if (p_path == NULL) {
2841        /* if there was no component after the user, it's an actor request */
2842        msg = msg_actor(&snac);
2843        *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
2844
2845        const char *ua = xs_dict_get(req, "user-agent");
2846
2847        snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
2848    }
2849    else
2850    if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
2851        xs *id = xs_fmt("%s/%s", snac.actor, p_path);
2852        xs *list = xs_list_new();
2853        msg = msg_collection(&snac, id);
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        xs *items = xs_number_new(xs_list_len(list));
2876        msg = xs_dict_set(msg, "orderedItems", list);
2877        msg = xs_dict_set(msg, "totalItems",   items);
2878    }
2879    else
2880    if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
2881        xs *id = xs_fmt("%s/%s", snac.actor, p_path);
2882        msg = msg_collection(&snac, id);
2883    }
2884    else
2885    if (xs_startswith(p_path, "p/")) {
2886        xs *id = xs_fmt("%s/%s", snac.actor, p_path);
2887
2888        status = object_get(id, &msg);
2889
2890        /* don't return non-public objects */
2891        if (valid_status(status) && !is_msg_public(msg))
2892            status = HTTP_STATUS_NOT_FOUND;
2893    }
2894    else
2895        status = HTTP_STATUS_NOT_FOUND;
2896
2897    if (status == HTTP_STATUS_OK && msg != NULL) {
2898        *body   = xs_json_dumps(msg, 4);
2899        *b_size = strlen(*body);
2900    }
2901
2902    snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
2903
2904    user_free(&snac);
2905
2906    return status;
2907}
2908
2909
2910int activitypub_post_handler(const xs_dict *req, const char *q_path,
2911                             char *payload, int p_size,
2912                             char **body, int *b_size, char **ctype)
2913/* processes an input message */
2914{
2915    (void)b_size;
2916
2917    int status = HTTP_STATUS_ACCEPTED;
2918    const char *i_ctype = xs_dict_get(req, "content-type");
2919    snac snac;
2920    const char *v;
2921
2922    if (i_ctype == NULL) {
2923        *body  = xs_str_new("no content-type");
2924        *ctype = "text/plain";
2925        return HTTP_STATUS_BAD_REQUEST;
2926    }
2927
2928    if (xs_is_null(payload)) {
2929        *body  = xs_str_new("no payload");
2930        *ctype = "text/plain";
2931        return HTTP_STATUS_BAD_REQUEST;
2932    }
2933
2934    if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
2935        xs_str_in(i_ctype, "application/ld+json") == -1)
2936        return 0;
2937
2938    /* decode the message */
2939    xs *msg = xs_json_loads(payload);
2940    const char *id = xs_dict_get(msg, "id");
2941
2942    if (msg == NULL) {
2943        srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
2944
2945        srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
2946
2947        *body  = xs_str_new("JSON error");
2948        *ctype = "text/plain";
2949        return HTTP_STATUS_BAD_REQUEST;
2950    }
2951
2952    if (id && is_instance_blocked(id)) {
2953        srv_debug(1, xs_fmt("full instance block for %s", id));
2954
2955        *body  = xs_str_new("blocked");
2956        *ctype = "text/plain";
2957        return HTTP_STATUS_FORBIDDEN;
2958    }
2959
2960    /* get the user and path */
2961    xs *l = xs_split_n(q_path, "/", 2);
2962
2963    if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
2964        enqueue_shared_input(msg, req, 0);
2965        return HTTP_STATUS_ACCEPTED;
2966    }
2967
2968    if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
2969        /* strange q_path */
2970        srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
2971        return HTTP_STATUS_NOT_FOUND;
2972    }
2973
2974    const char *uid = xs_list_get(l, 1);
2975    if (!user_open(&snac, uid)) {
2976        /* invalid user */
2977        srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
2978        return HTTP_STATUS_NOT_FOUND;
2979    }
2980
2981    /* if it has a digest, check it now, because
2982       later the payload won't be exactly the same */
2983    if ((v = xs_dict_get(req, "digest")) != NULL) {
2984        xs *s1 = xs_sha256_base64(payload, p_size);
2985        xs *s2 = xs_fmt("SHA-256=%s", s1);
2986
2987        if (strcmp(s2, v) != 0) {
2988            srv_log(xs_fmt("digest check FAILED"));
2989
2990            *body  = xs_str_new("bad digest");
2991            *ctype = "text/plain";
2992            status = HTTP_STATUS_BAD_REQUEST;
2993        }
2994    }
2995
2996    /* if the message is from a muted actor, reject it right now */
2997    if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
2998        if (is_muted(&snac, v)) {
2999            snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
3000
3001            *body  = xs_str_new("rejected");
3002            *ctype = "text/plain";
3003            status = HTTP_STATUS_FORBIDDEN;
3004        }
3005    }
3006
3007    if (valid_status(status)) {
3008        enqueue_input(&snac, msg, req, 0);
3009        *ctype = "application/activity+json";
3010    }
3011
3012    user_free(&snac);
3013
3014    return status;
3015}