From 60c2a50ed9a0efc8bbbdbeb48b4a96a1eb80f946 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 11 Mar 2024 06:00:21 +0100 Subject: [PATCH] New function content_check(). --- activitypub.c | 7 ++++++- data.c | 40 ++++++++++++++++++++++++++++++++++++++++ snac.h | 2 ++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/activitypub.c b/activitypub.c index 1976012..22a12fa 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1921,10 +1921,15 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req) snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to)); } else { + if (content_check("filter_reject.txt", object)) { + snac_log(snac, xs_fmt("rejected by content %s", id)); + return 1; + } + timeline_request(snac, &in_reply_to, &wrk, 0); if (timeline_add(snac, id, object)) { - snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id)); + snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id)); do_notify = 1; } diff --git a/data.c b/data.c index 4d9824a..67536fd 100644 --- a/data.c +++ b/data.c @@ -9,6 +9,7 @@ #include "xs_glob.h" #include "xs_set.h" #include "xs_time.h" +#include "xs_regex.h" #include "snac.h" @@ -2006,6 +2007,45 @@ int instance_unblock(const char *instance) } +/** content filtering **/ + +int content_check(const char *file, const xs_dict *msg) +/* checks if message content matches any of the regexes in file */ +{ + xs *fn = xs_fmt("%s/%s", srv_basedir, file); + FILE *f; + int r = 0; + char *v = xs_dict_get(msg, "content"); + + if (xs_type(v) == XSTYPE_STRING && *v) { + if ((f = fopen(fn, "r")) != NULL) { + srv_debug(1, xs_fmt("content_check: loading regexes from %s", fn)); + + xs *c = xs_regex_replace(v, "<[^>]+>", " "); + c = xs_regex_replace_i(c, " {2,}", " "); + c = xs_tolower_i(c); + + while (!r && !feof(f)) { + xs *rx = xs_strip_i(xs_readline(f)); + + if (*rx) { + xs *l = xs_regex_select_n(c, rx, 1); + + if (xs_list_len(l)) { + srv_debug(1, xs_fmt("content_check: match for '%s'", rx)); + r = 1; + } + } + } + + fclose(f); + } + } + + return r; +} + + /** notifications **/ xs_str *notify_check_time(snac *snac, int reset) diff --git a/snac.h b/snac.h index 1afbfc7..f2bcedb 100644 --- a/snac.h +++ b/snac.h @@ -204,6 +204,8 @@ int is_instance_blocked(const char *instance); int instance_block(const char *instance); int instance_unblock(const char *instance); +int content_check(const char *file, const xs_dict *msg); + void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries); void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries); void enqueue_output_raw(const char *keyid, const char *seckey,