]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: var-expand - Add dovecot key for distributions attributes
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Tue, 16 Jan 2024 12:30:14 +0000 (13:30 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:12 +0000 (12:34 +0200)
src/lib/test-var-expand.c
src/lib/var-expand.c

index 76f259d32eb43479d8a55a9a0a4dc0621d57c229..3cbe22b5ba4689d632be0ba142e26453a9d71a37 100644 (file)
@@ -7,7 +7,12 @@
 #include "hostpid.h"
 #include "var-expand.h"
 #include "var-expand-private.h"
+#include "dovecot-version.h"
+
 #include <unistd.h>
+#ifdef HAVE_SYS_UTSNAME_H
+#  include <sys/utsname.h>
+#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();
 }
index 8ef40a7723c509441b8664333cf94fa2edabc038..41aa533d282c6472db01ec6bd9e43047b3be5293 100644 (file)
@@ -15,6 +15,7 @@
 #include "strescape.h"
 #include "var-expand.h"
 #include "var-expand-private.h"
+#include "dovecot-version.h"
 
 #include <unistd.h>
 #include <ctype.h>
@@ -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