From: Timo Sirainen Date: Mon, 18 Nov 2024 10:46:33 +0000 (+0200) Subject: lib-var-expand: Add "generate" provider X-Git-Tag: 2.4.0~226 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=45624c2a61c7fa00a4fb0f0ed9672be5821b2ac4;p=thirdparty%2Fdovecot%2Fcore.git lib-var-expand: Add "generate" provider This can be used to generate guid, guid128 and uuid with various formats. --- diff --git a/src/lib-var-expand/test-var-expand.c b/src/lib-var-expand/test-var-expand.c index 0bd9431e24..4c8582b68d 100644 --- a/src/lib-var-expand/test-var-expand.c +++ b/src/lib-var-expand/test-var-expand.c @@ -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 }; diff --git a/src/lib-var-expand/var-expand.c b/src/lib-var-expand/var-expand.c index efe9684c10..960412045b 100644 --- a/src/lib-var-expand/var-expand.c +++ b/src/lib-var-expand/var-expand.c @@ -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 };