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