2.52 data.c
   1/* snac - A simple, minimalistic ActivityPub instance */
   2/* copyright (c) 2022 - 2024 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
  14#include "snac.h"
  15
  16#include <time.h>
  17#include <sys/stat.h>
  18#include <sys/file.h>
  19#include <sys/time.h>
  20#include <fcntl.h>
  21#include <pthread.h>
  22
  23double disk_layout = 2.7;
  24
  25/* storage serializer */
  26pthread_mutex_t data_mutex = {0};
  27
  28int snac_upgrade(xs_str **error);
  29
  30
  31int srv_open(char *basedir, int auto_upgrade)
  32/* opens a server */
  33{
  34    int ret = 0;
  35    xs *cfg_file = NULL;
  36    FILE *f;
  37    xs_str *error = NULL;
  38
  39    pthread_mutex_init(&data_mutex, NULL);
  40
  41    srv_basedir = xs_str_new(basedir);
  42
  43    if (xs_endswith(srv_basedir, "/"))
  44        srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  45
  46    cfg_file = xs_fmt("%s/server.json", basedir);
  47
  48    if ((f = fopen(cfg_file, "r")) == NULL)
  49        error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  50    else {
  51        /* read full config file */
  52        srv_config = xs_json_load(f);
  53        fclose(f);
  54
  55        /* parse */
  56
  57        if (srv_config == NULL)
  58            error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  59        else {
  60            char *host;
  61            char *prefix;
  62            char *dbglvl;
  63            char *proto;
  64
  65            host   = xs_dict_get(srv_config, "host");
  66            prefix = xs_dict_get(srv_config, "prefix");
  67            dbglvl = xs_dict_get(srv_config, "dbglevel");
  68            proto  = xs_dict_get_def(srv_config, "protocol", "https");
  69
  70            if (host == NULL || prefix == NULL)
  71                error = xs_str_new("ERROR: cannot get server data");
  72            else {
  73                srv_baseurl = xs_fmt("%s:/" "/%s%s", proto, host, prefix);
  74
  75                dbglevel = (int) xs_number_get(dbglvl);
  76
  77                if ((dbglvl = getenv("DEBUG")) != NULL) {
  78                    dbglevel = atoi(dbglvl);
  79                    error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  80                }
  81
  82                if (auto_upgrade)
  83                    ret = snac_upgrade(&error);
  84                else {
  85                    if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
  86                        error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  87                    else
  88                        ret = 1;
  89                }
  90            }
  91
  92        }
  93    }
  94
  95    if (error != NULL)
  96        srv_log(error);
  97
  98    /* create the queue/ subdir, just in case */
  99    xs *qdir = xs_fmt("%s/queue", srv_basedir);
 100    mkdirx(qdir);
 101
 102    xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
 103    mkdirx(ibdir);
 104
 105    xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
 106    mkdirx(tmpdir);
 107
 108#ifdef __APPLE__
 109/* Apple uses st_atimespec instead of st_atim etc */
 110#define st_atim st_atimespec
 111#define st_ctim st_ctimespec
 112#define st_mtim st_mtimespec
 113#endif
 114
 115#ifdef __OpenBSD__
 116    char *v = xs_dict_get(srv_config, "disable_openbsd_security");
 117
 118    if (v && xs_type(v) == XSTYPE_TRUE) {
 119        srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
 120    }
 121    else {
 122        int smail = xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE;
 123
 124        srv_debug(1, xs_fmt("Calling unveil()"));
 125        unveil(basedir,                "rwc");
 126        unveil("/tmp",                 "rwc");
 127        unveil("/etc/resolv.conf",     "r");
 128        unveil("/etc/hosts",           "r");
 129        unveil("/etc/ssl/openssl.cnf", "r");
 130        unveil("/etc/ssl/cert.pem",    "r");
 131        unveil("/usr/share/zoneinfo",  "r");
 132
 133        if (smail)
 134            unveil("/usr/sbin/sendmail",   "x");
 135
 136        unveil(NULL,                   NULL);
 137        srv_debug(1, xs_fmt("Calling pledge()"));
 138
 139        if (smail)
 140            pledge("stdio rpath wpath cpath flock inet proc exec dns fattr", NULL);
 141        else
 142            pledge("stdio rpath wpath cpath flock inet proc dns fattr", NULL);
 143    }
 144#endif /* __OpenBSD__ */
 145
 146    /* read (and drop) emojis.json, possibly creating it */
 147    xs_free(emojis());
 148
 149    return ret;
 150}
 151
 152
 153void srv_free(void)
 154{
 155    xs_free(srv_basedir);
 156    xs_free(srv_config);
 157    xs_free(srv_baseurl);
 158
 159    pthread_mutex_destroy(&data_mutex);
 160}
 161
 162
 163void user_free(snac *snac)
 164/* frees a user snac */
 165{
 166    xs_free(snac->uid);
 167    xs_free(snac->basedir);
 168    xs_free(snac->config);
 169    xs_free(snac->config_o);
 170    xs_free(snac->key);
 171    xs_free(snac->links);
 172    xs_free(snac->actor);
 173    xs_free(snac->md5);
 174}
 175
 176
 177int user_open(snac *user, const char *uid)
 178/* opens a user */
 179{
 180    int ret = 0;
 181
 182    *user = (snac){0};
 183
 184    if (validate_uid(uid)) {
 185        xs *cfg_file = NULL;
 186        FILE *f;
 187
 188        xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
 189
 190        if (mtime(t) == 0.0) {
 191            /* user folder does not exist; try with a different case */
 192            xs *lcuid = xs_tolower_i(xs_dup(uid));
 193            xs *ulist = user_list();
 194            xs_list *p = ulist;
 195            xs_str *v;
 196
 197            while (xs_list_iter(&p, &v)) {
 198                xs *v2 = xs_tolower_i(xs_dup(v));
 199
 200                if (strcmp(lcuid, v2) == 0) {
 201                    user->uid = xs_dup(v);
 202                    break;
 203                }
 204            }
 205        }
 206        else
 207            user->uid = xs_str_new(uid);
 208
 209        if (user->uid == NULL)
 210            return ret;
 211
 212        user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
 213
 214        cfg_file = xs_fmt("%s/user.json", user->basedir);
 215
 216        if ((f = fopen(cfg_file, "r")) != NULL) {
 217            /* read full config file */
 218            user->config = xs_json_load(f);
 219            fclose(f);
 220
 221            if (user->config != NULL) {
 222                xs *key_file = xs_fmt("%s/key.json", user->basedir);
 223
 224                if ((f = fopen(key_file, "r")) != NULL) {
 225                    user->key = xs_json_load(f);
 226                    fclose(f);
 227
 228                    if (user->key != NULL) {
 229                        user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
 230                        user->md5   = xs_md5_hex(user->actor, strlen(user->actor));
 231
 232                        /* everything is ok right now */
 233                        ret = 1;
 234
 235                        /* does it have a configuration override? */
 236                        xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
 237                        if ((f = fopen(cfg_file_o, "r")) != NULL) {
 238                            user->config_o = xs_json_load(f);
 239                            fclose(f);
 240
 241                            if (user->config_o == NULL)
 242                                srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
 243                        }
 244
 245                        if (user->config_o == NULL)
 246                            user->config_o = xs_dict_new();
 247                    }
 248                    else
 249                        srv_log(xs_fmt("error parsing '%s'", key_file));
 250                }
 251                else
 252                    srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
 253            }
 254            else
 255                srv_log(xs_fmt("error parsing '%s'", cfg_file));
 256        }
 257        else
 258            srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
 259
 260        /* verified links */
 261        xs *links_file = xs_fmt("%s/links.json", user->basedir);
 262
 263        if ((f = fopen(links_file, "r")) != NULL) {
 264            user->links = xs_json_load(f);
 265            fclose(f);
 266        }
 267    }
 268    else
 269        srv_debug(1, xs_fmt("invalid user '%s'", uid));
 270
 271    if (!ret)
 272        user_free(user);
 273
 274    return ret;
 275}
 276
 277
 278xs_list *user_list(void)
 279/* returns the list of user ids */
 280{
 281    xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
 282    return xs_glob(spec, 1, 0);
 283}
 284
 285
 286int user_open_by_md5(snac *snac, const char *md5)
 287/* iterates all users searching by md5 */
 288{
 289    xs *ulist  = user_list();
 290    xs_list *p = ulist;
 291    xs_str *v;
 292
 293    while (xs_list_iter(&p, &v)) {
 294        user_open(snac, v);
 295
 296        if (strcmp(snac->md5, md5) == 0)
 297            return 1;
 298
 299        user_free(snac);
 300    }
 301
 302    return 0;
 303}
 304
 305
 306double mtime_nl(const char *fn, int *n_link)
 307/* returns the mtime and number of links of a file or directory, or 0.0 */
 308{
 309    struct stat st;
 310    double r = 0.0;
 311    int n = 0;
 312
 313    if (fn && stat(fn, &st) != -1) {
 314        r = (double) st.st_mtim.tv_sec;
 315        n = st.st_nlink;
 316    }
 317
 318    if (n_link)
 319        *n_link = n;
 320
 321    return r;
 322}
 323
 324
 325#define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
 326
 327double f_ctime(const char *fn)
 328/* returns the ctime of a file or directory, or 0.0 */
 329{
 330    struct stat st;
 331    double r = 0.0;
 332
 333    if (fn && stat(fn, &st) != -1) {
 334        /* return the lowest of ctime and mtime;
 335           there are operations that change the ctime, like link() */
 336        r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
 337    }
 338
 339    return r;
 340}
 341
 342
 343int is_md5_hex(const char *md5)
 344{
 345    return xs_is_hex(md5) && strlen(md5) == 32;
 346}
 347
 348
 349/** database 2.1+ **/
 350
 351/** indexes **/
 352
 353
 354int index_add_md5(const char *fn, const char *md5)
 355/* adds an md5 to an index */
 356{
 357    int status = 201; /* Created */
 358    FILE *f;
 359
 360    if (!is_md5_hex(md5)) {
 361        srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
 362        return 400;
 363    }
 364
 365    pthread_mutex_lock(&data_mutex);
 366
 367    if ((f = fopen(fn, "a")) != NULL) {
 368        flock(fileno(f), LOCK_EX);
 369
 370        /* ensure the position is at the end after getting the lock */
 371        fseek(f, 0, SEEK_END);
 372
 373        fprintf(f, "%s\n", md5);
 374        fclose(f);
 375    }
 376    else
 377        status = 500;
 378
 379    pthread_mutex_unlock(&data_mutex);
 380
 381    return status;
 382}
 383
 384
 385int index_add(const char *fn, const char *id)
 386/* adds an id to an index */
 387{
 388    xs *md5 = xs_md5_hex(id, strlen(id));
 389    return index_add_md5(fn, md5);
 390}
 391
 392
 393int index_del_md5(const char *fn, const char *md5)
 394/* deletes an md5 from an index */
 395{
 396    int status = 404;
 397    FILE *f;
 398
 399    pthread_mutex_lock(&data_mutex);
 400
 401    if ((f = fopen(fn, "r+")) != NULL) {
 402        char line[256];
 403
 404        while (fgets(line, sizeof(line), f) != NULL) {
 405            line[32] = '\0';
 406
 407            if (strcmp(line, md5) == 0) {
 408                /* found! just rewind, overwrite it with garbage
 409                   and an eventual call to index_gc() will clean it
 410                   [yes: this breaks index_len()] */
 411                fseek(f, -33, SEEK_CUR);
 412                fwrite("-", 1, 1, f);
 413                status = 200;
 414
 415                break;
 416            }
 417        }
 418
 419        fclose(f);
 420    }
 421    else
 422        status = 410;
 423
 424    pthread_mutex_unlock(&data_mutex);
 425
 426    return status;
 427}
 428
 429
 430int index_del(const char *fn, const char *id)
 431/* deletes an id from an index */
 432{
 433    xs *md5 = xs_md5_hex(id, strlen(id));
 434    return index_del_md5(fn, md5);
 435}
 436
 437
 438int index_gc(const char *fn)
 439/* garbage-collects an index, deleting objects that are not here */
 440{
 441    FILE *i, *o;
 442    int gc = -1;
 443
 444    pthread_mutex_lock(&data_mutex);
 445
 446    if ((i = fopen(fn, "r")) != NULL) {
 447        xs *nfn = xs_fmt("%s.new", fn);
 448        char line[256];
 449
 450        if ((o = fopen(nfn, "w")) != NULL) {
 451            gc = 0;
 452
 453            while (fgets(line, sizeof(line), i) != NULL) {
 454                line[32] = '\0';
 455
 456                if (line[0] != '-' && object_here_by_md5(line))
 457                    fprintf(o, "%s\n", line);
 458                else
 459                    gc++;
 460            }
 461
 462            fclose(o);
 463
 464            xs *ofn = xs_fmt("%s.bak", fn);
 465
 466            unlink(ofn);
 467            link(fn, ofn);
 468            rename(nfn, fn);
 469        }
 470
 471        fclose(i);
 472    }
 473
 474    pthread_mutex_unlock(&data_mutex);
 475
 476    return gc;
 477}
 478
 479
 480int index_in_md5(const char *fn, const char *md5)
 481/* checks if the md5 is already in the index */
 482{
 483    FILE *f;
 484    int ret = 0;
 485
 486    if ((f = fopen(fn, "r")) != NULL) {
 487        flock(fileno(f), LOCK_SH);
 488
 489        char line[256];
 490
 491        while (!ret && fgets(line, sizeof(line), f) != NULL) {
 492            line[32] = '\0';
 493
 494            if (strcmp(line, md5) == 0)
 495                ret = 1;
 496        }
 497
 498        fclose(f);
 499    }
 500
 501    return ret;
 502}
 503
 504
 505int index_in(const char *fn, const char *id)
 506/* checks if the object id is already in the index */
 507{
 508    xs *md5 = xs_md5_hex(id, strlen(id));
 509    return index_in_md5(fn, md5);
 510}
 511
 512
 513int index_first(const char *fn, char *line, int size)
 514/* reads the first entry of an index */
 515{
 516    FILE *f;
 517    int ret = 0;
 518
 519    if ((f = fopen(fn, "r")) != NULL) {
 520        flock(fileno(f), LOCK_SH);
 521
 522        if (fgets(line, size, f) != NULL) {
 523            line[32] = '\0';
 524            ret = 1;
 525        }
 526
 527        fclose(f);
 528    }
 529
 530    return ret;
 531}
 532
 533
 534int index_len(const char *fn)
 535/* returns the number of elements in an index */
 536{
 537    struct stat st;
 538    int len = 0;
 539
 540    if (stat(fn, &st) != -1)
 541        len = st.st_size / 33;
 542
 543    return len;
 544}
 545
 546
 547xs_list *index_list(const char *fn, int max)
 548/* returns an index as a list */
 549{
 550    xs_list *list = xs_list_new();
 551    FILE *f;
 552    int n = 0;
 553
 554    if ((f = fopen(fn, "r")) != NULL) {
 555        flock(fileno(f), LOCK_SH);
 556
 557        char line[256];
 558
 559        while (n < max && fgets(line, sizeof(line), f) != NULL) {
 560            if (line[0] != '-') {
 561                line[32] = '\0';
 562                list = xs_list_append(list, line);
 563                n++;
 564            }
 565        }
 566
 567        fclose(f);
 568    }
 569
 570    return list;
 571}
 572
 573
 574xs_list *index_list_desc(const char *fn, int skip, int show)
 575/* returns an index as a list, in reverse order */
 576{
 577    xs_list *list = xs_list_new();
 578    FILE *f;
 579    int n = 0;
 580
 581    if ((f = fopen(fn, "r")) != NULL) {
 582        flock(fileno(f), LOCK_SH);
 583
 584        char line[256];
 585
 586        /* move to the end minus one entry (or more, if skipping entries) */
 587        if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
 588            while (n < show && fgets(line, sizeof(line), f) != NULL) {
 589                if (line[0] != '-') {
 590                    line[32] = '\0';
 591                    list = xs_list_append(list, line);
 592                    n++;
 593                }
 594
 595                /* move backwards 2 entries */
 596                if (fseek(f, -66, SEEK_CUR) == -1)
 597                    break;
 598            }
 599        }
 600
 601        fclose(f);
 602    }
 603
 604    return list;
 605}
 606
 607
 608/** objects **/
 609
 610static xs_str *_object_fn_by_md5(const char *md5, const char *func)
 611{
 612    xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
 613    xs_str *ret;
 614    int ok = 1;
 615
 616    /* an object deleted from an index; fail but don't bark */
 617    if (md5[0] == '-')
 618        ok = 0;
 619    else
 620    if (!is_md5_hex(md5)) {
 621        srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
 622        ok = 0;
 623    }
 624
 625    if (ok) {
 626        mkdirx(bfn);
 627        ret = xs_fmt("%s/%s.json", bfn, md5);
 628    }
 629    else
 630        ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
 631
 632    return ret;
 633}
 634
 635
 636static xs_str *_object_fn(const char *id)
 637{
 638    xs *md5 = xs_md5_hex(id, strlen(id));
 639    return _object_fn_by_md5(md5, "_object_fn");
 640}
 641
 642
 643int object_here_by_md5(const char *id)
 644/* checks if an object is already downloaded */
 645{
 646    xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
 647    return mtime(fn) > 0.0;
 648}
 649
 650
 651int object_here(const char *id)
 652/* checks if an object is already downloaded */
 653{
 654    xs *fn = _object_fn(id);
 655    return mtime(fn) > 0.0;
 656}
 657
 658
 659int object_get_by_md5(const char *md5, xs_dict **obj)
 660/* returns a stored object, optionally of the requested type */
 661{
 662    int status = 404;
 663    xs *fn     = _object_fn_by_md5(md5, "object_get_by_md5");
 664    FILE *f;
 665
 666    if ((f = fopen(fn, "r")) != NULL) {
 667        *obj = xs_json_load(f);
 668        fclose(f);
 669
 670        if (*obj)
 671            status = 200;
 672    }
 673    else
 674        *obj = NULL;
 675
 676    return status;
 677}
 678
 679
 680int object_get(const char *id, xs_dict **obj)
 681/* returns a stored object, optionally of the requested type */
 682{
 683    xs *md5 = xs_md5_hex(id, strlen(id));
 684    return object_get_by_md5(md5, obj);
 685}
 686
 687
 688int _object_add(const char *id, const xs_dict *obj, int ow)
 689/* stores an object */
 690{
 691    int status = 201; /* Created */
 692    xs *fn     = _object_fn(id);
 693    FILE *f;
 694
 695    if (mtime(fn) > 0.0) {
 696        if (!ow) {
 697            /* object already here */
 698            srv_debug(1, xs_fmt("object_add object already here %s", id));
 699            return 204; /* No content */
 700        }
 701        else
 702            status = 200;
 703    }
 704
 705    if ((f = fopen(fn, "w")) != NULL) {
 706        flock(fileno(f), LOCK_EX);
 707
 708        xs_json_dump(obj, 4, f);
 709        fclose(f);
 710
 711        /* does this object has a parent? */
 712        char *in_reply_to = xs_dict_get(obj, "inReplyTo");
 713
 714        if (!xs_is_null(in_reply_to) && *in_reply_to) {
 715            /* update the children index of the parent */
 716            xs *c_idx = _object_fn(in_reply_to);
 717
 718            c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
 719
 720            if (!index_in(c_idx, id)) {
 721                index_add(c_idx, id);
 722                srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
 723            }
 724            else
 725                srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
 726
 727            /* create a one-element index with the parent */
 728            xs *p_idx = xs_replace(fn, ".json", "_p.idx");
 729
 730            if (mtime(p_idx) == 0.0) {
 731                index_add(p_idx, in_reply_to);
 732                srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
 733            }
 734        }
 735    }
 736    else {
 737        srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
 738        status = 500;
 739    }
 740
 741    srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
 742
 743    return status;
 744}
 745
 746
 747int object_add(const char *id, const xs_dict *obj)
 748/* stores an object */
 749{
 750    return _object_add(id, obj, 0);
 751}
 752
 753
 754int object_add_ow(const char *id, const xs_dict *obj)
 755/* stores an object (overwriting allowed) */
 756{
 757    return _object_add(id, obj, 1);
 758}
 759
 760
 761int object_del_by_md5(const char *md5)
 762/* deletes an object by its md5 */
 763{
 764    int status = 404;
 765    xs *fn     = _object_fn_by_md5(md5, "object_del_by_md5");
 766
 767    if (unlink(fn) != -1) {
 768        status = 200;
 769
 770        /* also delete associated indexes */
 771        xs *spec  = xs_dup(fn);
 772        spec      = xs_replace_i(spec, ".json", "*.idx");
 773        xs *files = xs_glob(spec, 0, 0);
 774        char *p, *v;
 775
 776        p = files;
 777        while (xs_list_iter(&p, &v)) {
 778            srv_debug(1, xs_fmt("object_del index %s", v));
 779            unlink(v);
 780        }
 781    }
 782
 783    srv_debug(1, xs_fmt("object_del %s %d", fn, status));
 784
 785    return status;
 786}
 787
 788
 789int object_del(const char *id)
 790/* deletes an object */
 791{
 792    xs *md5 = xs_md5_hex(id, strlen(id));
 793    return object_del_by_md5(md5);
 794}
 795
 796
 797int object_del_if_unref(const char *id)
 798/* deletes an object if its n_links < 2 */
 799{
 800    xs *fn = _object_fn(id);
 801    int n_links;
 802    int ret = 0;
 803
 804    if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
 805        ret = object_del(id);
 806
 807    return ret;
 808}
 809
 810
 811double object_ctime_by_md5(const char *md5)
 812{
 813    xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
 814    return f_ctime(fn);
 815}
 816
 817
 818double object_ctime(const char *id)
 819{
 820    xs *md5 = xs_md5_hex(id, strlen(id));
 821    return object_ctime_by_md5(md5);
 822}
 823
 824
 825double object_mtime_by_md5(const char *md5)
 826{
 827    xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
 828    return mtime(fn);
 829}
 830
 831
 832double object_mtime(const char *id)
 833{
 834    xs *md5 = xs_md5_hex(id, strlen(id));
 835    return object_mtime_by_md5(md5);
 836}
 837
 838
 839void object_touch(const char *id)
 840{
 841    xs *md5 = xs_md5_hex(id, strlen(id));
 842    xs *fn = _object_fn_by_md5(md5, "object_touch");
 843
 844    if (mtime(fn))
 845        utimes(fn, NULL);
 846}
 847
 848
 849xs_str *_object_index_fn(const char *id, const char *idxsfx)
 850/* returns the filename of an object's index */
 851{
 852    xs_str *fn = _object_fn(id);
 853    return xs_replace_i(fn, ".json", idxsfx);
 854}
 855
 856
 857int object_likes_len(const char *id)
 858/* returns the number of likes (without reading the index) */
 859{
 860    xs *fn = _object_index_fn(id, "_l.idx");
 861    return index_len(fn);
 862}
 863
 864
 865int object_announces_len(const char *id)
 866/* returns the number of announces (without reading the index) */
 867{
 868    xs *fn = _object_index_fn(id, "_a.idx");
 869    return index_len(fn);
 870}
 871
 872
 873xs_list *object_children(const char *id)
 874/* returns the list of an object's children */
 875{
 876    xs *fn = _object_index_fn(id, "_c.idx");
 877    return index_list(fn, XS_ALL);
 878}
 879
 880
 881xs_list *object_likes(const char *id)
 882{
 883    xs *fn = _object_index_fn(id, "_l.idx");
 884    return index_list(fn, XS_ALL);
 885}
 886
 887
 888xs_list *object_announces(const char *id)
 889{
 890    xs *fn = _object_index_fn(id, "_a.idx");
 891    return index_list(fn, XS_ALL);
 892}
 893
 894
 895int object_parent(const char *md5, char *buf, int size)
 896/* returns the object parent, if any */
 897{
 898    xs *fn = _object_fn_by_md5(md5, "object_parent");
 899
 900    fn = xs_replace_i(fn, ".json", "_p.idx");
 901    return index_first(fn, buf, size);
 902}
 903
 904
 905int object_admire(const char *id, const char *actor, int like)
 906/* actor likes or announces this object */
 907{
 908    int status = 200;
 909    xs *fn     = _object_fn(id);
 910
 911    fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
 912
 913    if (!index_in(fn, actor)) {
 914        status = index_add(fn, actor);
 915
 916        srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
 917    }
 918
 919    return status;
 920}
 921
 922
 923int object_unadmire(const char *id, const char *actor, int like)
 924/* actor no longer likes or announces this object */
 925{
 926    int status;
 927    xs *fn = _object_fn(id);
 928
 929    fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
 930
 931    status = index_del(fn, actor);
 932
 933    if (valid_status(status))
 934        index_gc(fn);
 935
 936    srv_debug(0,
 937        xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status));
 938
 939    return status;
 940}
 941
 942
 943int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
 944/* adds or deletes from a user cache */
 945{
 946    xs *ofn = _object_fn(id);
 947    xs *l   = xs_split(ofn, "/");
 948    xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
 949    xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
 950    int ret;
 951
 952    if (del) {
 953        ret = unlink(cfn);
 954        index_del(idx, id);
 955    }
 956    else {
 957        if ((ret = link(ofn, cfn)) != -1)
 958            index_add(idx, id);
 959    }
 960
 961    return ret;
 962}
 963
 964
 965int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
 966/* caches an object into a user cache */
 967{
 968    return _object_user_cache(snac, id, cachedir, 0);
 969}
 970
 971
 972int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
 973/* deletes an object from a user cache */
 974{
 975    return _object_user_cache(snac, id, cachedir, 1);
 976}
 977
 978
 979int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
 980/* checks if an object is stored in a cache */
 981{
 982    xs *md5 = xs_md5_hex(id, strlen(id));
 983    xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
 984
 985    return !!(mtime(cfn) != 0.0);
 986}
 987
 988
 989xs_list *object_user_cache_list(snac *snac, const char *cachedir, int max, int inv)
 990/* returns the objects in a cache as a list */
 991{
 992    xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
 993    return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
 994}
 995
 996
 997/** specialized functions **/
 998
 999/** followers **/
