]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-send: use newsmtp to reduce code duplication
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 18 Jul 2023 09:34:46 +0000 (11:34 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 18 Jul 2023 09:34:46 +0000 (11:34 +0200)
include/send_mail.h
src/mlmmj-send.c
src/send_mail.c
tests/mlmmj.c

index e0d1e7d4e4760a04e806ae06be7165291a2572b9..c7fce13b3370b8fe8ca12c9638e29f4d3ad634e0 100644 (file)
@@ -36,7 +36,7 @@ struct mail {
        bool addtohdr;
 };
 
-int newsmtp(struct ml *ml);
+int newsmtp(struct ml *ml, const char *relayhost);
 int initsmtp(int *sockfd, const char *relayhost, unsigned short port, const char *heloname);
 int endsmtp(int *sockfd);
 int send_mail(int sockfd, struct mail *mail, int listfd, int ctrlfd, bool bounce);
index 2f81590db589b56b44796b1335f5e057b1a3d775..8bf321361b3821ba96e1be01c0e961163f041124 100644 (file)
@@ -279,12 +279,11 @@ int main(int argc, char **argv)
        char listctrl = 0;
        char *verp = NULL;
        char *verpfrom;
-       char *reply, *smtphelo, *requeuefilename;
+       char *reply, *requeuefilename;
        DIR *subddir;
        struct dirent *dp;
        struct stat st;
        strlist stl = tll_init();
-       unsigned short smtpport;
        struct sigaction sigact;
        struct mail mail = { 0 };
        struct ml ml;
@@ -439,19 +438,9 @@ int main(int argc, char **argv)
                xasprintf(&archivefilename, "%s/archive/%d", ml.dir, mindex);
        }
 
-       if(!relayhost)
-               relayhost = ctrlvalue(ml.ctrlfd, "relayhost");
-       if(!relayhost)
-               relayhost = xstrdup(RELAYHOST);
-
-       smtpport = ctrlushort(ml.ctrlfd, "smtpport", 25);
-       if ((smtphelo = ctrlvalue(ml.ctrlfd, "smtphelo")) == NULL) {
-               smtphelo = hostnamestr();
-       }
-
        switch(listctrl) {
        case '2': /* Moderators */
-               initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+               sockfd = newsmtp(&ml, relayhost);
                mail.from = bounceaddr;
                mail.replyto = NULL;
                if(send_mail_many_fd(sockfd, &mail, &ml, subfd, NULL)) {
@@ -462,7 +451,7 @@ int main(int argc, char **argv)
                }
                break;
        case '3': /* resending earlier failed mails */
-               initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+               sockfd = newsmtp(&ml, relayhost);
                mail.from = NULL;
                mail.replyto = NULL;
                if(send_mail_many_fd(sockfd, &mail, &ml, subfd, mailfilename)) {
@@ -474,7 +463,7 @@ int main(int argc, char **argv)
                unlink(subfilename);
                break;
        case '4': /* send mails to owner */
-               initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+               sockfd = newsmtp(&ml, relayhost);
                mail.from = bounceaddr;
                mail.replyto = NULL;
                if(send_mail_many_fd(sockfd, &mail, &ml, subfd, mailfilename)) {
@@ -521,7 +510,7 @@ int main(int argc, char **argv)
                }
                
                if(verp) {
-                       initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+                       sockfd = newsmtp(&ml, relayhost);
                        if(sockfd > -1) {
                            if(write_mail_from(sockfd, verpfrom, verp)) {
                                log_error(LOG_ARGS,
@@ -572,7 +561,7 @@ int main(int argc, char **argv)
                                                        tll_remove_and_free(stl, it, free);
                                }
                                if(tll_length(stl) == maxverprecips) {
-                                       initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+                                       sockfd = newsmtp(&ml, relayhost);
                                        if(verp) {
                                                mail.from = verpfrom;
                                                mail.replyto = NULL;
@@ -601,7 +590,7 @@ int main(int argc, char **argv)
                        fclose(f);
                }
                if(tll_length(stl)) {
-                       initsmtp(&sockfd, relayhost, smtpport, smtphelo);
+                       sockfd = newsmtp(&ml, relayhost);
                        if(verp) {
                                mail.from = verpfrom;
                                mail.replyto = NULL;
@@ -627,7 +616,6 @@ int main(int argc, char **argv)
        }
 
        free(verp);
-       free(smtphelo);
 
        if(archive) {
                if(!ctrlarchive) {
index 4b26e897624d83791af6e46aedaa836c2b531d21..3196b865be6d182c48c027bbe5d371e2894f03e2 100644 (file)
@@ -352,13 +352,16 @@ send_mail(int sockfd, struct mail *mail, int listfd, int ctrlfd, bool bounce)
 }
 
 int
-newsmtp(struct ml *ml)
+newsmtp(struct ml *ml, const char *rhost)
 {
        int sockfd;
-       char *relayhost, *smtphelo;
+       char *relayhost = NULL, *smtphelo;
        unsigned short smtpport = ctrlushort(ml->ctrlfd, "smtpport", 25);
 
-       relayhost = ctrlvalue(ml->ctrlfd, "relayhost");
+       if (rhost != NULL)
+               relayhost = xstrdup(relayhost);
+       if (relayhost == NULL)
+               relayhost = ctrlvalue(ml->ctrlfd, "relayhost");
        if (relayhost == NULL)
                relayhost = xstrdup(RELAYHOST);
        smtphelo = ctrlvalue(ml->ctrlfd, "smtphelo");
@@ -403,7 +406,7 @@ send_single_mail(struct mail *mail, struct ml *ml, bool bounce)
 {
        int sockfd;
 
-       sockfd = newsmtp(ml);
+       sockfd = newsmtp(ml, NULL);
        if (sockfd == -1)
                return (false);
        if (send_mail(sockfd, mail, ml->fd, ml->ctrlfd, bounce)) {
index b80510a215f5cbedd1b18ada5eb7d7acc0d77206..bdc3b3e3c91a2e707765267d39f07206ae8f9364 100644 (file)
@@ -2004,7 +2004,7 @@ ATF_TC_BODY(newsmtp, tc)
        ml.dir = "list";
        ATF_REQUIRE_MSG(ml_open(&ml, false), "impossible to open the mailing list");
        atf_utils_create_file("list/control/smtpport", "25678");
-       int fd = newsmtp(&ml);
+       int fd = newsmtp(&ml, NULL);
        ATF_REQUIRE_MSG(fd == -1, "Socket should not have open");
        int smtppipe[2];
        ATF_REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, smtppipe) >= 0);
@@ -2038,7 +2038,7 @@ ATF_TC_BODY(newsmtp, tc)
        atf_utils_readline(smtppipe[0]);
        atf_utils_create_file("list/control/smtphelo", "heloname");
        atf_utils_create_file("list/control/relayhost", "127.0.0.1");
-       fd = newsmtp(&ml);
+       fd = newsmtp(&ml, NULL);
        ATF_REQUIRE_MSG(fd != -1, "Invalid socket");
        endsmtp(&fd);
        atf_utils_wait(p, 0, "EHLO heloname\r\nQUIT\r\n", "");