commit 60c2a50

default  ·  2024-03-11 05:00:21 +0000 UTC
parent 951e6b2
New function content_check().
3 files changed,  +48, -1
M data.c
M snac.h
+6, -1
 1@@ -1921,10 +1921,15 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
 2                 snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
 3             }
 4             else {
 5+                if (content_check("filter_reject.txt", object)) {
 6+                    snac_log(snac, xs_fmt("rejected by content %s", id));
 7+                    return 1;
 8+                }
 9+
10                 timeline_request(snac, &in_reply_to, &wrk, 0);
11 
12                 if (timeline_add(snac, id, object)) {
13-                    snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
14+                    snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
15                     do_notify = 1;
16                 }
17 
M data.c
+40, -0
 1@@ -9,6 +9,7 @@
 2 #include "xs_glob.h"
 3 #include "xs_set.h"
 4 #include "xs_time.h"
 5+#include "xs_regex.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -2006,6 +2007,45 @@ int instance_unblock(const char *instance)
10 }
11 
12 
13+/** content filtering **/
14+
15+int content_check(const char *file, const xs_dict *msg)
16+/* checks if message content matches any of the regexes in file */
17+{
18+    xs *fn = xs_fmt("%s/%s", srv_basedir, file);
19+    FILE *f;
20+    int r = 0;
21+    char *v = xs_dict_get(msg, "content");
22+
23+    if (xs_type(v) == XSTYPE_STRING && *v) {
24+        if ((f = fopen(fn, "r")) != NULL) {
25+            srv_debug(1, xs_fmt("content_check: loading regexes from %s", fn));
26+
27+            xs *c = xs_regex_replace(v, "<[^>]+>", " ");
28+            c = xs_regex_replace_i(c, " {2,}", " ");
29+            c = xs_tolower_i(c);
30+
31+            while (!r && !feof(f)) {
32+                xs *rx = xs_strip_i(xs_readline(f));
33+
34+                if (*rx) {
35+                    xs *l = xs_regex_select_n(c, rx, 1);
36+
37+                    if (xs_list_len(l)) {
38+                        srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
39+                        r = 1;
40+                    }
41+                }
42+            }
43+
44+            fclose(f);
45+        }
46+    }
47+
48+    return r;
49+}
50+
51+
52 /** notifications **/
53 
54 xs_str *notify_check_time(snac *snac, int reset)
M snac.h
+2, -0
1@@ -204,6 +204,8 @@ int is_instance_blocked(const char *instance);
2 int instance_block(const char *instance);
3 int instance_unblock(const char *instance);
4 
5+int content_check(const char *file, const xs_dict *msg);
6+
7 void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries);
8 void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries);
9 void enqueue_output_raw(const char *keyid, const char *seckey,