From: Baptiste Daroussin Date: Thu, 13 Nov 2025 13:07:29 +0000 (+0100) Subject: Use a generic function instead of findit X-Git-Tag: RELEASE_1_7_0~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f70a843ef39fee32b51aae659577cd32e227453d;p=thirdparty%2Fmlmmj.git Use a generic function instead of findit --- diff --git a/contrib/receivestrip/mlmmj-receive-strip.c b/contrib/receivestrip/mlmmj-receive-strip.c index 976d9881..267ac5d9 100644 --- a/contrib/receivestrip/mlmmj-receive-strip.c +++ b/contrib/receivestrip/mlmmj-receive-strip.c @@ -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); } diff --git a/include/utils.h b/include/utils.h index 3de12e51..e9945ea3 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Baptiste Daroussin + * Copyright (C) 2025 Baptiste Daroussin * * 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); diff --git a/src/do_all_the_voodoo_here.c b/src/do_all_the_voodoo_here.c index 184ac5c9..6102a6f7 100644 --- a/src/do_all_the_voodoo_here.c +++ b/src/do_all_the_voodoo_here.c @@ -34,20 +34,8 @@ #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? */ diff --git a/src/utils.c b/src/utils.c index 9b206021..4747ace1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Baptiste Daroussin + * Copyright (C) 2021-2025 Baptiste Daroussin * * 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); +} diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 9c74d318..1330262a 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -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()); }