snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit 61957a86da212cb0784dfc3de7bfcafe3dc7589b
parent 090c7cc166809ef9f9eb5cb40d97046002c1a305
Author: default <nobody@localhost>
Date:   Thu, 29 Jun 2023 08:07:10 +0200

Added instance blocking to data storage.

Diffstat:
Mdata.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msnac.h | 4++++
2 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/data.c b/data.c @@ -1751,6 +1751,72 @@ xs_list *inbox_list(void) } +/** instance-wide operations **/ + +xs_str *_instance_block_fn(const char *instance) +{ + xs *s1 = xs_replace(instance, "https:/" "/", ""); + xs *l = xs_split(s1, "/"); + char *p = xs_list_get(l, 0); + xs *md5 = xs_md5_hex(p, strlen(p)); + + return xs_fmt("%s/block/%s", srv_basedir, md5); +} + + +int is_instance_blocked(const char *instance) +{ + xs *fn = _instance_block_fn(instance); + + return !!(mtime(fn) != 0.0); +} + + +int instance_block(const char *instance) +/* blocks a full instance */ +{ + int ret; + + /* create the subdir */ + xs *dir = xs_fmt("%s/block/", srv_basedir); + mkdirx(dir); + + if (!is_instance_blocked(instance)) { + xs *fn = _instance_block_fn(instance); + FILE *f; + + if ((f = fopen(fn, "w")) != NULL) { + fprintf(f, "%s\n", instance); + fclose(f); + + ret = 0; + } + else + ret = -1; + } + else + ret = -2; + + return ret; +} + + +int instance_unblock(const char *instance) +/* unblocks a full instance */ +{ + int ret; + + if (is_instance_blocked(instance)) { + xs *fn = _instance_block_fn(instance); + ret = unlink(fn); + } + else + ret = -2; + + return ret; +} + + /** notifications **/ xs_str *notify_check_time(snac *snac, int reset) diff --git a/snac.h b/snac.h @@ -161,6 +161,10 @@ void inbox_add(const char *inbox); void inbox_add_by_actor(const xs_dict *actor); xs_list *inbox_list(void); +int is_instance_blocked(const char *instance); +int instance_block(const char *instance); +int instance_unblock(const char *instance); + void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries); void enqueue_output_raw(const char *keyid, const char *seckey, xs_dict *msg, xs_str *inbox, int retries);