]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
push-notification: Deduplicate MessageNew and MessageAppend events' data gathering...
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 7 Nov 2019 16:49:02 +0000 (18:49 +0200)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 22 Nov 2019 10:35:55 +0000 (12:35 +0200)
It would have been simpler to share their structs, but that can't be done cleanly
without breaking the API's backwards compatibility.

src/plugins/push-notification/Makefile.am
src/plugins/push-notification/push-notification-event-message-common.c [new file with mode: 0644]
src/plugins/push-notification/push-notification-event-message-common.h
src/plugins/push-notification/push-notification-event-messageappend.c
src/plugins/push-notification/push-notification-event-messagenew.c

index 469b96be5e832fec1c102c34024365855a33b257..c065e3853ea1e7a8e94df89dfe2249dfe64bdb33 100644 (file)
@@ -36,6 +36,7 @@ lib20_push_notification_plugin_la_SOURCES = \
        push-notification-event-messagenew.c \
        push-notification-event-messageread.c \
        push-notification-event-messagetrash.c \
+       push-notification-event-message-common.c \
        push-notification-events.c \
        push-notification-events-rfc5423.c \
        push-notification-plugin.c \
diff --git a/src/plugins/push-notification/push-notification-event-message-common.c b/src/plugins/push-notification/push-notification-event-message-common.c
new file mode 100644 (file)
index 0000000..3d3f12c
--- /dev/null
@@ -0,0 +1,79 @@
+/* Copyright (c) 2015-2019 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "mail-storage.h"
+#include "push-notification-event-message-common.h"
+
+void push_notification_message_fill(struct mail *mail, pool_t pool,
+                                   enum push_notification_event_message_flags event_flags,
+                                   const char **from, const char **to,
+                                   const char **subject,
+                                   time_t *date, int *date_tz,
+                                   const char **message_id,
+                                   enum mail_flags *flags, bool *flags_set,
+                                   const char *const **keywords,
+                                   const char **snippet)
+{
+       const char *value;
+       time_t tmp_date;
+       int tmp_tz;
+
+       if ((*from == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_FROM) != 0 &&
+           (mail_get_first_header(mail, "From", &value) >= 0)) {
+               *from = p_strdup(pool, value);
+       }
+
+       if ((*to == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_TO) != 0 &&
+           (mail_get_first_header(mail, "To", &value) >= 0)) {
+               *to = p_strdup(pool, value);
+       }
+
+       if ((*subject == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT) != 0 &&
+           (mail_get_first_header(mail, "Subject", &value) >= 0)) {
+               *subject = p_strdup(pool, value);
+       }
+
+       if ((*date == -1) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_DATE) != 0 &&
+           (mail_get_date(mail, &tmp_date, &tmp_tz) >= 0)) {
+               *date = tmp_date;
+               *date_tz = tmp_tz;
+       }
+
+       if ((*message_id == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID) != 0 &&
+           (mail_get_first_header(mail, "Message-ID", &value) >= 0)) {
+               *message_id = p_strdup(pool, value);
+       }
+
+       if (!*flags_set &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_FLAGS) != 0) {
+               *flags = mail_get_flags(mail);
+               *flags_set = TRUE;
+       }
+
+       if ((*keywords == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_KEYWORDS) != 0) {
+               const char *const *mail_kws = mail_get_keywords(mail);
+               ARRAY_TYPE(const_string) kws;
+               p_array_init(&kws, pool, 2);
+               for (;*mail_kws != NULL; mail_kws++) {
+                       value = p_strdup(pool, *mail_kws);
+                       array_append(&kws, &value, 1);
+               }
+               array_append_zero(&kws);
+               *keywords = array_idx(&kws, 0);
+       }
+
+       if ((*snippet == NULL) &&
+           (event_flags & PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET) != 0 &&
+           (mail_get_special(mail, MAIL_FETCH_BODY_SNIPPET, &value) >= 0)) {
+               /* [0] contains the snippet algorithm, skip over it */
+               i_assert(value[0] != '\0');
+               *snippet = p_strdup(pool, value + 1);
+       }
+}
index 58189487ef1ee74073d4ff5e3931f2ca8e11c345..0c68e08f92d39591db66a2e0b8e653d5bc93a6a3 100644 (file)
@@ -23,6 +23,15 @@ enum push_notification_event_message_flags {
     PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID = 0x80,
 };
 
+void push_notification_message_fill(struct mail *mail, pool_t pool,
+                                   enum push_notification_event_message_flags event_flags,
+                                   const char **from, const char **to,
+                                   const char **subject,
+                                   time_t *date, int *date_tz,
+                                   const char **message_id,
+                                   enum mail_flags *flags, bool *flags_set,
+                                   const char *const **keywords,
+                                   const char **snippet);
 
 #endif /* PUSH_NOTIFICATION_EVENT_MESSAGE_COMMON_H */
 
