]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
plugins/push-notification: push-notification-settings - Add new settings API infrastr...
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Thu, 7 Sep 2023 10:23:38 +0000 (12:23 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:10 +0000 (12:34 +0200)
src/config/Makefile.am
src/plugins/push-notification/Makefile.am
src/plugins/push-notification/push-notification-settings.c [new file with mode: 0644]
src/plugins/push-notification/push-notification-settings.h [new file with mode: 0644]

index cf2b60c1302dc59d99cd86a4dea4ab79be98d013..0e5290fa5a53ce65c08d5bd8b52b930fcf5b5836 100644 (file)
@@ -10,6 +10,7 @@ AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib-test \
        -I$(top_srcdir)/src/lib-dns \
        -I$(top_srcdir)/src/lib-mail \
+       -I$(top_srcdir)/src/lib-http \
        -I$(top_srcdir)/src/lib-settings \
        -I$(top_srcdir)/src/lib-master \
        -I$(top_srcdir)/src/lib-pop3 \
index daa43a2d4d1bb1b578294de2706e77475aa7381a..eab09a202891d82d309bc3d6e03fe528e7d7cd2e 100644 (file)
@@ -41,6 +41,7 @@ lib20_push_notification_plugin_la_SOURCES = \
        push-notification-events.c \
        push-notification-events-rfc5423.c \
        push-notification-plugin.c \
+       push-notification-settings.c \
        push-notification-triggers.c \
        push-notification-txn-mbox.c \
        push-notification-txn-msg.c
@@ -63,6 +64,7 @@ headers = \
        push-notification-events.h \
        push-notification-events-rfc5423.h \
        push-notification-plugin.h \
+       push-notification-settings.h \
        push-notification-triggers.h \
        push-notification-txn-mbox.h \
        push-notification-txn-msg.h
diff --git a/src/plugins/push-notification/push-notification-settings.c b/src/plugins/push-notification/push-notification-settings.c
new file mode 100644 (file)
index 0000000..bc5ce51
--- /dev/null
@@ -0,0 +1,163 @@
+/* Copyright (c) 2023 Dovecot authors, see the included COPYING file */
+
+#include <unistd.h>
+
+#include "lib.h"
+#include "array.h"
+#include "settings.h"
+#include "settings-parser.h"
+
+#include "push-notification-settings.h"
+
+/* <settings checks> */
+#include "http-url.h"
+/* </settings checks> */
+
+#define PUSH_NOTIFICATION_DRIVER_OX_DEFAULT_CACHE_TTL_MSECS (60 * 1000)
+
+static bool push_notification_lua_settings_check(void *, pool_t, const char **);
+static bool push_notification_settings_check(void *, pool_t, const char **);
+static bool push_notification_ox_settings_check(void *, pool_t, const char **);
+
+#undef DEF
+#define DEF(type, name) \
+       SETTING_DEFINE_STRUCT_##type("push_notification_lua_"#name, name, struct push_notification_lua_settings)
+
+static const struct setting_define push_notification_lua_setting_defines[] = {
+       DEF(STR, path),
+
+       SETTING_DEFINE_LIST_END,
+};
+
+static const struct push_notification_lua_settings push_notification_lua_default_settings = {
+       .path = "",
+};
+
+const struct setting_parser_info push_notification_lua_setting_parser_info = {
+       .name = "push_notification_lua",
+
+       .defines = push_notification_lua_setting_defines,
+       .defaults = &push_notification_lua_default_settings,
+
+       .struct_size = sizeof(struct push_notification_lua_settings),
+       .pool_offset1 = 1 + offsetof(struct push_notification_lua_settings, pool),
+       .check_func = push_notification_lua_settings_check,
+};
+
+#undef DEF
+#define DEF(type, name) \
+       SETTING_DEFINE_STRUCT_##type("push_notification_ox_"#name, name, struct push_notification_ox_settings)
+
+static const struct setting_define push_notification_ox_setting_defines[] = {
+       DEF(STR, url),
+       DEF(TIME_MSECS, cache_ttl),
+       DEF(BOOL, user_from_metadata),
+
+       SETTING_DEFINE_LIST_END,
+};
+
+static const struct push_notification_ox_settings push_notification_ox_default_settings = {
+       .url = "",
+       .cache_ttl = PUSH_NOTIFICATION_DRIVER_OX_DEFAULT_CACHE_TTL_MSECS,
+       .user_from_metadata = FALSE,
+};
+
+const struct setting_parser_info push_notification_ox_setting_parser_info = {
+       .name = "push_notification_ox",
+
+       .defines = push_notification_ox_setting_defines,
+       .defaults = &push_notification_ox_default_settings,
+
+       .struct_size = sizeof(struct push_notification_ox_settings),
+       .pool_offset1 = 1 + offsetof(struct push_notification_ox_settings, pool),
+       .check_func = push_notification_ox_settings_check,
+};
+
+#undef DEF
+#define DEF(type, name) \
+       SETTING_DEFINE_STRUCT_##type("push_notification_"#name, name, struct push_notification_settings)
+
+static const struct setting_define push_notification_setting_defines[] = {
+       DEF(STR, name),
+       DEF(STR, driver),
+       {
+               .type = SET_FILTER_ARRAY,
+               .key = PUSH_NOTIFICATION_SETTINGS_FILTER_NAME,
+               .offset = offsetof(struct push_notification_settings, push_notifications),
+               .filter_array_field_name = "name",
+       },
+
+       SETTING_DEFINE_LIST_END,
+};
+
+static const struct push_notification_settings push_notification_default_settings = {
+       .name = "",
+       .driver = "",
+       .push_notifications = ARRAY_INIT,
+};
+
+const struct setting_parser_info push_notification_setting_parser_info = {
+       .name = "push_notification",
+
+       .defines = push_notification_setting_defines,
+       .defaults = &push_notification_default_settings,
+
+       .struct_size = sizeof(struct push_notification_settings),
+       .pool_offset1 = 1 + offsetof(struct push_notification_settings, pool),
+
+       .check_func = push_notification_settings_check,
+};
+
+/* <settings checks> */
+static bool
+push_notification_lua_settings_check(void *_set, pool_t ATTR_UNUSED pool,
+                                    const char **error_r)
+{
+       struct push_notification_lua_settings *set = _set;
+
+       if (set->path[0] != '\0' && access(set->path, F_OK) != 0) {
+               *error_r = t_strdup_printf(
+                               "push_notification_lua_path: access(%s) failed: %m",
+                               set->path);
+               return FALSE;
+       }
+       return TRUE;
+}
+
+static bool
+push_notification_settings_check(void *_set, pool_t pool ATTR_UNUSED,
+                                const char **error_r ATTR_UNUSED)
+{
+       struct push_notification_settings *set = _set;
+
+       if (set->driver[0] == '\0')
+               set->driver = set->name;
+       return TRUE;
+}
+
+static bool
+push_notification_ox_settings_check(void *_set, pool_t pool,
+                                   const char **error_r)
+{
+       struct push_notification_ox_settings *set = _set;
+       const char *error;
+
+       if (set->url[0] != '\0') {
+               if (http_url_parse(set->url, NULL, HTTP_URL_ALLOW_USERINFO_PART,
+                                  pool, &set->parsed_url, &error) < 0) {
+                       *error_r = t_strdup_printf(
+                               "Invalid push_notification_ox_url '%s': %s",
+                               set->url, error);
+                       return FALSE;
+               }
+       } else
+               set->parsed_url = NULL;
+
+       if (set->cache_ttl == 0) {
+               *error_r = "push_notification_ox_cache_ttl must not be 0";
+               return FALSE;
+       }
+
+       return TRUE;
+}
+/* </settings checks> */
diff --git a/src/plugins/push-notification/push-notification-settings.h b/src/plugins/push-notification/push-notification-settings.h
new file mode 100644 (file)
index 0000000..d287941
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef PUSH_NOTIFICATION_SETTINGS_H
+#define PUSH_NOTIFICATION_SETTINGS_H
+
+/* <settings checks> */
+#define PUSH_NOTIFICATION_SETTINGS_FILTER_NAME "push_notification"
+/* </settings checks> */
+
+struct push_notification_lua_settings {
+       pool_t pool;
+
+       const char *path;
+};
+
+struct push_notification_ox_settings {
+       pool_t pool;
+
+       const char *url;
+       unsigned int cache_ttl;
+       bool user_from_metadata;
+
+       /* Generated: */
+       struct http_url *parsed_url;
+};
+
+struct push_notification_settings {
+       pool_t pool;
+       const char *name;
+       const char *driver;
+
+       ARRAY_TYPE(const_string) push_notifications;
+};
+
+extern const struct setting_parser_info push_notification_lua_setting_parser_info;
+extern const struct setting_parser_info push_notification_ox_setting_parser_info;
+extern const struct setting_parser_info push_notification_setting_parser_info;
+
+#endif