summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authordefault <nobody@localhost>2024-03-11 06:00:21 +0100
committerdefault <nobody@localhost>2024-03-11 06:00:21 +0100
commit60c2a50ed9a0efc8bbbdbeb48b4a96a1eb80f946 (patch)
tree70e577d7064b3f1f52138d984e0a693d8e161b6c /data.c
parent951e6b23151d298d9babeec81bc9926c11bdb3b3 (diff)
New function content_check().
Diffstat (limited to 'data.c')
-rw-r--r--data.c40
1 files changed, 40 insertions, 0 deletions
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)