]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-var-expand: Add "generate" provider
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 18 Nov 2024 10:46:33 +0000 (12:46 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 17 Jan 2025 08:40:01 +0000 (10:40 +0200)
This can be used to generate guid, guid128 and uuid with various formats.

src/lib-var-expand/test-var-expand.c
src/lib-var-expand/var-expand.c

index 0bd9431e24aa69d491c3f4d2562ec7ee098108ba..4c8582b68d07a4b69c56d1ba2f7cddd7c65981c2 100644 (file)
@@ -924,6 +924,41 @@ static void test_var_expand_set_copy(void)
        test_end();
 }
 
+static void test_var_expand_generate(void)
+{
+       const char *error;
+       string_t *str = t_str_new(64);
+
+       test_begin("var_expand(generate)");
+
+       test_assert(var_expand(str, "%{generate:guid}", NULL, &error) == 0);
+       test_assert(strstr(str_c(str), my_hostname) != NULL);
+
+       str_truncate(str, 0);
+       test_assert(var_expand(str, "%{generate:guid128}", NULL, &error) == 0);
+       test_assert(str_len(str) == 32 && strspn(str_c(str), "0123456789abcdef") == 32);
+
+       str_truncate(str, 0);
+       test_assert(var_expand(str, "%{generate:uuid}", NULL, &error) == 0);
+       test_assert(str_len(str) == 36 && strspn(str_c(str), "0123456789abcdef-") == 36);
+
+       str_truncate(str, 0);
+       test_assert(var_expand(str, "%{generate:uuid:record}", NULL, &error) == 0);
+       test_assert(str_len(str) == 36 && strspn(str_c(str), "0123456789abcdef-") == 36);
+
+       str_truncate(str, 0);
+       test_assert(var_expand(str, "%{generate:uuid:compact}", NULL, &error) == 0);
+       test_assert(str_len(str) == 32 && strspn(str_c(str), "0123456789abcdef") == 32);
+
+       str_truncate(str, 0);
+       test_assert(var_expand(str, "%{generate:uuid:microsoft}", NULL, &error) == 0);
+       test_assert(str_len(str) == 38 && str_c(str)[0] == '{' &&
+                   strspn(str_c(str)+1, "0123456789abcdef-") == 36 &&
+                   str_c(str)[37] == '}');
+
+       test_end();
+}
+
 int main(void)
 {
        void (*const tests[])(void) = {
@@ -941,6 +976,7 @@ int main(void)
                test_var_expand_parameter_sorted,
                test_var_expand_perc,
                test_var_expand_set_copy,
+               test_var_expand_generate,
                NULL
        };
 
index efe9684c10d103775c279c2c0e96ba203afed19e..960412045b888f7cae988ece394ec4e49fffd175 100644 (file)
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "array.h"
 #include "cpu-count.h"
+#include "guid.h"
 #include "hostpid.h"
 #include "str.h"
 #include "time-util.h"
@@ -217,6 +218,35 @@ static int var_expand_time(const char *key, const char **value_r,
        return 0;
 }
 
+static int var_expand_generate(const char *key, const char **value_r,
+                              void *context ATTR_UNUSED, const char **error_r)
+{
+       guid_128_t guid;
+
+       if (strcmp(key, "guid") == 0) {
+               *value_r = guid_generate();
+               return 0;
+       }
+       if (strcmp(key, "guid128") == 0) {
+               guid_128_generate(guid);
+               *value_r = guid_128_to_string(guid);
+               return 0;
+       }
+       if (str_begins(key, "uuid", &key)) {
+               guid_128_uuid4_generate(guid);
+               if (key[0] == '\0' || strcmp(key, ":record") == 0)
+                       *value_r = guid_128_to_uuid_string(guid, FORMAT_RECORD);
+               else if (strcmp(key, ":compact") == 0)
+                       *value_r = guid_128_to_uuid_string(guid, FORMAT_COMPACT);
+               else if (strcmp(key, ":microsoft") == 0)
+                       *value_r = guid_128_to_uuid_string(guid, FORMAT_MICROSOFT);
+               else
+                       ERROR_UNSUPPORTED_KEY(key);
+               return 0;
+       }
+       ERROR_UNSUPPORTED_KEY(key);
+}
+
 static const struct var_expand_provider internal_providers[] = {
        { .key = "process", .func = var_expand_process },
        { .key = "system", .func = var_expand_system  },
@@ -225,6 +255,7 @@ static const struct var_expand_provider internal_providers[] = {
        { .key = "event", .func = var_expand_event },
        { .key = "date", .func = var_expand_date },
        { .key = "time", .func = var_expand_time },
+       { .key = "generate", .func = var_expand_generate },
        VAR_EXPAND_TABLE_END
 };