From: Karl Fleischmann Date: Tue, 16 Jan 2024 12:30:14 +0000 (+0100) Subject: lib: var-expand - Add dovecot key for distributions attributes X-Git-Tag: 2.4.1~1048 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11ac1bb26ce6f0d3026873a214d1845d1ac6f3ea;p=thirdparty%2Fdovecot%2Fcore.git lib: var-expand - Add dovecot key for distributions attributes --- diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index 76f259d32e..3cbe22b5ba 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -7,7 +7,12 @@ #include "hostpid.h" #include "var-expand.h" #include "var-expand-private.h" +#include "dovecot-version.h" + #include +#ifdef HAVE_SYS_UTSNAME_H +# include +#endif struct var_expand_test { const char *in; @@ -568,6 +573,43 @@ static void test_var_expand_system() test_end(); } +static void +test_var_expand_dovecot(void) +{ + static const struct var_expand_table table[] = { + { '\0', NULL, NULL } + }; + + int ret; + const char *error; + string_t *dest = t_str_new(64); + test_begin("var_expand_dovecot"); + + /* Available keys should be correctly expanded. */ + static const struct var_expand_test tests[] = { + { "%{dovecot:name}", PACKAGE_NAME, 1 }, + { "%{dovecot:version}", PACKAGE_VERSION, 1 }, + { "%{dovecot:support-url}", PACKAGE_WEBPAGE, 1 }, + { "%{dovecot:support-email}", PACKAGE_BUGREPORT, 1 }, + { "%{dovecot:revision}", DOVECOT_REVISION, 1 }, + }; + + for (size_t i = 0; i < N_ELEMENTS(tests); i++) { + str_truncate(dest, 0); + + ret = var_expand(dest, tests[i].in, table, &error); + test_assert_idx(tests[i].ret == ret, i); + test_assert_idx(strcmp(tests[i].out, str_c(dest)) == 0, i); + } + + /* Make sure invalid keys are rejected. */ + str_truncate(dest, 0); + test_assert(var_expand(dest, "%{dovecot:invalid}", table, &error) == 0); + test_assert(strcmp(error, "Unsupported dovecot key 'invalid'") == 0); + + test_end(); +} + void test_var_expand(void) { test_var_expand_ranges(); @@ -581,4 +623,5 @@ void test_var_expand(void) test_var_expand_if(); test_var_expand_merge_tables(); test_var_expand_system(); + test_var_expand_dovecot(); } diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 8ef40a7723..41aa533d28 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -15,6 +15,7 @@ #include "strescape.h" #include "var-expand.h" #include "var-expand-private.h" +#include "dovecot-version.h" #include #include @@ -425,6 +426,34 @@ var_expand_system(struct var_expand_context *ctx ATTR_UNUSED, return 0; } +static int +var_expand_dovecot(struct var_expand_context *ctx ATTR_UNUSED, + const char *key, const char *field, + const char **result_r, const char **error_r) +{ + i_assert(strcmp(key, "dovecot") == 0); + + if (strcmp(field, "name") == 0) { + *result_r = PACKAGE_NAME; + return 1; + } else if (strcmp(field, "version") == 0) { + *result_r = PACKAGE_VERSION; + return 1; + } else if (strcmp(field, "support-url") == 0) { + *result_r = PACKAGE_WEBPAGE; + return 1; + } else if (strcmp(field, "support-email") == 0) { + *result_r = PACKAGE_BUGREPORT; + return 1; + } else if (strcmp(field, "revision") == 0) { + *result_r = DOVECOT_REVISION; + return 1; + } + + *error_r = t_strdup_printf("Unsupported dovecot key '%s'", field); + return 0; +} + static int var_expand_func(const struct var_expand_func_table *const *func_tables, const char *key, const char *data, void *const *contexts, @@ -863,6 +892,11 @@ void var_expand_extensions_init(void) func = array_append_space(&var_expand_extensions); func->key = "process"; func->func = var_expand_process; + + /* dovecot */ + func = array_append_space(&var_expand_extensions); + func->key = "dovecot"; + func->func = var_expand_dovecot; } void