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