1000
1001int follower_add(snac *snac, const char *actor)
1002/* adds a follower */
1003{
1004    int ret = object_user_cache_add(snac, actor, "followers");
1005
1006    snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
1007
1008    return ret == -1 ? 500 : 200;
1009}
1010
1011
1012int follower_del(snac *snac, const char *actor)
1013/* deletes a follower */
1014{
1015    int ret = object_user_cache_del(snac, actor, "followers");
1016
1017    snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
1018
1019    return ret == -1 ? 404 : 200;
1020}
1021
1022
1023int follower_check(snac *snac, const char *actor)
1024/* checks if someone is a follower */
1025{
1026    return object_user_cache_in(snac, actor, "followers");
1027}
1028
1029
1030xs_list *follower_list(snac *snac)
1031/* returns the list of followers */
1032{
1033    xs *list       = object_user_cache_list(snac, "followers", XS_ALL, 0);
1034    xs_list *fwers = xs_list_new();
1035    char *p, *v;
1036
1037    /* resolve the list of md5 to be a list of actors */
1038    p = list;
1039    while (xs_list_iter(&p, &v)) {
1040        xs *a_obj = NULL;
1041
1042        if (valid_status(object_get_by_md5(v, &a_obj))) {
1043            const char *actor = xs_dict_get(a_obj, "id");
1044
1045            if (!xs_is_null(actor)) {
1046                /* check if the actor is still cached */
1047                xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
1048
1049                if (mtime(fn) > 0.0)
1050                    fwers = xs_list_append(fwers, actor);
1051            }
1052        }
1053    }
1054
1055    return fwers;
1056}
1057
1058
1059/** timeline **/
1060
1061double timeline_mtime(snac *snac)
1062{
1063    xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1064    return mtime(fn);
1065}
1066
1067
1068int timeline_touch(snac *snac)
1069/* changes the date of the timeline index */
1070{
1071    xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1072    return utimes(fn, NULL);
1073}
1074
1075
1076xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
1077/* get the filename of an entry by md5 from any timeline */
1078{
1079    xs_str *fn = NULL;
1080
1081    if (xs_is_hex(md5) && strlen(md5) == 32) {
1082        fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
1083
1084        if (mtime(fn) == 0.0) {
1085            fn = xs_free(fn);
1086            fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
1087
1088            if (mtime(fn) == 0.0)
1089                fn = xs_free(fn);
1090        }
1091    }
1092
1093    return fn;
1094}
1095
1096
1097int timeline_here(snac *snac, const char *md5)
1098/* checks if an object is in the user cache */
1099{
1100    xs *fn = timeline_fn_by_md5(snac, md5);
1101
1102    return !(fn == NULL);
1103}
1104
1105
1106int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1107/* gets a message from the timeline */
1108{
1109    int status = 404;
1110    FILE *f    = NULL;
1111
1112    xs *fn = timeline_fn_by_md5(snac, md5);
1113
1114    if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
1115        *msg = xs_json_load(f);
1116        fclose(f);
1117
1118        if (*msg != NULL)
1119            status = 200;
1120    }
1121
1122    return status;
1123}
1124
1125
1126int timeline_del(snac *snac, char *id)
1127/* deletes a message from the timeline */
1128{
1129    /* delete from the user's caches */
1130    object_user_cache_del(snac, id, "public");
1131    object_user_cache_del(snac, id, "private");
1132
1133    /* try to delete the object if it's not used elsewhere */
1134    return object_del_if_unref(id);
1135}
1136
1137
1138void timeline_update_indexes(snac *snac, const char *id)
1139/* updates the indexes */
1140{
1141    object_user_cache_add(snac, id, "private");
1142
1143    if (xs_startswith(id, snac->actor)) {
1144        xs *msg = NULL;
1145
1146        if (valid_status(object_get(id, &msg))) {
1147            /* if its ours and is public, also store in public */
1148            if (is_msg_public(msg)) {
1149                object_user_cache_add(snac, id, "public");
1150
1151                /* also add it to the instance public timeline */
1152                xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
1153                index_add(ipt, id);
1154            }
1155        }
1156    }
1157}
1158
1159
1160int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
1161/* adds a message to the timeline */
1162{
1163    int ret = object_add(id, o_msg);
1164    timeline_update_indexes(snac, id);
1165
1166    tag_index(id, o_msg);
1167
1168    list_distribute(snac, NULL, o_msg);
1169
1170    snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
1171
1172    return ret;
1173}
1174
1175
1176int timeline_admire(snac *snac, const char *id, const char *admirer, int like)
1177/* updates a timeline entry with a new admiration */
1178{
1179    /* if we are admiring this, add to both timelines */
1180    if (!like && strcmp(admirer, snac->actor) == 0) {
1181        object_user_cache_add(snac, id, "public");
1182        object_user_cache_add(snac, id, "private");
1183    }
1184
1185    int ret = object_admire(id, admirer, like);
1186
1187    snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
1188            like ? "Like" : "Announce", id, admirer));
1189
1190    return ret;
1191}
1192
1193
1194xs_list *timeline_top_level(snac *snac, xs_list *list)
1195/* returns the top level md5 entries from this index */
1196{
1197    xs_set seen;
1198    xs_list *p;
1199    xs_str *v;
1200
1201    xs_set_init(&seen);
1202
1203    p = list;
1204    while (xs_list_iter(&p, &v)) {
1205        char line[256] = "";
1206
1207        strncpy(line, v, sizeof(line));
1208
1209        for (;;) {
1210            char line2[256];
1211
1212            /* if it doesn't have a parent, use this */
1213            if (!object_parent(line, line2, sizeof(line2)))
1214                break;
1215
1216            /* well, there is a parent... but is it here? */
1217            if (!timeline_here(snac, line2))
1218                break;
1219
1220            /* it's here! try again with its own parent */
1221            strncpy(line, line2, sizeof(line));
1222        }
1223
1224        xs_set_add(&seen, line);
1225    }
1226
1227    return xs_set_result(&seen);
1228}
1229
1230
1231xs_list *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show)
1232/* returns a timeline (with all entries) */
1233{
1234    xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
1235
1236    return index_list_desc(idx, skip, show);
1237}
1238
1239
1240xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show)
1241/* returns a timeline (only top level entries) */
1242{
1243    int c_max;
1244
1245    /* maximum number of items in the timeline */
1246    c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
1247
1248    /* never more timeline entries than the configured maximum */
1249    if (show > c_max)
1250        show = c_max;
1251
1252    xs *list = timeline_simple_list(snac, idx_name, skip, show);
1253
1254    return timeline_top_level(snac, list);
1255}
1256
1257
1258xs_list *timeline_instance_list(int skip, int show)
1259/* returns the timeline for the full instance */
1260{
1261    xs *idx = xs_fmt("%s/public.idx", srv_basedir);
1262
1263    return index_list_desc(idx, skip, show);
1264}
1265
1266
1267/** following **/
1268
1269/* this needs special treatment and cannot use the object db as is,
1270   with a link to a cached author, because we need the Follow object
1271   in case we need to unfollow (Undo + original Follow) */
1272
1273xs_str *_following_fn(snac *snac, const char *actor)
1274{
1275    xs *md5 = xs_md5_hex(actor, strlen(actor));
1276    return xs_fmt("%s/following/%s.json", snac->basedir, md5);
1277}
1278
1279
1280int following_add(snac *snac, const char *actor, const xs_dict *msg)
1281/* adds to the following list */
1282{
1283    int ret = 201; /* created */
1284    xs *fn = _following_fn(snac, actor);
1285    FILE *f;
1286    xs *p_object = NULL;
1287
1288    if (valid_status(following_get(snac, actor, &p_object))) {
1289        /* object already exists; if it's of type Accept,
1290           the actor is already being followed and confirmed,
1291           so do nothing */
1292        char *type = xs_dict_get(p_object, "type");
1293
1294        if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1295            snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
1296            return 200;
1297        }
1298    }
1299
1300    if ((f = fopen(fn, "w")) != NULL) {
1301        xs_json_dump(msg, 4, f);
1302        fclose(f);
1303
1304        /* get the filename of the actor object */
1305        xs *actor_fn = _object_fn(actor);
1306
1307        /* increase its reference count */
1308        fn = xs_replace_i(fn, ".json", "_a.json");
1309        link(actor_fn, fn);
1310    }
1311    else
1312        ret = 500;
1313
1314    snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
1315
1316    return ret;
1317}
1318
1319
1320int following_del(snac *snac, const char *actor)
1321/* we're not following this actor any longer */
1322{
1323    xs *fn = _following_fn(snac, actor);
1324
1325    snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
1326
1327    unlink(fn);
1328
1329    /* also delete the reference to the author */
1330    fn = xs_replace_i(fn, ".json", "_a.json");
1331    unlink(fn);
1332
1333    return 200;
1334}
1335
1336
1337int following_check(snac *snac, const char *actor)
1338/* checks if we are following this actor */
1339{
1340    xs *fn = _following_fn(snac, actor);
1341
1342    return !!(mtime(fn) != 0.0);
1343}
1344
1345
1346int following_get(snac *snac, const char *actor, xs_dict **data)
1347/* returns the 'Follow' object */
1348{
1349    xs *fn = _following_fn(snac, actor);
1350    FILE *f;
1351    int status = 200;
1352
1353    if ((f = fopen(fn, "r")) != NULL) {
1354        *data = xs_json_load(f);
1355        fclose(f);
1356    }
1357    else
1358        status = 404;
1359
1360    return status;
1361}
1362
1363
1364xs_list *following_list(snac *snac)
1365/* returns the list of people being followed */
1366{
1367    xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
1368    xs *glist = xs_glob(spec, 0, 0);
1369    xs_list *p;
1370    xs_str *v;
1371    xs_list *list = xs_list_new();
1372
1373    /* iterate the list of files */
1374    p = glist;
1375    while (xs_list_iter(&p, &v)) {
1376        FILE *f;
1377
1378        /* load the follower data */
1379        if ((f = fopen(v, "r")) != NULL) {
1380            xs *o = xs_json_load(f);
1381            fclose(f);
1382
1383            if (o != NULL) {
1384                const char *type = xs_dict_get(o, "type");
1385
1386                if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1387                    const char *actor = xs_dict_get(o, "actor");
1388
1389                    if (!xs_is_null(actor)) {
1390                        list = xs_list_append(list, actor);
1391
1392                        /* check if there is a link to the actor object */
1393                        xs *v2 = xs_replace(v, ".json", "_a.json");
1394
1395                        if (mtime(v2) == 0.0) {
1396                            /* no; add a link to it */
1397                            xs *actor_fn = _object_fn(actor);
1398                            link(actor_fn, v2);
1399                        }
1400                    }
1401                }
1402            }
1403        }
1404    }
1405
1406    return list;
1407}
1408
1409
1410xs_str *_muted_fn(snac *snac, const char *actor)
1411{
1412    xs *md5 = xs_md5_hex(actor, strlen(actor));
1413    return xs_fmt("%s/muted/%s", snac->basedir, md5);
1414}
1415
1416
1417void mute(snac *snac, const char *actor)
1418/* mutes a moron */
1419{
1420    xs *fn = _muted_fn(snac, actor);
1421    FILE *f;
1422
1423    if ((f = fopen(fn, "w")) != NULL) {
1424        fprintf(f, "%s\n", actor);
1425        fclose(f);
1426
1427        snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
1428    }
1429}
1430
1431
1432void unmute(snac *snac, const char *actor)
1433/* actor is no longer a moron */
1434{
1435    xs *fn = _muted_fn(snac, actor);
1436
1437    unlink(fn);
1438
1439    snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
1440}
1441
1442
1443int is_muted(snac *snac, const char *actor)
1444/* check if someone is muted */
1445{
1446    xs *fn = _muted_fn(snac, actor);
1447
1448    return !!(mtime(fn) != 0.0);
1449}
1450
1451
1452/** pinning **/
1453
1454xs_str *_pinned_fn(snac *user, const char *id)
1455{
1456    xs *md5 = xs_md5_hex(id, strlen(id));
1457    return xs_fmt("%s/pinned/%s.json", user->basedir, md5);
1458}
1459
1460
1461int is_pinned(snac *user, const char *id)
1462/* returns true if this note is pinned */
1463{
1464    xs *fn = _pinned_fn(user, id);
1465    return !!(mtime(fn) != 0.0);
1466}
1467
1468
1469int is_pinned_by_md5(snac *user, const char *md5)
1470{
1471    xs *fn = xs_fmt("%s/pinned/%s.json", user->basedir, md5);
1472    return !!(mtime(fn) != 0.0);
1473}
1474
1475
1476int pin(snac *user, const char *id)
1477/* pins a message */
1478{
1479    int ret = -2;
1480
1481    if (xs_startswith(id, user->actor)) {
1482        if (is_pinned(user, id))
1483            ret = -3;
1484        else {
1485            /* create the subfolder, if it does not exist */
1486            xs *fn = xs_fmt("%s/pinned/", user->basedir);
1487            mkdirx(fn);
1488
1489            ret = object_user_cache_add(user, id, "pinned");
1490        }
1491    }
1492
1493    return ret;
1494}
1495
1496
1497int unpin(snac *user, const char *id)
1498/* unpin a message */
1499{
1500    int ret = object_user_cache_del(user, id, "pinned");
1501
1502    if (ret != -1) {
1503        /* delete from the index */
1504        xs *idx = xs_fmt("%s/pinned.idx", user->basedir);
1505        index_del(idx, id);
1506    }
1507
1508    return ret;
1509}
1510
1511
1512xs_list *pinned_list(snac *user)
1513/* return the lists of pinned posts */
1514{
1515    return object_user_cache_list(user, "pinned", XS_ALL, 1);
1516}
1517
1518
1519xs_str *_hidden_fn(snac *snac, const char *id)
1520{
1521    xs *md5 = xs_md5_hex(id, strlen(id));
1522    return xs_fmt("%s/hidden/%s", snac->basedir, md5);
1523}
1524
1525
1526void hide(snac *snac, const char *id)
1527/* hides a message tree */
1528{
1529    xs *fn = _hidden_fn(snac, id);
1530    FILE *f;
1531
1532    if ((f = fopen(fn, "w")) != NULL) {
1533        fprintf(f, "%s\n", id);
1534        fclose(f);
1535
1536        snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
1537
1538        /* hide all the children */
1539        xs *chld = object_children(id);
1540        char *p, *v;
1541
1542        p = chld;
1543        while (xs_list_iter(&p, &v)) {
1544            xs *co = NULL;
1545
1546            /* resolve to get the id */
1547            if (valid_status(object_get_by_md5(v, &co))) {
1548                if ((v = xs_dict_get(co, "id")) != NULL)
1549                    hide(snac, v);
1550            }
1551        }
1552    }
1553}
1554
1555
1556int is_hidden(snac *snac, const char *id)
1557/* check is id is hidden */
1558{
1559    xs *fn = _hidden_fn(snac, id);
1560
1561    return !!(mtime(fn) != 0.0);
1562}
1563
1564
1565int actor_add(const char *actor, xs_dict *msg)
1566/* adds an actor */
1567{
1568    return object_add_ow(actor, msg);
1569}
1570
1571
1572int actor_get(const char *actor, xs_dict **data)
1573/* returns an already downloaded actor */
1574{
1575    int status = 200;
1576    xs_dict *d = NULL;
1577
1578    if (xs_startswith(actor, srv_baseurl)) {
1579        /* it's a (possible) local user */
1580        xs *l = xs_split(actor, "/");
1581        const char *uid = xs_list_get(l, -1);
1582        snac user;
1583
1584        if (!xs_is_null(uid) && user_open(&user, uid)) {
1585            if (data)
1586                *data = msg_actor(&user);
1587
1588            user_free(&user);
1589            return 200;
1590        }
1591        else
1592            return 404;
1593    }
1594
1595    /* read the object */
1596    if (!valid_status(status = object_get(actor, &d))) {
1597        d = xs_free(d);
1598        return status;
1599    }
1600
1601    /* if the object is corrupted, discard it */
1602    if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
1603        srv_debug(1, xs_fmt("corrupted actor object %s", actor));
1604        d = xs_free(d);
1605        return 404;
1606    }
1607
1608    if (data)
1609        *data = d;
1610    else
1611        d = xs_free(d);
1612
1613    xs *fn = _object_fn(actor);
1614    double max_time;
1615
1616    /* maximum time for the actor data to be considered stale */
1617    max_time = 3600.0 * 36.0;
1618
1619    if (mtime(fn) + max_time < (double) time(NULL)) {
1620        /* actor data exists but also stinks */
1621        status = 205; /* "205: Reset Content" "110: Response Is Stale" */
1622    }
1623
1624    return status;
1625}
1626
1627
1628int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
1629/* gets an actor and requests a refresh if it's stale */
1630{
1631    int status = actor_get(actor, data);
1632
1633    if (status == 205 && user && !xs_startswith(actor, srv_baseurl))
1634        enqueue_actor_refresh(user, actor);
1635
1636    return status;
1637}
1638
1639
1640/** user limiting (announce blocks) **/
1641
1642int limited(snac *user, const char *id, int cmd)
1643/* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
1644{
1645    int ret = 0;
1646    xs *dir = xs_fmt("%s/limited", user->basedir);
1647    xs *md5 = xs_md5_hex(id, strlen(id));
1648    xs *fn  = xs_fmt("%s/%s", dir, md5);
1649
1650    switch (cmd) {
1651    case 0: /** check **/
1652        ret = !!(mtime(fn) > 0.0);
1653        break;
1654
1655    case 1: /** limit **/
1656        mkdirx(dir);
1657
1658        if (mtime(fn) > 0.0)
1659            ret = -1;
1660        else {
1661            FILE *f;
1662
1663            if ((f = fopen(fn, "w")) != NULL) {
1664                fprintf(f, "%s\n", id);
1665                fclose(f);
1666            }
1667            else
1668                ret = -2;
1669        }
1670        break;
1671
1672    case 2: /** unlimit **/
1673        if (mtime(fn) > 0.0)
1674            ret = unlink(fn);
1675        else
1676            ret = -1;
1677        break;
1678    }
1679
1680    return ret;
1681}
1682
1683
1684/** tag indexing **/
1685
1686void tag_index(const char *id, const xs_dict *obj)
1687/* update the tag indexes for this object */
1688{
1689    xs_list *tags = xs_dict_get(obj, "tag");
1690
1691    if (is_msg_public(obj) && xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
1692        xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
1693
1694        mkdirx(g_tag_dir);
1695
1696        xs_dict *v;
1697        while (xs_list_iter(&tags, &v)) {
1698            char *type = xs_dict_get(v, "type");
1699            char *name = xs_dict_get(v, "name");
1700
1701            if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
1702                while (*name == '#' || *name == '@')
1703                    name++;
1704
1705                if (*name == '\0')
1706                    continue;
1707
1708                name = xs_tolower_i(name);
1709
1710                xs *md5_tag   = xs_md5_hex(name, strlen(name));
1711                xs *tag_dir   = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
1712                mkdirx(tag_dir);
1713
1714                xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
1715                index_add(g_tag_idx, id);
1716
1717                FILE *f;
1718                xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
1719                if ((f = fopen(g_tag_name, "w")) != NULL) {
1720                    fprintf(f, "%s\n", name);
1721                    fclose(f);
1722                }
1723
1724                srv_debug(0, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
1725            }
1726        }
1727    }
1728}
1729
1730
1731xs_list *tag_search(char *tag, int skip, int show)
1732/* returns the list of posts tagged with tag */
1733{
1734    if (*tag == '#')
1735        tag++;
1736
1737    xs *lw_tag = xs_tolower_i(xs_dup(tag));
1738    xs *md5    = xs_md5_hex(lw_tag, strlen(lw_tag));
1739    xs *idx    = xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
1740
1741    return index_list_desc(idx, skip, show);
1742}
1743
1744
1745/** lists **/
1746
1747xs_val *list_maint(snac *user, const char *list, int op)
1748/* list maintenance */
1749{
1750    xs_val *l = NULL;
1751
1752    switch (op) {
1753    case 0: /** list of lists **/
1754        {
1755            FILE *f;
1756            xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
1757            xs *ls   = xs_glob(spec, 0, 0);
1758            int c = 0;
1759            char *v;
1760
1761            l = xs_list_new();
1762
1763            while (xs_list_next(ls, &v, &c)) {
1764                if ((f = fopen(v, "r")) != NULL) {
1765                    xs *title = xs_readline(f);
1766                    fclose(f);
1767
1768                    title = xs_strip_i(title);
1769
1770                    xs *v2 = xs_replace(v, ".id", "");
1771                    xs *l2 = xs_split(v2, "/");
1772
1773                    /* return [ list_id, list_title ] */
1774                    l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title));
1775                }
1776            }
1777        }
1778
1779        break;
1780
1781    case 1: /** create new list (list is the name) **/
1782        {
1783            xs *lol = list_maint(user, NULL, 0);
1784            int c = 0;
1785            xs_list *v;
1786            int add = 1;
1787
1788            /* check if this list name already exists */
1789            while (xs_list_next(lol, &v, &c)) {
1790                if (strcmp(xs_list_get(v, 1), list) == 0) {
1791                    add = 0;
1792                    break;
1793                }
1794            }
1795
1796            if (add) {
1797                FILE *f;
1798                xs *dir = xs_fmt("%s/list/", user->basedir);
1799                xs *id  = xs_fmt("%010x", time(NULL));
1800
1801                mkdirx(dir);
1802
1803                xs *fn = xs_fmt("%s%s.id", dir, id);
1804
1805                if ((f = fopen(fn, "w")) != NULL) {
1806                    fprintf(f, "%s\n", list);
1807                    fclose(f);
1808                }
1809
1810                l = xs_stock(XSTYPE_TRUE);
1811            }
1812            else
1813                l = xs_stock(XSTYPE_FALSE);
1814        }
1815
1816        break;
1817
1818    case 2: /** delete list (list is the id) **/
1819        {
1820            if (xs_is_hex(list)) {
1821                xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
1822                unlink(fn);
1823
1824                fn = xs_replace_i(fn, ".id", ".lst");
1825                unlink(fn);
1826
1827                fn = xs_replace_i(fn, ".lst", ".idx");
1828                unlink(fn);
1829            }
1830        }
1831
1832        break;
1833    }
1834
1835    return l;
1836}
1837
1838
1839xs_val *list_content(snac *user, const char *list, const char *actor_md5, int op)
1840/* list content management */
1841{
1842    xs_val *l = NULL;
1843
1844    if (!xs_is_hex(list))
1845        return NULL;
1846
1847    if (actor_md5 != NULL && !xs_is_hex(actor_md5))
1848        return NULL;
1849
1850    xs *fn = xs_fmt("%s/list/%s.lst", user->basedir, list);
1851
1852    switch (op) {
1853    case 0: /** list content **/
1854        l = index_list(fn, XS_ALL);
1855
1856        break;
1857
1858    case 1: /** append actor to list **/
1859        if (actor_md5 != NULL) {
1860            if (!index_in(fn, actor_md5))
1861                index_add_md5(fn, actor_md5);
1862        }
1863
1864        break;
1865
1866    case 2: /** delete actor from list **/
1867        if (actor_md5 != NULL)
1868            index_del_md5(fn, actor_md5);
1869
1870        break;
1871
1872    case 3: /** list timeline **/
1873        fn = xs_replace_i(fn, ".lst", ".idx");
1874
1875        l = index_list_desc(fn, 0, 2048);
1876
1877        break;
1878    }
1879
1880    return l;
1881}
1882
1883
1884void list_distribute(snac *user, const char *who, const xs_dict *post)
1885/* distributes the post to all appropriate lists */
1886{
1887    char *id = xs_dict_get(post, "id");
1888
1889    /* if who is not set, use the attributedTo in the message */
1890    if (xs_is_null(who))
1891        who = get_atto(post);
1892
1893    if (xs_type(who) == XSTYPE_STRING && xs_type(id) == XSTYPE_STRING) {
1894        xs *a_md5 = xs_md5_hex(who, strlen(who));
1895        xs *i_md5 = xs_md5_hex(id, strlen(id));
1896        xs *spec  = xs_fmt("%s/list/" "*.lst", user->basedir);
1897        xs *ls    = xs_glob(spec, 0, 0);
1898        int c = 0;
1899        char *v;
1900
1901        while (xs_list_next(ls, &v, &c)) {
1902            /* is the actor in this list? */
1903            if (index_in_md5(v, a_md5)) {
1904                /* it is; add post md5 to its timeline */
1905                xs *idx = xs_replace(v, ".lst", ".idx");
1906                index_add_md5(idx, i_md5);
1907
1908                snac_debug(user, 1, xs_fmt("listed post %s in %s", id, idx));
1909            }
1910        }
1911    }
1912}
1913
1914
1915/** static data **/
1916
1917static int _load_raw_file(const char *fn, xs_val **data, int *size,
1918                        const char *inm, xs_str **etag)
1919/* loads a cached file */
1920{
1921    int status = 404;
1922
1923    if (fn) {
1924        double tm = mtime(fn);
1925
1926        if (tm > 0.0) {
1927            /* file exists; build the etag */
1928            xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
1929
1930            /* if if-none-match is set, check if it's the same */
1931            if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
1932                /* client has the newest version */
1933                status = 304;
1934            }
1935            else {
1936                /* newer or never downloaded; read the full file */
1937                FILE *f;
1938
1939                if ((f = fopen(fn, "rb")) != NULL) {
1940                    *size = XS_ALL;
1941                    *data = xs_read(f, size);
1942                    fclose(f);
1943
1944                    status = 200;
1945                }
1946            }
1947
1948            /* if caller wants the etag, return it */
1949            if (etag != NULL)
1950                *etag = xs_dup(e);
1951
1952            srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
1953        }
1954    }
1955
1956    return status;
1957}
1958
1959
1960xs_str *_static_fn(snac *snac, const char *id)
1961/* gets the filename for a static file */
1962{
1963    if (strchr(id, '/'))
1964        return NULL;
1965    else
1966        return xs_fmt("%s/static/%s", snac->basedir, id);
1967}
1968
1969
1970int static_get(snac *snac, const char *id, xs_val **data, int *size,
1971                const char *inm, xs_str **etag)
1972/* returns static content */
1973{
1974    xs *fn = _static_fn(snac, id);
1975
1976    return _load_raw_file(fn, data, size, inm, etag);
1977}
1978
1979
1980void static_put(snac *snac, const char *id, const char *data, int size)
1981/* writes status content */
1982{
1983    xs *fn = _static_fn(snac, id);
1984    FILE *f;
1985
1986    if (fn && (f = fopen(fn, "wb")) != NULL) {
1987        fwrite(data, size, 1, f);
1988        fclose(f);
1989    }
1990}
1991
1992
1993void static_put_meta(snac *snac, const char *id, const char *str)
1994/* puts metadata (i.e. a media description string) to id */
1995{
1996    xs *fn = _static_fn(snac, id);
1997
1998    if (fn) {
1999        fn = xs_str_cat(fn, ".txt");
2000        FILE *f;
2001
2002        if ((f = fopen(fn, "w")) != NULL) {
2003            fprintf(f, "%s\n", str);
2004            fclose(f);
2005        }
2006    }
2007}
2008
2009
2010xs_str *static_get_meta(snac *snac, const char *id)
2011/* gets metadata from a media */
2012{
2013    xs *fn    = _static_fn(snac, id);
2014    xs_str *r = NULL;
2015
2016    if (fn) {
2017        fn = xs_str_cat(fn, ".txt");
2018        FILE *f;
2019
2020        if ((f = fopen(fn, "r")) != NULL) {
2021            r = xs_strip_i(xs_readline(f));
2022            fclose(f);
2023        }
2024    }
2025    else
2026        r = xs_str_new("");
2027
2028    return r;
2029}
2030
2031
2032/** history **/
2033
2034xs_str *_history_fn(snac *snac, const char *id)
2035/* gets the filename for the history */
2036{
2037    if (strchr(id, '/'))
2038        return NULL;
2039    else
2040        return xs_fmt("%s/history/%s", snac->basedir, id);
2041}
2042
2043
2044double history_mtime(snac *snac, const char *id)
2045{
2046    double t = 0.0;
2047    xs *fn = _history_fn(snac, id);
2048
2049    if (fn != NULL)
2050        t = mtime(fn);
2051
2052    return t;
2053}
2054
2055
2056void history_add(snac *snac, const char *id, const char *content, int size,
2057                    xs_str **etag)
2058/* adds something to the history */
2059{
2060    xs *fn = _history_fn(snac, id);
2061    FILE *f;
2062
2063    if (fn && (f = fopen(fn, "w")) != NULL) {
2064        fwrite(content, size, 1, f);
2065        fclose(f);
2066
2067        if (etag) {
2068            double tm = mtime(fn);
2069            *etag = xs_fmt("W/\"snac-%.0lf\"", tm);
2070        }
2071    }
2072}
2073
2074
2075int history_get(snac *snac, const char *id, xs_str **content, int *size,
2076                const char *inm, xs_str **etag)
2077{
2078    xs *fn = _history_fn(snac, id);
2079
2080    return _load_raw_file(fn, content, size, inm, etag);
2081}
2082
2083
2084int history_del(snac *snac, const char *id)
2085{
2086    xs *fn = _history_fn(snac, id);
2087
2088    if (fn)
2089        return unlink(fn);
2090    else
2091        return -1;
2092}
2093
2094
2095xs_list *history_list(snac *snac)
2096{
2097    xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
2098
2099    return xs_glob(spec, 1, 1);
2100}
2101
2102
2103void lastlog_write(snac *snac, const char *source)
2104/* writes the last time the user logged in */
2105{
2106    xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
2107    FILE *f;
2108
2109    if ((f = fopen(fn, "w")) != NULL) {
2110        fprintf(f, "%lf %s\n", ftime(), source);
2111        fclose(f);
2112    }
2113}
2114
2115
2116/** inbox collection **/
2117
2118void inbox_add(const char *inbox)
2119/* collects a shared inbox */
2120{
2121    /* don't collect ourselves */
2122    if (xs_startswith(inbox, srv_baseurl))
2123        return;
2124
2125    xs *md5 = xs_md5_hex(inbox, strlen(inbox));
2126    xs *fn  = xs_fmt("%s/inbox/%s", srv_basedir, md5);
2127    FILE *f;
2128
2129    if ((f = fopen(fn, "w")) != NULL) {
2130        fprintf(f, "%s\n", inbox);
2131        fclose(f);
2132    }
2133}
2134
2135
2136void inbox_add_by_actor(const xs_dict *actor)
2137/* collects an actor's shared inbox, if it has one */
2138{
2139    char *v;
2140
2141    if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
2142        !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
2143        /* only collect this inbox if its instance is not blocked */
2144        if (!is_instance_blocked(v))
2145            inbox_add(v);
2146    }
2147}
2148
2149
2150xs_list *inbox_list(void)
2151/* returns the collected inboxes as a list */
2152{
2153    xs_list *ibl = xs_list_new();
2154    xs *spec     = xs_fmt("%s/inbox/" "*", srv_basedir);
2155    xs *files    = xs_glob(spec, 0, 0);
2156    xs_list *p   = files;
2157    xs_val *v;
2158
2159    while (xs_list_iter(&p, &v)) {
2160        FILE *f;
2161
2162        if ((f = fopen(v, "r")) != NULL) {
2163            xs *line = xs_readline(f);
2164
2165            if (line) {
2166                line = xs_strip_i(line);
2167                ibl  = xs_list_append(ibl, line);
2168            }
2169
2170            fclose(f);
2171        }
2172    }
2173
2174    return ibl;
2175}
2176
2177
2178/** instance-wide operations **/
2179
2180xs_str *_instance_block_fn(const char *instance)
2181{
2182    xs *s   = xs_replace(instance, "http:/" "/", "");
2183    xs *s1  = xs_replace(s, "https:/" "/", "");
2184    xs *l   = xs_split(s1, "/");
2185    char *p = xs_list_get(l, 0);
2186    xs *md5 = xs_md5_hex(p, strlen(p));
2187
2188    return xs_fmt("%s/block/%s", srv_basedir, md5);
2189}
2190
2191
2192int is_instance_blocked(const char *instance)
2193{
2194    xs *fn = _instance_block_fn(instance);
2195
2196    return !!(mtime(fn) != 0.0);
2197}
2198
2199
2200int instance_block(const char *instance)
2201/* blocks a full instance */
2202{
2203    int ret;
2204
2205    /* create the subdir */
2206    xs *dir = xs_fmt("%s/block/", srv_basedir);
2207    mkdirx(dir);
2208
2209    if (!is_instance_blocked(instance)) {
2210        xs *fn = _instance_block_fn(instance);
2211        FILE *f;
2212
2213        if ((f = fopen(fn, "w")) != NULL) {
2214            fprintf(f, "%s\n", instance);
2215            fclose(f);
2216
2217            ret = 0;
2218        }
2219        else
2220            ret = -1;
2221    }
2222    else
2223        ret = -2;
2224
2225    return ret;
2226}
2227
2228
2229int instance_unblock(const char *instance)
2230/* unblocks a full instance */
2231{
2232    int ret;
2233
2234    if (is_instance_blocked(instance)) {
2235        xs *fn = _instance_block_fn(instance);
2236        ret = unlink(fn);
2237    }
2238    else
2239        ret = -2;
2240
2241    return ret;
2242}
2243
2244
2245/** content filtering **/
2246
2247int content_check(const char *file, const xs_dict *msg)
2248/* checks if a message's content matches any of the regexes in file */
2249/* file format: one regex per line */
2250{
2251    xs *fn = xs_fmt("%s/%s", srv_basedir, file);
2252    FILE *f;
2253    int r = 0;
2254    char *v = xs_dict_get(msg, "content");
2255
2256    if (xs_type(v) == XSTYPE_STRING && *v) {
2257        if ((f = fopen(fn, "r")) != NULL) {
2258            srv_debug(1, xs_fmt("content_check: loading regexes from %s", fn));
2259
2260            /* massage content (strip HTML tags, etc.) */
2261            xs *c = xs_regex_replace(v, "<[^>]+>", " ");
2262            c = xs_regex_replace_i(c, " {2,}", " ");
2263            c = xs_tolower_i(c);
2264
2265            while (!r && !feof(f)) {
2266                xs *rx = xs_strip_i(xs_readline(f));
2267
2268                if (*rx) {
2269                    xs *l = xs_regex_select_n(c, rx, 1);
2270
2271                    if (xs_list_len(l)) {
2272                        srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
2273                        r = 1;
2274                    }
2275                }
2276            }
2277
2278            fclose(f);
2279        }
2280    }
2281
2282    return r;
2283}
2284
2285
2286/** notifications **/
2287
2288xs_str *notify_check_time(snac *snac, int reset)
2289/* gets or resets the latest notification check time */
2290{
2291    xs_str *t = NULL;
2292    xs *fn    = xs_fmt("%s/notifydate.txt", snac->basedir);
2293    FILE *f;
2294
2295    if (reset) {
2296        if ((f = fopen(fn, "w")) != NULL) {
2297            t = tid(0);
2298            fprintf(f, "%s\n", t);
2299            fclose(f);
2300        }
2301    }
2302    else {
2303        if ((f = fopen(fn, "r")) != NULL) {
2304            t = xs_readline(f);
2305            fclose(f);
2306        }
2307        else
2308            /* never set before */
2309            t = xs_fmt("%16.6f", 0.0);
2310    }
2311
2312    return t;
2313}
2314
2315
2316void notify_add(snac *snac, const char *type, const char *utype,
2317                const char *actor, const char *objid)
2318/* adds a new notification */
2319{
2320    xs *ntid = tid(0);
2321    xs *fn   = xs_fmt("%s/notify/", snac->basedir);
2322    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
2323    FILE *f;
2324
2325    /* create the directory */
2326    mkdirx(fn);
2327    fn = xs_str_cat(fn, ntid);
2328    fn = xs_str_cat(fn, ".json");
2329
2330    xs *noti = xs_dict_new();
2331
2332    noti = xs_dict_append(noti, "id",    ntid);
2333    noti = xs_dict_append(noti, "type",  type);
2334    noti = xs_dict_append(noti, "utype", utype);
2335    noti = xs_dict_append(noti, "actor", actor);
2336    noti = xs_dict_append(noti, "date",  date);
2337
2338    if (!xs_is_null(objid))
2339        noti = xs_dict_append(noti, "objid", objid);
2340
2341    if ((f = fopen(fn, "w")) != NULL) {
2342        xs_json_dump(noti, 4, f);
2343        fclose(f);
2344    }
2345
2346    /* add it to the index if it already exists */
2347    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
2348
2349    if (mtime(idx) != 0.0) {
2350        pthread_mutex_lock(&data_mutex);
2351
2352        if ((f = fopen(idx, "a")) != NULL) {
2353            fprintf(f, "%-32s\n", ntid);
2354            fclose(f);
2355        }
2356
2357        pthread_mutex_unlock(&data_mutex);
2358    }
2359}
2360
2361
2362xs_dict *notify_get(snac *snac, const char *id)
2363/* gets a notification */
2364{
2365    /* base file */
2366    xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
2367
2368    /* strip spaces and add extension */
2369    fn = xs_strip_i(fn);
2370    fn = xs_str_cat(fn, ".json");
2371
2372    FILE *f;
2373    xs_dict *out = NULL;
2374
2375    if ((f = fopen(fn, "r")) != NULL) {
2376        out = xs_json_load(f);
2377        fclose(f);
2378    }
2379
2380    return out;
2381}
2382
2383
2384xs_list *notify_list(snac *snac, int skip, int show)
2385/* returns a list of notification ids */
2386{
2387    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
2388
2389    if (mtime(idx) == 0.0) {
2390        /* create the index from scratch */
2391        FILE *f;
2392
2393        pthread_mutex_lock(&data_mutex);
2394
2395        if ((f = fopen(idx, "w")) != NULL) {
2396            xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
2397            xs *lst  = xs_glob(spec, 1, 0);
2398            xs_list *p = lst;
2399            char *v;
2400
2401            while (xs_list_iter(&p, &v)) {
2402                char *p = strrchr(v, '.');
2403                if (p) {
2404                    *p = '\0';
2405                    fprintf(f, "%-32s\n", v);
2406                }
2407            }
2408
2409            fclose(f);
2410        }
2411
2412        pthread_mutex_unlock(&data_mutex);
2413    }
2414
2415    return index_list_desc(idx, skip, show);
2416}
2417
2418
2419int notify_new_num(snac *snac)
2420/* counts the number of new notifications */
2421{
2422    xs *t = notify_check_time(snac, 0);
2423    xs *lst = notify_list(snac, 0, XS_ALL);
2424    int cnt = 0;
2425
2426    xs_list *p = lst;
2427    xs_str *v;
2428
2429    while (xs_list_iter(&p, &v)) {
2430        xs *id = xs_strip_i(xs_dup(v));
2431
2432        /* old? count no more */
2433        if (strcmp(id, t) < 0)
2434            break;
2435
2436        cnt++;
2437    }
2438
2439    return cnt;
2440}
2441
2442
2443void notify_clear(snac *snac)
2444/* clears all notifications */
2445{
2446    xs *spec   = xs_fmt("%s/notify/" "*", snac->basedir);
2447    xs *lst    = xs_glob(spec, 0, 0);
2448    xs_list *p = lst;
2449    xs_str *v;
2450
2451    while (xs_list_iter(&p, &v))
2452        unlink(v);
2453
2454    xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
2455
2456    if (mtime(idx) != 0.0) {
2457        pthread_mutex_lock(&data_mutex);
2458        truncate(idx, 0);
2459        pthread_mutex_unlock(&data_mutex);
2460    }
2461}
2462
2463
2464/** the queue **/
2465
2466static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
2467/* writes safely to the queue */
2468{
2469    xs *tfn = xs_fmt("%s.tmp", fn);
2470    FILE *f;
2471
2472    if ((f = fopen(tfn, "w")) != NULL) {
2473        xs_json_dump(msg, 4, f);
2474        fclose(f);
2475
2476        rename(tfn, fn);
2477    }
2478
2479    return msg;
2480}
2481
2482
2483static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
2484/* creates a queue message */
2485{
2486    int qrt  = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
2487    xs *ntid = tid(retries * 60 * qrt);
2488    xs *rn   = xs_number_new(retries);
2489
2490    xs_dict *qmsg = xs_dict_new();
2491
2492    qmsg = xs_dict_append(qmsg, "type",    type);
2493    qmsg = xs_dict_append(qmsg, "message", msg);
2494    qmsg = xs_dict_append(qmsg, "retries", rn);
2495    qmsg = xs_dict_append(qmsg, "ntid",    ntid);
2496
2497    return qmsg;
2498}
2499
2500
2501void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
2502/* enqueues an input message */
2503{
2504    xs *qmsg   = _new_qmsg("input", msg, retries);
2505    char *ntid = xs_dict_get(qmsg, "ntid");
2506    xs *fn     = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
2507
2508    qmsg = xs_dict_append(qmsg, "req", req);
2509
2510    qmsg = _enqueue_put(fn, qmsg);
2511
2512    snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
2513}
2514
2515
2516void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
2517/* enqueues an input message from the shared input */
2518{
2519    xs *qmsg   = _new_qmsg("input", msg, retries);
2520    char *ntid = xs_dict_get(qmsg, "ntid");
2521    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
2522
2523    qmsg = xs_dict_append(qmsg, "req", req);
2524
2525    qmsg = _enqueue_put(fn, qmsg);
2526
2527    srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
2528}
2529
2530
2531void enqueue_output_raw(const char *keyid, const char *seckey,
2532                        xs_dict *msg, xs_str *inbox, int retries, int p_status)
2533/* enqueues an output message to an inbox */
2534{
2535    xs *qmsg   = _new_qmsg("output", msg, retries);
2536    char *ntid = xs_dict_get(qmsg, "ntid");
2537    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
2538
2539    xs *ns = xs_number_new(p_status);
2540    qmsg = xs_dict_append(qmsg, "p_status", ns);
2541
2542    qmsg = xs_dict_append(qmsg, "inbox",  inbox);
2543    qmsg = xs_dict_append(qmsg, "keyid",  keyid);
2544    qmsg = xs_dict_append(qmsg, "seckey", seckey);
2545
2546    /* if it's to be sent right now, bypass the disk queue and post the job */
2547    if (retries == 0 && p_state != NULL)
2548        job_post(qmsg, 0);
2549    else {
2550        qmsg = _enqueue_put(fn, qmsg);
2551        srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
2552    }
2553}
2554
2555
2556void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries, int p_status)
2557/* enqueues an output message to an inbox */
2558{
2559    if (xs_startswith(inbox, snac->actor)) {
2560        snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
2561        return;
2562    }
2563
2564    char *seckey = xs_dict_get(snac->key, "secret");
2565
2566    enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
2567}
2568
2569
2570void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries)
2571/* enqueues an output message for an actor */
2572{
2573    xs *inbox = get_actor_inbox(actor);
2574
2575    if (!xs_is_null(inbox))
2576        enqueue_output(snac, msg, inbox, retries, 0);
2577    else
2578        snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
2579}
2580
2581
2582void enqueue_email(xs_str *msg, int retries)
2583/* enqueues an email message to be sent */
2584{
2585    xs *qmsg   = _new_qmsg("email", msg, retries);
2586    char *ntid = xs_dict_get(qmsg, "ntid");
2587    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
2588
2589    qmsg = _enqueue_put(fn, qmsg);
2590
2591    srv_debug(1, xs_fmt("enqueue_email %d", retries));
2592}
2593
2594
2595void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
2596/* enqueues a message to be sent via Telegram */
2597{
2598    xs *qmsg   = _new_qmsg("telegram", msg, 0);
2599    char *ntid = xs_dict_get(qmsg, "ntid");
2600    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
2601
2602    qmsg = xs_dict_append(qmsg, "bot",      bot);
2603    qmsg = xs_dict_append(qmsg, "chat_id",  chat_id);
2604
2605    qmsg = _enqueue_put(fn, qmsg);
2606
2607    srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
2608}
2609
2610void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
2611/* enqueues a message to be sent via ntfy */
2612{
2613    xs *qmsg   = _new_qmsg("ntfy", msg, 0);
2614    char *ntid = xs_dict_get(qmsg, "ntid");
2615    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
2616
2617    qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
2618    qmsg = xs_dict_append(qmsg, "ntfy_token",  ntfy_token);
2619
2620
2621    qmsg = _enqueue_put(fn, qmsg);
2622
2623    srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
2624}
2625
2626void enqueue_message(snac *snac, const xs_dict *msg)
2627/* enqueues an output message */
2628{
2629    xs *qmsg   = _new_qmsg("message", msg, 0);
2630    char *ntid = xs_dict_get(qmsg, "ntid");
2631    xs *fn     = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
2632
2633    qmsg = _enqueue_put(fn, qmsg);
2634
2635    snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
2636}
2637
2638
2639void enqueue_close_question(snac *user, const char *id, int end_secs)
2640/* enqueues the closing of a question */
2641{
2642    xs *qmsg = _new_qmsg("close_question", id, 0);
2643    xs *ntid = tid(end_secs);
2644    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
2645
2646    qmsg = xs_dict_set(qmsg, "ntid", ntid);
2647
2648    qmsg = _enqueue_put(fn, qmsg);
2649
2650    snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
2651}
2652
2653
2654void enqueue_object_request(snac *user, const char *id, int forward_secs)
2655/* enqueues the request of an object in the future */
2656{
2657    xs *qmsg = _new_qmsg("object_request", id, 0);
2658    xs *ntid = tid(forward_secs);
2659    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
2660
2661    qmsg = xs_dict_set(qmsg, "ntid", ntid);
2662
2663    qmsg = _enqueue_put(fn, qmsg);
2664
2665    snac_debug(user, 0, xs_fmt("enqueue_object_request %s %d", id, forward_secs));
2666}
2667
2668
2669void enqueue_verify_links(snac *user)
2670/* enqueues a link verification */
2671{
2672    xs *qmsg   = _new_qmsg("verify_links", "", 0);
2673    char *ntid = xs_dict_get(qmsg, "ntid");
2674    xs *fn     = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
2675
2676    qmsg = _enqueue_put(fn, qmsg);
2677
2678    snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
2679}
2680
2681
2682void enqueue_actor_refresh(snac *user, const char *actor)
2683/* enqueues an actor refresh */
2684{
2685    xs *qmsg   = _new_qmsg("actor_refresh", "", 0);
2686    char *ntid = xs_dict_get(qmsg, "ntid");
2687    xs *fn     = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
2688
2689    qmsg = xs_dict_append(qmsg, "actor", actor);
2690
2691    qmsg = _enqueue_put(fn, qmsg);
2692
2693    snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s", actor));
2694}
2695
2696
2697void enqueue_request_replies(snac *user, const char *id)
2698/* enqueues a request for the replies of a message */
2699{
2700    /* test first if this precise request is already in the queue */
2701    xs *queue = user_queue(user);
2702    xs_list *p = queue;
2703    xs_str *v;
2704
2705    while (xs_list_iter(&p, &v)) {
2706        xs *q_item = queue_get(v);
2707
2708        if (q_item != NULL) {
2709            const char *type = xs_dict_get(q_item, "type");
2710            const char *msg  = xs_dict_get(q_item, "message");
2711
2712            if (type && msg && strcmp(type, "request_replies") == 0 && strcmp(msg, id) == 0) {
2713                /* don't requeue */
2714                snac_debug(user, 1, xs_fmt("enqueue_request_replies already here %s", id));
2715                return;
2716            }
2717        }
2718    }
2719
2720    /* not there; enqueue the request with a small delay */
2721    xs *qmsg = _new_qmsg("request_replies", id, 0);
2722    xs *ntid = tid(10);
2723    xs *fn   = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
2724
2725    qmsg = xs_dict_set(qmsg, "ntid", ntid);
2726
2727    qmsg = _enqueue_put(fn, qmsg);
2728
2729    snac_debug(user, 2, xs_fmt("enqueue_request_replies %s", id));
2730}
2731
2732
2733int was_question_voted(snac *user, const char *id)
2734/* returns true if the user voted in this poll */
2735{
2736    xs *children = object_children(id);
2737    int voted = 0;
2738    xs_list *p;
2739    xs_str *md5;
2740
2741    p = children;
2742    while (xs_list_iter(&p, &md5)) {
2743        xs *obj = NULL;
2744
2745        if (valid_status(object_get_by_md5(md5, &obj))) {
2746            char *atto = get_atto(obj);
2747            if (atto && strcmp(atto, user->actor) == 0 &&
2748                !xs_is_null(xs_dict_get(obj, "name"))) {
2749                voted = 1;
2750                break;
2751            }
2752        }
2753    }
2754
2755    return voted;
2756}
2757
2758
2759xs_list *user_queue(snac *snac)
2760/* returns a list with filenames that can be dequeued */
2761{
2762    xs *spec      = xs_fmt("%s/queue/" "*.json", snac->basedir);
2763    xs_list *list = xs_list_new();
2764    time_t t      = time(NULL);
2765    xs_list *p;
2766    xs_val *v;
2767
2768    xs *fns = xs_glob(spec, 0, 0);
2769
2770    p = fns;
2771    while (xs_list_iter(&p, &v)) {
2772        /* get the retry time from the basename */
2773        char *bn  = strrchr(v, '/');
2774        time_t t2 = atol(bn + 1);
2775
2776        if (t2 > t)
2777            snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
2778        else {
2779            list = xs_list_append(list, v);
2780            snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
2781        }
2782    }
2783
2784    return list;
2785}
2786
2787
2788xs_list *queue(void)
2789/* returns a list with filenames that can be dequeued */
2790{
2791    xs *spec      = xs_fmt("%s/queue/" "*.json", srv_basedir);
2792    xs_list *list = xs_list_new();
2793    time_t t      = time(NULL);
2794    xs_list *p;
2795    xs_val *v;
2796
2797    xs *fns = xs_glob(spec, 0, 0);
2798
2799    p = fns;
2800    while (xs_list_iter(&p, &v)) {
2801        /* get the retry time from the basename */
2802        char *bn  = strrchr(v, '/');
2803        time_t t2 = atol(bn + 1);
2804
2805        if (t2 > t)
2806            srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
2807        else {
2808            list = xs_list_append(list, v);
2809            srv_debug(2, xs_fmt("queue ready for %s", v));
2810        }
2811    }
2812
2813    return list;
2814}
2815
2816
2817xs_dict *queue_get(const char *fn)
2818/* gets a file from a queue */
2819{
2820    FILE *f;
2821    xs_dict *obj = NULL;
2822
2823    if ((f = fopen(fn, "r")) != NULL) {
2824        obj = xs_json_load(f);
2825        fclose(f);
2826    }
2827
2828    return obj;
2829}
2830
2831
2832xs_dict *dequeue(const char *fn)
2833/* dequeues a message */
2834{
2835    xs_dict *obj = queue_get(fn);
2836
2837    unlink(fn);
2838
2839    return obj;
2840}
2841
2842
2843/** the purge **/
2844
2845static int _purge_file(const char *fn, time_t mt)
2846/* purge fn if it's older than days */
2847{
2848    int ret = 0;
2849
2850    if (mtime(fn) < mt) {
2851        /* older than the minimum time: delete it */
2852        unlink(fn);
2853        srv_debug(2, xs_fmt("purged %s", fn));
2854        ret = 1;
2855    }
2856
2857    return ret;
2858}
2859
2860
2861static void _purge_dir(const char *dir, int days)
2862/* purges all files in a directory older than days */
2863{
2864    int cnt = 0;
2865
2866    if (days) {
2867        time_t mt = time(NULL) - days * 24 * 3600;
2868        xs *spec  = xs_fmt("%s/" "*", dir);
2869        xs *list  = xs_glob(spec, 0, 0);
2870        xs_list *p;
2871        xs_str *v;
2872
2873        p = list;
2874        while (xs_list_iter(&p, &v))
2875            cnt += _purge_file(v, mt);
2876
2877        srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
2878    }
2879}
2880
2881
2882static void _purge_user_subdir(snac *snac, const char *subdir, int days)
2883/* purges all files in a user subdir older than days */
2884{
2885    xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
2886
2887    _purge_dir(u_subdir, days);
2888}
2889
2890
2891void purge_server(void)
2892/* purge global server data */
2893{
2894    xs *spec = xs_fmt("%s/object/??", srv_basedir);
2895    xs *dirs = xs_glob(spec, 0, 0);
2896    xs_list *p;
2897    xs_str *v;
2898    int cnt = 0;
2899    int icnt = 0;
2900
2901    time_t mt = time(NULL) - 7 * 24 * 3600;
2902
2903    p = dirs;
2904    while (xs_list_iter(&p, &v)) {
2905        xs_list *p2;
2906        xs_str *v2;
2907
2908        {
2909            xs *spec2 = xs_fmt("%s/" "*.json", v);
2910            xs *files = xs_glob(spec2, 0, 0);
2911
2912            p2 = files;
2913            while (xs_list_iter(&p2, &v2)) {
2914                int n_link;
2915
2916                /* old and with no hard links? */
2917                if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
2918                    xs *s1    = xs_replace(v2, ".json", "");
2919                    xs *l     = xs_split(s1, "/");
2920                    char *md5 = xs_list_get(l, -1);
2921
2922                    object_del_by_md5(md5);
2923                    cnt++;
2924                }
2925            }
2926        }
2927
2928        {
2929            /* look for stray indexes */
2930            xs *speci = xs_fmt("%s/" "*_?.idx", v);
2931            xs *idxfs = xs_glob(speci, 0, 0);
2932
2933            p2 = idxfs;
2934            while (xs_list_iter(&p2, &v2)) {
2935                /* old enough to consider? */
2936                if (mtime(v2) < mt) {
2937                    /* check if the indexed object is here */
2938                    xs *o = xs_dup(v2);
2939                    char *ext = strchr(o, '_');
2940
2941                    if (ext) {
2942                        *ext = '\0';
2943                        o = xs_str_cat(o, ".json");
2944
2945                        if (mtime(o) == 0.0) {
2946                            /* delete */
2947                            unlink(v2);
2948                            srv_debug(1, xs_fmt("purged %s", v2));
2949                            icnt++;
2950                        }
2951                    }
2952                }
2953            }
2954
2955            /* delete index backups */
2956            xs *specb = xs_fmt("%s/" "*.bak", v);
2957            xs *bakfs = xs_glob(specb, 0, 0);
2958
2959            p2 = bakfs;
2960            while (xs_list_iter(&p2, &v2)) {
2961                unlink(v2);
2962                srv_debug(1, xs_fmt("purged %s", v2));
2963            }
2964        }
2965    }
2966
2967    /* purge collected inboxes */
2968    xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
2969    _purge_dir(ib_dir, 7);
2970
2971    /* purge the instance timeline */
2972    xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
2973    int itl_gc = index_gc(itl_fn);
2974
2975    /* purge tag indexes */
2976    xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
2977    xs *tag_dirs = xs_glob(tag_spec, 0, 0);
2978    p = tag_dirs;
2979
2980    int tag_gc = 0;
2981    while (xs_list_iter(&p, &v)) {
2982        xs *spec2 = xs_fmt("%s/" "*.idx", v);
2983        xs *files = xs_glob(spec2, 0, 0);
2984        xs_list *p2;
2985        xs_str *v2;
2986
2987        p2 = files;
2988        while (xs_list_iter(&p2, &v2)) {
2989            tag_gc += index_gc(v2);
2990            xs *bak = xs_fmt("%s.bak", v2);
2991            unlink(bak);
2992
2993            if (index_len(v2) == 0) {
2994                /* there are no longer any entry with this tag;
2995                   purge it completely */
2996                unlink(v2);
2997                xs *dottag = xs_replace(v2, ".idx", ".tag");
2998                unlink(dottag);
2999            }
3000        }
3001    }
3002
3003    srv_debug(1, xs_fmt("purge: global "
3004            "(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
3005}
3006
3007
3008void purge_user(snac *snac)
3009/* do the purge for this user */
3010{
3011    int priv_days, pub_days, user_days = 0;
3012    char *v;
3013    int n;
3014
3015    priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
3016    pub_days  = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
3017
3018    if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
3019        (v = xs_dict_get(snac->config, "purge_days")) != NULL)
3020        user_days = xs_number_get(v);
3021
3022    if (user_days) {
3023        /* override admin settings only if they are lesser */
3024        if (priv_days == 0 || user_days < priv_days)
3025            priv_days = user_days;
3026
3027        if (pub_days == 0 || user_days < pub_days)
3028            pub_days = user_days;
3029    }
3030
3031    _purge_user_subdir(snac, "hidden",  priv_days);
3032    _purge_user_subdir(snac, "private", priv_days);
3033
3034    _purge_user_subdir(snac, "public",  pub_days);
3035
3036    const char *idxs[] = { "followers.idx", "private.idx", "public.idx", "pinned.idx", NULL };
3037
3038    for (n = 0; idxs[n]; n++) {
3039        xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
3040        int gc = index_gc(idx);
3041        srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
3042    }
3043
3044    /* purge lists */
3045    {
3046        xs *spec = xs_fmt("%s/list/" "*.idx", snac->basedir);
3047        xs *lol  = xs_glob(spec, 0, 0);
3048        int c = 0;
3049        char *v;
3050
3051        while (xs_list_next(lol, &v, &c)) {
3052            int gc = index_gc(v);
3053            srv_debug(1, xs_fmt("purge: %s %d", v, gc));
3054        }
3055    }
3056
3057    /* unrelated to purging, but it's a janitorial process, so what the hell */
3058    verify_links(snac);
3059}
3060
3061
3062void purge_all(void)
3063/* purge all users */
3064{
3065    snac snac;
3066    xs *list = user_list();
3067    char *p, *uid;
3068
3069    p = list;
3070    while (xs_list_iter(&p, &uid)) {
3071        if (user_open(&snac, uid)) {
3072            purge_user(&snac);
3073            user_free(&snac);
3074        }
3075    }
3076
3077    purge_server();
3078
3079#ifndef NO_MASTODON_API
3080    mastoapi_purge();
3081#endif
3082}
3083
3084
3085/** archive **/
3086
3087void srv_archive(const char *direction, const char *url, xs_dict *req,
3088                 const char *payload, int p_size,
3089                 int status, xs_dict *headers,
3090                 const char *body, int b_size)
3091/* archives a connection */
3092{
3093    /* obsessive archiving */
3094    xs *date = tid(0);
3095    xs *dir  = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
3096    FILE *f;
3097
3098    if (mkdirx(dir) != -1) {
3099        xs *meta_fn = xs_fmt("%s/_META", dir);
3100
3101        if ((f = fopen(meta_fn, "w")) != NULL) {
3102            xs *j1 = xs_json_dumps(req, 4);
3103            xs *j2 = xs_json_dumps(headers, 4);
3104
3105            fprintf(f, "dir: %s\n", direction);
3106
3107            if (url)
3108                fprintf(f, "url: %s\n", url);
3109
3110            fprintf(f, "req: %s\n", j1);
3111            fprintf(f, "p_size: %d\n", p_size);
3112            fprintf(f, "status: %d\n", status);
3113            fprintf(f, "response: %s\n", j2);
3114            fprintf(f, "b_size: %d\n", b_size);
3115            fclose(f);
3116        }
3117
3118        if (p_size && payload) {
3119            xs *payload_fn = NULL;
3120            xs *payload_fn_raw = NULL;
3121            char *v = xs_dict_get(req, "content-type");
3122
3123            if (v && xs_str_in(v, "json") != -1) {
3124                payload_fn = xs_fmt("%s/payload.json", dir);
3125
3126                if ((f = fopen(payload_fn, "w")) != NULL) {
3127                    xs *v1 = xs_json_loads(payload);
3128                    xs *j1 = NULL;
3129
3130                    if (v1 != NULL)
3131                        j1 = xs_json_dumps(v1, 4);
3132
3133                    if (j1 != NULL)
3134                        fwrite(j1, strlen(j1), 1, f);
3135                    else
3136                        fwrite(payload, p_size, 1, f);
3137
3138                    fclose(f);
3139                }
3140            }
3141
3142            payload_fn_raw = xs_fmt("%s/payload", dir);
3143
3144            if ((f = fopen(payload_fn_raw, "w")) != NULL) {
3145                fwrite(payload, p_size, 1, f);
3146                fclose(f);
3147            }
3148        }
3149
3150        if (b_size && body) {
3151            xs *body_fn = NULL;
3152            char *v = xs_dict_get(headers, "content-type");
3153
3154            if (v && xs_str_in(v, "json") != -1) {
3155                body_fn = xs_fmt("%s/body.json", dir);
3156
3157                if ((f = fopen(body_fn, "w")) != NULL) {
3158                    xs *v1 = xs_json_loads(body);
3159                    xs *j1 = NULL;
3160
3161                    if (v1 != NULL)
3162                        j1 = xs_json_dumps(v1, 4);
3163
3164                    if (j1 != NULL)
3165                        fwrite(j1, strlen(j1), 1, f);
3166                    else
3167                        fwrite(body, b_size, 1, f);
3168
3169                    fclose(f);
3170                }
3171            }
3172            else {
3173                body_fn = xs_fmt("%s/body", dir);
3174
3175                if ((f = fopen(body_fn, "w")) != NULL) {
3176                    fwrite(body, b_size, 1, f);
3177                    fclose(f);
3178                }
3179            }
3180        }
3181    }
3182}
3183
3184
3185void srv_archive_error(const char *prefix, const xs_str *err,
3186                       const xs_dict *req, const xs_val *data)
3187/* archives an error */
3188{
3189    xs *ntid = tid(0);
3190    xs *fn   = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
3191    FILE *f;
3192
3193    if ((f = fopen(fn, "w")) != NULL) {
3194        fprintf(f, "Error: %s\n", err);
3195
3196        if (req) {
3197            fprintf(f, "Request headers:\n");
3198
3199            xs_json_dump(req, 4, f);
3200
3201            fprintf(f, "\n");
3202        }
3203
3204        if (data) {
3205            fprintf(f, "Data:\n");
3206
3207            if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
3208                xs_json_dump(data, 4, f);
3209            }
3210            else
3211                fprintf(f, "%s", data);
3212
3213            fprintf(f, "\n");
3214        }
3215
3216        fclose(f);
3217    }
3218}
3219
3220
3221void srv_archive_qitem(char *prefix, xs_dict *q_item)
3222/* archives a q_item in the error folder */
3223{
3224    xs *ntid = tid(0);
3225    xs *fn   = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
3226    FILE *f;
3227
3228    if ((f = fopen(fn, "w")) != NULL) {
3229        xs_json_dump(q_item, 4, f);
3230        fclose(f);
3231    }
3232}