]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
utils: add function to convert a control file to time_t
authorBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 15:30:23 +0000 (17:30 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 15:30:23 +0000 (17:30 +0200)
include/ctrlvalue.h
include/utils.h
src/ctrlvalue.c
src/mlmmj-maintd.c
src/utils.c
tests/mlmmj.c

index 3551ffefa45b96aac39bbc13ec63a1e256b0c074..e9417bb394c9ae37dd6af7d0bf1fb40b1c18f579 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <sys/limits.h>
 #include <stdint.h>
+#include <time.h>
 
 char *ctrlvalue(const char *listdir, const char *ctrlstr);
 char *ctrlcontent(const char *listdir, const char *ctrlstr);
@@ -49,5 +50,6 @@ ctrlushort(const char *listdir, const char *ctrlstr, unsigned short fallback)
 {
        return (ctrluim(listdir, ctrlstr, 0, USHRT_MAX, fallback));
 }
+time_t ctrltimet(const char *listdir, const char *ctrlstr, time_t fallback);
 
 #endif /* CTRLVALUE_H */
index 7e4b4bd8e22566cf7ad869d7873aaaf7b274b472..6fd6afdc3e0be5b0d92c07e7a63d247a81b0c2f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2021 Baptiste Daroussin <bapt@FreeBSD.org>
+ * Copyright (C) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
 
 #pragma once
 
+#include <time.h>
+
 char *lowercase(const char *);
 intmax_t strtoim(const char *np, intmax_t minval, intmax_t maxval,
     const char **errpp);
 uintmax_t strtouim(const char *, uintmax_t, uintmax_t, const char **);
 void exec_or_die(const char *arg, ...);
 int exec_and_wait(const char *arg, ...);
+time_t strtotimet(const char *np, const char **errpp);
index a2b8f54817f527b06e348b6b0ba8fe5d7844b9c6..15b698f1d1f9cd8067dc5d265495bebc2511c606 100644 (file)
@@ -124,3 +124,23 @@ ctrluim(const char *listdir, const char *ctrlstr, uintmax_t min, uintmax_t max,
        free(val);
        return (ret);
 }
+
+time_t
+ctrltimet(const char *listdir, const char *ctrlstr, time_t fallback)
+{
+       const char *errstr;
+       char *val = ctrlvalue(listdir, ctrlstr);
+       time_t ret;
+
+       if (val == NULL)
+               return (fallback);
+
+       ret = strtotimet(val, &errstr);
+       if (errstr != NULL) {
+               log_error(LOG_ARGS, "Invalid value for '%s': %s", ctrlstr,
+                   errstr);
+               return (fallback);
+       }
+       free(val);
+       return (ret);
+}
index f64236f5d9dfb2a92176affba9769a96c358a451..aebde8dcc2fbba4950009364bf10f721cc78cd5b 100644 (file)
@@ -111,19 +111,11 @@ int delolder(const char *dirname, time_t than)
 int clean_moderation(const char *listdir)
 {
 
-       time_t modreqlife = 0;
-       char *modreqlifestr;
+       time_t modreqlife;
        char *moddirname;
        int ret;
 
-       modreqlifestr = ctrlvalue(listdir, "modreqlife");
-       if(modreqlifestr) {
-               modreqlife = atol(modreqlifestr);
-               free(modreqlifestr);
-       }
-       if(modreqlife == 0)
-               modreqlife = MODREQLIFE;
-
+       modreqlife = ctrltimet(listdir, "modreqlife", MODREQLIFE);
        moddirname = concatstr(2, listdir, "/moderation");
        ret = delolder(moddirname, modreqlife);
                
@@ -192,10 +184,9 @@ int resend_queue(const char *listdir, const char *mlmmjsend)
        struct dirent *dp;
        char *mailname, *fromname, *toname, *reptoname, *from, *to, *repto;
        char *ch, *dirname = concatstr(2, listdir, "/queue/");
-       char *bouncelifestr;
        struct stat st;
        int fromfd, tofd, fd, err = 0;
-       time_t t, bouncelife = 0;
+       time_t t, bouncelife;
 
        if(chdir(dirname) < 0) {
                log_error(LOG_ARGS, "Could not chdir(%s)", dirname);
@@ -293,14 +284,7 @@ int resend_queue(const char *listdir, const char *mlmmjsend)
                }
 
                /* before we try again, check and see if it's old */
-               bouncelifestr = ctrlvalue(listdir, "bouncelife");
-               if(bouncelifestr) {
-                       bouncelife = atol(bouncelifestr);
-                       free(bouncelifestr);
-               }
-               if(bouncelife == 0)
-                       bouncelife = BOUNCELIFE;
-
+               bouncelife = ctrltimet(listdir, "bouncelife", BOUNCELIFE);
                t = time(NULL);
                if(t - st.st_mtime > bouncelife) {
                        unlink(mailname);
@@ -561,11 +545,10 @@ int unsub_bouncers(const char *listdir, const char *mlmmjunsub)
        DIR *bouncedir;
        char *dirname = concatstr(2, listdir, "/bounce/");
        char *probefile, *address, *a, *firstbounce, *bouncedata;
-       char *bouncelifestr;
        struct dirent *dp;
        struct stat st;
        int fd;
-       time_t bouncetime, t, bouncelife = 0;
+       time_t bouncetime, t, bouncelife;
        
        if(chdir(dirname) < 0) {
                log_error(LOG_ARGS, "Could not chdir(%s)", dirname);
@@ -581,14 +564,7 @@ int unsub_bouncers(const char *listdir, const char *mlmmjunsub)
 
        free(dirname);
 
-       bouncelifestr = ctrlvalue(listdir, "bouncelife");
-       if(bouncelifestr) {
-               bouncelife = atol(bouncelifestr);
-               free(bouncelifestr);
-       }
-       
-       if(bouncelife == 0)
-               bouncelife = BOUNCELIFE;
+       bouncelife = ctrltimet(listdir, "bouncelife", BOUNCELIFE);
 
        while((dp = readdir(bouncedir)) != NULL) {
                if((strcmp(dp->d_name, "..") == 0) ||
index 0a6eef6bbecf7506dc1af3e8b45d856b01c71825..cab6f7bb5cfc87b88487ce20f8b7e84f8c35757a 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <sys/wait.h>
+#include <sys/param.h>
 
 #include <ctype.h>
 #include <err.h>
@@ -66,6 +67,14 @@ strtoim(const char *np, intmax_t minval, intmax_t maxval, const char **errpp)
        return (ret);
 }
 
+time_t
+strtotimet(const char *np, const char **errpp)
+{
+       if (sizeof(time_t) == 4)
+               return ((time_t)strtoim(np, INT_MIN, INT_MAX, errpp));
+       return ((time_t)strtoim(np, LLONG_MIN, LLONG_MAX, errpp));
+}
+
 uintmax_t
 strtouim(const char *np, uintmax_t minval, uintmax_t maxval, const char **errpp)
 {
index 0bbb67bdaad2a0f71855659c6c8925993ad14f2d..dfe5b66d36aea321fba790cdd50dd4dcfc6eb2f4 100644 (file)
@@ -69,6 +69,7 @@ ATF_TC_WITHOUT_HEAD(write_replyto);
 ATF_TC_WITHOUT_HEAD(write_rcpt_to);
 ATF_TC_WITHOUT_HEAD(write_mail_from);
 ATF_TC_WITHOUT_HEAD(write_mailbody_from_map);
+ATF_TC_WITHOUT_HEAD(strtotimet);
 
 #ifndef NELEM
 #define NELEM(array)    (sizeof(array) / sizeof((array)[0]))
@@ -673,6 +674,18 @@ ATF_TC_BODY(write_mailbody_from_map, tc)
        close(fd1);
 }
 
+ATF_TC_BODY(strtotimet, tc)
+{
+       const char *errp;
+       char *str;
+       ATF_REQUIRE_EQ(strtotimet("10", &errp), 10);
+       ATF_CHECK(errp == NULL);
+       asprintf(&str, "1%"PRIdMAX, INTMAX_MAX);
+       ATF_REQUIRE_EQ(strtotimet(str, &errp), 0);
+       free(str);
+       ATF_REQUIRE(errp != NULL);
+       ATF_REQUIRE_STREQ(errp, "too large");
+}
 
 ATF_TP_ADD_TCS(tp)
 {
@@ -698,6 +711,7 @@ ATF_TP_ADD_TCS(tp)
        ATF_TP_ADD_TC(tp, write_rcpt_to);
        ATF_TP_ADD_TC(tp, write_mail_from);
        ATF_TP_ADD_TC(tp, write_mailbody_from_map);
+       ATF_TP_ADD_TC(tp, strtotimet);
 
        return (atf_no_error());
 }