]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
config_sub: deduplicate and test code
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 15 Feb 2023 14:22:36 +0000 (15:22 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 15 Feb 2023 14:22:36 +0000 (15:22 +0100)
include/subscriberfuncs.h
src/mlmmj-sub.c
src/mlmmj-unsub.c
src/subscriberfuncs.c
tests/mlmmj.c

index c7f708e962186d44e724ae395fd697cf43b2871d..fa3ae21771570fdcdc7683784eee1003f32b7d05 100644 (file)
@@ -34,3 +34,4 @@ void notify_sub(struct ml *ml, const char *subaddr, enum subtype typesub,
 void generate_subscription(struct ml *ml, const char *subaddr, enum subtype typesub, bool sub);
 void generate_subconfirm(struct ml *ml, const char *subaddr, enum subtype typesub,
     enum subreason reasonsub, bool sub);
+void confirm_sub(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub);
index 7d9a241a8002b888a6e68f6f104e0f6b8d9166ef..5c8bd9140687f7d3f4f61f45809de447d5671fb9 100644 (file)
@@ -272,47 +272,6 @@ freedone:
        free(modfilename);
 }
 
-void confirm_sub(struct ml *ml, const char *subaddr,
-               const char *mlmmjsend, enum subtype typesub,
-               enum subreason reasonsub)
-{
-       text *txt;
-       char *queuefilename, *fromaddr, *listtext;
-
-       gen_addr(fromaddr, ml, "bounces-help");
-
-       switch(typesub) {
-               default:
-               case SUB_NORMAL:
-                       listtext = xstrdup("sub-ok");
-                       break;
-               case SUB_DIGEST:
-                       listtext = xstrdup("sub-ok-digest");
-                       break;
-               case SUB_NOMAIL:
-                       listtext = xstrdup("sub-ok-nomail");
-                       break;
-               case SUB_BOTH:
-                       /* No legacy list text as feature didn't exist. */
-                       listtext = xstrdup("sub-ok");
-                       break;
-       }
-
-       txt = open_text(ml->fd, "finish", "sub",
-                       subreason_strs[reasonsub], subtype_strs[typesub],
-                       listtext);
-       free(listtext);
-       MY_ASSERT(txt);
-       register_default_unformatted(txt, ml);
-       register_unformatted(txt, "subaddr", subaddr);
-       queuefilename = prepstdreply(txt, ml, "$helpaddr$", subaddr, NULL);
-       MY_ASSERT(queuefilename);
-       close_text(txt);
-
-       exec_or_die(mlmmjsend, "-l", "1", "-L", ml->dir, "-T", subaddr, "-F",
-           fromaddr, "-m", queuefilename, NULL);
-}
-
 static void print_help(const char *prg)
 {
        printf("Usage: %s -L /path/to/list {-a john@doe.org | -m str}\n"
@@ -551,7 +510,7 @@ int main(int argc, char **argv)
 
                if(childpid < 0) {
                        log_error(LOG_ARGS, "Could not fork; owner not notified");
-                       confirm_sub(&ml, address, mlmmjsend, typesub, reasonsub);
+                       confirm_sub(&ml, address, typesub, reasonsub, true);
                }
                
                if(childpid > 0) {
@@ -562,7 +521,7 @@ int main(int argc, char **argv)
 
                /* child confirms subscription */
                if(childpid == 0)
-                       confirm_sub(&ml, address, mlmmjsend, typesub, reasonsub);
+                       confirm_sub(&ml, address, typesub, reasonsub, true);
        }
 
        notifysub = !quiet && reasonsub != SUB_SWITCH &&
index 21cf5af0c00586b8bb4e93119e6f4165284d5d79..da8a2490bda43f9872aeee0d4b3460fd88b25deb 100644 (file)
 #include "prepstdreply.h"
 #include "utils.h"
 
-void confirm_unsub(struct ml *ml, const char *subaddr, const char *mlmmjsend,
-                  enum subtype typesub, enum subreason reasonsub)
-{
-       text *txt;
-       char *queuefilename, *fromaddr, *listtext;
-
-       gen_addr(fromaddr, ml, "bounces-help");
-
-       switch(typesub) {
-               default:
-               case SUB_NORMAL:
-                       listtext = xstrdup("unsub-ok");
-                       break;
-               case SUB_DIGEST:
-                       listtext = xstrdup("unsub-ok-digest");
-                       break;
-               case SUB_NOMAIL:
-                       listtext = xstrdup("unsub-ok-nomail");
-                       break;
-       }
-
-       txt = open_text(ml->fd, "finish", "unsub",
-                       subreason_strs[reasonsub], subtype_strs[typesub],
-                       listtext);
-       free(listtext);
-       MY_ASSERT(txt);
-       register_default_unformatted(txt, ml);
-       register_unformatted(txt, "subaddr", subaddr);
-       queuefilename = prepstdreply(txt, ml, "$helpaddr$", subaddr, NULL);
-       MY_ASSERT(queuefilename);
-       close_text(txt);
-
-       exec_or_die(mlmmjsend, "-l", "1", "-L", ml->dir, "-T", subaddr, "-F",
-           fromaddr, "-m", queuefilename, NULL);
-}
-
 static void print_help(const char *prg)
 {
        printf("Usage: %s -L /path/to/list -a john@doe.org\n"
@@ -253,7 +217,7 @@ int main(int argc, char **argv)
 
                if(childpid < 0) {
                        log_error(LOG_ARGS, "Could not fork");
-                       confirm_unsub(&ml, address, mlmmjsend, typesub, reasonsub);
+                       confirm_sub(&ml, address, typesub, reasonsub, false);
                }
 
                if(childpid > 0) {
@@ -264,7 +228,7 @@ int main(int argc, char **argv)
 
                /* child confirms subscription */
                if(childpid == 0)
-                       confirm_unsub(&ml, address, mlmmjsend, typesub, reasonsub);
+                       confirm_sub(&ml, address, typesub, reasonsub, false);
        }
 
        notifysub = !quiet && statctrl(ml.ctrlfd, "notifysub");
index a2a5c68b9dfe4aa3e11dec9d286eefabc75594f6..0c546ab8fccf2c4262a50cace7b94376c90feefc 100644 (file)
@@ -334,3 +334,49 @@ generate_subconfirm(struct ml *ml, const char *subaddr, enum subtype typesub,
        exit(EXIT_SUCCESS);
 }
 
+void
+confirm_sub(struct ml *ml, const char *subaddr, enum subtype typesub,
+    enum subreason reasonsub, bool sub)
+{
+       text *txt;
+       char *queuefilename, *fromaddr;
+       const char *listtext;
+       struct mail mail;
+
+       memset(&mail, 0, sizeof(mail));
+       gen_addr(fromaddr, ml, "bounces-help");
+
+       switch(typesub) {
+               default:
+               case SUB_BOTH: /* FALLTHROUGH */
+               case SUB_NORMAL:
+                       listtext = sub ? "sub-ok" : "unsub-ok";
+                       break;
+               case SUB_DIGEST:
+                       listtext = sub ? "sub-ok-digest" : "unsub-ok-digest";
+                       break;
+               case SUB_NOMAIL:
+                       listtext = sub ? "sub-ok-nomail" : "unsub-ok-nomail";
+                       break;
+       }
+
+       txt = open_text(ml->fd, "finish", sub ? "sub" : "unsub",
+                       subreason_strs[reasonsub], subtype_strs[typesub],
+                       listtext);
+       MY_ASSERT(txt);
+       register_default_unformatted(txt, ml);
+       register_unformatted(txt, "subaddr", subaddr);
+       queuefilename = prepstdreply(txt, ml, "$helpaddr$", subaddr, NULL);
+       MY_ASSERT(queuefilename);
+       close_text(txt);
+
+       mail.to = subaddr;
+       mail.from = fromaddr;
+       mail.fp = fopen(queuefilename, "r");
+       if (!send_single_mail(&mail, ml, false)) {
+               save_queue(queuefilename, &mail);
+               exit(EXIT_FAILURE);
+       }
+       exit(EXIT_SUCCESS);
+}
+
index 001aed7bc846d0267f4125ba3632c7f61a19c5cf..e58dc70f3bec39556909de90f2e400797f874465 100644 (file)
@@ -141,6 +141,7 @@ ATF_TC_WITHOUT_HEAD(save_queue);
 ATF_TC_WITHOUT_HEAD(send_single_mail);
 ATF_TC_WITHOUT_HEAD(generate_subscription);
 ATF_TC_WITHOUT_HEAD(generate_subconfirm);
+ATF_TC_WITHOUT_HEAD(confirm_sub);
 
 ATF_TC_BODY(random_int, tc)
 {
@@ -2317,7 +2318,146 @@ ATF_TC_BODY(generate_subconfirm, tc)
                atf_utils_cat_file(path, "");
                atf_tc_fail("invalid file");
        }
-       return;
+}
+
+ATF_TC_BODY(confirm_sub, tc)
+{
+       char *dir;
+       const char *path;
+       init_ml(true);
+       struct ml ml;
+       ml_init(&ml);
+       ml.dir = "list";
+       ml_open(&ml, false);
+       rmdir("list/text");
+       xasprintf(&dir, "%s/../listtexts/en",
+           atf_tc_get_config_var(tc, "srcdir"));
+
+       symlink(dir, "list/text");
+       atf_utils_create_file("list/control/smtpport", "25678");
+       atf_utils_create_file("list/control/smtphelo", "heloname");
+       int smtppipe[2];
+       ATF_REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, smtppipe) >= 0);
+       pid_t p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       pid_t p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_NORMAL, SUB_ADMIN, true);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       path = "mailout.txt";
+       if (!atf_utils_grep_file(".*An administrator has subscribed you to the normal version of the list.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+       if (!atf_utils_grep_file(".*The email address you are subscribed with is <test@plop>.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_DIGEST, SUB_ADMIN, true);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*An administrator has subscribed you to the digest version of the list.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+       if (!atf_utils_grep_file(".*The email address you are subscribed with is <test@plop>.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_NOMAIL, SUB_CONFIRM, true);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*Thank you for confirming your subscription. You have now been added to the.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+       if (!atf_utils_grep_file(".*no-mail version of the list..*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_BOTH, SUB_REQUEST, true);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*Thank you for your request to join us. You have now been added to the.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+       if (!atf_utils_grep_file(".*The email address you are subscribed with is <test@plop>.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_NORMAL, SUB_ADMIN, false);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*An administrator has removed you from the list.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_DIGEST, SUB_ADMIN, false);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*An administrator has removed you from the list.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_NOMAIL, SUB_CONFIRM, false);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*Thank you for confirming your unsubscribe. You have now been removed from.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file unsubscribe nomail confirm");
+       }
+
+       p2 = single_mail_reception(smtppipe[1]);
+       atf_utils_readline(smtppipe[0]);
+       p = atf_utils_fork();
+       if (p == 0) {
+               confirm_sub(&ml, "test@plop", SUB_BOTH, SUB_REQUEST, false);
+       }
+       atf_utils_wait(p2, 0, "save:mailout.txt", "");
+       atf_utils_wait(p, 0, "", "");
+       if (!atf_utils_grep_file(".*You have now been removed from the list.*", path)) {
+               atf_utils_cat_file(path, "");
+               atf_tc_fail("invalid file");
+       }
 }
 
 ATF_TP_ADD_TCS(tp)
@@ -2398,6 +2538,7 @@ ATF_TP_ADD_TCS(tp)
        ATF_TP_ADD_TC(tp, send_single_mail);
        ATF_TP_ADD_TC(tp, generate_subscription);
        ATF_TP_ADD_TC(tp, generate_subconfirm);
+       ATF_TP_ADD_TC(tp, confirm_sub);
 
        return (atf_no_error());
 }