bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "lib.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "array.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "mail-storage.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "mail-types.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-drivers.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-events.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-event-flagsclear.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-txn-msg.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#define EVENT_NAME "FlagsClear"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic struct push_notification_event_flagsclear_config default_config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void *push_notification_event_flagsclear_default_config(void)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&default_config);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return &default_config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_event_flagsclear_debug_msg
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(struct push_notification_txn_event *event)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data *data = event->data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *keyword;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((data->flags_clear & MAIL_ANSWERED) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Answered flag cleared", EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((data->flags_clear & MAIL_FLAGGED) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Flagged flag cleared", EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((data->flags_clear & MAIL_DELETED) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Deleted flag cleared", EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((data->flags_clear & MAIL_SEEN) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Seen flag cleared", EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((data->flags_clear & MAIL_DRAFT) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Draft flag cleared", EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_foreach(&data->keywords_clear, keyword) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_debug("%s: Keyword clear [%s]", EVENT_NAME, *keyword);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic struct push_notification_event_flagsclear_data *
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_event_flagsclear_get_data(struct push_notification_txn *ptxn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_config *ec)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_config *config =
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (struct push_notification_event_flagsclear_config *)ec->config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data *data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data = push_notification_txn_msg_get_eventdata(msg, EVENT_NAME);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (data == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data = p_new(ptxn->pool,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data->flags_clear = 0;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data->flags_old = 0;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz p_array_init(&data->keywords_clear, ptxn->pool, 4);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (config->store_old == TRUE) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz p_array_init(&data->keywords_old, ptxn->pool, 4);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_txn_msg_set_eventdata(ptxn, msg, ec, data);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_event_flagsclear_flags_event(
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn *ptxn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_config *ec,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail *mail,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz enum mail_flags old_flags)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_config *config =
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (struct push_notification_event_flagsclear_config *)ec->config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data *data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz enum mail_flags flag_check_always[] = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAIL_ANSWERED,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAIL_DELETED,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAIL_DRAFT,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAIL_FLAGGED,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAIL_SEEN
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz };
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz enum mail_flags flags;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz unsigned int i;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data = push_notification_event_flagsclear_get_data(ptxn, msg, ec);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz flags = mail_get_flags(mail);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (i = 0; i < N_ELEMENTS(flag_check_always); i++) {
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen if ((flags & flag_check_always[i]) == 0 &&
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen (old_flags & flag_check_always[i]) != 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data->flags_clear |= flag_check_always[i];
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (config->store_old == TRUE) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data->flags_old = old_flags;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_event_flagsclear_keywords_event(
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn *ptxn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_config *ec,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail *mail,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *old_keywords)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_config *config =
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (struct push_notification_event_flagsclear_config *)ec->config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data *data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *keywords, *const *kp, *ok;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz data = push_notification_event_flagsclear_get_data(ptxn, msg, ec);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz keywords = mail_get_keywords(mail);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (; *old_keywords != NULL; old_keywords++) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (kp = keywords; *kp != NULL; kp++) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (strcmp(*old_keywords, *kp) == 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz break;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (*kp == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ok = p_strdup(ptxn->pool, *old_keywords);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_append(&data->keywords_clear, &ok, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (config->store_old == TRUE) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ok = p_strdup(ptxn->pool, *old_keywords);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_append(&data->keywords_old, &ok, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_event_flagsclear_free_msg(
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_event *event)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_flagsclear_data *data = event->data;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (array_is_created(&data->keywords_clear)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_free(&data->keywords_clear);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (array_is_created(&data->keywords_old)) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz array_free(&data->keywords_old);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* Event definition */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzextern struct push_notification_event push_notification_event_flagsclear;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_event push_notification_event_flagsclear = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .name = EVENT_NAME,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .init = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .default_config = push_notification_event_flagsclear_default_config
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz },
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .msg = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .debug_msg = push_notification_event_flagsclear_debug_msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .free_msg = push_notification_event_flagsclear_free_msg
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz },
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .msg_triggers = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .flagchange = push_notification_event_flagsclear_flags_event,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .keywordchange = push_notification_event_flagsclear_keywords_event
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz};