]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add some event handling functions.
authorSergey Kitov <sergey.kitov@open-xchange.com>
Mon, 26 Feb 2018 14:33:19 +0000 (16:33 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Fri, 7 Sep 2018 06:58:49 +0000 (09:58 +0300)
event_has_all_categories() - checks if given event contains all the
categories of an another event.
event_has_all_fields() - checks if given event contains all the fields
of an another event.
event_dup() - duplicates an event.
event_copy_categories_fields() - copies all categories and fields from
source to dest.

src/lib/lib-event.c
src/lib/lib-event.h

index 416e6b56dd962d15106521218691261844a28925..4c66fdf515ca69663425c5916cf686264db54303 100644 (file)
@@ -46,6 +46,75 @@ static void event_copy_parent_defaults(struct event *event,
        event->forced_debug = parent->forced_debug;
 }
 
+static bool
+event_find_category(struct event *event, const struct event_category *category);
+
+static struct event_field *
+event_find_field_int(struct event *event, const char *key);
+
+void event_copy_categories_fields(struct event *to, struct event *from)
+{
+       unsigned int cat_count;
+       struct event_category *const *categories = event_get_categories(from, &cat_count);
+       while (cat_count-- > 0)
+               event_add_category(to, categories[cat_count]);
+       const struct event_field *fld;
+       if (!array_is_created(&from->fields))
+               return;
+       array_foreach(&from->fields, fld) {
+               switch (fld->value_type) {
+               case EVENT_FIELD_VALUE_TYPE_STR:
+                       event_add_str(to, fld->key, fld->value.str);
+                       break;
+               case EVENT_FIELD_VALUE_TYPE_INTMAX:
+                       event_add_int(to, fld->key, fld->value.intmax);
+                       break;
+               case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
+                       event_add_timeval(to, fld->key, &fld->value.timeval);
+                       break;
+               default:
+                       break;
+               }
+       }
+}
+
+bool event_has_all_categories(struct event *event, const struct event *other)
+{
+       struct event_category **cat;
+       if (!array_is_created(&other->categories))
+               return TRUE;
+       if (!array_is_created(&event->categories))
+               return FALSE;
+       array_foreach_modifiable(&other->categories, cat) {
+               if (!event_find_category(event, *cat))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+bool event_has_all_fields(struct event *event, const struct event *other)
+{
+       struct event_field *fld;
+       if (!array_is_created(&other->fields))
+               return TRUE;
+       array_foreach_modifiable(&other->fields, fld) {
+               if (event_find_field_int(event, fld->key) == NULL)
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+struct event *event_dup(const struct event *source)
+{
+       struct event *ret = event_create(source->parent);
+       string_t *str = t_str_new(256);
+       const char *err;
+       event_export(source, str);
+       if (!event_import(ret, str_c(str), &err))
+               i_panic("event_import(%s) failed: %s", str_c(str), err);
+       return ret;
+}
+
 #undef event_create
 struct event *event_create(struct event *parent, const char *source_filename,
                           unsigned int source_linenum)
index cd4a84abdb2f831e364a9ccd9df7434462b30b92..d2838469757722166f0af43c8c12a7217d6e4a64 100644 (file)
@@ -73,6 +73,19 @@ struct event_passthrough {
        struct event *(*event)(void);
 };
 
+/* Returns TRUE if the event has all the categories that the "other" event has (and maybe more). */
+bool event_has_all_categories(struct event *event, const struct event *other);
+/* Returns TRUE if the event has all the fields that the "other" event has (and maybe more).
+   Only the fields in the events themselves are checked. Parent events' fields are not checked. */
+bool event_has_all_fields(struct event *event, const struct event *other);
+
+/* Returns the source event duplicated into a new event. */
+struct event *event_dup(const struct event *source);
+/* Copy all categories and fields from source to dest.
+   Only the fields and categories in source event itself are copied.
+   Parent events' fields and categories aren't copied. */
+void event_copy_categories_fields(struct event *dest, struct event *source);
+
 /* Create a new empty event under the parent event, or NULL for root event. */
 struct event *event_create(struct event *parent, const char *source_filename,
                           unsigned int source_linenum);