]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-sub: simplifly code to map types and related strings
authorBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 17 Feb 2023 19:39:05 +0000 (20:39 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 17 Feb 2023 19:39:05 +0000 (20:39 +0100)
include/mlmmj.h
src/listcontrol.c
src/mlmmj-sub.c
tests/mlmmj-sub.sh

index 68ef72f39996c999f70522612a9d954f1c912df6..6d6ed8c5631458e6076dd3019e25fc47d0171fc7 100644 (file)
 #define __unused __attribute__((__unused__))
 #endif
 
+#ifndef NELEM
+#define NELEM(array) (sizeof(array) / sizeof((array)[0]))
+#endif
+
 #define RELAYHOST "127.0.0.1"
 #define READ_BUFSIZE 2048
 #define DEFAULT_RECIPDELIM "+"  /* Default recipient delimiter */
index 198e8b157bc46b7a125b38fa2b3081a2cf838039..9bbfe75eddc50931fbb72dba622924d59b1a6a07 100644 (file)
@@ -80,10 +80,6 @@ static struct ctrl_command ctrl_commands[] = {
        { "list",               false, true , true ,CTRL_LIST }
 };
 
-#ifndef NELEM
-#define NELEM(array) (sizeof(array) / sizeof((array)[0]))
-#endif
-
 struct ctrl_command *
 get_ctrl_command(const char *controlstr, char **param)
 {
index f65728f7eaae067a848c9d15a504d34151f61e1f..2514ad20566ea50e9b342d313669fb6cb5a6b746 100644 (file)
 #include "send_help.h"
 #include "xstring.h"
 
+static char *subtypes[7] = {
+       "SUB_NORMAL",
+       "SUB_DIGEST",
+       "SUB_NOMAIL",
+       NULL,
+       NULL,
+       "SUB_BOTH",
+       NULL,
+};
+
 static void moderate_sub(struct ml *ml, const char *subaddr,
                const char *mlmmjsend, enum subtype typesub,
                enum subreason reasonsub)
@@ -66,23 +76,8 @@ static void moderate_sub(struct ml *ml, const char *subaddr,
        pid_t childpid, pid;
        xstring *str = NULL;
 
-       /* generate the file in moderation/ */
-       switch(typesub) {
-               default:
-               case SUB_NORMAL:
-                       type = "SUB_NORMAL";
-                       break;
-               case SUB_DIGEST:
-                       type = "SUB_DIGEST";
-                       break;
-               case SUB_NOMAIL:
-                       type = "SUB_NOMAIL";
-                       break;
-               case SUB_BOTH:
-                       type = "SUB_BOTH";
-                       break;
-       }
-       
+       type = subtypes[typesub];
+
        for (;;) {
                cookie = random_str();
                xasprintf(&modfilename, "moderation/subscribe%s", cookie);
@@ -239,24 +234,13 @@ void getaddrandtype(const char *listdir, const char *modstr,
        chomp(readaddr);
        *addrptr = readaddr;
 
-       if(strncmp(readtype, "SUB_NORMAL", 10) == 0) {
-               *subtypeptr = SUB_NORMAL;
-               goto freedone;
-       }
-
-       if(strncmp(readtype, "SUB_DIGEST", 10) == 0) {
-               *subtypeptr = SUB_DIGEST;
-               goto freedone;
-       }
-
-       if(strncmp(readtype, "SUB_NOMAIL", 10) == 0) {
-               *subtypeptr = SUB_NOMAIL;
-               goto freedone;
-       }
-
-       if(strncmp(readtype, "SUB_BOTH", 8) == 0) {
-               *subtypeptr = SUB_BOTH;
-               goto freedone;
+       for (size_t i = 0; i < NELEM(subtypes); i++) {
+               if (subtypes[i] == NULL)
+                       continue;
+               if (strcmp(subtypes[i], readtype) == 0) {
+                       *subtypeptr = i;
+                       goto freedone;
+               }
        }
 
        log_error(LOG_ARGS, "Type %s not valid in %s", readtype,
@@ -436,7 +420,7 @@ int main(int argc, char **argv)
        }
 
        if(modstr) {
-               getaddrandtype(listdir, modstr, &address, &typesub);
+               getaddrandtype(ml.dir, modstr, &address, &typesub);
                reasonsub = SUB_PERMIT;
        }
 
index 62b1f7c3df25a58867526e5089a982f01b5a2ed8..d79d1b363916b294f20a0b4fdc7f3777cf68aad1 100755 (executable)
@@ -247,6 +247,10 @@ EOF
        atf_check -s exit:1 test -f mail-3.txt
        atf_check -o match:"^ml/moderation/subscribe.*" find ml/moderation/ -type f
        atf_check -o inline:"john@doe.org\nSUB_NORMAL\n" cat ml/moderation/subscribe*
+       file=$(ls ml/moderation/subscribe*)
+       atf_check ${mlmmjsub} -L ml -m ${file##*subscribe} -c
+       atf_check -o inline:"john@doe.org\n" cat ml/subscribers.d/j
+       atf_check -s exit:1 test -f $file
 }
 
 add_normal_then_nomail_body()