From 4a4b27f95d31d23e73f9ecaa34e5db5c2191950f Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 5 Jul 2023 14:00:53 +0200 Subject: [PATCH] subscription: move moderation handling into the library for testing purpose --- include/subscriberfuncs.h | 1 + src/mlmmj-sub.c | 66 ++------------------------------------- src/subscriberfuncs.c | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 64 deletions(-) diff --git a/include/subscriberfuncs.h b/include/subscriberfuncs.h index 577a21bf..8c39bceb 100644 --- a/include/subscriberfuncs.h +++ b/include/subscriberfuncs.h @@ -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); diff --git a/src/mlmmj-sub.c b/src/mlmmj-sub.c index 66802719..178466be 100644 --- a/src/mlmmj-sub.c +++ b/src/mlmmj-sub.c @@ -50,15 +50,7 @@ #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; } diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index b0789764..4390476c 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -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); +} -- 2.47.2