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