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 "hash.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "http-client.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "http-url.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "ioloop.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "istream.h"
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen#include "settings-parser.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "json-parser.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "mailbox-attribute.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "mail-storage-private.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "str.h"
7a60e1dc9e93ef3f7c7fe1af6385a0bfa1e31bc3Timo Sirainen#include "strescape.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-drivers.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-event-messagenew.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-events.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#include "push-notification-txn-msg.h"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#define OX_LOG_LABEL "OX Push Notification: "
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz#define OX_METADATA_KEY \
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER "vendor/vendor.dovecot/http-notify"
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* Default values. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic const char *const default_events[] = { "MessageNew", NULL };
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic const char *const default_mboxes[] = { "INBOX", NULL };
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen#define DEFAULT_CACHE_LIFETIME_SECS 60
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen#define DEFAULT_TIMEOUT_MSECS 2000
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen#define DEFAULT_RETRY_COUNT 1
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* This is data that is shared by all plugin users. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_driver_ox_global {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct http_client *http_client;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz int refcount;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz};
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic struct push_notification_driver_ox_global *ox_global = NULL;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* This is data specific to an OX driver. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_driver_ox_config {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct http_url *http_url;
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen unsigned int cached_ox_metadata_lifetime_secs;
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen bool use_unsafe_username;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen unsigned int http_max_retries;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen unsigned int http_timeout_msecs;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen char *cached_ox_metadata;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen time_t cached_ox_metadata_timestamp;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz};
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* This is data specific to an OX driver transaction. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_driver_ox_txn {
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen const char *unsafe_user;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz};
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainenpush_notification_driver_ox_init_global(struct mail_user *user,
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen struct push_notification_driver_ox_config *config)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct http_client_settings http_set;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (ox_global->http_client == NULL) {
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen /* this is going to use the first user's settings, but these are
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen unlikely to change between users so it shouldn't matter much. */
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&http_set);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz http_set.debug = user->mail_debug;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen http_set.max_attempts = config->http_max_retries+1;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen http_set.request_timeout_msecs = config->http_timeout_msecs;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ox_global->http_client = http_client_init(&http_set);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic int
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzpush_notification_driver_ox_init(struct push_notification_driver_config *config,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_user *user, pool_t pool,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz void **context, const char **error_r)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_driver_ox_config *dconfig;
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz const char *error, *tmp;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz /* Valid config keys: cache_lifetime, url */
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz tmp = hash_table_lookup(config->config, (const char *)"url");
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz if (tmp == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz *error_r = OX_LOG_LABEL "Driver requires the url parameter";
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return -1;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz dconfig = p_new(pool, struct push_notification_driver_ox_config, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz if (http_url_parse(tmp, NULL, HTTP_URL_ALLOW_USERINFO_PART, pool,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz &dconfig->http_url, &error) < 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz *error_r = t_strdup_printf(OX_LOG_LABEL "Failed to parse OX REST URL %s: %s",
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz tmp, error);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return -1;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen dconfig->use_unsafe_username =
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen hash_table_lookup(config->config, (const char *)"user_from_metadata") != NULL;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz push_notification_driver_debug(OX_LOG_LABEL, user, "Using URL %s", tmp);
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz tmp = hash_table_lookup(config->config, (const char *)"cache_lifetime");
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen if (tmp == NULL)
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen dconfig->cached_ox_metadata_lifetime_secs = DEFAULT_CACHE_LIFETIME_SECS;
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen else if (settings_get_time(tmp, &dconfig->cached_ox_metadata_lifetime_secs, &error) < 0) {
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen *error_r = t_strdup_printf(OX_LOG_LABEL "Failed to parse OX cache_lifetime %s: %s",
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen tmp, error);
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen return -1;
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz }
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen tmp = hash_table_lookup(config->config, (const char *)"max_retries");
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen if ((tmp == NULL) ||
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen (str_to_uint(tmp, &dconfig->http_max_retries) < 0)) {
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen dconfig->http_max_retries = DEFAULT_RETRY_COUNT;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen }
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen tmp = hash_table_lookup(config->config, (const char *)"timeout_msecs");
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen if ((tmp == NULL) ||
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen (str_to_uint(tmp, &dconfig->http_timeout_msecs) < 0)) {
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen dconfig->http_timeout_msecs = DEFAULT_TIMEOUT_MSECS;
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen }
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz "Using cache lifetime: %u",
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen dconfig->cached_ox_metadata_lifetime_secs);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (ox_global == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ox_global = i_new(struct push_notification_driver_ox_global, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ox_global->refcount = 0;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz ++ox_global->refcount;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz *context = dconfig;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return 0;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic const char *push_notification_driver_ox_get_metadata
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(struct push_notification_driver_txn *dtxn)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
dfebce6840b843e8ae76f632537db4cfd29041fcTimo Sirainen struct push_notification_driver_ox_config *dconfig = dtxn->duser->context;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_attribute_value attr;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mailbox *inbox;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_namespace *ns;
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz bool success = FALSE, use_existing_txn = FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz int ret;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz if ((dconfig->cached_ox_metadata != NULL) &&
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen ((dconfig->cached_ox_metadata_timestamp +
630c1df5b6aaec452b0ef040df56cffd978fe9c3Timo Sirainen (time_t)dconfig->cached_ox_metadata_lifetime_secs) > ioloop_time)) {
dfebce6840b843e8ae76f632537db4cfd29041fcTimo Sirainen return dconfig->cached_ox_metadata;
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz }
dfebce6840b843e8ae76f632537db4cfd29041fcTimo Sirainen
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz /* Get canonical INBOX, where private server-level metadata is stored.
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz * See imap/cmd-getmetadata.c */
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz if ((dtxn->ptxn->t != NULL) && dtxn->ptxn->mbox->inbox_user) {
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz inbox = dtxn->ptxn->mbox;
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz use_existing_txn = TRUE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz } else {
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz ns = mail_namespace_find_inbox(dtxn->ptxn->muser->namespaces);
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz inbox = mailbox_alloc(ns->list, "INBOX", MAILBOX_FLAG_READONLY);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi ret = mailbox_attribute_get(inbox, MAIL_ATTRIBUTE_TYPE_PRIVATE,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz OX_METADATA_KEY, &attr);
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi if (ret < 0) {
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi i_error(OX_LOG_LABEL "Skipped because unable to get attribute: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(inbox, NULL));
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi } else if (ret == 0) {
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi push_notification_driver_debug(OX_LOG_LABEL, dtxn->ptxn->muser,
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi "Skipped because not active (/private/"OX_METADATA_KEY" METADATA not set)");
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi } else {
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi success = TRUE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz if (!use_existing_txn) {
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz mailbox_free(&inbox);
cf93f9d7d747a14db76c1d1fd21dad9e0a102088Michael Slusarz }
f61bbf70eb2ead907c0e3cc190bb556b93306b48Timo Sirainen if (!success)
f61bbf70eb2ead907c0e3cc190bb556b93306b48Timo Sirainen return NULL;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
101becc1042f7b3633324aa91d9b39c4537bfe0eTimo Sirainen i_free(dconfig->cached_ox_metadata);
101becc1042f7b3633324aa91d9b39c4537bfe0eTimo Sirainen dconfig->cached_ox_metadata = i_strdup(attr.value);
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz dconfig->cached_ox_metadata_timestamp = ioloop_time;
8b4073f4a7fd6423cb73b1cddf72acbec04598c6Michael Slusarz
f61bbf70eb2ead907c0e3cc190bb556b93306b48Timo Sirainen return dconfig->cached_ox_metadata;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic bool push_notification_driver_ox_begin_txn
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(struct push_notification_driver_txn *dtxn)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *args;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_messagenew_config *config;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *key, *mbox_curr, *md_value, *value;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz bool mbox_found = FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_driver_ox_txn *txn;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz md_value = push_notification_driver_ox_get_metadata(dtxn);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (md_value == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_user *user = dtxn->ptxn->muser;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz /* Unused keys: events, expire, folder */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz /* TODO: To be implemented later(?) */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *events = default_events;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz time_t expire = INT_MAX;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz const char *const *mboxes = default_mboxes;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (expire < ioloop_time) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz "Skipped due to expiration (%ld < %ld)",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (long)expire, (long)ioloop_time);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mbox_curr = mailbox_get_vname(dtxn->ptxn->mbox);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (; *mboxes != NULL; mboxes++) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (strcmp(mbox_curr, *mboxes) == 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mbox_found = TRUE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz break;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (mbox_found == FALSE) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz "Skipped because %s is not a watched mailbox",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz mbox_curr);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz txn = p_new(dtxn->ptxn->pool, struct push_notification_driver_ox_txn, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz /* Valid keys: user */
7a60e1dc9e93ef3f7c7fe1af6385a0bfa1e31bc3Timo Sirainen args = t_strsplit_tabescaped(md_value);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (; *args != NULL; args++) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz key = *args;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz value = strchr(key, '=');
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (value != NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz key = t_strdup_until(key, value++);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (strcmp(key, "user") == 0) {
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen txn->unsafe_user = p_strdup(dtxn->ptxn->pool, value);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen if (txn->unsafe_user == NULL) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_error(OX_LOG_LABEL "No user provided in config");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return FALSE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen push_notification_driver_debug(OX_LOG_LABEL, user, "User (%s)", txn->unsafe_user);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz for (; *events != NULL; events++) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (strcmp(*events, "MessageNew") == 0) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz config = p_new(dtxn->ptxn->pool,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_messagenew_config, 1);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz config->flags = PUSH_NOTIFICATION_MESSAGE_HDR_FROM |
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT |
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_event_init(dtxn, "MessageNew", config);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz "Handling MessageNew event");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz dtxn->context = txn;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz return TRUE;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_driver_ox_http_callback
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(const struct http_response *response, struct mail_user *user)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz switch (response->status / 100) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz case 2:
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz // Success.
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (user->mail_debug) {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
ce3fc9190e82d8d9e9604afd4ebbee1d61957764Stephan Bosch "Notification sent successfully: %s",
ce3fc9190e82d8d9e9604afd4ebbee1d61957764Stephan Bosch http_response_get_message(response));
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz break;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz default:
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz // Error.
ce3fc9190e82d8d9e9604afd4ebbee1d61957764Stephan Bosch i_error(OX_LOG_LABEL "Error when sending notification: %s",
ce3fc9190e82d8d9e9604afd4ebbee1d61957764Stephan Bosch http_response_get_message(response));
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz break;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* Callback needed for i_stream_add_destroy_callback() in
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz * push_notification_driver_ox_process_msg. */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void str_free_i(string_t *str)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz str_free(&str);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templinstatic int push_notification_driver_ox_get_mailbox_status
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin(struct push_notification_driver_txn *dtxn,
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin struct mailbox_status *r_box_status)
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin{
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin /* The already opened mailbox. We cannot use or sync it, because we are within a save transaction. */
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin struct mailbox *mbox = dtxn->ptxn->mbox;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin struct mailbox *box;
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin int ret;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin /* open and sync new instance of the same mailbox to get most recent status */
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin box = mailbox_alloc(mailbox_get_namespace(mbox)->list, mailbox_get_name(mbox), MAILBOX_FLAG_READONLY);
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin if (mailbox_sync(box, 0) < 0) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi i_error("mailbox_sync(%s) failed: %s", mailbox_get_vname(mbox), mailbox_get_last_internal_error(box, NULL));
f948338e87fe0af30e2370b6d663654765b3c5bbTimo Sirainen ret = -1;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin } else {
f948338e87fe0af30e2370b6d663654765b3c5bbTimo Sirainen /* only 'unseen' is needed at the moment */
f948338e87fe0af30e2370b6d663654765b3c5bbTimo Sirainen mailbox_get_open_status(box, STATUS_UNSEEN, r_box_status);
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin push_notification_driver_debug(OX_LOG_LABEL, dtxn->ptxn->muser, "Got status of mailbox '%s': (unseen: %u)",
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin mailbox_get_vname(box), r_box_status->unseen);
f948338e87fe0af30e2370b6d663654765b3c5bbTimo Sirainen ret = 0;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin }
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin mailbox_free(&box);
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin return ret;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin}
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_driver_ox_process_msg
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(struct push_notification_driver_txn *dtxn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_txn_msg *msg)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_driver_ox_config *dconfig =
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (struct push_notification_driver_ox_config *)dtxn->duser->context;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct http_client_request *http_req;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_event_messagenew_data *messagenew;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct istream *payload;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz string_t *str;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct push_notification_driver_ox_txn *txn =
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz (struct push_notification_driver_ox_txn *)dtxn->context;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz struct mail_user *user = dtxn->ptxn->muser;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin struct mailbox_status box_status;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin bool status_success = TRUE;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin
282323a5269a0121d9407f098680bf8e7e02f47cSteffen Templin if (push_notification_driver_ox_get_mailbox_status(dtxn, &box_status) < 0) {
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin status_success = FALSE;
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz messagenew = push_notification_txn_msg_get_eventdata(msg, "MessageNew");
3cbb235b07a41dfe005fda389d722247605495d5Michael Slusarz if (messagenew == NULL) {
3cbb235b07a41dfe005fda389d722247605495d5Michael Slusarz return;
3cbb235b07a41dfe005fda389d722247605495d5Michael Slusarz }
3cbb235b07a41dfe005fda389d722247605495d5Michael Slusarz
0f58da3fd10d43bf701eba068cb3b84a216f89f1Timo Sirainen push_notification_driver_ox_init_global(user, dconfig);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz http_req = http_client_request_url(ox_global->http_client, "PUT",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz dconfig->http_url,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_ox_http_callback,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz user);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz http_client_request_add_header(http_req, "Content-Type",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz "application/json; charset=utf-8");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz str = str_new(default_pool, 256);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz str_append(str, "{\"user\":\"");
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen json_append_escaped(str, dconfig->use_unsafe_username ?
cad784f10db7b8f53661246fbbab1d4789e1a25aTimo Sirainen txn->unsafe_user : user->username);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz str_append(str, "\",\"event\":\"messageNew\",\"folder\":\"");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz json_append_escaped(str, msg->mailbox);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz str_printfa(str, "\",\"imap-uidvalidity\":%u,\"imap-uid\":%u",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz msg->uid_validity, msg->uid);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (messagenew->from != NULL) {
3722c793bbea1495000de35815d31091a75f3f9cSteffen Templin str_append(str, ",\"from\":\"");
3722c793bbea1495000de35815d31091a75f3f9cSteffen Templin json_append_escaped(str, messagenew->from);
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_append(str, "\"");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (messagenew->subject != NULL) {
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_append(str, ",\"subject\":\"");
3722c793bbea1495000de35815d31091a75f3f9cSteffen Templin json_append_escaped(str, messagenew->subject);
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_append(str, "\"");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (messagenew->snippet != NULL) {
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_append(str, ",\"snippet\":\"");
3722c793bbea1495000de35815d31091a75f3f9cSteffen Templin json_append_escaped(str, messagenew->snippet);
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_append(str, "\"");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin if (status_success) {
3df60858b15645fbc64ac18842c2545da0439c08Michael Koch str_printfa(str, ",\"unseen\":%u", box_status.unseen);
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin }
5e55914597b8f92b83543d133a8c07d808b3f3ddSteffen Templin str_append(str, "}");
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz push_notification_driver_debug(OX_LOG_LABEL, user,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz "Sending notification: %s", str_c(str));
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz payload = i_stream_create_from_data(str_data(str), str_len(str));
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_stream_add_destroy_callback(payload, str_free_i, str);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz http_client_request_set_payload(http_req, payload, FALSE);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz http_client_request_submit(http_req);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_stream_unref(&payload);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_driver_ox_deinit
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz(struct push_notification_driver_user *duser ATTR_UNUSED)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
101becc1042f7b3633324aa91d9b39c4537bfe0eTimo Sirainen struct push_notification_driver_ox_config *dconfig = duser->context;
101becc1042f7b3633324aa91d9b39c4537bfe0eTimo Sirainen
101becc1042f7b3633324aa91d9b39c4537bfe0eTimo Sirainen i_free(dconfig->cached_ox_metadata);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if (ox_global != NULL) {
6f380c1e809a53ce2561577b9fa4df39695809d6Timo Sirainen if (ox_global->http_client != NULL)
6f380c1e809a53ce2561577b9fa4df39695809d6Timo Sirainen http_client_wait(ox_global->http_client);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_assert(ox_global->refcount > 0);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz --ox_global->refcount;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstatic void push_notification_driver_ox_cleanup(void)
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz{
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz if ((ox_global != NULL) && (ox_global->refcount <= 0)) {
3e13cdffb51ff975f70f347d143c89c19f46091fTimo Sirainen if (ox_global->http_client != NULL) {
3e13cdffb51ff975f70f347d143c89c19f46091fTimo Sirainen http_client_deinit(&ox_global->http_client);
3e13cdffb51ff975f70f347d143c89c19f46091fTimo Sirainen }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz i_free_and_null(ox_global);
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz}
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz/* Driver definition */
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzextern struct push_notification_driver push_notification_driver_ox;
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarzstruct push_notification_driver push_notification_driver_ox = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .name = "ox",
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .v = {
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .init = push_notification_driver_ox_init,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .begin_txn = push_notification_driver_ox_begin_txn,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .process_msg = push_notification_driver_ox_process_msg,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .deinit = push_notification_driver_ox_deinit,
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz .cleanup = push_notification_driver_ox_cleanup
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz }
51ed197520dd9ea534fbc3bc1790ebe3cb5421e2Michael M Slusarz};