]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Use a generic function instead of findit
authorBaptiste Daroussin <bapt@FreeBSD.org>
Thu, 13 Nov 2025 13:07:29 +0000 (14:07 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Thu, 13 Nov 2025 13:21:55 +0000 (14:21 +0100)
contrib/receivestrip/mlmmj-receive-strip.c
include/utils.h
src/do_all_the_voodoo_here.c
src/utils.c
tests/mlmmj.c

index 976d98812f18574d388cd994dfda3c043a77a338..267ac5d9f384b12c39ebdfa09a15207b0cef2f8f 100644 (file)
@@ -47,7 +47,6 @@
 #include "ctrlvalue.h"
 #include "ctrlvalues.h"
 #include "utils.h"
-#include "do_all_the_voodoo_here.h"
 
 #include "log_error.h"
 #include "wrappers.h"
@@ -92,10 +91,10 @@ static int read_hdrs(int fd, strlist *allhdrs, strlist* delmime, strlist* denymi
        parse_content_type(allhdrs,&mime_type,boundary);
        if(mime_type) {
                /* check if this part should be stripped */
-               if(delmime && findit(mime_type, delmime))
+               if(delmime && line_startwithany(mime_type, delmime, true))
                        result = MIME_STRIP;
                /* check if the mail should be denied */
-               if(denymime && findit(mime_type, denymime))
+               if(denymime && line_startwithany(mime_type, denymime, true))
                        *deny = 1;
                free(mime_type);
        }
index 3de12e51ac4b6b87b7cd5e1bc5c5f3d0ece3640b..e9945ea321d304b70b1b657c211516b1cdc3d2c0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
+ * Copyright (C) 2025 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
@@ -41,3 +41,4 @@ time_t strtotimet(const char *np, const char **errpp);
 bool lock(int fd, bool write);
 bool find_email(strlist *list, strlist *matches, const char *delim, char **recipextra);
 char *readlf(int fd, bool oneline);
+bool line_startwithany(const char *el, const strlist *list, bool ignore_case);
index 184ac5c9f4c23b33b2b6b58e384c24f45d8ca07f..6102a6f76281159dbc67c6e9154fd0a9271dbe32 100644 (file)
 #include "log_error.h"
 #include "wrappers.h"
 #include "find_email_adr.h"
+#include "utils.h"
 
-bool
-findit(const char *line, const strlist *headers)
-{
-       size_t len;
-
-       tll_foreach(*headers, it) {
-               len = strlen(it->item);
-               if(strncasecmp(line, it->item, len) == 0)
-                       return true;
-       }
-
-       return false;
-}
 
 /* read line and populate a struct to save a list important header see mlmmj-process.c:254 and mlmmj.h:97 */
 void getinfo(const char *line, struct mailhdr *readhdrs)
@@ -163,7 +151,7 @@ int do_all_the_voodoo_here(int infd, int outfd, int hdrfd, int footfd,
                }
 
                /* Should it be stripped? */
-               if(!delhdrs || !findit(hdrline, delhdrs))
+               if(!delhdrs || !line_startwithany(hdrline, delhdrs, true))
                        dprintf(outfd, "%s", unfolded);
 
                /* Should Reply-To be added? */
index 9b206021d8241fa70dc87c782431bdd8f66a9115..4747ace1f159d5603cfe8fd32acbc5053ae597c6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2021-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+ * Copyright (C) 2021-2025 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
@@ -337,3 +337,20 @@ readlf(int fd, bool oneline)
        }
        return (buf);
 }
+
+typedef int (*cmp_fn_t)(const char *, const char *, size_t);
+
+bool
+line_startwithany(const char *el, const strlist *list,  bool ignore_case)
+{
+       size_t len;
+       cmp_fn_t cmp = ignore_case ? strncasecmp : strncmp;
+
+       tll_foreach(*list, it) {
+               len = strlen(it->item);
+               if (cmp(el, it->item, len) == 0)
+                       return (true);
+       }
+
+       return (false);
+}
index 9c74d3189e69c5f7bdfa640054b47750fb37ab12..1330262ac02d7aaa86dbb3b40013110e6752c6b5 100644 (file)
@@ -167,6 +167,7 @@ ATF_TC_WITHOUT_HEAD(parse_content_type);
 ATF_TC_WITHOUT_HEAD(find_in_list);
 ATF_TC_WITHOUT_HEAD(do_access);
 ATF_TC_WITHOUT_HEAD(unistr_header_to_utf8);
+ATF_TC_WITHOUT_HEAD(line_startwithany);
 
 ATF_TC_BODY(random_int, tc)
 {
@@ -3081,6 +3082,19 @@ ATF_TC_BODY(unistr_header_to_utf8, tc)
     ATF_REQUIRE_STREQ(out, "useless but why not");
 }
 
+ATF_TC_BODY(line_startwithany, tc)
+{
+       strlist l  = tll_init();
+       tll_push_back(l, "bla");
+       ATF_REQUIRE(!line_startwithany("BLA", &l, false));
+       ATF_REQUIRE(line_startwithany("BLA", &l, true));
+       ATF_REQUIRE(!line_startwithany("BLOP", &l, false));
+       ATF_REQUIRE(!line_startwithany("BLOP", &l, true));
+       tll_push_back(l, "BLOP");
+       ATF_REQUIRE(line_startwithany("BLOP", &l, false));
+       ATF_REQUIRE(line_startwithany("BLOP", &l, true));
+}
+
 ATF_TP_ADD_TCS(tp)
 {
        ATF_TP_ADD_TC(tp, random_int);
@@ -3175,6 +3189,7 @@ ATF_TP_ADD_TCS(tp)
        ATF_TP_ADD_TC(tp, find_in_list);
        ATF_TP_ADD_TC(tp, do_access);
        ATF_TP_ADD_TC(tp, unistr_header_to_utf8);
+       ATF_TP_ADD_TC(tp, line_startwithany);
 
        return (atf_no_error());
 }