From: Timo Sirainen Date: Thu, 7 Nov 2019 16:49:02 +0000 (+0200) Subject: push-notification: Deduplicate MessageNew and MessageAppend events' data gathering... X-Git-Tag: 2.3.10~281 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be5e2c6ca0a527ebfb080a546a2891c1b86c2bf7;p=thirdparty%2Fdovecot%2Fcore.git push-notification: Deduplicate MessageNew and MessageAppend events' data gathering code It would have been simpler to share their structs, but that can't be done cleanly without breaking the API's backwards compatibility. --- diff --git a/src/plugins/push-notification/Makefile.am b/src/plugins/push-notification/Makefile.am index 469b96be5e..c065e3853e 100644 --- a/src/plugins/push-notification/Makefile.am +++ b/src/plugins/push-notification/Makefile.am @@ -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 index 0000000000..3d3f12c8a2 --- /dev/null +++ b/src/plugins/push-notification/push-notification-event-message-common.c @@ -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); + } +} diff --git a/src/plugins/push-notification/push-notification-event-message-common.h b/src/plugins/push-notification/push-notification-event-message-common.h index 58189487ef..0c68e08f92 100644 --- a/src/plugins/push-notification/push-notification-event-message-common.h +++ b/src/plugins/push-notification/push-notification-event-message-common.h @@ -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 */ diff --git a/src/plugins/push-notification/push-notification-event-messageappend.c b/src/plugins/push-notification/push-notification-event-messageappend.c index d9e39a9ecf..4bc4c6651b 100644 --- a/src/plugins/push-notification/push-notification-event-messageappend.c +++ b/src/plugins/push-notification/push-notification-event-messageappend.c @@ -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); } diff --git a/src/plugins/push-notification/push-notification-event-messagenew.c b/src/plugins/push-notification/push-notification-event-messagenew.c index 40ab8ee76b..20a6dd21c8 100644 --- a/src/plugins/push-notification/push-notification-event-messagenew.c +++ b/src/plugins/push-notification/push-notification-event-messagenew.c @@ -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); }