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