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