index d9e39a9ecf29018bad14cede77c2fd9ca8a04e2c..4bc4c6651bab7e0044d0c5ca6ace5d8f262271f3 100644 (file)
@@ -66,9 +66,6 @@ push_notification_event_messageappend_event(struct push_notification_txn *ptxn,
     struct push_notification_event_messageappend_config *config =
         (struct push_notification_event_messageappend_config *)ec->config;
     struct push_notification_event_messageappend_data *data;
-    const char *value;
-    time_t date;
-    int tz;
 
     if (config->flags == 0) {
         return;
@@ -82,63 +79,13 @@ push_notification_event_messageappend_event(struct push_notification_txn *ptxn,
         push_notification_txn_msg_set_eventdata(ptxn, msg, ec, data);
     }
 
-    if ((data->to == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_TO) != 0 &&
-        (mail_get_first_header(mail, "To", &value) >= 0)) {
-        data->to = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->from == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_FROM) != 0 &&
-        (mail_get_first_header(mail, "From", &value) >= 0)) {
-        data->from = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->subject == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT) != 0 &&
-        (mail_get_first_header(mail, "Subject", &value) >= 0)) {
-        data->subject = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->snippet == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET) != 0 &&
-        (mail_get_special(mail, MAIL_FETCH_BODY_SNIPPET, &value) >= 0)) {
-        /* [0] contains the snippet algorithm, skip over it */
-        i_assert(value[0] != '\0');
-        data->snippet = p_strdup(ptxn->pool, value + 1);
-    }
-
-    if ((data->date == -1) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_DATE) != 0 &&
-        (mail_get_date(mail, &date, &tz) >= 0)) {
-        data->date = date;
-        data->date_tz = tz;
-    }
-
-    if (!data->flags_set &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_FLAGS) != 0) {
-        data->flags = mail_get_flags(mail);
-        data->flags_set = TRUE;
-    }
-
-    if ((data->keywords == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_KEYWORDS) != 0) {
-        const char *const *mail_kws = mail_get_keywords(mail);
-        ARRAY_TYPE(const_string) kws;
-        p_array_init(&kws, ptxn->pool, 2);
-        for (;*mail_kws != NULL; mail_kws++) {
-           value = p_strdup(ptxn->pool, *mail_kws);
-           array_append(&kws, &value, 1);
-        }
-        array_append_zero(&kws);
-        data->keywords = array_idx(&kws, 0);
-    }
-
-    if ((data->message_id == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID) != 0 &&
-        (mail_get_first_header(mail, "Message-ID", &value) >= 0)) {
-        data->message_id = p_strdup(ptxn->pool, value);
-    }
+    push_notification_message_fill(mail, ptxn->pool, config->flags,
+                                  &data->from, &data->to, &data->subject,
+                                  &data->date, &data->date_tz,
+                                  &data->message_id,
+                                  &data->flags, &data->flags_set,
+                                  &data->keywords,
+                                  &data->snippet);
 }
 
 
index 40ab8ee76bfddf1abcfd9c14f19ca562291338dd..20a6dd21c8ff8a7f66dce4f219dad0a13d4a80b5 100644 (file)
@@ -66,9 +66,6 @@ push_notification_event_messagenew_event(struct push_notification_txn *ptxn,
     struct push_notification_event_messagenew_config *config =
         (struct push_notification_event_messagenew_config *)ec->config;
     struct push_notification_event_messagenew_data *data;
-    time_t date;
-    int tz;
-    const char *value;
 
     if (config->flags == 0) {
         return;
@@ -83,63 +80,13 @@ push_notification_event_messagenew_event(struct push_notification_txn *ptxn,
         push_notification_txn_msg_set_eventdata(ptxn, msg, ec, data);
     }
 
-    if ((data->to == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_TO) != 0 &&
-        (mail_get_first_header(mail, "To", &value) >= 0)) {
-        data->to = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->from == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_FROM) != 0 &&
-        (mail_get_first_header(mail, "From", &value) >= 0)) {
-        data->from = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->subject == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT) != 0 &&
-        (mail_get_first_header(mail, "Subject", &value) >= 0)) {
-        data->subject = p_strdup(ptxn->pool, value);
-    }
-
-    if ((data->date == -1) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_DATE) != 0 &&
-        (mail_get_date(mail, &date, &tz) >= 0)) {
-        data->date = date;
-        data->date_tz = tz;
-    }
-
-    if ((data->snippet == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET) != 0 &&
-        (mail_get_special(mail, MAIL_FETCH_BODY_SNIPPET, &value) >= 0)) {
-        /* [0] contains the snippet algorithm, skip over it */
-        i_assert(value[0] != '\0');
-        data->snippet = p_strdup(ptxn->pool, value + 1);
-    }
-
-    if (!data->flags_set &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_FLAGS) != 0) {
-        data->flags = mail_get_flags(mail);
-        data->flags_set = TRUE;
-    }
-
-    if ((data->keywords == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_KEYWORDS) != 0) {
-        const char *const *mail_kws = mail_get_keywords(mail);
-        ARRAY_TYPE(const_string) kws;
-        p_array_init(&kws, ptxn->pool, 2);
-        for (;*mail_kws != NULL; mail_kws++) {
-           value = p_strdup(ptxn->pool, *mail_kws);
-           array_append(&kws, &value, 1);
-        }
-        array_append_zero(&kws);
-        data->keywords = array_idx(&kws, 0);
-    }
-
-    if ((data->message_id == NULL) &&
-        (config->flags & PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID) != 0 &&
-        (mail_get_first_header(mail, "Message-ID", &value) >= 0)) {
-        data->message_id = p_strdup(ptxn->pool, value);
-    }
+    push_notification_message_fill(mail, ptxn->pool, config->flags,
+                                  &data->from, &data->to, &data->subject,
+                                  &data->date, &data->date_tz,
+                                  &data->message_id,
+                                  &data->flags, &data->flags_set,
+                                  &data->keywords,
+                                  &data->snippet);
 }