serv_inboxrules.c: compound fields such as From now work with substring match
authorArt Cancro <ajc@citadel.org>
Thu, 9 Mar 2023 23:16:18 +0000 (18:16 -0500)
committerArt Cancro <ajc@citadel.org>
Thu, 9 Mar 2023 23:16:18 +0000 (18:16 -0500)
citadel/server/modules/inboxrules/serv_inboxrules.c

index f65a376783d7a61bc6ffd6febfd4ec33d1bc68cd..d973ba5cc040a6c6d7456f3e894dd86cd575fdbd 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
+#include <regex.h>
 #include <libcitadel.h>
 #include "../../citadel_defs.h"
 #include "../../server.h"
@@ -493,7 +494,7 @@ void inbox_do_msg(long msgnum, void *userdata) {
        }
 
        for (i=0; i<ii->num_rules; ++i) {
-               syslog(LOG_DEBUG, "inboxrules: processing rule %d is %s", i, field_keys[ ii->rules[i].compared_field ]);
+               syslog(LOG_DEBUG, "inboxrules: processing rule: %d , field: %s", i, field_keys[ ii->rules[i].compared_field ]);
                rule_activated = 0;
 
                // Before doing a field compare, check to see if we have the correct parts of the message in memory.
@@ -635,32 +636,44 @@ void inbox_do_msg(long msgnum, void *userdata) {
 
                                // For all of the above fields, we can compare the field we've loaded into the buffer.
                                syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
-                               int substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
+                               int substring_match = 0;
+                               int regex_match = 0;
                                int exact_match = 0;
                                if (compare_compound) {
                                        char *sep = strchr(compare_me, '|');
                                        if (sep) {
                                                *sep = 0;
+                                               ++sep;
                                                exact_match =
                                                        (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
-                                                       + (strcasecmp(++sep, ii->rules[i].compared_value) ? 0 : 1)
+                                                       + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
+                                               ;
+                                               substring_match =
+                                                       (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
+                                                       + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
                                                ;
                                        }
                                }
                                else {
                                        exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
+                                       substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
                                }
                                syslog(LOG_DEBUG, "substring match: %d", substring_match);
-                               syslog(LOG_DEBUG, "exact match: %d", exact_match);
+                               syslog(LOG_DEBUG, "    regex match: %d", regex_match);
+                               syslog(LOG_DEBUG, "    exact match: %d", exact_match);
                                switch(ii->rules[i].field_compare_op) {
                                        case fcomp_contains:
-                                       case fcomp_matches:
                                                rule_activated = substring_match;
                                                break;
+                                       case fcomp_matches:
+                                               rule_activated = regex_match;
+                                               break;
                                        case fcomp_notcontains:
-                                       case fcomp_notmatches:
                                                rule_activated = !substring_match;
                                                break;
+                                       case fcomp_notmatches:
+                                               rule_activated = !regex_match;
+                                               break;
                                        case fcomp_is:
                                                rule_activated = exact_match;
                                                break;