2.92 data.c
   1/* snac - A simple, minimalistic ActivityPub instance */
   2/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
   3
   4#include "xs.h"
   5#include "xs_hex.h"
   6#include "xs_io.h"
   7#include "xs_json.h"
   8#include "xs_openssl.h"
   9#include "xs_glob.h"
  10#include "xs_set.h"
  11#include "xs_time.h"
  12#include "xs_regex.h"
  13#include "xs_match.h"
  14#include "xs_unicode.h"
  15#include "xs_random.h"
  16#include "xs_po.h"
  17#include "xs_http.h"
  18
  19#include "snac.h"
  20
  21#include <time.h>
  22#include <sys/stat.h>
  23#include <sys/file.h>
  24#include <sys/time.h>
  25#include <fcntl.h>
  26#include <pthread.h>
  27
  28double disk_layout = 2.7;
  29
  30/* storage serializer */
  31pthread_mutex_t data_mutex = {0};
  32
  33int snac_upgrade(xs_str **error);
  34
  35
  36int srv_open(const char *basedir, int auto_upgrade)
  37/* opens a server */
  38{
  39    int ret = 0;
  40    xs *cfg_file = NULL;
  41    FILE *f;
  42    xs_str *error = NULL;
  43
  44    /* MUST be three letters */
  45    months[0] = LL("Jan");
  46    months[1] = LL("Feb");
  47    months[2] = LL("Mar");
  48    months[3] = LL("Apr");
  49    months[4] = LL("May");
  50    months[5] = LL("Jun");
  51    months[6] = LL("Jul");
  52    months[7] = LL("Aug");
  53    months[8] = LL("Sep");
  54    months[9] = LL("Oct");
  55    months[10] = LL("Nov");
  56    months[11] = LL("Dec");
  57
  58    pthread_mutex_init(&data_mutex, NULL);
  59
  60    srv_basedir = xs_str_new(basedir);
  61
  62    if (xs_endswith(srv_basedir, "/"))
  63        srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  64
  65    cfg_file = xs_fmt("%s/server.json", basedir);
  66
  67    if ((f = fopen(cfg_file, "r")) == NULL)
  68        error = xs_fmt("ERROR: cannot open '%s'", cfg_file);
  69    else {
  70        /* read full config file */
  71        srv_config = xs_json_load(f);
  72        fclose(f);
  73
  74        /* parse */
  75
  76        if (srv_config == NULL)
  77            error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  78        else {
  79            const char *host;
  80            const char *prefix;
  81            const char *dbglvl;
  82            const char *proto;
  83
  84            host   = xs_dict_get(srv_config, "host");
  85            prefix = xs_dict_get(srv_config, "prefix");
  86            dbglvl = xs_dict_get(srv_config, "dbglevel");
  87            proto  = xs_dict_get_def(srv_config, "protocol", "https");
  88
  89            if (host == NULL || prefix == NULL)
  90                error = xs_str_new("ERROR: cannot get server data");
  91            else {
  92                srv_baseurl = xs_fmt("%s:/" "/%s%s", proto, host, prefix);
  93
  94                dbglevel = (int) xs_number_get(dbglvl);
  95
  96                if ((dbglvl = getenv("DEBUG")) != NULL) {
  97                    dbglevel = atoi(dbglvl);
  98                    error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  99                }
 100
 101                if (auto_upgrade)
 102                    ret = snac_upgrade(&error);
 103                else if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
 104                    error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
 105                else if (!check_strip_tool())
 106                    error = xs_fmt("ERROR: strip_exif enabled but commands not found or working");
 107                else
 108                    ret = 1;
 109            }
 110
 111        }
 112    }
 113
 114    if (error != NULL)
 115        srv_log(error);
 116
 117    if (!ret)
 118        return ret;
 119
 120    /* create the queue/ subdir, just in case */
 121    xs *qdir = xs_fmt("%s/queue", srv_basedir);
 122    mkdirx(qdir);
 123
 124    xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
 125    mkdirx(ibdir);
 126
 127    xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
 128    mkdirx(tmpdir);
 129
 130    xs *faildir = xs_fmt("%s/failure", srv_basedir);
 131    mkdirx(faildir);
 132
 133#ifdef __APPLE__
 134/* Apple uses st_atimespec instead of st_atim etc */
 135#define st_atim st_atimespec
 136#define st_ctim st_ctimespec
 137#define st_mtim st_mtimespec
 138#endif
 139
 140    sbox_enter(srv_basedir);
 141
 142    /* read (and drop) emojis.json, possibly creating it */
 143    xs_free(emojis());
 144
 145    /* if style.css does not exist, create it */
 146    xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
 147
 148    if (mtime(css_fn) == 0) {
 149        srv_log(xs_fmt("Writing style.css"));
 150        write_default_css();
 151    }
 152
 153    /* create the proxy token seed */
 154    {
 155        char rnd[16];
 156        xs_rnd_buf(rnd, sizeof(rnd));
 157
 158        srv_proxy_token_seed = xs_hex_enc(rnd, sizeof(rnd));
 159    }
 160
 161    /* ensure user directories include important subdirectories */
 162    xs *users = user_list();
 163    const char *uid;
 164
 165    xs_list_foreach(users, uid) {
 166        xs *impdir = xs_fmt("%s/user/%s/import", srv_basedir, uid);
 167        xs *expdir = xs_fmt("%s/user/%s/export", srv_basedir, uid);
 168
 169        mkdirx(impdir);
 170        mkdirx(expdir);
 171    }
 172
 173    /* languages */
 174    srv_langs = xs_dict_new();
 175    srv_langs = xs_dict_set(srv_langs, "en", xs_stock(XSTYPE_NULL));
 176
 177    xs *l_dir = xs_fmt("%s/lang/", srv_basedir);
 178    mkdirx(l_dir);
 179
 180    l_dir = xs_str_cat(l_dir, "*.po");
 181    xs *pos = xs_glob(l_dir, 0, 0);
 182    const char *po;
 183
 184    xs_list_foreach(pos, po) {
 185        xs *d = xs_po_to_dict(po);
 186
 187        if (xs_is_dict(d)) {
 188            xs *l = xs_split(po, "/");
 189            xs *id = xs_dup(xs_list_get(l, -1));
 190            id = xs_replace_i(id, ".po", "");
 191
 192            srv_langs = xs_dict_set(srv_langs, id, d);
 193        }
 194    }
 195
 196    return ret;
 197}
 198
 199
 200void srv_free(void)
 201{
 202    xs_free(srv_basedir);
 203    xs_free(srv_config);
 204    xs_free(srv_baseurl);
 205
 206    pthread_mutex_destroy(&data_mutex);
 207}
 208
 209
 210void user_free(snac *snac)
 211/* frees a user snac */
 212{
 213    xs_free(snac->uid);
 214    xs_free(snac->basedir);
 215    xs_free(snac->config);
 216    xs_free(snac->config_o);
 217    xs_free(snac->key);
 218    xs_free(snac->links);
 219    xs_free(snac->actor);
 220    xs_free(snac->md5);
 221}
 222
 223
 224int user_open(snac *user, const char *uid)
 225/* opens a user */
 226{
 227    int ret = 0;
 228
 229    *user = (snac){0};
 230
 231    if (validate_uid(uid)) {
 232        xs *cfg_file = NULL;
 233        FILE *f;
 234
 235        xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
 236
 237        if (mtime(t) == 0.0) {
 238            /* user folder does not exist; try with a different case */
 239            xs *lcuid = xs_tolower_i(xs_dup(uid));
 240            xs *ulist = user_list();
 241            xs_list *p = ulist;
 242            const xs_str *v;
 243
 244            while (xs_list_iter(&p, &v)) {
 245                xs *v2 = xs_tolower_i(xs_dup(v));
 246
 247                if (strcmp(lcuid, v2) == 0) {
 248                    user->uid = xs_dup(v);
 249                    break;
 250                }
 251            }
 252        }
 253        else
 254            user->uid = xs_str_new(uid);
 255
 256        if (user->uid == NULL)
 257            return ret;
 258
 259        user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
 260
 261        cfg_file = xs_fmt("%s/user.json", user->basedir);
 262
 263        if ((f = fopen(cfg_file, "r")) != NULL) {
 264            /* read full config file */
 265            user->config = xs_json_load(f);
 266            fclose(f);
 267
 268            if (user->config != NULL) {
 269                xs *key_file = xs_fmt("%s/key.json", user->basedir);
 270
 271                if ((f = fopen(key_file, "r")) != NULL) {
 272                    user->key = xs_json_load(f);
 273                    fclose(f);
 274
 275                    if (user->key != NULL) {
 276                        user->actor     = xs_fmt("%s/%s", srv_baseurl, user->uid);
 277                        user->actor_alt = xs_fmt("%s/users/%s", srv_baseurl ,user->uid);
 278                        user->md5       = xs_md5_hex(user->actor, strlen(user->actor));
 279
 280                        /* everything is ok right now */
 281                        ret = 1;
 282
 283                        /* does it have a configuration override? */
 284                        xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
 285                        if ((f = fopen(cfg_file_o, "r")) != NULL) {
 286                            user->config_o = xs_json_load(f);
 287                            fclose(f);
 288
 289                            if (user->config_o == NULL)
 290                                srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
 291                        }
 292
 293                        if (user->config_o == NULL)
 294                            user->config_o = xs_dict_new();
 295                    }
 296                    else
 297                        srv_log(xs_fmt("error parsing '%s'", key_file));
 298                }
 299                else
 300                    srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
 301            }
 302            else
 303                srv_log(xs_fmt("error parsing '%s'", cfg_file));
 304
 305            user->tz = xs_dict_get_def(user->config, "tz", "UTC");
 306        }
 307        else
 308            srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
 309
 310        /* verified links */
 311        xs *links_file = xs_fmt("%s/links.json", user->basedir);
 312
 313        if ((f = fopen(links_file, "r")) != NULL) {
 314            user->links = xs_json_load(f);
 315            fclose(f);
 316        }
 317    }
 318    else
 319        srv_debug(2, xs_fmt("invalid user '%s'", uid));
 320
 321    if (!ret)
 322        user_free(user);
 323
 324    return ret;
 325}
 326
 327
 328xs_list *user_list(void)
 329/* returns the list of user ids */
 330{
 331    xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
 332    return xs_glob(spec, 1, 0);
 333}
 334
 335
 336int user_open_by_md5(snac *snac, const char *md5)
 337/* iterates all users searching by md5 */
 338{
 339    xs *ulist  = user_list();
 340    xs_list *p = ulist;
 341    const xs_str *v;
 342
 343    while (xs_list_iter(&p, &v)) {
 344        user_open(snac, v);
 345
 346        if (strcmp(snac->md5, md5) == 0)
 347            return 1;
 348
 349        user_free(snac);
 350    }
 351
 352    return 0;
 353}
 354
 355
 356int user_persist(snac *snac, int publish)
 357/* store user */
 358{
 359    xs *fn  = xs_fmt("%s/user.json", snac->basedir);
 360    xs *bfn = xs_fmt("%s.bak", fn);
 361    FILE *f;
 362
 363    if (publish) {
 364        /* check if any of the relevant fields have really changed */
 365        if ((f = fopen(fn, "r")) != NULL) {
 366            xs *old = xs_json_load(f);
 367            fclose(f);
 368
 369            if (old != NULL) {
 370                int nw = 0;
 371                const char *fields[] = { "header", "avatar", "name", "bio", "alias", "alias_raw",
 372                                         "metadata", "latitude", "longitude", NULL };
 373
 374                for (int n = 0; fields[n]; n++) {
 375                    const char *of = xs_dict_get(old, fields[n]);
 376                    const char *nf = xs_dict_get(snac->config, fields[n]);
 377
 378                    if (of == NULL && nf == NULL)
 379                        continue;
 380
 381                    if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) {
 382                        nw = 1;
 383                        break;
 384                    }
 385                }
 386
 387                if (!nw)
 388                    publish = 0;
 389                else {
 390                    /* uncache the actor object */
 391                    object_del(snac->actor);
 392                }
 393            }
 394        }
 395    }
 396
 397    rename(fn, bfn);
 398
 399    if ((f = fopen(fn, "w")) != NULL) {
 400        xs_json_dump(snac->config, 4, f);
 401        fclose(f);
 402    }
 403    else
 404        rename(bfn, fn);
 405
 406    history_del(snac, "timeline.html_");
 407    timeline_touch(snac);
 408
 409    if (publish) {
 410        xs *a_msg = msg_actor(snac);
 411        xs *u_msg = msg_update(snac, a_msg);
 412
 413        enqueue_message(snac, u_msg);
 414
 415        enqueue_verify_links(snac);
 416    }
 417
 418    return 0;
 419}
 420
 421
 422double mtime_nl(const char *fn, int *n_link)
 423/* returns the mtime and number of links of a file or directory, or 0.0 */
 424{
 425    struct stat st;
 426    double r = 0.0;
 427    int n = 0;
 428
 429    if (fn && stat(fn, &st) != -1) {
 430        r = (double) st.st_mtim.tv_sec;
 431        n = st.st_nlink;
 432    }
 433
 434    if (n_link)
 435        *n_link = n;
 436
 437    return r;
 438}
 439
 440
 441#define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
 442
 443double f_ctime(const char *fn)
 444/* returns the ctime of a file or directory, or 0.0 */
 445{
 446    struct stat st;
 447    double r = 0.0;
 448
 449    if (fn && stat(fn, &st) != -1) {
 450        /* return the lowest of ctime and mtime;
 451           there are operations that change the ctime, like link() */
 452        r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
 453    }
 454
 455    return r;
 456}
 457
 458
 459int is_md5_hex(const char *md5)
 460{
 461    return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
 462}
 463
 464
 465/** database 2.1+ **/
 466
 467/** indexes **/
 468
 469
 470int index_add_md5(const char *fn, const char *md5)
 471/* adds an md5 to an index */
 472{
 473    int status = HTTP_STATUS_CREATED;
 474    FILE *f;
 475
 476    if (!is_md5_hex(md5)) {
 477        srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
 478        return HTTP_STATUS_BAD_REQUEST;
 479    }
 480
 481    pthread_mutex_lock(&data_mutex);
 482
 483    if ((f = fopen(fn, "a")) != NULL) {
 484        flock(fileno(f), LOCK_EX);
 485
 486        /* ensure the position is at the end after getting the lock */
 487        fseek(f, 0, SEEK_END);
 488
 489        fprintf(f, "%s\n", md5);
 490        fclose(f);
 491    }
 492    else
 493        status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
 494
 495    pthread_mutex_unlock(&data_mutex);
 496
 497    return status;
 498}
 499
 500
 501int index_add(const char *fn, const char *id)
 502/* adds an id to an index */
 503{
 504    xs *md5 = xs_md5_hex(id, strlen(id));
 505    return index_add_md5(fn, md5);
 506}
 507
 508
 509int index_del_md5(const char *fn, const char *md5)
 510/* deletes an md5 from an index */
 511{
 512    int status = HTTP_STATUS_NOT_FOUND;
 513    FILE *f;
 514
 515    pthread_mutex_lock(&data_mutex);
 516
 517    if ((f = fopen(fn, "r+")) != NULL) {
 518        char line[256];
 519
 520        while (fgets(line, sizeof(line), f) != NULL) {
 521            line[MD5_HEX_SIZE - 1] = '\0';
 522
 523            if (strcmp(line, md5) == 0) {
 524                /* found! just rewind, overwrite it with garbage
 525                   and an eventual call to index_gc() will clean it
 526                   [yes: this breaks index_len()] */
 527                fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
 528                fwrite("-", 1, 1, f);
 529                status = HTTP_STATUS_OK;
 530
 531                break;
 532            }
 533        }
 534
 535        fclose(f);
 536    }
 537    else
 538        status = HTTP_STATUS_GONE;
 539
 540    pthread_mutex_unlock(&data_mutex);
 541
 542    return status;
 543}
 544
 545
 546int index_del(const char *fn, const char *id)
 547/* deletes an id from an index */
 548{
 549    xs *md5 = xs_md5_hex(id, strlen(id));
 550    return index_del_md5(fn, md5);
 551}
 552
 553
 554int index_gc(const char *fn)
 555/* garbage-collects an index, deleting objects that are not here */
 556{
 557    FILE *i, *o;
 558    int gc = -1;
 559
 560    pthread_mutex_lock(&data_mutex);
 561
 562    if ((i = fopen(fn, "r")) != NULL) {
 563        xs *nfn = xs_fmt("%s.new", fn);
 564        char line[256];
 565
 566        if ((o = fopen(nfn, "w")) != NULL) {
 567            gc = 0;
 568
 569            while (fgets(line, sizeof(line), i) != NULL) {
 570                line[MD5_HEX_SIZE - 1] = '\0';
 571
 572                if (line[0] != '-' && object_here_by_md5(line))
 573                    fprintf(o, "%s\n", line);
 574                else
 575                    gc++;
 576            }
 577
 578            fclose(o);
 579
 580            xs *ofn = xs_fmt("%s.bak", fn);
 581
 582            unlink(ofn);
 583            link(fn, ofn);
 584            rename(nfn, fn);
 585        }
 586
 587        fclose(i);
 588    }
 589
 590    pthread_mutex_unlock(&data_mutex);
 591
 592    return gc;
 593}
 594
 595
 596int index_in_md5(const char *fn, const char *md5)
 597/* checks if the md5 is already in the index */
 598{
 599    FILE *f;
 600    int ret = 0;
 601
 602    if ((f = fopen(fn, "r")) != NULL) {
 603        flock(fileno(f), LOCK_SH);
 604
 605        char line[256];
 606
 607        while (!ret && fgets(line, sizeof(line), f) != NULL) {
 608            line[MD5_HEX_SIZE - 1] = '\0';
 609
 610            if (strcmp(line, md5) == 0)
 611                ret = 1;
 612        }
 613
 614        fclose(f);
 615    }
 616
 617    return ret;
 618}
 619
 620
 621int index_in(const char *fn, const char *id)
 622/* checks if the object id is already in the index */
 623{
 624    xs *md5 = xs_md5_hex(id, strlen(id));
 625    return index_in_md5(fn, md5);
 626}
 627
 628
 629int index_first(const char *fn, char md5[MD5_HEX_SIZE])
 630/* reads the first entry of an index */
 631{
 632    FILE *f;
 633    int ret = 0;
 634
 635    if ((f = fopen(fn, "r")) != NULL) {
 636        if (fread(md5, MD5_HEX_SIZE, 1, f)) {
 637            md5[MD5_HEX_SIZE - 1] = '\0';
 638            ret = 1;
 639        }
 640
 641        fclose(f);
 642    }
 643
 644    return ret;
 645}
 646
 647
 648int index_len(const char *fn)
 649/* returns the number of elements in an index */
 650{
 651    struct stat st;
 652    int len = 0;
 653
 654    if (stat(fn, &st) != -1)
 655        len = st.st_size / MD5_HEX_SIZE;
 656
 657    return len;
 658}
 659
 660
 661xs_list *index_list(const char *fn, int max)
 662/* returns an index as a list */
 663{
 664    xs_list *list = xs_list_new();
 665    FILE *f;
 666    int n = 0;
 667
 668    if ((f = fopen(fn, "r")) != NULL) {
 669        flock(fileno(f), LOCK_SH);
 670
 671        char line[256];
 672
 673        while (n < max && fgets(line, sizeof(line), f) != NULL) {
 674            if (line[0] != '-') {
 675                line[MD5_HEX_SIZE - 1] = '\0';
 676                list = xs_list_append(list, line);
 677                n++;
 678            }
 679        }
 680
 681        fclose(f);
 682    }
 683
 684    return list;
 685}
 686
 687
 688int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
 689/* reads the next entry of a desc index */
 690{
 691    for (;;) {
 692        /* move backwards 2 entries */
 693        if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
 694            return 0;
 695
 696        /* read and md5 */
 697        if (!fread(md5, MD5_HEX_SIZE, 1, f))
 698            return 0;
 699
 700        if (md5[0] != '-')
 701            break;
 702    }
 703
 704    md5[MD5_HEX_SIZE - 1] = '\0';
 705
 706    return 1;
 707}
 708
 709
 710int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
 711/* reads the first entry of a desc index */
 712{
 713    /* try to position at the end and then back to the first element */
 714    if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
 715        return 0;
 716
 717    /* try to read an md5 */
 718    if (!fread(md5, MD5_HEX_SIZE, 1, f))
 719        return 0;
 720
 721    /* null-terminate */
 722    md5[MD5_HEX_SIZE - 1] = '\0';
 723
 724    /* deleted? retry next */
 725    if (md5[0] == '-')
 726        return index_desc_next(f, md5);
 727
 728    return 1;
 729}
 730
 731int index_asc_first(FILE *f,char md5[MD5_HEX_SIZE], const char *seek_md5)
 732/* reads the first entry of an ascending index, starting from a given md5 */
 733{
 734    fseek(f, SEEK_SET, 0);
 735    while (fread(md5, MD5_HEX_SIZE, 1, f)) {
 736        md5[MD5_HEX_SIZE - 1] = '\0';
 737        if (strcmp(md5,seek_md5) == 0) {
 738            return index_asc_next(f, md5);
 739        }
 740    }
 741    return 0;
 742}
 743
 744int index_asc_next(FILE *f, char md5[MD5_HEX_SIZE])
 745/* reads the next entry of an ascending index */
 746{
 747    for (;;) {
 748        /* read an md5 */
 749        if (!fread(md5, MD5_HEX_SIZE, 1, f))
 750            return 0;
 751
 752        /* deleted, skip */
 753        if (md5[0] != '-')
 754            break;
 755    }
 756
 757    md5[MD5_HEX_SIZE - 1] = '\0';
 758
 759    return 1;
 760}
 761
 762
 763xs_list *index_list_desc(const char *fn, int skip, int show)
 764/* returns an index as a list, in reverse order */
 765{
 766    xs_list *list = xs_list_new();
 767    FILE *f;
 768
 769    if ((f = fopen(fn, "r")) != NULL) {
 770        char md5[MD5_HEX_SIZE];
 771
 772        if (index_desc_first(f, md5, skip)) {
 773            int n = 1;
 774
 775            do {
 776                list = xs_list_append(list, md5);
 777            } while (n++ < show && index_desc_next(f, md5));
 778        }
 779
 780        fclose(f);
 781    }
 782
 783    return list;
 784}
 785
 786
 787/** objects **/
 788
 789static xs_str *_object_fn_by_md5(const char *md5, const char *func)
 790{
 791    xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
 792    xs_str *ret;
 793    int ok = 1;
 794
 795    /* an object deleted from an index; fail but don't bark */
 796    if (md5[0] == '-')
 797        ok = 0;
 798    else
 799    if (!is_md5_hex(md5)) {
 800        srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
 801        ok = 0;
 802    }
 803
 804    if (ok) {
 805        mkdirx(bfn);
 806        ret = xs_fmt("%s/%s.json", bfn, md5);
 807    }
 808    else
 809        ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
 810
 811    return ret;
 812}
 813
 814
 815static xs_str *_object_fn(const char *id)
 816{
 817    xs *md5 = xs_md5_hex(id, strlen(id));
 818    return _object_fn_by_md5(md5, "_object_fn");
 819}
 820
 821
 822int object_here_by_md5(const char *id)
 823/* checks if an object is already downloaded */
 824{
 825    xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
 826    return mtime(fn) > 0.0;
 827}
 828
 829
 830int object_here(const char *id)
 831/* checks if an object is already downloaded */
 832{
 833    xs *fn = _object_fn(id);
 834    return mtime(fn) > 0.0;
 835}
 836
 837
 838int object_get_by_md5(const char *md5, xs_dict **obj)
 839/* returns a stored object, optionally of the requested type */
 840{
 841    int status = HTTP_STATUS_NOT_FOUND;
 842    xs *fn     = _object_fn_by_md5(md5, "object_get_by_md5");
 843    FILE *f;
 844
 845    if ((f = fopen(fn, "r")) != NULL) {
 846        *obj = xs_json_load(f);
 847        fclose(f);
 848
 849        if (*obj)
 850            status = HTTP_STATUS_OK;
 851    }
 852    else
 853        *obj = NULL;
 854
 855    return status;
 856}
 857
 858
 859int object_get(const char *id, xs_dict **obj)
 860/* returns a stored object, optionally of the requested type */
 861{
 862    xs *md5 = xs_md5_hex(id, strlen(id));
 863    return object_get_by_md5(md5, obj);
 864}
 865
 866
 867int _object_add(const char *id, const xs_dict *obj, int ow)
 868/* stores an object */
 869{
 870    int status = HTTP_STATUS_CREATED; /* Created */
 871    xs *fn     = _object_fn(id);
 872    FILE *f;
 873
 874    if (mtime(fn) > 0.0) {
 875        if (!ow) {
 876            /* object already here */
 877            srv_debug(1, xs_fmt("object_add object already here %s", id));
 878            return HTTP_STATUS_NO_CONTENT;
 879        }
 880        else
 881            status = HTTP_STATUS_OK;
 882    }
 883
 884    if ((f = fopen(fn, "w")) != NULL) {
 885        flock(fileno(f), LOCK_EX);
 886
 887        xs_json_dump(obj, 4, f);
 888        fclose(f);
 889
 890        /* does this object has a parent? */
 891        const char *in_reply_to = get_in_reply_to(obj);
 892
 893        if (!xs_is_null(in_reply_to) && *in_reply_to) {
 894            /* update the children index of the parent */
 895            xs *c_idx = _object_fn(in_reply_to);
 896
 897            c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
 898
 899            if (!index_in(c_idx, id)) {
 900                index_add(c_idx, id);
 901                srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
 902            }
 903            else
 904                srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
 905
 906            /* create a one-element index with the parent */
 907            xs *p_idx = xs_replace(fn, ".json", "_p.idx");
 908
 909            if (mtime(p_idx) == 0.0) {
 910                index_add(p_idx, in_reply_to);
 911                srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
 912            }
 913        }
 914    }
 915    else {
 916        srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
 917        status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
 918    }
 919
 920    srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
 921
 922    return status;
 923}
 924
 925
 926int object_add(const char *id, const xs_dict *obj)
 927/* stores an object */
 928{
 929    return _object_add(id, obj, 0);
 930}
 931
 932
 933int object_add_ow(const char *id, const xs_dict *obj)
 934/* stores an object (overwriting allowed) */
 935{
 936    return _object_add(id, obj, 1);
 937}
 938
 939
 940int object_del_by_md5(const char *md5)
 941/* deletes an object by its md5 */
 942{
 943    int status = HTTP_STATUS_NOT_FOUND;
 944    xs *fn     = _object_fn_by_md5(md5, "object_del_by_md5");
 945
 946    if (unlink(fn) != -1) {
 947        status = HTTP_STATUS_OK;
 948
 949        /* also delete associated indexes */
 950        xs *spec  = xs_dup(fn);
 951        spec      = xs_replace_i(spec, ".json", "*.idx");
 952        xs *files = xs_glob(spec, 0, 0);
 953        char *p;
 954        const char *v;
 955
 956        p = files;
 957        while (xs_list_iter(&p, &v)) {
 958            srv_debug(1, xs_fmt("object_del index %s", v));
 959            unlink(v);
 960        }
 961    }
 962
 963    srv_debug(1, xs_fmt("object_del %s %d", fn, status));
 964
 965    return status;
 966}
 967
 968
 969int object_del(const char *id)
 970/* deletes an object */
 971{
 972    xs *md5 = xs_md5_hex(id, strlen(id));
 973    return object_del_by_md5(md5);
 974}
 975
 976
 977int object_del_if_unref(const char *id)
 978/* deletes an object if its n_links < 2 */
 979{
 980    xs *fn = _object_fn(id);
 981    int n_links;
 982    int ret = 0;
 983
 984    if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
 985        ret = object_del(id);
 986
 987    return ret;
 988}
 989
 990
 991double object_ctime_by_md5(const char *md5)
 992{
 993    xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
 994    return f_ctime(fn);
 995}
 996
 997
 998double object_ctime(const char *id)
 999{
1000    xs *md5 = xs_md5_hex(id, strlen(id));
1001    return object_ctime_by_md5(md5);
1002}
1003
1004
1005double object_mtime_by_md5(const char *md5)
1006{
1007    xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
1008    return mtime(fn);
1009}
1010
1011
1012double object_mtime(const char *id)
1013{
1014    xs *md5 = xs_md5_hex(id, strlen(id));
1015    return object_mtime_by_md5(md5);
1016}
1017
1018
1019void object_touch(const char *id)
1020{
1021    xs *md5 = xs_md5_hex(id, strlen(id));
1022    xs *fn = _object_fn_by_md5(md5, "object_touch");
1023
1024    if (mtime(fn))
1025        utimes(fn, NULL);
1026}
1027
1028
1029xs_str *_object_index_fn(const char *id, const char *idxsfx)
1030/* returns the filename of an object's index */
1031{
1032    xs_str *fn = _object_fn(id);
1033    return xs_replace_i(fn, ".json", idxsfx);
1034}
1035
1036
1037int object_likes_len(const char *id)
1038/* returns the number of likes (without reading the index) */
1039{
1040    xs *fn = _object_index_fn(id, "_l.idx");
1041    return index_len(fn);
1042}
1043
1044
1045int object_announces_len(const char *id)
1046/* returns the number of announces (without reading the index) */
1047{
1048    xs *fn = _object_index_fn(id, "_a.idx");
1049    return index_len(fn);
1050}
1051
1052
1053xs_list *object_children(const char *id)
1054/* returns the list of an object's children */
1055{
1056    xs *fn = _object_index_fn(id, "_c.idx");
1057    return index_list(fn, XS_ALL);
1058}
1059
1060
1061xs_list *object_get_emoji_reacts(const char *id)
1062/* returns the list of an object's emoji reactions */
1063{
1064    xs *fn = _object_index_fn(id, "_e.idx");
1065    return index_list(fn, XS_ALL);
1066}
1067
1068
1069xs_list *object_likes(const char *id)
1070{
1071    xs *fn = _object_index_fn(id, "_l.idx");
1072    return index_list(fn, XS_ALL);
1073}
1074
1075
1076xs_list *object_announces(const char *id)
1077{
1078    xs *fn = _object_index_fn(id, "_a.idx");
1079    return index_list(fn, XS_ALL);
1080}
1081
1082
1083int object_parent(const char *md5, char parent[MD5_HEX_SIZE])
1084/* returns the object parent, if any */
1085{
1086    xs *fn = _object_fn_by_md5(md5, "object_parent");
1087
1088    fn = xs_replace_i(fn, ".json", "_p.idx");
1089    return index_first(fn, parent);
1090}
1091
1092
1093int object_admire(const char *id, const char *actor, int like)
1094/* actor likes or announces this object */
1095{
1096    int status = HTTP_STATUS_OK;
1097    xs *fn     = _object_fn(id);
1098
1099    fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
1100
1101    if (!index_in(fn, actor)) {
1102        status = index_add(fn, actor);
1103
1104        srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
1105    }
1106
1107    return status;
1108}
1109
1110
1111int object_unadmire(const char *id, const char *actor, int like)
1112/* actor retrives their likes, announces or emojis this object */
1113{
1114    switch (like) {
1115    case 0:
1116        like = 'a';
1117        break;
1118    case 1:
1119        like = 'l';
1120        break;
1121    case 2:
1122        like = 'e';
1123        break;
1124    }
1125    int status;
1126    xs *fn = _object_fn(id);
1127
1128    char sfx[7] = "_x.idx";
1129    sfx[1] = like;
1130
1131    fn = xs_replace_i(fn, ".json", sfx);
1132
1133    status = index_del(fn, actor);
1134
1135    if (valid_status(status))
1136        index_gc(fn);
1137
1138    srv_debug(0,
1139        xs_fmt("object_unadmire (%s) %s %s %d", like >= 'e' ?
1140            (like == 'l' ? "Like" : "EmojiReact") : "Announce" , actor, fn, status));
1141
1142    return status;
1143}
1144
1145int object_emoji_react(const char *mid, const char *eid)
1146/* actor reacts w/ an emoji */
1147{
1148    int status = HTTP_STATUS_OK;
1149    xs *fn     = _object_fn(mid);
1150
1151    fn = xs_replace_i(fn, ".json", "_e.idx");
1152
1153    if (!index_in(fn, eid)) {
1154        status = index_add(fn, eid);
1155
1156        srv_debug(1, xs_fmt("object_emoji_react (%s) added %s to %s", "EmojiReact", eid, fn));
1157    }
1158
1159    return status;
1160}
1161
1162
1163int object_rm_emoji_react(const char *mid, const char *eid)
1164/* actor retrives their emoji reaction */
1165{
1166    int status;
1167    xs *fn = _object_fn(mid);
1168
1169    fn = xs_replace_i(fn, ".json", "_e.idx");
1170
1171    status = index_del(fn, eid);
1172    object_del(eid);
1173
1174    if (valid_status(status))
1175        index_gc(fn);
1176
1177    srv_debug(0,
1178        xs_fmt("object_unadmire (EmojiReact) %s %s %d", eid, fn, status));
1179
1180    return status;
1181}
1182
1183
1184xs_str *object_user_cache_fn_by_md5(snac *user, const char *md5, const char *cachedir)
1185{
1186    return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
1187}
1188
1189
1190xs_str *object_user_cache_fn(snac *user, const char *id, const char *cachedir)
1191{
1192    xs *md5 = xs_md5_hex(id, strlen(id));
1193    return object_user_cache_fn_by_md5(user, md5, cachedir);
1194}
1195
1196
1197xs_str *object_user_cache_index_fn(snac *user, const char *cachedir)
1198{
1199    return xs_fmt("%s/%s.idx", user->basedir, cachedir);
1200}
1201
1202
1203int _object_user_cache(snac *user, const char *id, const char *cachedir, int del)
1204/* adds or deletes from a user cache */
1205{
1206    xs *ofn = _object_fn(id);
1207    xs *cfn = object_user_cache_fn(user, id, cachedir);
1208    xs *idx = object_user_cache_index_fn(user, cachedir);
1209    int ret;
1210
1211    if (del) {
1212        ret = unlink(cfn);
1213        index_del(idx, id);
1214    }
1215    else {
1216        /* create the subfolder, if it does not exist */
1217        xs *dir = xs_fmt("%s/%s/", user->basedir, cachedir);
1218        mkdirx(dir);
1219
1220        if ((ret = link(ofn, cfn)) != -1)
1221            index_add(idx, id);
1222    }
1223
1224    return ret;
1225}
1226
1227
1228int object_user_cache_add(snac *user, const char *id, const char *cachedir)
1229/* caches an object into a user cache */
1230{
1231    return _object_user_cache(user, id, cachedir, 0);
1232}
1233
1234
1235int object_user_cache_del(snac *user, const char *id, const char *cachedir)
1236/* deletes an object from a user cache */
1237{
1238    return _object_user_cache(user, id, cachedir, 1);
1239}
1240
1241
1242int object_user_cache_in(snac *user, const char *id, const char *cachedir)
1243/* checks if an object is stored in a cache */
1244{
1245    xs *cfn = object_user_cache_fn(user, id, cachedir);
1246    return !!(mtime(cfn) != 0.0);
1247}
1248
1249
1250int object_user_cache_in_by_md5(snac *user, const char *md5, const char *cachedir)
1251/* checks if an object is stored in a cache */
1252{
1253    xs *cfn = object_user_cache_fn_by_md5(user, md5, cachedir);
1254    return !!(mtime(cfn) != 0.0);
1255}
1256
1257
1258xs_list *object_user_cache_list(snac *user, const char *cachedir, int max, int inv)
1259/* returns the objects in a cache as a list */
1260{
1261    xs *idx = xs_fmt("%s/%s.idx", user->basedir, cachedir);
1262    return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
1263}
1264
1265
1266/** specialized functions **/
1267
1268/** followers **/
1269
1270int follower_add(snac *snac, const char *actor)
1271/* adds a follower */
1272{
1273    int ret = object_user_cache_add(snac, actor, "followers");
1274
1275    snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
1276
1277    return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
1278}
1279
1280
1281int follower_del(snac *snac, const char *actor)
1282/* deletes a follower */
1283{
1284    int ret = object_user_cache_del(snac, actor, "followers");
1285
1286    snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
1287
1288    return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
1289}
1290
1291
1292int follower_check(snac *snac, const char *actor)
1293/* checks if someone is a follower */
1294{
1295    return object_user_cache_in(snac, actor, "followers");
1296}
1297
1298
1299int follower_list_len(snac *snac)
1300/* returns the number of followers */
1301{
1302    xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
1303    return xs_list_len(list);
1304}
1305
1306
1307xs_list *follower_list(snac *snac)
1308/* returns the list of followers */
1309{
1310    xs *list       = object_user_cache_list(snac, "followers", XS_ALL, 0);
1311    xs_list *fwers = xs_list_new();
1312    char *p;
1313    const char *v;
1314
1315    /* resolve the list of md5 to be a list of actors */
1316    p = list;
1317    while (xs_list_iter(&p, &v)) {
1318        xs *a_obj = NULL;
1319
1320        if (valid_status(object_get_by_md5(v, &a_obj))) {
1321            const char *actor = xs_dict_get(a_obj, "id");
1322
1323            if (!xs_is_null(actor)) {
1324                /* check if the actor is still cached */
1325                xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
1326
1327                if (mtime(fn) > 0.0)
1328                    fwers = xs_list_append(fwers, actor);
1329            }
1330        }
1331    }
1332
1333    return fwers;
1334}
1335
1336
1337/** pending followers **/
1338
1339int pending_add(snac *user, const char *actor, const xs_dict *msg)
1340/* stores the follow message for later confirmation */
1341{
1342    xs *dir = xs_fmt("%s/pending", user->basedir);
1343    xs *md5 = xs_md5_hex(actor, strlen(actor));
1344    xs *fn  = xs_fmt("%s/%s.json", dir, md5);
1345    FILE *f;
1346
1347    mkdirx(dir);
1348
1349    if ((f = fopen(fn, "w")) == NULL)
1350        return -1;
1351
1352    xs_json_dump(msg, 4, f);
1353    fclose(f);
1354
1355    return 0;
1356}
1357
1358
1359int pending_check(snac *user, const char *actor)
1360/* checks if there is a pending follow confirmation for the actor */
1361{
1362    xs *md5 = xs_md5_hex(actor, strlen(actor));
1363    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1364
1365    return mtime(fn) != 0;
1366}
1367
1368
1369xs_dict *pending_get(snac *user, const char *actor)
1370/* returns the pending follow confirmation for the actor */
1371{
1372    xs *md5 = xs_md5_hex(actor, strlen(actor));
1373    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1374    xs_dict *msg = NULL;
1375    FILE *f;
1376
1377    if ((f = fopen(fn, "r")) != NULL) {
1378        msg = xs_json_load(f);
1379        fclose(f);
1380    }
1381
1382    return msg;
1383}
1384
1385
1386void pending_del(snac *user, const char *actor)
1387/* deletes a pending follow confirmation for the actor */
1388{
1389    xs *md5 = xs_md5_hex(actor, strlen(actor));
1390    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1391
1392    unlink(fn);
1393}
1394
1395
1396xs_list *pending_list(snac *user)
1397/* returns a list of pending follow confirmations */
1398{
1399    xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
1400    xs *l = xs_glob(spec, 0, 0);
1401    xs_list *r = xs_list_new();
1402    const char *v;
1403
1404    xs_list_foreach(l, v) {
1405        FILE *f;
1406        xs *msg = NULL;
1407
1408        if ((f = fopen(v, "r")) == NULL)
1409            continue;
1410
1411        msg = xs_json_load(f);
1412        fclose(f);
1413
1414        if (msg == NULL)
1415            continue;
1416
1417        const char *actor = xs_dict_get(msg, "actor");
1418
1419        if (xs_type(actor) == XSTYPE_STRING)
1420            r = xs_list_append(r, actor);
1421    }
1422
1423    return r;
1424}
1425
1426
1427int pending_count(snac *user)
1428/* returns the number of pending follow confirmations */
1429{
1430    xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
1431    xs *l = xs_glob(spec, 0, 0);
1432
1433    return xs_list_len(l);
1434}
1435
1436
1437int is_msg_mine(snac *user, const char *id)
1438/* returns true if a post id is by the given user */
1439{
1440    int ret = 0;
1441
1442    if (xs_is_string(id)) {
1443        xs *s1 = xs_fmt("%s/", user->actor);
1444        ret = xs_startswith(id, s1);
1445    }
1446
1447    return ret;
1448}
1449
1450
1451/** timeline **/
1452
1453double timeline_mtime(snac *snac)
1454{
1455    xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1456    return mtime(fn);
1457}
1458
1459
1460int timeline_touch(snac *snac)
1461/* changes the date of the timeline index */
1462{
1463    xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1464    return utimes(fn, NULL);
1465}
1466
1467
1468xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
1469/* get the filename of an entry by md5 from any timeline */
1470{
1471    xs_str *fn = NULL;
1472
1473    if (is_md5_hex(md5)) {
1474        fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
1475
1476        if (mtime(fn) == 0.0) {
1477            fn = xs_free(fn);
1478            fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
1479
1480            if (mtime(fn) == 0.0)
1481                fn = xs_free(fn);
1482        }
1483    }
1484
1485    return fn;
1486}
1487
1488
1489int timeline_here_by_md5(snac *snac, const char *md5)
1490/* checks if an object is in the user cache */
1491{
1492    xs *fn = timeline_fn_by_md5(snac, md5);
1493
1494    return !(fn == NULL);
1495}
1496
1497
1498int timeline_here(snac *user, const char *id)
1499{
1500    xs *md5 = xs_md5_hex(id, strlen(id));
1501
1502    return timeline_here_by_md5(user, md5);
1503}
1504
1505
1506int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1507/* gets a message from the timeline */
1508{
1509    int status = HTTP_STATUS_NOT_FOUND;
1510    FILE *f    = NULL;
1511
1512    xs *fn = timeline_fn_by_md5(snac, md5);
1513
1514    if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
1515        *msg = xs_json_load(f);
1516        fclose(f);
1517
1518        if (*msg != NULL)
1519            status = HTTP_STATUS_OK;
1520    }
1521
1522    return status;
1523}
1524
1525
1526int timeline_del(snac *snac, const char *id)
1527/* deletes a message from the timeline */
1528{
1529    /* delete from the user's caches */
1530    object_user_cache_del(snac, id, "public");
1531    object_user_cache_del(snac, id, "private");
1532
1533    unpin(snac, id);
1534    unbookmark(snac, id);
1535
1536    /* try to delete the object if it's not used elsewhere */
1537    return object_del_if_unref(id);
1538}
1539
1540
1541void timeline_update_indexes(snac *snac, const char *id)
1542/* updates the indexes */
1543{
1544    object_user_cache_add(snac, id, "private");
1545
1546    if (is_msg_mine(snac, id)) {
1547        xs *msg = NULL;
1548
1549        if (valid_status(object_get(id, &msg))) {
1550            const int scope = get_msg_visibility(msg);
1551            /* if its ours and is public, also store in public */
1552            if (scope == SCOPE_PUBLIC) {
1553                if (object_user_cache_add(snac, id, "public") >= 0) {
1554                    /* also add it to the instance public timeline */
1555                    xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
1556                    index_add(ipt, id);
1557                }
1558                else
1559                    srv_debug(1, xs_fmt("Not added to public instance index %s, visibility %d", id, scope));
1560            }
1561            else
1562                /* also add it to public, it will be discarded later */
1563                object_user_cache_add(snac, id, "public");
1564        }
1565    }
1566}
1567
1568
1569int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
1570/* adds a message to the timeline */
1571{
1572    int ret = object_add(id, o_msg);
1573    timeline_update_indexes(snac, id);
1574
1575    tag_index(id, o_msg);
1576
1577    list_distribute(snac, NULL, o_msg);
1578
1579    snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
1580
1581    return ret;
1582}
1583
1584
1585int timeline_emoji_react(const char *act, const char *id, const xs_dict *msg_o)
1586/* adds an emoji reaction to a message */
1587{
1588    xs *msg = xs_dup(msg_o);
1589    msg = xs_dict_append(msg, "attributedTo", act);
1590    msg = xs_dict_set(msg, "type", "EmojiReact");
1591    const char *emote_id = xs_dict_get(msg, "id");
1592
1593    int ret = object_add(emote_id, msg);
1594    if (ret == HTTP_STATUS_OK || ret == HTTP_STATUS_CREATED)
1595        ret = object_emoji_react(id, emote_id);
1596
1597    return ret;
1598}
1599
1600
1601int timeline_admire(snac *snac, const char *id,
1602                    const char *admirer, int like, const xs_dict *msg)
1603/* updates a timeline entry with a new admiration or emoji reaction */
1604{
1605    int ret;
1606    const char *content = xs_dict_get_path(msg, "content");
1607    const char *type = xs_dict_get_path(msg, "type");
1608
1609    /* if we are admiring this, add to both timelines */
1610    if (!like && strcmp(admirer, snac->actor) == 0) {
1611        object_user_cache_add(snac, id, "public");
1612        object_user_cache_add(snac, id, "private");
1613    }
1614
1615    /* use utf <3 as a like, as it is ugly */
1616    if (type && xs_match(type, "Like|EmojiReact|Emoji") &&
1617            content && strcmp(content, "❤") != 0) {
1618        ret = timeline_emoji_react(snac->actor, id, msg);
1619        snac_debug(snac, 1, xs_fmt("timeline_emoji_react %s", id));
1620    }
1621
1622    else {
1623        ret = object_admire(id, admirer, like);
1624        snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
1625                like ? "Like" : "Announce", id, admirer));
1626    }
1627
1628    return ret;
1629}
1630
1631
1632xs_list *timeline_top_level(snac *snac, const xs_list *list)
1633/* returns the top level md5 entries from this index */
1634{
1635    xs_set seen;
1636    const xs_str *v;
1637
1638    xs_set_init(&seen);
1639
1640    int c = 0;
1641    while (xs_list_next(list, &v, &c)) {
1642        char line[MD5_HEX_SIZE] = "";
1643
1644        strncpy(line, v, sizeof(line));
1645
1646        for (;;) {
1647            char line2[MD5_HEX_SIZE];
1648
1649            /* if it doesn't have a parent, use this */
1650            if (!object_parent(line, line2))
1651                break;
1652
1653            /* well, there is a parent... but is it here? */
1654            if (!timeline_here_by_md5(snac, line2))
1655                break;
1656
1657            /* it's here! try again with its own parent */
1658            strncpy(line, line2, sizeof(line));
1659        }
1660
1661        xs_set_add(&seen, line);
1662    }
1663
1664    return xs_set_result(&seen);
1665}
1666
1667
1668xs_str *user_index_fn(snac *user, const char *idx_name)
1669/* returns the filename of a user index */
1670{
1671    return xs_fmt("%s/%s.idx", user->basedir, idx_name);
1672}
1673
1674
1675xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show, int *more)
1676/* returns a timeline (with all entries) */
1677{
1678    xs *idx = user_index_fn(user, idx_name);
1679
1680    /* if a more flag is sent, request one more */
1681    xs_list *lst = index_list_desc(idx, skip, show + (more != NULL ? 1 : 0));
1682
1683    if (more != NULL) {
1684        if (xs_list_len(lst) > show) {
1685            *more = 1;
1686            lst = xs_list_del(lst, -1);
1687        }
1688        else
1689            *more = 0;
1690    }
1691
1692    return lst;
1693}
1694
1695
1696xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show, int *more)
1697/* returns a timeline (only top level entries) */
1698{
1699    int c_max;
1700
1701    /* maximum number of items in the timeline */
1702    c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
1703
1704    /* never more timeline entries than the configured maximum */
1705    if (show > c_max)
1706        show = c_max;
1707
1708    xs *list = timeline_simple_list(snac, idx_name, skip, show, more);
1709
1710    return timeline_top_level(snac, list);
1711}
1712
1713
1714void timeline_add_mark(snac *user)
1715/* adds an "already seen" mark to the private timeline */
1716{
1717    xs *fn = xs_fmt("%s/private.idx", user->basedir);
1718    char last_entry[MD5_HEX_SIZE] = "";
1719    FILE *f;
1720
1721    /* get the last entry in the index */
1722    if ((f = fopen(fn, "r")) != NULL) {
1723        index_desc_first(f, last_entry, 0);
1724        fclose(f);
1725    }
1726
1727    /* is the last entry *not* a mark? */
1728    if (strcmp(last_entry, MD5_ALREADY_SEEN_MARK) != 0) {
1729        /* add it */
1730        index_add_md5(fn, MD5_ALREADY_SEEN_MARK);
1731    }
1732}
1733
1734
1735xs_str *instance_index_fn(void)
1736{
1737    return xs_fmt("%s/public.idx", srv_basedir);
1738}
1739
1740
1741xs_list *timeline_instance_list(int skip, int show)
1742/* returns the timeline for the full instance */
1743{
1744    xs *idx = instance_index_fn();
1745    xs *lst = index_list_desc(idx, skip, show);
1746
1747    /* make the list unique */
1748    xs_set rep;
1749    xs_set_init(&rep);
1750    const char *md5;
1751
1752    xs_list_foreach(lst, md5)
1753        xs_set_add(&rep, md5);
1754
1755    return xs_set_result(&rep);
1756}
1757
1758
1759/** following **/
1760
1761/* this needs special treatment and cannot use the object db as is,
1762   with a link to a cached author, because we need the Follow object
1763   in case we need to unfollow (Undo + original Follow) */
1764
1765xs_str *_following_fn(snac *snac, const char *actor)
1766{
1767    xs *md5 = xs_md5_hex(actor, strlen(actor));
1768    return xs_fmt("%s/following/%s.json", snac->basedir, md5);
1769}
1770
1771
1772int following_add(snac *snac, const char *actor, const xs_dict *msg)
1773/* adds to the following list */
1774{
1775    int ret = HTTP_STATUS_CREATED;
1776    xs *fn = _following_fn(snac, actor);
1777    FILE *f;
1778    xs *p_object = NULL;
1779
1780    if (valid_status(following_get(snac, actor, &p_object))) {
1781        /* object already exists; if it's of type Accept,
1782           the actor is already being followed and confirmed,
1783           so do nothing */
1784        const char *type = xs_dict_get(p_object, "type");
1785
1786        if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1787            snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
1788            return HTTP_STATUS_OK;
1789        }
1790    }
1791
1792    if ((f = fopen(fn, "w")) != NULL) {
1793        xs_json_dump(msg, 4, f);
1794        fclose(f);
1795
1796        /* get the filename of the actor object */
1797        xs *actor_fn = _object_fn(actor);
1798
1799        /* increase its reference count */
1800        fn = xs_replace_i(fn, ".json", "_a.json");
1801        link(actor_fn, fn);
1802    }
1803    else
1804        ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
1805
1806    snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
1807
1808    return ret;
1809}
1810
1811
1812int following_del(snac *snac, const char *actor)
1813/* we're not following this actor any longer */
1814{
1815    xs *fn = _following_fn(snac, actor);
1816
1817    snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
1818
1819    unlink(fn);
1820
1821    /* also delete the reference to the author */
1822    fn = xs_replace_i(fn, ".json", "_a.json");
1823    unlink(fn);
1824
1825    return HTTP_STATUS_OK;
1826}
1827
1828
1829int following_check(snac *snac, const char *actor)
1830/* checks if we are following this actor */
1831{
1832    xs *fn = _following_fn(snac, actor);
1833
1834    return !!(mtime(fn) != 0.0);
1835}
1836
1837
1838int following_get(snac *snac, const char *actor, xs_dict **data)
1839/* returns the 'Follow' object */
1840{
1841    xs *fn = _following_fn(snac, actor);
1842    FILE *f;
1843    int status = HTTP_STATUS_OK;
1844
1845    if ((f = fopen(fn, "r")) != NULL) {
1846        *data = xs_json_load(f);
1847        fclose(f);
1848    }
1849    else
1850        status = HTTP_STATUS_NOT_FOUND;
1851
1852    return status;
1853}
1854
1855
1856int following_list_len(snac *snac)
1857/* returns number of people being followed */
1858{
1859    xs *spec = xs_fmt("%s/following/" "*_a.json", snac->basedir);
1860    xs *glist = xs_glob(spec, 0, 0);
1861    return xs_list_len(glist);
1862}
1863
1864
1865xs_list *following_list(snac *snac)
1866/* returns the list of people being followed */
1867{
1868    xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
1869    xs *glist = xs_glob(spec, 0, 0);
1870    xs_list *p;
1871    const xs_str *v;
1872    xs_list *list = xs_list_new();
1873
1874    /* iterate the list of files */
1875    p = glist;
1876    while (xs_list_iter(&p, &v)) {
1877        FILE *f;
1878
1879        /* load the follower data */
1880        if ((f = fopen(v, "r")) != NULL) {
1881            xs *o = xs_json_load(f);
1882            fclose(f);
1883
1884            if (o != NULL) {
1885                const char *type = xs_dict_get(o, "type");
1886
1887                if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1888                    const char *actor = xs_dict_get(o, "actor");
1889
1890                    if (!xs_is_null(actor)) {
1891                        list = xs_list_append(list, actor);
1892
1893                        /* check if there is a link to the actor object */
1894                        xs *v2 = xs_replace(v, ".json", "_a.json");
1895
1896                        if (mtime(v2) == 0.0) {
1897                            /* no; add a link to it */
1898                            xs *actor_fn = _object_fn(actor);
1899                            link(actor_fn, v2);
1900                        }
1901                    }
1902                }
1903            }
1904        }
1905    }
1906
1907    return list;
1908}
1909
1910
1911xs_str *_muted_fn(snac *snac, const char *actor)
1912{
1913    xs *md5 = xs_md5_hex(actor, strlen(actor));
1914    return xs_fmt("%s/muted/%s", snac->basedir, md5);
1915}
1916
1917
1918void mute(snac *snac, const char *actor)
1919/* mutes a moron */
1920{
1921    xs *fn = _muted_fn(snac, actor);
1922    FILE *f;
1923
1924    if ((f = fopen(fn, "w")) != NULL) {
1925        fprintf(f, "%s\n", actor);
1926        fclose(f);
1927
1928        snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
1929    }
1930}
1931
1932
1933void unmute(snac *snac, const char *actor)
1934/* actor is no longer a moron */
1935{
1936    xs *fn = _muted_fn(snac, actor);
1937
1938    unlink(fn);
1939
1940    snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
1941}
1942
1943
1944int is_muted(snac *snac, const char *actor)
1945/* check if someone is muted */
1946{
1947    xs *fn = _muted_fn(snac, actor);
1948
1949    return !!(mtime(fn) != 0.0);
1950}
1951
1952
1953xs_list *muted_list(snac *user)
1954/* returns the list (actor URLs) of the muted morons */
1955{
1956    xs_list *l = xs_list_new();
1957    xs *spec = xs_fmt("%s/muted/" "*", user->basedir);
1958    xs *files = xs_glob(spec, 0, 0);
1959    const char *fn;
1960
1961    xs_list_foreach(files, fn) {
1962        FILE *f;
1963
1964        if ((f = fopen(fn, "r")) != NULL) {
1965            xs *actor = xs_strip_i(xs_readline(f));
1966            fclose(f);
1967
1968            l = xs_list_append(l, actor);
1969        }
1970    }
1971
1972    return l;
1973}
1974
1975/** emojis react **/
1976
1977xs_str *emoji_reacted(snac *user, const char *id)
1978/* returns the emoji an user reacted to a message */
1979{
1980    xs *emojis = object_get_emoji_reacts(id);
1981    int c = 0;
1982    const char *v;
1983
1984    while (xs_list_next(emojis, &v, &c)) {
1985        xs *msg = NULL;
1986        if (object_get_by_md5(v, &msg)) {
1987            const xs_val *act = xs_dict_get(msg, "actor");
1988            if (act && strcmp(act, user->actor) == 0)
1989                return xs_dup(xs_dict_get(msg, "content"));
1990        }
1991    }
1992    return NULL;
1993}
1994
1995/** bookmarking **/
1996
1997int is_bookmarked(snac *user, const char *id)
1998/* returns true if this note is bookmarked */
1999{
2000    return object_user_cache_in(user, id, "bookmark");
2001}
2002
2003
2004int bookmark(snac *user, const char *id)
2005/* bookmarks a post */
2006{
2007    if (is_bookmarked(user, id))
2008        return -3;
2009
2010    return object_user_cache_add(user, id, "bookmark");
2011}
2012
2013
2014int unbookmark(snac *user, const char *id)
2015/* unbookmarks a post */
2016{
2017    return object_user_cache_del(user, id, "bookmark");
2018}
2019
2020
2021xs_list *bookmark_list(snac *user)
2022/* return the lists of bookmarked posts */
2023{
2024    return object_user_cache_list(user, "bookmark", XS_ALL, 1);
2025}
2026
2027
2028xs_str *bookmark_index_fn(snac *user)
2029{
2030    return object_user_cache_index_fn(user, "bookmark");
2031}
2032
2033
2034/** pinning **/
2035
2036int is_pinned(snac *user, const char *id)
2037/* returns true if this note is pinned */
2038{
2039    return object_user_cache_in(user, id, "pinned");
2040}
2041
2042
2043int is_pinned_by_md5(snac *user, const char *md5)
2044{
2045    return object_user_cache_in_by_md5(user, md5, "pinned");
2046}
2047
2048
2049int pin(snac *user, const char *id)
2050/* pins a message */
2051{
2052    int ret = -2;
2053
2054    if (is_msg_mine(user, id)) {
2055        if (is_pinned(user, id))
2056            ret = -3;
2057        else
2058            ret = object_user_cache_add(user, id, "pinned");
2059    }
2060
2061    return ret;
2062}
2063
2064
2065int unpin(snac *user, const char *id)
2066/* unpin a message */
2067{
2068    return object_user_cache_del(user, id, "pinned");
2069}
2070
2071
2072xs_list *pinned_list(snac *user)
2073/* return the lists of pinned posts */
2074{
2075    return object_user_cache_list(user, "pinned", XS_ALL, 1);
2076}
2077
2078
2079/** drafts **/
2080
2081int is_draft(snac *user, const char *id)
2082/* returns true if this note is a draft */
2083{
2084    return object_user_cache_in(user, id, "draft");
2085}
2086
2087
2088void draft_del(snac *user, const char *id)
2089/* delete a message from the draft cache */
2090{
2091    object_user_cache_del(user, id, "draft");
2092}
2093
2094
2095void draft_add(snac *user, const char *id, const xs_dict *msg)
2096/* store the message as a draft */
2097{
2098    /* delete from the index, in case it was already there */
2099    draft_del(user, id);
2100
2101    /* overwrite object */
2102    object_add_ow(id, msg);
2103
2104    /* [re]add to the index */
2105    object_user_cache_add(user, id, "draft");
2106}
2107
2108
2109xs_list *draft_list(snac *user)
2110/* return the lists of drafts */
2111{
2112    return object_user_cache_list(user, "draft", XS_ALL, 1);
2113}
2114
2115
2116/** scheduled posts **/
2117
2118int is_scheduled(snac *user, const char *id)
2119/* returns true if this note is scheduled for future sending */
2120{
2121    return object_user_cache_in(user, id, "sched");
2122}
2123
2124
2125void schedule_del(snac *user, const char *id)
2126/* deletes an scheduled post */
2127{
2128    object_user_cache_del(user, id, "sched");
2129}
2130
2131
2132void schedule_add(snac *user, const char *id, const xs_dict *msg)
2133/* schedules this post for later */
2134{
2135    /* delete from the index, in case it was already there */
2136    schedule_del(user, id);
2137
2138    /* overwrite object */
2139    object_add_ow(id, msg);
2140
2141    /* [re]add to the index */
2142    object_user_cache_add(user, id, "sched");
2143}
2144
2145
2146xs_list *scheduled_list(snac *user)
2147/* return the list of scheduled posts */
2148{
2149    return object_user_cache_list(user, "sched", XS_ALL, 1);
2150}
2151
2152
2153void scheduled_process(snac *user)
2154/* processes the scheduled list, sending those ready to be sent */
2155{
2156    xs *posts = scheduled_list(user);
2157    const char *md5;
2158    xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
2159
2160    xs_list_foreach(posts, md5) {
2161        xs *msg = NULL;
2162
2163        if (valid_status(object_get_by_md5(md5, &msg))) {
2164            if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
2165                /* due date! */
2166                const char *id = xs_dict_get(msg, "id");
2167
2168                timeline_add(user, id, msg);
2169
2170                xs *c_msg = msg_create(user, msg);
2171                enqueue_message(user, c_msg);
2172
2173                schedule_del(user, id);
2174            }
2175        }
2176    }
2177}
2178
2179
2180/** hiding **/
2181
2182xs_str *_hidden_fn(snac *snac, const char *id)
2183{
2184    xs *md5 = xs_md5_hex(id, strlen(id));
2185    return xs_fmt("%s/hidden/%s", snac->basedir, md5);
2186}
2187
2188
2189void hide(snac *snac, const char *id)
2190/* hides an object and its children (if it's a post) */
2191{
2192    xs *fn = _hidden_fn(snac, id);
2193    FILE *f;
2194
2195    if ((f = fopen(fn, "w")) != NULL) {
2196        fprintf(f, "%s\n", id);
2197        fclose(f);
2198
2199        snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
2200
2201        /* hide all the children */
2202        xs *chld = object_children(id);
2203        char *p;
2204        const char *v;
2205
2206        p = chld;
2207        while (xs_list_iter(&p, &v)) {
2208            xs *co = NULL;
2209
2210            /* resolve to get the id */
2211            if (valid_status(object_get_by_md5(v, &co))) {
2212                const char *id = xs_dict_get(co, "id");
2213                if (id != NULL)
2214                    hide(snac, id);
2215            }
2216        }
2217    }
2218}
2219
2220
2221int is_hidden(snac *snac, const char *id)
2222/* check is id is hidden */
2223{
2224    xs *fn = _hidden_fn(snac, id);
2225
2226    return !!(mtime(fn) != 0.0);
2227}
2228
2229
2230int unhide(snac *user, const char *id)
2231/* unhides an object */
2232{
2233    xs *fn = _hidden_fn(user, id);
2234    return unlink(fn);
2235}
2236
2237
2238int actor_add(const char *actor, const xs_dict *msg)
2239/* adds an actor */
2240{
2241    return object_add_ow(actor, msg);
2242}
2243
2244
2245int actor_get(const char *actor, xs_dict **data)
2246/* returns an already downloaded actor */
2247{
2248    int status = HTTP_STATUS_OK;
2249    xs_dict *d = NULL;
2250
2251    if (xs_startswith(actor, srv_baseurl)) {
2252        /* it's a (possible) local user */
2253        xs *l = xs_split(actor, "/");
2254        const char *uid = xs_list_get(l, -1);
2255        snac user;
2256
2257        if (!xs_is_null(uid) && user_open(&user, uid)) {
2258            if (data)
2259                *data = msg_actor(&user);
2260
2261            user_free(&user);
2262            return HTTP_STATUS_OK;
2263        }
2264        else
2265            return HTTP_STATUS_NOT_FOUND;
2266    }
2267
2268    /* read the object */
2269    if (!valid_status(status = object_get(actor, &d))) {
2270        d = xs_free(d);
2271        return status;
2272    }
2273
2274    /* if the object is corrupted, discard it */
2275    if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
2276        srv_debug(1, xs_fmt("corrupted actor object %s", actor));
2277        d = xs_free(d);
2278        return HTTP_STATUS_NOT_FOUND;
2279    }
2280
2281    if (data)
2282        *data = d;
2283    else
2284        d = xs_free(d);
2285
2286    xs *fn = _object_fn(actor);
2287    double max_time;
2288
2289    /* maximum time for the actor data to be considered stale */
2290    max_time = 3600.0 * 36.0;
2291
2292    if (mtime(fn) + max_time < (double) time(NULL)) {
2293        /* actor data exists but also stinks */
2294        status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
2295    }
2296
2297    return status;
2298}
2299
2300
2301int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
2302/* gets an actor and requests a refresh if it's stale */
2303{
2304    int status = actor_get(actor, data);
2305
2306    if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
2307        enqueue_actor_refresh(user, actor, 0);
2308
2309    return status;
2310}
2311
2312
2313/** user limiting (announce blocks) **/
2314
2315int limited(snac *user, const char *id, int cmd)
2316/* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
2317{
2318    int ret = 0;
2319    xs *dir = xs_fmt("%s/limited", user->basedir);
2320    xs *md5 = xs_md5_hex(id, strlen(id));
2321    xs *fn  = xs_fmt("%s/%s", dir, md5);
2322
2323    switch (cmd) {
2324    case 0: /** check **/
2325        ret = !!(mtime(fn) > 0.0);
2326        break;
2327
2328    case 1: /** limit **/
2329        mkdirx(dir);
2330
2331        if (mtime(fn) > 0.0)
2332            ret = -1;
2333        else {
2334            FILE *f;
2335
2336            if ((f = fopen(fn, "w")) != NULL) {
2337                fprintf(f, "%s\n", id);
2338                fclose(f);
2339            }
2340            else
2341                ret = -2;
2342        }
2343        break;
2344
2345    case 2: /** unlimit **/
2346        if (mtime(fn) > 0.0)
2347            ret = unlink(fn);
2348        else
2349            ret = -1;
2350        break;
2351    }
2352
2353    return ret;
2354}
2355
2356
2357/** tag indexing **/
2358
2359void tag_index(const char *id, const xs_dict *obj)
2360/* update the tag indexes for this object */
2361{
2362    const xs_list *tags = xs_dict_get(obj, "tag");
2363    xs *md5_id = xs_md5_hex(id, strlen(id));
2364
2365    if (get_msg_visibility(obj) != SCOPE_PUBLIC)
2366        return;
2367
2368    if (xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
2369        xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
2370
2371        mkdirx(g_tag_dir);
2372
2373        const xs_dict *v;
2374        int ct = 0;
2375        while (xs_list_next(tags, &v, &ct)) {
2376            const char *type = xs_dict_get(v, "type");
2377            const char *name = xs_dict_get(v, "name");
2378
2379            if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
2380                while (*name == '#' || *name == '@')
2381                    name++;
2382
2383                if (*name == '\0')
2384                    continue;
2385
2386                xs* name_cased = xs_utf8_to_lower((xs_str *)name);
2387
2388                xs *md5_tag   = xs_md5_hex(name_cased, strlen(name_cased));
2389                xs *tag_dir   = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
2390                mkdirx(tag_dir);
2391
2392                xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
2393
2394                if (!index_in_md5(g_tag_idx, md5_id))
2395                    index_add_md5(g_tag_idx, md5_id);
2396
2397                FILE *f;
2398                xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
2399                if ((f = fopen(g_tag_name, "w")) != NULL) {
2400                    fprintf(f, "%s\n", name_cased);
2401                    fclose(f);
2402                }
2403
2404                srv_debug(1, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
2405            }
2406        }
2407    }
2408}
2409
2410
2411xs_str *tag_fn(const char *tag)
2412{
2413    if (*tag == '#')
2414        tag++;
2415
2416    xs *lw_tag = xs_utf8_to_lower(tag);
2417    xs *md5    = xs_md5_hex(lw_tag, strlen(lw_tag));
2418
2419    return xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
2420}
2421
2422
2423xs_list *tag_search(const char *tag, int skip, int show)
2424/* returns the list of posts tagged with tag */
2425{
2426    xs *idx = tag_fn(tag);
2427
2428    return index_list_desc(idx, skip, show);
2429}
2430
2431
2432/** lists **/
2433
2434xs_val *list_maint(snac *user, const char *list, int op)
2435/* list maintenance */
2436{
2437    xs_val *l = NULL;
2438
2439    switch (op) {
2440    case 0: /** list of lists **/
2441        {
2442            FILE *f;
2443            xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
2444            xs *ls   = xs_glob(spec, 0, 0);
2445            int c = 0;
2446            const char *v;
2447
2448            l = xs_list_new();
2449
2450            while (xs_list_next(ls, &v, &c)) {
2451                if ((f = fopen(v, "r")) != NULL) {
2452                    xs *title = xs_readline(f);
2453                    fclose(f);
2454
2455                    title = xs_strip_i(title);
2456
2457                    xs *v2 = xs_replace(v, ".id", "");
2458                    xs *l2 = xs_split(v2, "/");
2459
2460                    /* return [ list_id, list_title ] */
2461                    xs *tmp_list = xs_list_append(xs_list_new(), xs_list_get(l2, -1), title);
2462                    l = xs_list_append(l, tmp_list);
2463                }
2464            }
2465        }
2466
2467        break;
2468
2469    case 1: /** create new list (list is the name) **/
2470        {
2471            xs *lol = list_maint(user, NULL, 0);
2472            int c = 0;
2473            const xs_list *v;
2474            int add = 1;
2475
2476            /* check if this list name already exists */
2477            while (xs_list_next(lol, &v, &c)) {
2478                if (strcmp(xs_list_get(v, 1), list) == 0) {
2479                    add = 0;
2480
2481                    l = xs_dup(xs_list_get(v, 0));
2482
2483                    break;
2484                }
2485            }
2486
2487            if (add) {
2488                FILE *f;
2489                xs *dir = xs_fmt("%s/list/", user->basedir);
2490
2491                l = xs_fmt("%010x", time(NULL));
2492
2493                mkdirx(dir);
2494
2495                xs *fn = xs_fmt("%s%s.id", dir, l);
2496
2497                if ((f = fopen(fn, "w")) != NULL) {
2498                    fprintf(f, "%s\n", list);
2499                    fclose(f);
2500                }
2501            }
2502        }
2503
2504        break;
2505
2506    case 2: /** delete list (list is the id) **/
2507        {
2508            if (xs_is_hex(list)) {
2509                xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
2510                unlink(fn);
2511
2512                fn = xs_replace_i(fn, ".id", ".lst");
2513                unlink(fn);
2514
2515                fn = xs_replace_i(fn, ".lst", ".idx");
2516                unlink(fn);
2517
2518                fn = xs_str_cat(fn, ".bak");
2519                unlink(fn);
2520            }
2521        }
2522
2523        break;
2524
2525    case 3: /** get list name **/
2526        if (xs_is_hex(list)) {
2527            FILE *f;
2528            xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
2529
2530            if ((f = fopen(fn, "r")) != NULL) {
2531                l = xs_strip_i(xs_readline(f));
2532                fclose(f);
2533            }
2534        }
2535
2536        break;
2537
2538    case 4: /** find list id by name **/
2539        if (xs_is_string(list)) {
2540            xs *lol = list_maint(user, NULL, 0);
2541            const xs_list *li;
2542
2543            xs_list_foreach(lol, li) {
2544                if (strcmp(list, xs_list_get(li, 1)) == 0) {
2545                    l = xs_dup(xs_list_get(li, 0));
2546                    break;
2547                }
2548            }
2549        }
2550    }
2551
2552    return l;
2553}
2554
2555
2556xs_str *list_timeline_fn(snac *user, const char *list)
2557{
2558    return xs_fmt("%s/list/%s.idx", user->basedir, list);
2559}
2560
2561
2562xs_list *list_timeline(snac *user, const char *list, int skip, int show)
2563/* returns the timeline of a list */
2564{
2565    xs_list *l = NULL;
2566
2567    if (!xs_is_hex(list))
2568        return NULL;
2569
2570    xs *fn = list_timeline_fn(user, list);
2571
2572    if (mtime(fn) > 0.0)
2573        l = index_list_desc(fn, skip, show);
2574    else
2575        l = xs_list_new();
2576
2577    return l;
2578}
2579
2580
2581xs_val *list_members(snac *user, const char *list, const char *actor_md5, int op)
2582/* list member management */
2583{
2584    xs_val *l = NULL;
2585
2586    if (!xs_is_hex(list))
2587        return NULL;
2588
2589    if (actor_md5 != NULL && !xs_is_hex(actor_md5))
2590        return NULL;
2591
2592    xs *fn = xs_fmt("%s/list/%s.lst", user->basedir, list);
2593
2594    switch (op) {
2595    case 0: /** list members **/
2596        l = index_list(fn, XS_ALL);
2597
2598        break;
2599
2600    case 1: /** append actor to list **/
2601        if (xs_is_string(actor_md5) && xs_is_hex(actor_md5)) {
2602            if (!index_in_md5(fn, actor_md5))
2603                index_add_md5(fn, actor_md5);
2604        }
2605
2606        break;
2607
2608    case 2: /** delete actor from list **/
2609        if (xs_is_string(actor_md5) && xs_is_hex(actor_md5))
2610            index_del_md5(fn, actor_md5);
2611
2612        break;
2613
2614    default:
2615        srv_log(xs_fmt("ERROR: list_content: bad op %d", op));
2616        break;
2617    }
2618
2619    return l;
2620}
2621
2622
2623void list_distribute(snac *user, const char *who, const xs_dict *post)
2624/* distributes the post to all appropriate lists */
2625{
2626    const char *id = xs_dict_get(post, "id");
2627
2628    /* if who is not set, use the attributedTo in the message */
2629    if (xs_is_null(who))
2630        who = get_atto(post);
2631
2632    if (xs_type(who) == XSTYPE_STRING && xs_type(id) == XSTYPE_STRING) {
2633        xs *a_md5 = xs_md5_hex(who, strlen(who));
2634        xs *i_md5 = xs_md5_hex(id, strlen(id));
2635        xs *spec  = xs_fmt("%s/list/" "*.lst", user->basedir);
2636        xs *ls    = xs_glob(spec, 0, 0);
2637        int c = 0;
2638        const char *v;
2639
2640        while (xs_list_next(ls, &v, &c)) {
2641            /* is the actor in this list? */
2642            if (index_in_md5(v, a_md5)) {
2643                /* it is; add post md5 to its timeline */
2644                xs *idx = xs_replace(v, ".lst", ".idx");
2645                index_add_md5(idx, i_md5);
2646
2647                snac_debug(user, 1, xs_fmt("listed post %s in %s", id, idx));
2648            }
2649        }
2650    }
2651}
2652
2653
2654/** static data **/
2655
2656static int _load_raw_file(const char *fn, xs_val **data, int *size,
2657                        const char *inm, xs_str **etag)
2658/* loads a cached file */
2659{
2660    int status = HTTP_STATUS_NOT_FOUND;
2661
2662    if (fn) {
2663        double tm = mtime(fn);
2664
2665        if (tm > 0.0) {
2666            /* file exists; build the etag */
2667            xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
2668
2669            /* if if-none-match is set, check if it's the same */
2670            if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
2671                /* client has the newest version */
2672                status = HTTP_STATUS_NOT_MODIFIED;
2673            }
2674            else {
2675                /* newer or never downloaded; read the full file */
2676                struct stat sb;
2677                FILE *f;
2678
2679                if (lstat(fn, &sb) == 0 && (sb.st_mode&S_IFMT) == S_IFREG) {
2680                    if ((f = fopen(fn, "rb")) != NULL) {
2681                        *size = XS_ALL;
2682                        *data = xs_read(f, size);
2683                        fclose(f);
2684
2685                        status = HTTP_STATUS_OK;
2686                    }
2687                }
2688            }
2689
2690            /* if caller wants the etag, return it */
2691            if (etag != NULL)
2692                *etag = xs_dup(e);
2693
2694            srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
2695        }
2696    }
2697
2698    return status;
2699}
2700
2701
2702xs_str *_static_fn(snac *snac, const char *id)
2703/* gets the filename for a static file */
2704{
2705    if (strstr(id, ".."))
2706        return NULL;
2707    else
2708        return xs_fmt("%s/static/%s", snac->basedir, id);
2709}
2710
2711
2712int static_get(snac *snac, const char *id, xs_val **data, int *size,
2713                const char *inm, xs_str **etag)
2714/* returns static content */
2715{
2716    xs *fn = _static_fn(snac, id);
2717
2718    return _load_raw_file(fn, data, size, inm, etag);
2719}
2720
2721
2722void static_put(snac *snac, const char *id, const char *data, int size)
2723/* writes status content */
2724{
2725    xs *fn = _static_fn(snac, id);
2726    FILE *f;
2727
2728    if (fn && (f = fopen(fn, "wb")) != NULL) {
2729        fwrite(data, size, 1, f);
2730        fclose(f);
2731
2732        strip_media(fn);
2733    }
2734}
2735
2736
2737void static_put_meta(snac *snac, const char *id, const char *str)
2738/* puts metadata (i.e. a media description string) to id */
2739{
2740    xs *fn = _static_fn(snac, id);
2741
2742    if (fn) {
2743        fn = xs_str_cat(fn, ".txt");
2744        FILE *f;
2745
2746        if ((f = fopen(fn, "w")) != NULL) {
2747            fprintf(f, "%s\n", str);
2748            fclose(f);
2749        }
2750    }
2751}
2752
2753
2754xs_str *static_get_meta(snac *snac, const char *id)
2755/* gets metadata from a media */
2756{
2757    xs *fn    = _static_fn(snac, id);
2758    xs_str *r = NULL;
2759
2760    if (fn) {
2761        fn = xs_str_cat(fn, ".txt");
2762        FILE *f;
2763
2764        if ((f = fopen(fn, "r")) != NULL) {
2765            r = xs_strip_i(xs_readline(f));
2766            fclose(f);
2767        }
2768    }
2769    else
2770        r = xs_str_new("");
2771
2772    return r;
2773}
2774
2775
2776/** history **/
2777
2778xs_str *_history_fn(snac *snac, const char *id)
2779/* gets the filename for the history */
2780{
2781    if (strchr(id, '/'))
2782        return NULL;
2783    else
2784        return xs_fmt("%s/history/%s", snac->basedir, id);
2785}
2786
2787
2788double history_mtime(snac *snac, const char *id)
2789{
2790    double t = 0.0;
2791    xs *fn = _history_fn(snac, id);
2792
2793    if (fn != NULL)
2794        t = mtime(fn);
2795
2796    return t;
2797}
2798
2799
2800void history_add(snac *snac, const char *id, const char *content, int size,
2801                    xs_str **etag)
2802/* adds something to the history */
2803{
2804    xs *fn = _history_fn(snac, id);
2805    FILE *f;
2806
2807    if (fn && (f = fopen(fn, "w")) != NULL) {
2808        fwrite(content, size, 1, f);
2809        fclose(f);
2810
2811        if (etag) {
2812            double tm = mtime(fn);
2813            *etag = xs_fmt("W/\"snac-%.0lf\"", tm);
2814        }
2815    }
2816}
2817
2818
2819int history_get(snac *snac, const char *id, xs_str **content, int *size,
2820                const char *inm, xs_str **etag)
2821{
2822    xs *fn = _history_fn(snac, id);
2823
2824    return _load_raw_file(fn, content, size, inm, etag);
2825}
2826
2827
2828int history_del(snac *snac, const char *id)
2829{
2830    xs *fn = _history_fn(snac, id);
2831
2832    if (fn)
2833        return unlink(fn);
2834    else
2835        return -1;
2836}
2837
2838
2839xs_list *history_list(snac *snac)
2840{
2841    xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
2842
2843    return xs_glob(spec, 1, 1);
2844}
2845
2846
2847void lastlog_write(snac *snac, const char *source)
2848/* writes the last time the user logged in */
2849{
2850    xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
2851    FILE *f;
2852
2853    if ((f = fopen(fn, "w")) != NULL) {
2854        fprintf(f, "%lf %s\n", ftime(), source);
2855        fclose(f);
2856    }
2857}
2858
2859
2860/** inbox collection **/
2861
2862void inbox_add(const char *inbox)
2863/* collects a shared inbox */
2864{
2865    /* don't collect ourselves */
2866    if (xs_startswith(inbox, srv_baseurl))
2867        return;
2868
2869    xs *md5 = xs_md5_hex(inbox, strlen(inbox));
2870    xs *fn  = xs_fmt("%s/inbox/%s", srv_basedir, md5);
2871    FILE *f;
2872
2873    if ((f = fopen(fn, "w")) != NULL) {
2874        fprintf(f, "%s\n", inbox);
2875        fclose(f);
2876    }
2877}
2878
2879
2880void inbox_add_by_actor(const xs_dict *actor)
2881/* collects an actor's shared inbox, if it has one */
2882{
2883    const char *v;
2884
2885    if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
2886        !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
2887        /* only collect this inbox if its instance is not blocked */
2888        if (!is_instance_blocked(v))
2889            inbox_add(v);
2890    }
2891}
2892
2893
2894xs_list *inbox_list(void)
2895/* returns the collected inboxes as a list */
2896{
2897    xs_list *ibl = xs_list_new();
2898    xs *spec     = xs_fmt("%s/inbox/" "*", srv_basedir);
2899    xs *files    = xs_glob(spec, 0, 0);
2900    const xs_val *v;
2901
2902    xs_list_foreach(files, v) {
2903        FILE *f;
2904
2905        if ((f = fopen(v, "r")) != NULL) {
2906            xs *line = xs_readline(f);
2907
2908            if (line && *line) {
2909                line = xs_strip_i(line);
2910
2911                if (!is_instance_blocked(line))
2912                    ibl = xs_list_append(ibl, line);
2913            }
2914
2915            fclose(f);
2916        }
2917    }
2918
2919    return ibl;
2920}
2921
2922
2923/** instance-wide operations **/
2924
2925xs_str *_instance_block_fn(const char *instance)
2926{
2927    xs *s   = xs_replace(instance, "http:/" "/", "");
2928    xs *s1  = xs_replace(s, "https:/" "/", "");
2929    xs *l   = xs_split(s1, "/");
2930    const char *p = xs_list_get(l, 0);
2931    xs *md5 = xs_md5_hex(p, strlen(p));
2932
2933    return xs_fmt("%s/block/%s", srv_basedir, md5);
2934}
2935
2936
2937int is_instance_blocked(const char *instance)
2938{
2939    xs *fn = _instance_block_fn(instance);
2940
2941    return !!(mtime(fn) != 0.0);
2942}
2943
2944
2945int instance_block(const char *instance)
2946/* blocks a full instance */
2947{
2948    int ret;
2949
2950    /* create the subdir */
2951    xs *dir = xs_fmt("%s/block/", srv_basedir);
2952    mkdirx(dir);
2953
2954    if (!is_instance_blocked(instance)) {
2955        xs *fn = _instance_block_fn(instance);
2956        FILE *f;
2957
2958        if ((f = fopen(fn, "w")) != NULL) {
2959            fprintf(f, "%s\n", instance);
2960            fclose(f);
2961
2962            ret = 0;
2963        }
2964        else
2965            ret = -1;
2966    }
2967    else
2968        ret = -2;
2969
2970    return ret;
2971}
2972
2973
2974int instance_unblock(const char *instance)
2975/* unblocks a full instance */
2976{
2977    int ret;
2978
2979    if (is_instance_blocked(instance)) {
2980        xs *fn = _instance_block_fn(instance);
2981        ret = unlink(fn);
2982    }
2983    else
2984        ret = -2;
2985
2986    return ret;
2987}
2988
2989
2990/** operations by content **/
2991
2992int content_match(const char *file, const xs_dict *msg)
2993/* checks if a message's content matches any of the regexes in file */
2994/* file format: one regex per line */
2995{
2996    xs *fn = xs_fmt("%s/%s", srv_basedir, file);
2997    FILE *f;
2998    int r = 0;
2999    const char *v = xs_dict_get(msg, "content");
3000
3001    if (xs_type(v) == XSTYPE_STRING && *v) {
3002        if ((f = fopen(fn, "r")) != NULL) {
3003            srv_debug(1, xs_fmt("content_match: loading regexes from %s", fn));
3004
3005            /* massage content (strip HTML tags, etc.) */
3006            xs *c1 = xs_regex_replace(v, "<[^>]+>", " ");
3007            c1 = xs_regex_replace_i(c1, " {2,}", " ");
3008            xs *c = xs_utf8_to_lower(c1);
3009
3010            while (!r && !feof(f)) {
3011                xs *rx = xs_strip_i(xs_readline(f));
3012
3013                if (*rx && xs_regex_match(c, rx)) {
3014                    srv_debug(1, xs_fmt("content_match: match for '%s'", rx));
3015                    r = 1;
3016                }
3017            }
3018
3019            fclose(f);
3020        }
3021    }
3022
3023    return r;
3024}
3025
3026
3027xs_list *content_search(snac *user, const char *regex,
3028                int priv, int skip, int show, int max_secs, int *timeout)
3029/* returns a list of posts which content matches the regex */
3030{
3031    if (regex == NULL || *regex == '\0')
3032        return xs_list_new();
3033
3034    xs *i_regex = xs_utf8_to_lower(regex);
3035
3036    xs_set seen;
3037
3038    xs_set_init(&seen);
3039
3040    if (max_secs == 0)
3041        max_secs = 3;
3042
3043    time_t t = time(NULL) + max_secs;
3044    *timeout = 0;
3045
3046    /* iterate all timelines simultaneously */
3047    xs_list *tls[3] = {0};
3048    const char *md5s[3] = {0};
3049    int c[3] = {0};
3050
3051    tls[0] = timeline_simple_list(user, "public", 0, XS_ALL, NULL);   /* public */
3052    tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
3053    tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL, NULL) : xs_list_new(); /* private or none */
3054
3055    /* first positioning */
3056    for (int n = 0; n < 3; n++)
3057        xs_list_next(tls[n], &md5s[n], &c[n]);
3058
3059    show += skip;
3060
3061    while (show > 0) {
3062        /* timeout? */
3063        if (time(NULL) > t) {
3064            *timeout = 1;
3065            break;
3066        }
3067
3068        /* find the newest post */
3069        int newest = -1;
3070        double mtime = 0.0;
3071
3072        for (int n = 0; n < 3; n++) {
3073            if (md5s[n] != NULL) {
3074                xs *fn = _object_fn_by_md5(md5s[n], "content_search");
3075                double mt;
3076
3077                while ((mt = mtime(fn)) == 0 && md5s[n] != NULL) {
3078                    /* object is not here: move to the next one */
3079                    if (xs_list_next(tls[n], &md5s[n], &c[n])) {
3080                        xs_free(fn);
3081                        fn = _object_fn_by_md5(md5s[n], "content_search_2");
3082                    }
3083                    else
3084                        md5s[n] = NULL;
3085                }
3086
3087                if (mt > mtime) {
3088                    newest = n;
3089                    mtime = mt;
3090                }
3091            }
3092        }
3093
3094        if (newest == -1)
3095            break;
3096
3097        const char *md5 = md5s[newest];
3098
3099        /* advance the chosen timeline */
3100        if (!xs_list_next(tls[newest], &md5s[newest], &c[newest]))
3101            md5s[newest] = NULL;
3102
3103        xs *post = NULL;
3104
3105        if (!valid_status(object_get_by_md5(md5, &post)))
3106            continue;
3107
3108        if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE))
3109            continue;
3110
3111        const char *id = xs_dict_get(post, "id");
3112
3113        if (id == NULL || is_hidden(user, id))
3114            continue;
3115
3116        /* recalculate the md5 id to be sure it's not repeated
3117           (it may have been searched by the "url" field instead of "id") */
3118        xs *new_md5 = xs_md5_hex(id, strlen(id));
3119        md5 = new_md5;
3120
3121        /* test for the post URL */
3122        if (strcmp(id, regex) == 0) {
3123            if (xs_set_add(&seen, md5) == 1)
3124                show--;
3125
3126            continue;
3127        }
3128
3129        /* test for the alternate post id */
3130        const char *url = xs_dict_get(post, "url");
3131        if (xs_type(url) == XSTYPE_STRING && strcmp(url, regex) == 0) {
3132            if (xs_set_add(&seen, md5) == 1)
3133                show--;
3134
3135            continue;
3136        }
3137
3138        xs *c = xs_str_new(NULL);
3139        const char *content = xs_dict_get(post, "content");
3140        const char *name    = xs_dict_get(post, "name");
3141        const char *atto    = get_atto(post);
3142
3143        if (!xs_is_null(content))
3144            c = xs_str_cat(c, content);
3145        if (!xs_is_null(name))
3146            c = xs_str_cat(c, " ", name);
3147        if (!xs_is_null(atto))
3148            c = xs_str_cat(c, " ", atto);
3149
3150        /* add alt-texts from attachments */
3151        const xs_list *atts = xs_dict_get(post, "attachment");
3152        int tc = 0;
3153        const xs_dict *att;
3154
3155        while (xs_list_next(atts, &att, &tc)) {
3156            const char *name = xs_dict_get(att, "name");
3157
3158            if (name != NULL)
3159                c = xs_str_cat(c, " ", name);
3160        }
3161
3162        /* strip HTML */
3163        c = xs_regex_replace_i(c, "<[^>]+>", " ");
3164        c = xs_regex_replace_i(c, " {2,}", " ");
3165
3166        /* convert to lowercase */
3167        xs *lc = xs_utf8_to_lower(c);
3168
3169        /* apply regex */
3170        if (xs_regex_match(lc, i_regex)) {
3171            if (xs_set_add(&seen, md5) == 1)
3172            show--;
3173        }
3174    }
3175
3176    xs_list *r = xs_set_result(&seen);
3177
3178    if (skip) {
3179        /* BAD */
3180        while (skip--) {
3181            r = xs_list_del(r, 0);
3182        }
3183    }
3184
3185    xs_free(tls[0]);
3186    xs_free(tls[1]);
3187    xs_free(tls[2]);
3188
3189    return r;
3190}
3191
3192
3193int actor_failure(const char *actor, int op)
3194/* actor failure maintenance */
3195{
3196    int ret = 0;
3197
3198    xs *md5 = xs_md5_hex(actor, strlen(actor));
3199    xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
3200
3201    switch (op) {
3202    case 0: /** check **/
3203        if (mtime(fn))
3204            ret = -1;
3205
3206        break;
3207
3208    case 1: /** register a failure **/
3209        if (mtime(fn) == 0.0) {
3210            FILE *f;
3211
3212            /* only create once, as the date will be used */
3213            if ((f = fopen(fn, "w")) != NULL) {
3214                fprintf(f, "%s\n", actor);
3215                fclose(f);
3216            }
3217        }
3218
3219        break;
3220
3221    case 2: /** clear a failure **/
3222        /* called whenever a message comes from this instance */
3223        unlink(fn);
3224
3225        break;
3226    }
3227
3228    return ret;
3229}
3230
3231
3232int instance_failure(const char *url, int op)
3233/* do some checks and accounting on instance failures */
3234{
3235    int ret = 0;
3236    xs *l = xs_split(url, "/");
3237    const char *hostname = xs_list_get(l, 2);
3238    double mt;
3239
3240    if (!xs_is_string(hostname))
3241        return 0;
3242
3243    xs *md5 = xs_md5_hex(hostname, strlen(hostname));
3244    xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
3245
3246    switch (op) {
3247    case 0: /** check **/
3248        if ((mt = mtime(fn)) != 0.0) {
3249            /* grace time */
3250            double seconds_failing = xs_number_get(xs_dict_get_def(srv_config, "max_failing_days", "15"))
3251                * (24 * 60 * 60);
3252
3253            if ((double)time(NULL) - mt > seconds_failing)
3254                ret = -1;
3255        }
3256
3257        break;
3258
3259    case 1: /** register a failure **/
3260        if (mtime(fn) == 0.0) {
3261            FILE *f;
3262
3263            /* only create once, as the date will be used */
3264            if ((f = fopen(fn, "w")) != NULL) {
3265                fprintf(f, "%s\n", hostname);
3266                fclose(f);
3267            }
3268        }
3269
3270        break;
3271
3272    case 2: /** clear a failure **/
3273        /* called whenever a message comes from this instance */
3274        unlink(fn);
3275
3276        break;
3277    }
3278
3279    return ret;
3280}
3281
3282
3283int grave(const char *objid, int op)
3284/* the graveyeard of deleted objects */
3285{
3286    int ret = 0;
3287    xs *dir = xs_fmt("%s/grave", srv_basedir);
3288    xs *md5 = xs_md5_hex(objid, strlen(objid));
3289    xs *fn  = xs_fmt("%s/%s", dir, md5);
3290    FILE *f;
3291
3292    switch (op) {
3293    case 0: /** check **/
3294        ret = mtime(fn) > 0.0 ? 1 : 0;
3295        break;
3296
3297    case 1: /** add **/
3298        mkdirx(dir);
3299
3300        if ((f = fopen(fn, "w")) != NULL) {
3301            fprintf(f, "%s\n", objid);
3302            fclose(f);
3303        }
3304
3305        break;
3306
3307    case 2: /** del **/
3308        unlink(fn);
3309        break;
3310    }
3311
3312    return ret;
3313}
3314
3315
3316/** notifications **/
3317
3318xs_str *notify_check_time(snac *snac, int reset)
3319/* gets or resets the latest notification check time */
3320{
3321    xs_str *t = NULL;
3322    xs *fn    = xs_fmt("%s/notifydate.txt", snac->basedir);
3323    FILE *f;
3324
3325    if (reset) {
3326        if ((f = fopen(fn, "w")) != NULL) {
3327            t = tid(0);
3328            fprintf(f, "%s\n", t);
3329            fclose(f);
3330        }
3331    }
3332    else {
3333        if ((f = fopen(fn, "r")) != NULL) {
3334            t = xs_readline(f);
3335            fclose(f);
3336        }
3337        else
3338            /* never set before */
3339            t = xs_fmt("%16.6f", 0.0);
3340    }
3341
3342    return t;
3343}
3344
3345xs_dict *markers_get(snac *snac, const xs_list *markers)
3346{
3347    xs *data = NULL;
3348    xs_dict *returns = xs_dict_new();
3349    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
3350    const xs_str *v = NULL;
3351    FILE *f;
3352
3353    if ((f = fopen(fn, "r")) != NULL) {
3354        data = xs_json_load(f);
3355        fclose(f);
3356    }
3357
3358    if (xs_is_null(data))
3359        data = xs_dict_new();
3360
3361    xs_list_foreach(markers, v) {
3362        const xs_dict *mark = xs_dict_get(data, v);
3363        if (!xs_is_null(mark)) {
3364            returns = xs_dict_append(returns, v, mark);
3365        }
3366    }
3367    return returns;
3368}
3369
3370xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
3371/* gets or sets notification marker */
3372{
3373    xs *data = NULL;
3374    xs_dict *written = xs_dict_new();
3375    xs *fn = xs_fmt("%s/markers.json", snac->basedir);
3376    FILE *f;
3377
3378    if ((f = fopen(fn, "r")) != NULL) {
3379        data = xs_json_load(f);
3380        fclose(f);
3381    }
3382
3383    if (xs_is_null(data))
3384        data = xs_dict_new();
3385
3386    if (!xs_is_null(home_marker)) {
3387        xs *home = xs_dict_new();
3388        xs *s_tid = tid(0);
3389        home = xs_dict_append(home, "last_read_id", home_marker);
3390        home = xs_dict_append(home, "version", xs_stock(0));
3391        home = xs_dict_append(home, "updated_at", s_tid);
3392        data = xs_dict_set(data, "home", home);
3393        written = xs_dict_append(written, "home", home);
3394    }
3395
3396    if (!xs_is_null(notify_marker)) {
3397        xs *notify = xs_dict_new();
3398        xs *s_tid = tid(0);
3399        notify = xs_dict_append(notify, "last_read_id", notify_marker);
3400        notify = xs_dict_append(notify, "version", xs_stock(0));
3401        notify = xs_dict_append(notify, "updated_at", s_tid);
3402        data = xs_dict_set(data, "notifications", notify);
3403        written = xs_dict_append(written, "notifications", notify);
3404    }
3405
3406    if ((f = fopen(fn, "w")) != NULL) {
3407        xs_json_dump(data, 4, f);
3408        fclose(f);
3409    }
3410
3411    return written;
3412}
3413
3414void notify_add(snac *snac, const char *type, const char *utype,
3415                const char *actor, const char *objid, const xs_dict *msg)
3416/* adds a new notification */
3417{
3418    xs *ntid = tid(0);
3419    xs *fn   = xs_fmt("%s/notify/", snac->basedir);
3420    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
3421    FILE *f;
3422
3423    /* create the directory */
3424    mkdirx(fn);
3425    fn = xs_str_cat(fn, ntid);
3426    fn = xs_str_cat(fn, ".json");
3427
3428    xs *noti = xs_dict_new();
3429
3430    noti = xs_dict_append(noti, "id",    ntid);
3431    noti = xs_dict_append(noti, "type",  type);
3432    noti = xs_dict_append(noti, "utype", utype);
3433    noti = xs_dict_append(noti, "actor", actor);
3434    noti = xs_dict_append(noti, "date",  date);
3435    noti = xs_dict_append(noti, "msg",   msg);
3436
3437    if (!xs_is_null(objid))
3438        noti = xs_dict_append(noti, "objid", objid);
3439
3440    if ((f = fopen(fn, "w")) != NULL) {
3441        xs_json_dump(noti, 4, f);
3442        fclose(f);
3443    }
3444
3445    /* add it to the index if it already exists */
3446    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3447
3448    if (mtime(idx) != 0.0) {
3449        pthread_mutex_lock(&data_mutex);
3450
3451        if ((f = fopen(idx, "a")) != NULL) {
3452            fprintf(f, "%-32s\n", ntid);
3453            fclose(f);
3454        }
3455
3456        pthread_mutex_unlock(&data_mutex);
3457    }
3458
3459    if (!xs_is_true(xs_dict_get(srv_config, "disable_notify_webhook")))
3460        enqueue_notify_webhook(snac, noti, 0);
3461}
3462
3463
3464xs_dict *notify_get(snac *snac, const char *id)
3465/* gets a notification */
3466{
3467    /* base file */
3468    xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
3469
3470    /* strip spaces and add extension */
3471    fn = xs_strip_i(fn);
3472    fn = xs_str_cat(fn, ".json");
3473
3474    FILE *f;
3475    xs_dict *out = NULL;
3476
3477    if ((f = fopen(fn, "r")) != NULL) {
3478        out = xs_json_load(f);
3479        fclose(f);
3480    }
3481
3482    return out;
3483}
3484
3485
3486xs_list *notify_list(snac *snac, int skip, int show)
3487/* returns a list of notification ids */
3488{
3489    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3490
3491    if (mtime(idx) == 0.0) {
3492        /* create the index from scratch */
3493        FILE *f;
3494
3495        pthread_mutex_lock(&data_mutex);
3496
3497        if ((f = fopen(idx, "w")) != NULL) {
3498            xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
3499            xs *lst  = xs_glob(spec, 1, 0);
3500            xs_list *p = lst;
3501            const char *v;
3502
3503            while (xs_list_iter(&p, &v)) {
3504                char *p = strrchr(v, '.');
3505                if (p) {
3506                    *p = '\0';
3507                    fprintf(f, "%-32s\n", v);
3508                }
3509            }
3510
3511            fclose(f);
3512        }
3513
3514        pthread_mutex_unlock(&data_mutex);
3515    }
3516
3517    return index_list_desc(idx, skip, show);
3518}
3519
3520
3521xs_list *notify_filter_list(snac *snac, xs_list *notifs)
3522/* apply user-defined notification filter to IDs */
3523{
3524    const xs_dict *n_filter = xs_dict_get(snac->config, "notify_filter");
3525    if (!n_filter) {
3526        return xs_dup(notifs);
3527    }
3528    const xs_val *n_def = xs_stock( XSTYPE_TRUE );
3529    int n_likes_on  = xs_is_true(xs_dict_get_def(n_filter, "likes", n_def));
3530    int n_reacts_on = xs_is_true(xs_dict_get_def(n_filter, "reacts", n_def));
3531    int n_ments_on  = xs_is_true(xs_dict_get_def(n_filter, "mentions", n_def));
3532    int n_ann_on    = xs_is_true(xs_dict_get_def(n_filter, "announces", n_def));
3533    int n_fol_on    = xs_is_true(xs_dict_get_def(n_filter, "follows", n_def));
3534    int n_unfol_on  = xs_is_true(xs_dict_get_def(n_filter, "unfollows", n_def));
3535    int n_folreq_on = xs_is_true(xs_dict_get_def(n_filter, "folreqs", n_def));
3536    int n_blocks_on = xs_is_true(xs_dict_get_def(n_filter, "blocks", n_def));
3537    int n_polls_on  = xs_is_true(xs_dict_get_def(n_filter, "polls", n_def));
3538
3539    const xs_str *v;
3540    xs_list *flt = xs_list_new();
3541
3542    xs_list_foreach(notifs, v) {
3543        xs *noti = notify_get(snac, v);
3544
3545        if (noti == NULL)
3546            continue;
3547
3548        const char *type  = xs_dict_get(noti, "type");
3549        const char *utype = xs_dict_get(noti, "utype");
3550        const char *actor_id = xs_dict_get(noti, "actor");
3551        if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
3552            continue;
3553        if (strcmp(type, "Create") == 0 && !n_ments_on)
3554            continue;
3555        if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0 && !n_polls_on)
3556            continue;
3557        if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0 && !n_unfol_on)
3558            continue;
3559        if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
3560            if (strcmp(type, "Like") == 0 && !n_likes_on)
3561                continue;
3562            if (strcmp(type, "EmojiReact") == 0 && !n_reacts_on)
3563                continue;
3564        }
3565        if (strcmp(type, "Follow") == 0) {
3566            if (pending_check(snac, actor_id)) {
3567                if (!n_folreq_on)
3568                    continue;
3569            }
3570            else
3571            if (!n_fol_on)
3572                continue;
3573        }
3574        if (strcmp(type, "Block") == 0 && !n_blocks_on)
3575            continue;
3576        if (strcmp(type, "Announce") == 0 && !n_ann_on)
3577            continue;
3578        flt = xs_list_append(flt, v);
3579    }
3580    return flt;
3581}
3582
3583
3584int notify_new_num(snac *snac)
3585/* counts the number of new notifications */
3586{
3587    xs *t = notify_check_time(snac, 0);
3588    xs *lst_unfilt = notify_list(snac, 0, XS_ALL);
3589    xs *lst = notify_filter_list(snac, lst_unfilt);
3590    int cnt = 0;
3591
3592    xs_list *p = lst;
3593    const xs_str *v;
3594
3595    while (xs_list_iter(&p, &v)) {
3596        xs *id = xs_strip_i(xs_dup(v));
3597
3598        /* old? count no more */
3599        if (strcmp(id, t) < 0)
3600            break;
3601
3602        cnt++;
3603    }
3604
3605    return cnt;
3606}
3607
3608
3609void notify_clear(snac *snac)
3610/* clears all notifications */
3611{
3612    xs *spec   = xs_fmt("%s/notify/" "*", snac->basedir);
3613    xs *lst    = xs_glob(spec, 0, 0);
3614    xs_list *p = lst;
3615    const xs_str *v;
3616
3617    while (xs_list_iter(&p, &v))
3618        unlink(v);
3619
3620    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3621
3622    if (mtime(idx) != 0.0) {
3623        pthread_mutex_lock(&data_mutex);
3624        truncate(idx, 0);
3625        pthread_mutex_unlock(&data_mutex);
3626    }
3627}
3628
3629
3630/** the queue **/
3631
3632static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
3633/* writes safely to the queue */
3634{
3635    xs *tfn = xs_fmt("%s.tmp", fn);
3636    FILE *f;
3637
3638    if ((f = fopen(tfn, "w")) != NULL) {
3639        xs_json_dump(msg, 4, f);
3640        fclose(f);
3641
3642        rename(tfn, fn);
3643    }
3644
3645    return msg;
3646}
3647
3648
3649static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
3650/* creates a queue message */
3651{
3652    int qrt  = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
3653    xs *ntid = tid(retries * 60 * qrt);
3654    xs *rn   = xs_number_new(retries);
3655
3656    xs_dict *qmsg = xs_dict_new();
3657
3658    qmsg = xs_dict_append(qmsg, "type",    type);
3659    qmsg = xs_dict_append(qmsg, "message", msg);
3660    qmsg = xs_dict_append(qmsg, "retries", rn);
3661    qmsg = xs_dict_append(qmsg, "ntid",    ntid);
3662
3663    return qmsg;
3664}
3665
3666
3667void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
3668/* enqueues an input message */
3669{
3670    xs *qmsg   = _new_qmsg("input", msg, retries);
3671    const char *ntid = xs_dict_get(qmsg, "ntid");
3672    xs *fn     = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
3673
3674    qmsg = xs_dict_append(qmsg, "req", req);
3675
3676    qmsg = _enqueue_put(fn, qmsg);
3677
3678    snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
3679}
3680
3681
3682void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
3683/* enqueues an input message from the shared input */
3684{
3685    xs *qmsg   = _new_qmsg("input", msg, retries);
3686    const char *ntid = xs_dict_get(qmsg, "ntid");
3687    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3688
3689    qmsg = xs_dict_append(qmsg, "req", req);
3690
3691    qmsg = _enqueue_put(fn, qmsg);
3692
3693    srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
3694}
3695
3696
3697void enqueue_output_raw(const char *keyid, const char *seckey,
3698                        const xs_dict *msg, const xs_str *inbox,
3699                        int retries, int p_status)
3700/* enqueues an output message to an inbox */
3701{
3702    xs *qmsg   = _new_qmsg("output", msg, retries);
3703    const char *ntid = xs_dict_get(qmsg, "ntid");
3704    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3705
3706    xs *ns = xs_number_new(p_status);
3707    qmsg = xs_dict_append(qmsg, "p_status", ns);
3708
3709    qmsg = xs_dict_append(qmsg, "inbox",  inbox);
3710    qmsg = xs_dict_append(qmsg, "keyid",  keyid);
3711    qmsg = xs_dict_append(qmsg, "seckey", seckey);
3712
3713    /* if it's to be sent right now, bypass the disk queue and post the job */
3714    if (retries == 0 && p_state != NULL)
3715        job_post(qmsg, 0);
3716    else {
3717        qmsg = _enqueue_put(fn, qmsg);
3718        srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
3719    }
3720}
3721
3722
3723void enqueue_output(snac *snac, const xs_dict *msg,
3724                    const xs_str *inbox, int retries, int p_status)
3725/* enqueues an output message to an inbox */
3726{
3727    if (is_msg_mine(snac, inbox)) {
3728        snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
3729        return;
3730    }
3731
3732    const char *seckey = xs_dict_get(snac->key, "secret");
3733
3734    enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
3735}
3736
3737
3738void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
3739                            const xs_str *actor, int retries)
3740/* enqueues an output message for an actor */
3741{
3742    xs *inbox = get_actor_inbox(actor, 1);
3743
3744    if (!xs_is_null(inbox))
3745        enqueue_output(snac, msg, inbox, retries, 0);
3746    else
3747        snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
3748}
3749
3750
3751void enqueue_email(const xs_dict *msg, int retries)
3752/* enqueues an email message to be sent */
3753{
3754    xs *qmsg   = _new_qmsg("email", msg, retries);
3755    const char *ntid = xs_dict_get(qmsg, "ntid");
3756    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3757
3758    qmsg = _enqueue_put(fn, qmsg);
3759
3760    srv_debug(1, xs_fmt("enqueue_email %d", retries));
3761}
3762
3763
3764void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
3765/* enqueues a message to be sent via Telegram */
3766{
3767    xs *qmsg   = _new_qmsg("telegram", msg, 0);
3768    const char *ntid = xs_dict_get(qmsg, "ntid");
3769    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3770
3771    qmsg = xs_dict_append(qmsg, "bot",      bot);
3772    qmsg = xs_dict_append(qmsg, "chat_id",  chat_id);
3773
3774    qmsg = _enqueue_put(fn, qmsg);
3775
3776    srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
3777}
3778
3779void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
3780/* enqueues a message to be sent via ntfy */
3781{
3782    xs *qmsg   = _new_qmsg("ntfy", msg, 0);
3783    const char *ntid = xs_dict_get(qmsg, "ntid");
3784    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3785
3786    qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
3787    qmsg = xs_dict_append(qmsg, "ntfy_token",  ntfy_token);
3788
3789
3790    qmsg = _enqueue_put(fn, qmsg);
3791
3792    srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
3793}
3794
3795void enqueue_message(snac *snac, const xs_dict *msg)
3796/* enqueues an output message */
3797{
3798    xs *qmsg   = _new_qmsg("message", msg, 0);
3799    const char *ntid = xs_dict_get(qmsg, "ntid");
3800    xs *fn     = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
3801
3802    qmsg = _enqueue_put(fn, qmsg);
3803
3804    snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
3805}
3806
3807
3808void enqueue_close_question(snac *user, const char *id, int end_secs)
3809/* enqueues the closing of a question */
3810{
3811    xs *qmsg = _new_qmsg("close_question", id, 0);
3812    xs *ntid = tid(end_secs);
3813    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3814
3815    qmsg = xs_dict_set(qmsg, "ntid", ntid);
3816
3817    qmsg = _enqueue_put(fn, qmsg);
3818
3819    snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
3820}
3821
3822
3823void enqueue_object_request(snac *user, const char *id, int forward_secs)
3824/* enqueues the request of an object in the future */
3825{
3826    xs *qmsg = _new_qmsg("object_request", id, 0);
3827    xs *ntid = tid(forward_secs);
3828    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3829
3830    qmsg = xs_dict_set(qmsg, "ntid", ntid);
3831
3832    qmsg = _enqueue_put(fn, qmsg);
3833
3834    snac_debug(user, 0, xs_fmt("enqueue_object_request %s %d", id, forward_secs));
3835}
3836
3837
3838void enqueue_verify_links(snac *user)
3839/* enqueues a link verification */
3840{
3841    xs *qmsg   = _new_qmsg("verify_links", "", 0);
3842    const char *ntid = xs_dict_get(qmsg, "ntid");
3843    xs *fn     = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3844
3845    qmsg = _enqueue_put(fn, qmsg);
3846
3847    snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
3848}
3849
3850
3851void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs)
3852/* enqueues an actor refresh */
3853{
3854    xs *qmsg = _new_qmsg("actor_refresh", "", 0);
3855    xs *ntid = tid(forward_secs);
3856    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3857
3858    qmsg = xs_dict_set(qmsg, "ntid", ntid);
3859    qmsg = xs_dict_append(qmsg, "actor", actor);
3860
3861    qmsg = _enqueue_put(fn, qmsg);
3862
3863    snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s", actor));
3864}
3865
3866
3867void enqueue_webmention(const xs_dict *msg)
3868/* enqueues a webmention for the post */
3869{
3870    xs *qmsg   = _new_qmsg("webmention", msg, 0);
3871    const char *ntid = xs_dict_get(qmsg, "ntid");
3872    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3873
3874    qmsg = _enqueue_put(fn, qmsg);
3875
3876    srv_debug(1, xs_fmt("enqueue_webmention"));
3877}
3878
3879
3880void enqueue_notify_webhook(snac *user, const xs_dict *noti, int retries)
3881/* enqueues a notification webhook */
3882{
3883    const char *webhook = xs_dict_get(user->config, "notify_webhook");
3884
3885    if (xs_is_string(webhook) && xs_match(webhook, "https://*|http://*")) { /** **/
3886        xs *msg = xs_dup(noti);
3887
3888        /* add more data */
3889        msg = xs_dict_set(msg, "target", user->actor);
3890        msg = xs_dict_set(msg, "uid", user->uid);
3891        msg = xs_dict_set(msg, "basedir", srv_basedir);
3892        msg = xs_dict_set(msg, "baseurl", srv_baseurl);
3893
3894        xs *actor_obj = NULL;
3895
3896        if (valid_status(object_get(xs_dict_get(noti, "actor"), &actor_obj)) && actor_obj)
3897            msg = xs_dict_set(msg, "account", actor_obj);
3898
3899        /* if this post is a reply, also add the inReplyTo object */
3900        const char *in_reply_to = xs_dict_get_path(msg, "msg.object.inReplyTo");
3901
3902        if (xs_is_string(in_reply_to)) {
3903            xs *irt_obj = NULL;
3904
3905            if (valid_status(object_get(in_reply_to, &irt_obj)))
3906                msg = xs_dict_set(msg, "reply", irt_obj);
3907        }
3908
3909        xs *qmsg = _new_qmsg("notify_webhook", msg, retries);
3910        const char *ntid = xs_dict_get(qmsg, "ntid");
3911        xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3912
3913        qmsg = _enqueue_put(fn, qmsg);
3914
3915        snac_debug(user, 1, xs_fmt("notify_webhook"));
3916    }
3917}
3918
3919
3920void enqueue_collect_replies(snac *user, const char *post)
3921/* enqueues a collect replies request */
3922{
3923    xs *qmsg = _new_qmsg("collect_replies", post, 0);
3924    const char *ntid = xs_dict_get(qmsg, "ntid");
3925    xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3926
3927    qmsg = _enqueue_put(fn, qmsg);
3928
3929    snac_debug(user, 1, xs_fmt("enqueue_collect_replies %s", post));
3930}
3931
3932
3933void enqueue_collect_outbox(snac *user, const char *actor_id)
3934/* enqueues a collect outbox request */
3935{
3936    xs *qmsg = _new_qmsg("collect_outbox", actor_id, 0);
3937    const char *ntid = xs_dict_get(qmsg, "ntid");
3938    xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3939
3940    qmsg = _enqueue_put(fn, qmsg);
3941
3942    snac_debug(user, 1, xs_fmt("enqueue_collect_outbox %s", actor_id));
3943}
3944
3945
3946void enqueue_fsck(void)
3947/* enqueues an fsck */
3948{
3949    xs *qmsg   = _new_qmsg("fsck", "", 0);
3950    const char *ntid = xs_dict_get(qmsg, "ntid");
3951    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3952
3953    qmsg = _enqueue_put(fn, qmsg);
3954}
3955
3956
3957int was_question_voted(snac *user, const char *id)
3958/* returns true if the user voted in this poll */
3959{
3960    xs *children = object_children(id);
3961    int voted = 0;
3962    xs_list *p;
3963    const xs_str *md5;
3964
3965    p = children;
3966    while (xs_list_iter(&p, &md5)) {
3967        xs *obj = NULL;
3968
3969        if (valid_status(object_get_by_md5(md5, &obj))) {
3970            const char *atto = get_atto(obj);
3971            if (atto && strcmp(atto, user->actor) == 0 &&
3972                !xs_is_null(xs_dict_get(obj, "name"))) {
3973                voted = 1;
3974                break;
3975            }
3976        }
3977    }
3978
3979    return voted;
3980}
3981
3982
3983xs_list *user_queue(snac *snac)
3984/* returns a list with filenames that can be dequeued */
3985{
3986    xs *spec      = xs_fmt("%s/queue/" "*.json", snac->basedir);
3987    xs_list *list = xs_list_new();
3988    time_t t      = time(NULL);
3989    xs_list *p;
3990    const xs_val *v;
3991
3992    xs *fns = xs_glob(spec, 0, 0);
3993
3994    p = fns;
3995    while (xs_list_iter(&p, &v)) {
3996        /* get the retry time from the basename */
3997        char *bn  = strrchr(v, '/');
3998        time_t t2 = atol(bn + 1);
3999
4000        if (t2 > t)
4001            snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
4002        else {
4003            list = xs_list_append(list, v);
4004            snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
4005        }
4006    }
4007
4008    return list;
4009}
4010
4011
4012xs_list *queue(void)
4013/* returns a list with filenames that can be dequeued */
4014{
4015    xs *spec      = xs_fmt("%s/queue/" "*.json", srv_basedir);
4016    xs_list *list = xs_list_new();
4017    time_t t      = time(NULL);
4018    xs_list *p;
4019    const xs_val *v;
4020
4021    xs *fns = xs_glob(spec, 0, 0);
4022
4023    p = fns;
4024    while (xs_list_iter(&p, &v)) {
4025        /* get the retry time from the basename */
4026        char *bn  = strrchr(v, '/');
4027        time_t t2 = atol(bn + 1);
4028
4029        if (t2 > t)
4030            srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
4031        else {
4032            list = xs_list_append(list, v);
4033            srv_debug(2, xs_fmt("queue ready for %s", v));
4034        }
4035    }
4036
4037    return list;
4038}
4039
4040
4041xs_dict *queue_get(const char *fn)
4042/* gets a file from a queue */
4043{
4044    FILE *f;
4045    xs_dict *obj = NULL;
4046
4047    if ((f = fopen(fn, "r")) != NULL) {
4048        obj = xs_json_load(f);
4049        fclose(f);
4050    }
4051
4052    return obj;
4053}
4054
4055
4056xs_dict *dequeue(const char *fn)
4057/* dequeues a message */
4058{
4059    xs_dict *obj = queue_get(fn);
4060
4061    unlink(fn);
4062
4063    return obj;
4064}
4065
4066
4067/** the purge **/
4068
4069static int _purge_file(const char *fn, time_t mt)
4070/* purge fn if it's older than days */
4071{
4072    int ret = 0;
4073
4074    if (mtime(fn) < mt) {
4075        /* older than the minimum time: delete it */
4076        unlink(fn);
4077        srv_debug(2, xs_fmt("purged %s", fn));
4078        ret = 1;
4079    }
4080
4081    return ret;
4082}
4083
4084
4085static void _purge_dir(const char *dir, int days)
4086/* purges all files in a directory older than days */
4087{
4088    int cnt = 0;
4089
4090    if (days) {
4091        time_t mt = time(NULL) - days * 24 * 3600;
4092        xs *spec  = xs_fmt("%s/" "*", dir);
4093        xs *list  = xs_glob(spec, 0, 0);
4094        xs_list *p;
4095        const xs_str *v;
4096
4097        p = list;
4098        while (xs_list_iter(&p, &v))
4099            cnt += _purge_file(v, mt);
4100
4101        srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
4102    }
4103}
4104
4105
4106static void _purge_user_subdir(snac *snac, const char *subdir, int days)
4107/* purges all files in a user subdir older than days */
4108{
4109    xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
4110
4111    _purge_dir(u_subdir, days);
4112}
4113
4114
4115void purge_server(void)
4116/* purge global server data */
4117{
4118    xs *spec = xs_fmt("%s/object/??", srv_basedir);
4119    xs *dirs = xs_glob(spec, 0, 0);
4120    xs_list *p;
4121    const xs_str *v;
4122    int cnt = 0;
4123    int icnt = 0;
4124
4125    time_t mt = time(NULL) - 7 * 24 * 3600;
4126
4127    p = dirs;
4128    while (xs_list_iter(&p, &v)) {
4129        xs_list *p2;
4130        const xs_str *v2;
4131
4132        {
4133            xs *spec2 = xs_fmt("%s/" "*.json", v);
4134            xs *files = xs_glob(spec2, 0, 0);
4135
4136            p2 = files;
4137            while (xs_list_iter(&p2, &v2)) {
4138                int n_link;
4139
4140                /* old and with no hard links? */
4141                if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
4142                    xs *s1    = xs_replace(v2, ".json", "");
4143                    xs *l     = xs_split(s1, "/");
4144                    const char *md5 = xs_list_get(l, -1);
4145
4146                    object_del_by_md5(md5);
4147                    cnt++;
4148                }
4149            }
4150        }
4151
4152        {
4153            /* look for stray indexes */
4154            xs *speci = xs_fmt("%s/" "*_?.idx", v);
4155            xs *idxfs = xs_glob(speci, 0, 0);
4156
4157            p2 = idxfs;
4158            while (xs_list_iter(&p2, &v2)) {
4159                /* old enough to consider? */
4160                if (mtime(v2) < mt) {
4161                    /* check if the indexed object is here */
4162                    xs *o = xs_dup(v2);
4163                    char *ext = strchr(o, '_');
4164
4165                    if (ext) {
4166                        *ext = '\0';
4167                        o = xs_str_cat(o, ".json");
4168
4169                        if (mtime(o) == 0.0) {
4170                            /* delete */
4171                            unlink(v2);
4172                            srv_debug(1, xs_fmt("purged %s", v2));
4173                            icnt++;
4174                        }
4175                    }
4176                }
4177            }
4178
4179            /* delete index backups */
4180            xs *specb = xs_fmt("%s/" "*.bak", v);
4181            xs *bakfs = xs_glob(specb, 0, 0);
4182
4183            p2 = bakfs;
4184            while (xs_list_iter(&p2, &v2)) {
4185                unlink(v2);
4186                srv_debug(1, xs_fmt("purged %s", v2));
4187            }
4188        }
4189    }
4190
4191    /* purge collected inboxes */
4192    xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
4193    _purge_dir(ib_dir, 7);
4194
4195    /* purge the instance timeline */
4196    xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
4197    int itl_gc = index_gc(itl_fn);
4198
4199    /* purge tag indexes */
4200    xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
4201    xs *tag_dirs = xs_glob(tag_spec, 0, 0);
4202    p = tag_dirs;
4203
4204    int tag_gc = 0;
4205    while (xs_list_iter(&p, &v)) {
4206        xs *spec2 = xs_fmt("%s/" "*.idx", v);
4207        xs *files = xs_glob(spec2, 0, 0);
4208        xs_list *p2;
4209        const xs_str *v2;
4210
4211        p2 = files;
4212        while (xs_list_iter(&p2, &v2)) {
4213            tag_gc += index_gc(v2);
4214            xs *bak = xs_fmt("%s.bak", v2);
4215            unlink(bak);
4216
4217            if (index_len(v2) == 0) {
4218                /* there are no longer any entry with this tag;
4219                   purge it completely */
4220                unlink(v2);
4221                xs *dottag = xs_replace(v2, ".idx", ".tag");
4222                unlink(dottag);
4223            }
4224        }
4225    }
4226
4227    srv_debug(1, xs_fmt("purge: global "
4228            "(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
4229}
4230
4231
4232void delete_purged_posts(snac *user, int days)
4233/* enqueues Delete activities for local purged messages */
4234{
4235    if (days == 0)
4236        return;
4237
4238    time_t mt = time(NULL) - days * 24 * 3600;
4239    xs *spec  = xs_fmt("%s/public/" "*.json", user->basedir);
4240    xs *list  = xs_glob(spec, 0, 0);
4241    const char *v;
4242
4243    xs_list_foreach(list, v) {
4244        if (mtime(v) < mt) {
4245            /* to be purged; is it a Note by us? */
4246            FILE *f;
4247
4248            if ((f = fopen(v, "r")) != NULL) {
4249                xs *msg = xs_json_load(f);
4250                fclose(f);
4251
4252                if (xs_is_dict(msg)) {
4253                    const char *id = xs_dict_get(msg, "id");
4254
4255                    if (xs_is_string(id) && is_msg_mine(user, id)) {
4256                        xs *d_msg = msg_delete(user, id);
4257
4258                        enqueue_message(user, d_msg);
4259
4260                        snac_log(user, xs_fmt("enqueued Delete for purged message %s", id));
4261                    }
4262                }
4263            }
4264        }
4265    }
4266}
4267
4268
4269void purge_static(snac *user)
4270/* purges the static directory of a user */
4271{
4272    xs *spec = xs_fmt("%s/static/""*", user->basedir);
4273    xs *fns = xs_glob(spec, 1, 0);
4274    xs *files = xs_dict_new();
4275    const char *k, *v;
4276    int cnt = 0;
4277
4278    /* fill a dict with the static resource urls */
4279    xs_list_foreach(fns, v) {
4280        if (strcmp(v, "style.css") == 0) {
4281            snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4282            continue;
4283        }
4284
4285        xs *s = xs_fmt("%s/s/%s", user->actor, v);
4286
4287        xs *fn = xs_fmt("%s/static/%s", user->basedir, v);
4288        struct stat st;
4289
4290        /* exclude non-files */
4291        if (stat(fn, &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG) {
4292            snac_debug(user, 2, xs_fmt("purge_static: excluding non-file '%s'", v));
4293            continue;
4294        }
4295
4296        files = xs_dict_set(files, s, v);
4297        cnt++;
4298    }
4299
4300    /* don't purge avatars nor headers */
4301    if (xs_is_string(v = xs_dict_get(user->config, "avatar")) && xs_dict_get(files, v)) {
4302        files = xs_dict_del(files, v);
4303        cnt--;
4304
4305        snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4306    }
4307    if (xs_is_string(v = xs_dict_get(user->config, "header")) && xs_dict_get(files, v)) {
4308        files = xs_dict_del(files, v);
4309        cnt--;
4310
4311        snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4312    }
4313    if (xs_is_string(v = xs_dict_get(srv_config, "favicon_url")) && xs_dict_get(files, v)) {
4314        files = xs_dict_del(files, v);
4315        cnt--;
4316
4317        snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4318    }
4319
4320    /* exclude emojis served from the static directory */
4321    xs *emo = emojis();
4322    xs_dict_foreach(emo, k, v) {
4323        if (xs_dict_get(files, v)) {
4324            files = xs_dict_del(files, v);
4325            cnt--;
4326
4327            snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4328        }
4329    }
4330
4331    if (cnt <= 0)
4332        return;
4333
4334    xs *tl = timeline_simple_list(user, "public", 0, XS_ALL, NULL);
4335    const char *md5;
4336
4337    xs_list_foreach(tl, md5) {
4338        if (cnt <= 0)
4339            break;
4340
4341        xs *msg = NULL;
4342
4343        if (!valid_status(object_get_by_md5(md5, &msg)))
4344            continue;
4345
4346        if (!is_msg_mine(user, xs_dict_get_def(msg, "id", "")))
4347            continue;
4348
4349        const xs_list *atts = xs_dict_get(msg, "attachment");
4350        if (!xs_is_list(atts))
4351            continue;
4352
4353        const xs_dict *d;
4354
4355        xs_list_foreach(atts, d) {
4356            const char *url;
4357
4358            if (xs_is_dict(d) && xs_is_string(url = xs_dict_get(d, "url"))) {
4359                if (xs_dict_get(files, url)) {
4360                    files = xs_dict_del(files, url);
4361                    cnt--;
4362
4363                    snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", url));
4364
4365                    xs *alt_text = xs_fmt("%s.txt", url);
4366
4367                    if (xs_dict_get(files, alt_text)) {
4368                        files = xs_dict_del(files, alt_text);
4369                        cnt--;
4370
4371                        snac_debug(user, 2, xs_fmt("purge_static: excluding alt_text '%s'", alt_text));
4372                    }
4373                }
4374            }
4375        }
4376    }
4377
4378    /* what is left in the files dict is not referenced anywhere */
4379    xs_dict_foreach(files, k, v) {
4380        xs *s = xs_fmt("%s/static/%s", user->basedir, v);
4381        snac_debug(user, 1, xs_fmt("purge_static: %s", s));
4382        unlink(s);
4383    }
4384}
4385
4386
4387void purge_user(snac *snac)
4388/* do the purge for this user */
4389{
4390    int priv_days, pub_days, user_days = 0;
4391    const char *v;
4392    int n;
4393
4394    priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
4395    pub_days  = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
4396
4397    if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
4398        (v = xs_dict_get(snac->config, "purge_days")) != NULL)
4399        user_days = xs_number_get(v);
4400
4401    if (user_days) {
4402        /* override admin settings only if they are lesser */
4403        if (priv_days == 0 || user_days < priv_days)
4404            priv_days = user_days;
4405
4406        if (pub_days == 0 || user_days < pub_days)
4407            pub_days = user_days;
4408    }
4409
4410    if (xs_is_true(xs_dict_get(srv_config, "propagate_local_purge")))
4411        delete_purged_posts(snac, pub_days);
4412
4413    _purge_user_subdir(snac, "hidden",  priv_days);
4414    _purge_user_subdir(snac, "private", priv_days);
4415    _purge_user_subdir(snac, "pending",  priv_days);
4416
4417    _purge_user_subdir(snac, "public",  pub_days);
4418
4419    const char *idxs[] = { "followers.idx", "private.idx", "public.idx",
4420                           "pinned.idx", "bookmark.idx", "draft.idx", "sched.idx", NULL };
4421
4422    for (n = 0; idxs[n]; n++) {
4423        xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
4424        int gc = index_gc(idx);
4425        srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
4426    }
4427
4428    /* purge lists */
4429    {
4430        xs *spec = xs_fmt("%s/list/" "*.idx", snac->basedir);
4431        xs *lol  = xs_glob(spec, 0, 0);
4432        int c = 0;
4433        const char *v;
4434
4435        while (xs_list_next(lol, &v, &c)) {
4436            int gc = index_gc(v);
4437            srv_debug(1, xs_fmt("purge: %s %d", v, gc));
4438        }
4439    }
4440
4441    /* purge static directories, if so desired */
4442    if (xs_is_true(xs_dict_get(srv_config, "purge_static")))
4443        purge_static(snac);
4444
4445    /* unrelated to purging, but it's a janitorial process, so what the hell */
4446    verify_links(snac);
4447}
4448
4449
4450void purge_all(void)
4451/* purge all users */
4452{
4453    snac snac;
4454    xs *list = user_list();
4455    char *p;
4456    const char *uid;
4457
4458    p = list;
4459    while (xs_list_iter(&p, &uid)) {
4460        if (user_open(&snac, uid)) {
4461            purge_user(&snac);
4462            user_free(&snac);
4463        }
4464    }
4465
4466    purge_server();
4467
4468#ifndef NO_MASTODON_API
4469    mastoapi_purge();
4470#endif
4471}
4472
4473
4474/** archive **/
4475
4476void srv_archive(const char *direction, const char *url, xs_dict *req,
4477                 const char *payload, int p_size,
4478                 int status, xs_dict *headers,
4479                 const char *body, int b_size)
4480/* archives a connection */
4481{
4482    /* obsessive archiving */
4483    xs *date = tid(0);
4484    xs *dir  = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
4485    FILE *f;
4486
4487    if (mkdirx(dir) != -1) {
4488        xs *meta_fn = xs_fmt("%s/_META", dir);
4489
4490        if ((f = fopen(meta_fn, "w")) != NULL) {
4491            xs *j1 = xs_json_dumps(req, 4);
4492            xs *j2 = xs_json_dumps(headers, 4);
4493
4494            fprintf(f, "dir: %s\n", direction);
4495
4496            if (url)
4497                fprintf(f, "url: %s\n", url);
4498
4499            fprintf(f, "req: %s\n", j1);
4500            fprintf(f, "p_size: %d\n", p_size);
4501            fprintf(f, "status: %d\n", status);
4502            fprintf(f, "response: %s\n", j2);
4503            fprintf(f, "b_size: %d\n", b_size);
4504            fclose(f);
4505        }
4506
4507        if (p_size && payload) {
4508            xs *payload_fn = NULL;
4509            xs *payload_fn_raw = NULL;
4510            const char *v = xs_dict_get(req, "content-type");
4511
4512            if (v && xs_str_in(v, "json") != -1) {
4513                payload_fn = xs_fmt("%s/payload.json", dir);
4514
4515                if ((f = fopen(payload_fn, "w")) != NULL) {
4516                    xs *v1 = xs_json_loads(payload);
4517                    xs *j1 = NULL;
4518
4519                    if (v1 != NULL)
4520                        j1 = xs_json_dumps(v1, 4);
4521
4522                    if (j1 != NULL)
4523                        fwrite(j1, strlen(j1), 1, f);
4524                    else
4525                        fwrite(payload, p_size, 1, f);
4526
4527                    fclose(f);
4528                }
4529            }
4530
4531            payload_fn_raw = xs_fmt("%s/payload", dir);
4532
4533            if ((f = fopen(payload_fn_raw, "w")) != NULL) {
4534                fwrite(payload, p_size, 1, f);
4535                fclose(f);
4536            }
4537        }
4538
4539        if (b_size && body) {
4540            xs *body_fn = NULL;
4541            const char *v = xs_dict_get(headers, "content-type");
4542
4543            if (v && xs_str_in(v, "json") != -1) {
4544                body_fn = xs_fmt("%s/body.json", dir);
4545
4546                if ((f = fopen(body_fn, "w")) != NULL) {
4547                    xs *v1 = xs_json_loads(body);
4548                    xs *j1 = NULL;
4549
4550                    if (v1 != NULL)
4551                        j1 = xs_json_dumps(v1, 4);
4552
4553                    if (j1 != NULL)
4554                        fwrite(j1, strlen(j1), 1, f);
4555                    else
4556                        fwrite(body, b_size, 1, f);
4557
4558                    fclose(f);
4559                }
4560            }
4561            else {
4562                body_fn = xs_fmt("%s/body", dir);
4563
4564                if ((f = fopen(body_fn, "w")) != NULL) {
4565                    fwrite(body, b_size, 1, f);
4566                    fclose(f);
4567                }
4568            }
4569        }
4570    }
4571}
4572
4573
4574void srv_archive_error(const char *prefix, const xs_str *err,
4575                       const xs_dict *req, const xs_val *data)
4576/* archives an error */
4577{
4578    xs *ntid = tid(0);
4579    xs *fn   = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
4580    FILE *f;
4581
4582    if ((f = fopen(fn, "w")) != NULL) {
4583        fprintf(f, "Error: %s\n", err);
4584
4585        if (req) {
4586            fprintf(f, "Request headers:\n");
4587
4588            xs_json_dump(req, 4, f);
4589
4590            fprintf(f, "\n");
4591        }
4592
4593        if (data) {
4594            fprintf(f, "Data:\n");
4595
4596            if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
4597                xs_json_dump(data, 4, f);
4598            }
4599            else
4600                fprintf(f, "%s", data);
4601
4602            fprintf(f, "\n");
4603        }
4604
4605        fclose(f);
4606    }
4607}
4608
4609
4610void srv_archive_qitem(const char *prefix, xs_dict *q_item)
4611/* archives a q_item in the error folder */
4612{
4613    xs *ntid = tid(0);
4614    xs *fn   = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
4615    FILE *f;
4616
4617    if ((f = fopen(fn, "w")) != NULL) {
4618        xs_json_dump(q_item, 4, f);
4619        fclose(f);
4620    }
4621}
4622
4623
4624t_announcement *announcement(const double after)
4625/* returns announcement text or NULL if none exists or it is olde than "after" */
4626{
4627    static const long int MAX_SIZE = 2048;
4628    static t_announcement a = {
4629        .text = NULL,
4630        .timestamp = 0.0,
4631    };
4632    static xs_str *fn = NULL;
4633    if (fn == NULL)
4634        fn = xs_fmt("%s/announcement.txt", srv_basedir);
4635
4636    const double ts = mtime(fn);
4637
4638    /* file does not exist or other than what was requested */
4639    if (ts == 0.0 || ts <= after)
4640        return NULL;
4641
4642    /* nothing changed, just return the current announcement */
4643    if (a.text != NULL && ts <= a.timestamp)
4644        return &a;
4645
4646    /* read and store new announcement */
4647    FILE *f;
4648
4649    if ((f = fopen(fn, "r")) != NULL) {
4650        fseek (f, 0, SEEK_END);
4651        const long int length = ftell(f);
4652
4653        if (length > MAX_SIZE) {
4654            /* this is probably unintentional */
4655            srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
4656        }
4657        else
4658        if (length > 0) {
4659            fseek (f, 0, SEEK_SET);
4660            char *buffer = malloc(length + 1);
4661            if (buffer) {
4662                fread(buffer, 1, length, f);
4663                buffer[length] = '\0';
4664
4665                free(a.text);
4666                a.text = buffer;
4667                a.timestamp = ts;
4668            }
4669            else {
4670                srv_log("Error allocating memory for announcement");
4671            }
4672        }
4673        else {
4674            /* an empty file means no announcement */
4675            free(a.text);
4676            a.text = NULL;
4677            a.timestamp = 0.0;
4678        }
4679
4680        fclose (f);
4681    }
4682
4683    if (a.text != NULL)
4684        return &a;
4685
4686    return NULL;
4687}
4688
4689
4690xs_str *make_url(const char *href, const char *proxy, int by_token)
4691/* makes an URL, possibly including proxying */
4692{
4693    xs_str *url = NULL;
4694
4695    if (proxy && !xs_startswith(href, srv_baseurl)) {
4696        xs *p = NULL;
4697
4698        if (by_token) {
4699            xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, proxy);
4700            xs *tk = xs_md5_hex(tks, strlen(tks));
4701
4702            p = xs_fmt("%s/y/%s/", proxy, tk);
4703        }
4704        else
4705            p = xs_fmt("%s/x/", proxy);
4706
4707        url = xs_replace(href, "https:/" "/", p);
4708    }
4709    else
4710        url = xs_dup(href);
4711
4712    return url;
4713}
4714
4715
4716/** bad login throttle **/
4717
4718xs_str *_badlogin_fn(const char *addr)
4719{
4720    xs *md5 = xs_md5_hex(addr, strlen(addr));
4721    xs *dir = xs_fmt("%s/badlogin", srv_basedir);
4722
4723    mkdirx(dir);
4724
4725    return xs_fmt("%s/%s", dir, md5);
4726}
4727
4728
4729int _badlogin_read(const char *fn, int *failures)
4730/* reads a badlogin file */
4731{
4732    int ok = 0;
4733    FILE *f;
4734
4735    pthread_mutex_lock(&data_mutex);
4736
4737    if ((f = fopen(fn, "r")) != NULL) {
4738        xs *l = xs_readline(f);
4739        fclose(f);
4740
4741        if (sscanf(l, "%d", failures) == 1)
4742            ok = 1;
4743    }
4744
4745    pthread_mutex_unlock(&data_mutex);
4746
4747    return ok;
4748}
4749
4750
4751int badlogin_check(const char *user, const char *addr)
4752/* checks if this address is authorized to try a login */
4753{
4754    int valid = 1;
4755
4756    if (xs_type(addr) == XSTYPE_STRING) {
4757        xs *fn = _badlogin_fn(addr);
4758        double mt = mtime(fn);
4759
4760        if (mt > 0) {
4761            int badlogin_expire = xs_number_get(xs_dict_get_def(srv_config,
4762                                        "badlogin_expire", "300"));
4763
4764            mt += badlogin_expire;
4765
4766            /* if file is expired, delete and give pass */
4767            if (mt < time(NULL)) {
4768                srv_debug(1, xs_fmt("Login from %s for %s allowed again", addr, user));
4769                unlink(fn);
4770            }
4771            else {
4772                int failures;
4773
4774                if (_badlogin_read(fn, &failures)) {
4775                    int badlogin_max = xs_number_get(xs_dict_get_def(srv_config,
4776                                            "badlogin_retries", "5"));
4777
4778                    if (failures >= badlogin_max) {
4779                        valid = 0;
4780
4781                        xs *d = xs_str_iso_date((time_t) mt);
4782
4783                        srv_debug(1,
4784                            xs_fmt("Login from %s for %s forbidden until %s", addr, user, d));
4785                    }
4786                }
4787            }
4788        }
4789    }
4790
4791    return valid;
4792}
4793
4794
4795void badlogin_inc(const char *user, const char *addr)
4796/* increments a bad login from this address */
4797{
4798    if (xs_type(addr) == XSTYPE_STRING) {
4799        int failures = 0;
4800        xs *fn = _badlogin_fn(addr);
4801        FILE *f;
4802
4803        _badlogin_read(fn, &failures);
4804
4805        pthread_mutex_lock(&data_mutex);
4806
4807        if ((f = fopen(fn, "w")) != NULL) {
4808            failures++;
4809
4810            fprintf(f, "%d %s %s\n", failures, addr, user);
4811            fclose(f);
4812
4813            srv_log(xs_fmt("Registered %d login failure(s) from %s for %s", failures, addr, user));
4814        }
4815
4816        pthread_mutex_unlock(&data_mutex);
4817    }
4818}
4819
4820
4821/** language strings **/
4822
4823const char *lang_str(const char *str, const snac *user)
4824/* returns a translated string */
4825{
4826    const char *n_str = str;
4827
4828    if (user && xs_is_dict(user->lang) && xs_is_string(str)) {
4829        n_str = xs_dict_get(user->lang, str);
4830
4831        if (xs_is_null(n_str) || *n_str == '\0')
4832            n_str = str;
4833    }
4834
4835    return n_str;
4836}
4837
4838
4839/** integrity checks **/
4840
4841void data_fsck(void)
4842{
4843    xs *list = user_list();
4844    const char *uid;
4845
4846    xs_list_foreach(list, uid) {
4847        snac user;
4848
4849        if (!user_open(&user, uid))
4850            continue;
4851
4852        {
4853            /* iterate all private posts and check that non-public posts
4854               from this user are also linked into the public directory,
4855               to avoid the don't-fucking-delete-my-own-private-posts purge bug */
4856
4857            xs *priv_spec = xs_fmt("%s/private/""*.json", user.basedir);
4858            xs *posts = xs_glob(priv_spec, 0, 0);
4859            const char *priv_fn;
4860
4861            xs_list_foreach(posts, priv_fn) {
4862                xs *pub_fn = xs_replace(priv_fn, "/private/", "/public/");
4863
4864                /* already there? look no more */
4865                if (mtime(pub_fn))
4866                    continue;
4867
4868                /* read the post */
4869                FILE *f;
4870                if ((f = fopen(priv_fn, "r")) == NULL)
4871                    continue;
4872
4873                xs *post = xs_json_load(f);
4874                fclose(f);
4875
4876                if (!xs_is_dict(post))
4877                    continue;
4878
4879                const char *attr_to = get_atto(post);
4880
4881                if (!xs_is_string(attr_to) || strcmp(attr_to, user.actor) != 0) {
4882                    /* not from this user */
4883                    continue;
4884                }
4885
4886                /* link */
4887                snac_debug(&user, 1, xs_fmt("fsck: fixed missing link %s", xs_dict_get(post, "id")));
4888                link(priv_fn, pub_fn);
4889            }
4890        }
4891
4892        user_free(&user);
4893    }
4894}