From 61957a86da212cb0784dfc3de7bfcafe3dc7589b Mon Sep 17 00:00:00 2001 From: default Date: Thu, 29 Jun 2023 08:07:10 +0200 Subject: [PATCH] Added instance blocking to data storage. --- data.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ snac.h | 4 ++++ 2 files changed, 70 insertions(+) diff --git a/data.c b/data.c index 9972865..117e16f 100644 --- 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 index 5a26ec2..13cfe8c 100644 --- 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);