push-notification-event-flagsset.c revision 51ed197520dd9ea534fbc3bc1790ebe3cb5421e2
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "lib.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "array.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "mail-storage.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "mail-types.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "push-notification-drivers.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "push-notification-events.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "push-notification-event-flagsset.h"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#include "push-notification-txn-msg.h"
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen#define EVENT_NAME "FlagsSet"
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainenstatic struct push_notification_event_flagsset_config default_config;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainenstatic void *push_notification_event_flagsset_default_config(void)
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen{
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen memset(&default_config, 0, sizeof(default_config));
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen return &default_config;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen}
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
83d0c8152f694ae5b5d1798abda5728dc886102aTimo Sirainenstatic void push_notification_event_flagsset_debug_msg
83d0c8152f694ae5b5d1798abda5728dc886102aTimo Sirainen(struct push_notification_txn_event *event)
83d0c8152f694ae5b5d1798abda5728dc886102aTimo Sirainen{
83d0c8152f694ae5b5d1798abda5728dc886102aTimo Sirainen struct push_notification_event_flagsset_data *data = event->data;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen const char *const *keyword;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (data->flags_set & MAIL_ANSWERED) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen i_debug("%s: Answered flag set", EVENT_NAME);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (data->flags_set & MAIL_FLAGGED) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen i_debug("%s: Flagged flag set", EVENT_NAME);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (data->flags_set & MAIL_DELETED) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen i_debug("%s: Deleted flag set", EVENT_NAME);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (data->flags_set & MAIL_SEEN) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen i_debug("%s: Seen flag set", EVENT_NAME);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (data->flags_set & MAIL_DRAFT) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen i_debug("%s: Draft flag set", EVENT_NAME);
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen }
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen array_foreach(&data->keywords_set, keyword) {
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen i_debug("%s: Keyword set [%s]", EVENT_NAME, *keyword);
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen }
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen}
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainenstatic struct push_notification_event_flagsset_data *
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainenpush_notification_event_flagsset_get_data(struct push_notification_txn *ptxn,
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen struct push_notification_txn_msg *msg,
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen struct push_notification_event_config *ec)
d4847b921058734e0668bc7690465c91523d9ec0Martti Rannanjärvi{
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen struct push_notification_event_flagsset_data *data;
29543188462c9348f365ec29115d777ffe4769d3Timo Sirainen
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen data = push_notification_txn_msg_get_eventdata(msg, EVENT_NAME);
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen if (data == NULL) {
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen data = p_new(ptxn->pool,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_event_flagsset_data, 1);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen data->flags_set = 0;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen p_array_init(&data->keywords_set, ptxn->pool, 4);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen push_notification_txn_msg_set_eventdata(ptxn, msg, ec, data);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
f3391d65cc830eab22ca6c5941774de682716edbTimo Sirainen return data;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen}
f3391d65cc830eab22ca6c5941774de682716edbTimo Sirainen
8528048c7b82c745bcfa5a6b1394251b3aaeb129Josef 'Jeff' Sipekstatic void push_notification_event_flagsset_flags_event(
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_txn *ptxn,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_event_config *ec,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen struct push_notification_txn_msg *msg,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen struct mail *mail,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen enum mail_flags old_flags)
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen{
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen struct push_notification_event_flagsset_config *config =
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen (struct push_notification_event_flagsset_config *)ec->config;
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen struct push_notification_event_flagsset_data *data;
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen enum mail_flags flag_check_always[] = {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen MAIL_ANSWERED,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen MAIL_DRAFT,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen MAIL_FLAGGED
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen };
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen enum mail_flags flags, flags_set = 0;
b056d19b7a07400b897104b146c8768280d24009Timo Sirainen unsigned int i;
b056d19b7a07400b897104b146c8768280d24009Timo Sirainen
b056d19b7a07400b897104b146c8768280d24009Timo Sirainen flags = mail_get_flags(mail);
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen for (i = 0; i < N_ELEMENTS(flag_check_always); i++) {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen if ((flags & flag_check_always[i]) &&
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen !(old_flags & flag_check_always[i])) {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen flags_set |= flag_check_always[i];
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen }
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen }
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen if (!config->hide_deleted &&
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen (flags & MAIL_DELETED) &&
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen !(old_flags & MAIL_DELETED)) {
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen flags_set |= MAIL_DELETED;
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen }
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen if (!config->hide_seen &&
7da0a157dbcb64d3e97c01bcc87262bd944c6890Timo Sirainen (flags & MAIL_SEEN) &&
7da0a157dbcb64d3e97c01bcc87262bd944c6890Timo Sirainen !(old_flags & MAIL_SEEN)) {
7da0a157dbcb64d3e97c01bcc87262bd944c6890Timo Sirainen flags_set |= MAIL_SEEN;
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen }
3a2f2adf5679aa383a2cab09f739d59233cada95Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen /* Only create data element if at least one flag was set. */
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (flags_set) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen data = push_notification_event_flagsset_get_data(ptxn, msg, ec);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen data->flags_set |= flags_set;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen}
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainenstatic void push_notification_event_flagsset_keywords_event(
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_txn *ptxn,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_event_config *ec,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_txn_msg *msg,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct mail *mail,
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen const char *const *old_keywords)
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen{
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_event_flagsset_data *data;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen const char *k, *const *keywords, *const *op;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen data = push_notification_event_flagsset_get_data(ptxn, msg, ec);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen keywords = mail_get_keywords(mail);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen for (; *keywords != NULL; keywords++) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen for (op = old_keywords; *op != NULL; op++) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (strcmp(*keywords, *op) == 0) {
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen break;
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen if (*op == NULL) {
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen k = p_strdup(ptxn->pool, *keywords);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen array_append(&data->keywords_set, &k, 1);
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen }
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen}
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainenstatic void push_notification_event_flagsset_free_msg(
e0aff4c7e3336ec4b5edbcfc3a72e1e118603ee2Timo Sirainen struct push_notification_txn_event *event)
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen{
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen struct push_notification_event_flagsset_data *data = event->data;
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen if (array_is_created(&data->keywords_set)) {
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen array_free(&data->keywords_set);
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen }
bb2471ff14f01390b47cce63b407820b5547df1aTimo Sirainen}
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen/* Event definition */
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainenextern struct push_notification_event push_notification_event_flagsset;
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainenstruct push_notification_event push_notification_event_flagsset = {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .name = EVENT_NAME,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .init = {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .default_config = push_notification_event_flagsset_default_config
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen },
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .msg = {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .debug_msg = push_notification_event_flagsset_debug_msg,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .free_msg = push_notification_event_flagsset_free_msg
28f7485aaa7c5e0e3aa85d5a65d0dfc1c7ec7b89Timo Sirainen },
28f7485aaa7c5e0e3aa85d5a65d0dfc1c7ec7b89Timo Sirainen .msg_triggers = {
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .flagchange = push_notification_event_flagsset_flags_event,
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen .keywordchange = push_notification_event_flagsset_keywords_event
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen }
f663bdd9d345a86e6a7924d83319bd04b2fcb600Timo Sirainen};