From: Baptiste Daroussin Date: Tue, 18 Jul 2023 09:34:46 +0000 (+0200) Subject: mlmmj-send: use newsmtp to reduce code duplication X-Git-Tag: RELEASE_1_4_0rc1~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db3555fe38798379d7fe189e720d2514f217a774;p=thirdparty%2Fmlmmj.git mlmmj-send: use newsmtp to reduce code duplication --- diff --git a/include/send_mail.h b/include/send_mail.h index e0d1e7d4..c7fce13b 100644 --- a/include/send_mail.h +++ b/include/send_mail.h @@ -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); diff --git a/src/mlmmj-send.c b/src/mlmmj-send.c index 2f81590d..8bf32136 100644 --- a/src/mlmmj-send.c +++ b/src/mlmmj-send.c @@ -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) { diff --git a/src/send_mail.c b/src/send_mail.c index 4b26e897..3196b865 100644 --- a/src/send_mail.c +++ b/src/send_mail.c @@ -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)) { diff --git a/tests/mlmmj.c b/tests/mlmmj.c index b80510a2..bdc3b3e3 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -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", "");