]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
subscription: move moderation handling into the library for testing purpose
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 12:00:53 +0000 (14:00 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 12:00:53 +0000 (14:00 +0200)
include/subscriberfuncs.h
src/mlmmj-sub.c
src/subscriberfuncs.c

index 577a21bf00320125ffc2903cca478b68b162b1bb..8c39bceb997107eacf1c5576d805844297d38e20 100644 (file)
@@ -36,3 +36,4 @@ void generate_subconfirm(struct ml *ml, const char *subaddr, enum subtype typesu
     enum subreason reasonsub, bool sub);
 void send_confirmation_mail(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub);
 bool do_unsubscribe(struct ml *ml, const char *addr, enum subtype typesub, enum subreason reasonsub, bool inform_not_subscribed, bool confirm_unsubscription, bool quiet, bool send_goodbye_mail);
+void mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum subtype *subtypeptr);
index 66802719fccedf785e62ea4f8bd17017bcdf98ad..178466be1bc8515dea13ebef29f00a32795bcb0c 100644 (file)
 #include "send_help.h"
 #include "xstring.h"
 
-static char *subtypes[7] = {
-       "SUB_NORMAL",
-       "SUB_DIGEST",
-       "SUB_NOMAIL",
-       NULL,
-       NULL,
-       "SUB_BOTH",
-       NULL,
-};
+extern char *subtypes[];
 
 static void moderate_sub(struct ml *ml, const char *subaddr,
                const char *mlmmjsend, enum subtype typesub,
@@ -205,60 +197,6 @@ static void moderate_sub(struct ml *ml, const char *subaddr,
        send_help(ml, queuefilename, subaddr);
 }
 
-void getaddrandtype(struct ml *ml, const char *modstr,
-               char **addrptr, enum subtype *subtypeptr)
-{
-       int fd;
-       char *readaddr, *readtype, *modfilename, *line = NULL;
-       FILE *f;
-       size_t linecap = 0;
-
-       if (strncmp(modstr, "subscribe", 9) == 0)
-                       modstr += 9;
-
-       xasprintf(&modfilename, "moderation/subscribe%s", modstr);
-
-       fd = openat(ml->fd, modfilename, O_RDONLY);
-       if(fd < 0) {
-               log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename);
-               exit(EXIT_FAILURE);
-       }
-       f = fdopen(fd, "r");
-
-       if (getline(&line, &linecap, f) <= 0) {
-               log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename);
-               exit(EXIT_FAILURE);
-       }
-       chomp(line);
-       readaddr = xstrdup(line);
-       if (getline(&line, &linecap, f) <= 0) {
-               log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename);
-               exit(EXIT_FAILURE);
-       }
-       chomp(line);
-       readtype = xstrdup(line);
-       fclose(f);
-
-       *addrptr = readaddr;
-
-       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/%s", readtype,
-           ml->dir, modfilename);
-
-freedone:
-       free(readtype);
-       unlinkat(ml->fd, modfilename, 0);
-       free(modfilename);
-}
-
 static void print_help(const char *prg)
 {
        printf("Usage: %s -L /path/to/list {-a john@doe.org | -m str}\n"
@@ -427,7 +365,7 @@ int main(int argc, char **argv)
        }
 
        if(modstr) {
-               getaddrandtype(&ml, modstr, &address, &typesub);
+               mod_get_addr_and_type(&ml, modstr, &address, &typesub);
                reasonsub = SUB_PERMIT;
        }
 
index b0789764c99fa31fde8ef3c755fdc609e0ed0557..4390476c1a33985ed2f30cc8edd3a1034a30a20b 100644 (file)
@@ -60,6 +60,16 @@ char * subreason_strs[] = {
        "switch"
 };
 
+char *subtypes[] = {
+       "SUB_NORMAL",
+       "SUB_DIGEST",
+       "SUB_NOMAIL",
+       NULL,
+       NULL,
+       "SUB_BOTH",
+       NULL,
+};
+
 bool
 find_subscriber(int fd, const char *address)
 {
@@ -397,3 +407,57 @@ do_unsubscribe(struct ml *ml, const char *addr, enum subtype typesub,
        free(address);
        return (true);
 }
+
+void
+mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum subtype *subtypeptr)
+{
+       int fd;
+       char *readaddr, *readtype, *modfilename, *line = NULL;
+       FILE *f;
+       size_t linecap = 0;
+
+       if (strncmp(modstr, "subscribe", 9) == 0)
+                       modstr += 9;
+
+       xasprintf(&modfilename, "moderation/subscribe%s", modstr);
+
+       fd = openat(ml->fd, modfilename, O_RDONLY);
+       if(fd < 0) {
+               log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename);
+               exit(EXIT_FAILURE);
+       }
+       f = fdopen(fd, "r");
+
+       if (getline(&line, &linecap, f) <= 0) {
+               log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename);
+               exit(EXIT_FAILURE);
+       }
+       chomp(line);
+       readaddr = xstrdup(line);
+       if (getline(&line, &linecap, f) <= 0) {
+               log_error(LOG_ARGS, "Could not parse %s/%s", ml->dir, modfilename);
+               exit(EXIT_FAILURE);
+       }
+       chomp(line);
+       readtype = xstrdup(line);
+       fclose(f);
+
+       *addrptr = readaddr;
+
+       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/%s", readtype,
+           ml->dir, modfilename);
+
+freedone:
+       free(readtype);
+       unlinkat(ml->fd, modfilename, 0);
+       free(modfilename);
+}