Push Notification Framework

Introduction

Dovecot’s Push Notification framework exposes RFC 5423 (Internet Message Store Events) events that occur in Dovecot to a system that can be used to report these events to external services.

These events (see RFC 5423#section-4.1 for descriptions) are available within the notification framework, although a driver may not implement all of them:

  • FlagsClear

  • FlagsSet

  • MailboxCreate

  • MailboxDelete

  • MailboxRename

  • MailboxSubscribe

  • MailboxUnsubscribe

  • MessageAppend

  • MessageExpunge

  • MessageNew

  • MessageRead

  • MessageTrash

These events are not supported by the notification framework:

Usage

To use push notifications, both the notify and the push_notification plugins need to be activated by defining in mail_plugins.

This can either be set globally or restricted to the protocols where you want push notifications to be generated. For example, to restrict to mail delivery notifications only, this config should be used:

protocol lmtp {
  mail_plugins = $mail_plugins notify push_notification
}

# If notifications are also needed for LDA-based delivery, add:
protocol lda {
  mail_plugins = $mail_plugins notify push_notification
}

Settings

See push-notification.

Drivers

A push notification driver is defined by the push_notification_driver setting.

The configuration value is the name of the driver, optionally followed by an : and driver-specific options (see drivers for options supported).

It is possible to specify multiple push notification drivers by adding a sequential number to the push_notification_driver label, starting with the number 2. There can be no numbering gaps for the labels; only the drivers that appear in sequential order will be processed.

Multiple driver configuration is useful if, for example, you want to process a single driver with different configurations.

Example:

plugin {
  push_notification_driver  = ox:url=http://example.com/foo
  push_notification_driver2 = ox:url=http://example.com/bar
  # This driver will NOT be processed, as it does not appear sequentially
  # with the other configuration options
  push_notification_driver4 = dlog
}

The list of drivers shipped with Dovecot core appears below.

DLOG (Debug log) [dlog]

plugin {
  push_notification_driver = dlog
}

This will cause notifications to end up in your debug log.

OX (Open-Xchange) driver [ox]

The OX backend supports sending notifications on MessageNew events (i.e. mail deliveries, not IMAP APPENDs).

This driver was designed for use with OX App Suite Push Notification API, but can be used by any push endpoint that implements this API, not just OX App Suite.

Configuration options:

Name

Required

Type

Description

url

YES

String

The HTTP end-point (URL + authentication information) to use is configured in the Dovecot configuration file. Contains authentication information needed for Basic Authentication (if any). Example: http<s> + "://" + <login> + ":" + <password> + "@" + <host> + ":" + <port> + "/preliminary/http-notify/v1/notify"

For HTTPS endpoints, system CAs are trusted by default, but internal CAs might need further configuration.

For further details on configuring the App Suite endpoint, see: https://documentation.open-xchange.com/latest/middleware/mail/dovecot/dovecot_push.html#configuration-of-dovecot-http-notify-plug-in

cache_lifetime

NO

Time

Cache lifetime for the METADATA entry for a user. (DEFAULT: 60 seconds)

max_retries

NO

Unsigned integer

The maximum number of times to retry a connection to the OX endpoint. Setting it to 0 will disable retries. (DEFAULT: 1)

timeout_msecs

NO

Millisecond Time

Time before HTTP request to OX endpoint will timeout. (DEFAULT: 2000)

user_from_metadata

NO

(Existence of setting)

Use the user stored in the METADATA entry instead of the user sent by OX endpoint. Does not require an argument; presence of the option activates the feature. (DEFAULT: user returned by endpoint response is used)

Example configuration:

plugin {
  push_notification_driver = ox:url=http://login:pass@node1.domain.tld:8009/preliminary/http-notify/v1/notify user_from_metadata timeout_msecs=10000
}

Metadata

The push notifications are enabled separately for each user using METADATA. Normally OX App Suite does this internally, but for e.g. testing purposes you can do this yourself:

doveadm mailbox metadata set -u user@example.com -s "" /private/vendor/vendor.dovecot/http-notify user=11@3

Example Payload

Push notification sent in JSON format with the following fields:

Name

Type

Description

event

string

RFC 5423 event type (currently only “MessageNew”)

folder

string

Mailbox name

from

string

RFC 2822 address of the message sender (MIME-encoded), if applicable

imap-uid

integer

UID of the message, if applicable

imap-uidvalidity

integer

RFC 3501 UIDVALIDITY value of the mailbox

snippet

string

Snippet of the message body (UTF-8), if applicable

subject

string

Subject of the message (MIME-encoded), if applicable

unseen

integer

RFC 3501 UNSEEN value of the mailbox

user

string

User identifier

Example (Content-Type: application/json; charset=utf-8):

{
  "user": "4@464646669",
  "imap-uidvalidity": 123412341,
  "imap-uid": 2345,
  "folder": "INBOX",
  "event": "MessageNew",
  "from": "=?utf-8?q?=C3=84?= <alice@barfoo.org>",
  "subject": "Test =?utf-8?q?p=C3=A4iv=C3=A4=C3=A4?=",
  "snippet": "Hey guys\nThis is only a test...",
  "unseen": 2
}

Lua [lua]

New in version v2.3.4.

You can use Lua to write custom push notification handlers.

See Dovecot Lua Support for general information on how Lua is implemented in Dovecot.

Configuration

Lua push notification handler requires mail_lua and push_notification_lua plugins to be loaded in addition to the plugins discussed above.

Name

Required

Type

Description

file

NO

String

The lua file to execute. If no script is specified, mail_lua_script will be used by default.

mail_plugins = $mail_plugins mail_lua notify push_notification push_notification_lua

plugin {
  push_notification_driver = lua:file=/path/to/lua/script
}

API Overview

The Lua driver hooks into all events, and calls matching functions when found in Lua script.

The driver supports all available push notification events.

All events are called within a transaction. The event is called with context and an event table, which contains the event parameters.

All events contain at least:

Name

Description

name

Name of the event name

user

Current mail user

Events are always called after the fact.

There has to be at least one event handler, or the transaction begin and end functions are never called. This is optimization to avoid roundtrip to Lua when it’s not needed.

Transactions
dovecot_lua_notify_begin_txn(user)

Start transaction. Return value is used as transaction context and is treated as opaque value by Lua driver. The user parameter is mail_user object.

dovecot_lua_notify_end_txn(context, success)

End transaction, context is unreferenced.

Mailbox Events

All mailbox events contain the following parameters:

Name

Description

mailbox

Name of the affected mailbox

Functions:

dovecot_lua_notify_event_mailbox_create(context, {name, mailbox})

Called when mailbox has been created.

dovecot_lua_notify_event_mailbox_delete(context, {name, mailbox})

Called when mailbox has been deleted.

dovecot_lua_notify_event_mailbox_rename(context, {name, mailbox, mailbox_old})

Called when mailbox has been renamed, old name is retained in mailbox_old attribute.

dovecot_lua_notify_event_mailbox_subscribe(context, {name, mailbox})

Called when mailbox has been subscribed to. The mailbox does not necessarily exist.

dovecot_lua_notify_event_mailbox_unsubscribe(context, {name, mailbox})

Called when mailbox has been unsubscribed from. The mailbox does not necessarily exist.

Message Events

All message events contain following parameters:

Name

Description

mailbox

Mailbox name

uid

Message UID

uid_validity

Mailbox UIDVALIDITY

Functions:

dovecot_lua_notify_event_message_new(context, {name, mailbox, uid, uid_validity, date, tz, from, from_address, from_display_name, to, to_address, to_display_name, subject, snippet})

Called when message is delivered.

dovecot_lua_notify_event_message_append(context, {name, mailbox, uid, uid_validity, from, from_address, from_display_name, to, to_address, to_display_name, subject, snippet})

Called when message is APPENDed to a mailbox (via IMAP).

dovecot_lua_notify_event_message_read(context, {name, mailbox, uid, uid_validity})

Called when message is marked as Seen.

dovecot_lua_notify_event_message_trash(context, {name, mailbox, uid, uid_validity})

Called when message is marked Deleted.

dovecot_lua_notify_event_message_expunge(context, {name, mailbox, uid, uid_validity})

Called when message is expunged.

dovecot_lua_notify_event_flags_set(context, {name, mailbox, uid, uid_validity, flags, keywords_set})

Called when message flags or keywords are set. flags is a bitmask. keywords_set is a table of strings of the keywords set by the event.

dovecot_lua_notify_event_flags_clear(context, {name, mailbox, uid, uid_validity, flags, keywords_clear, keywords_old})

Called when message flags or keywords are removed. flags is a bitmask. keywords_clear contains the keywords cleared, keywords_old is the table of keywords that were set before the event.

Example Scripts

Simple example with dovecot.http.client

 1local url = require 'socket.url'
 2
 3local client = nil
 4
 5function script_init()
 6  client = dovecot.http.client({debug=True, timeout=10000})
 7  return 0
 8end
 9
10local function table_get(t, k, d)
11  return t[k] or d
12end
13
14
15function dovecot_lua_notify_begin_txn(user)
16  return {messages={}, ep=user:plugin_getenv("push_lua_url"), username=user.username}
17end
18
19
20function dovecot_lua_notify_end_txn(ctx, success)
21  local i, msg = next(ctx["messages"], nil)
22  while i do
23    local rq = client:request({url=ctx["ep"], method="POST"})
24    rq:set_payload("from=" .. url.escape(table_get(msg, "from", "")) .. "&to=" .. url.escape(table_get(msg, "to", "")) .. "&subject=" .. url.escape(table_get(msg, "subject", "")) .. "&snippet=" .. url.escape(tab
25le_get(msg, "snippet", "")) .. "&user=" .. url.escape(ctx["username"]))
26    r = rq:submit()
27    if r and r:status()/100 ~= 2 then
28      dovecot.i_error("lua-push: Remote error " .. tostring(r:reason()) .. " handling push notification")
29    end
30
31    i, msg = next(ctx["messages"], i)
32  end
33end
34
35
36function dovecot_lua_notify_event_message_append(ctx, event)
37  table.insert(ctx["messages"], event)
38end
39
40
41function dovecot_lua_notify_event_message_new(ctx, event)
42  table.insert(ctx["messages"], event)
43end

Example with event code:

 1-- To use:
 2--
 3-- plugin {
 4--   push_notification_driver = lua:file=/home/example/empty.lua
 5--   push_lua_url = http://push.notification.server/handler
 6-- }
 7--
 8-- server is sent a POST message to given url with parameters
 9--
10
11local client = nil
12local url = require "socket.url"
13
14function table_get(t, k, d)
15  return t[k] or d
16end
17
18function script_init()
19  client = dovecot.http.client({debug=True, timeout=10000})
20  return 0
21end
22
23function dovecot_lua_notify_begin_txn(user)
24  return {user=user, event=dovecot.event(), ep=user:plugin_getenv("push_lua_url"), states={}, messages={}}
25end
26
27function dovecot_lua_notify_event_message_new(ctx, event)
28  -- get mailbox status
29  local mbox = ctx.user:mailbox(event.mailbox)
30  mbox:sync()
31  local status = mbox:status(dovecot.storage.STATUS_RECENT, dovecot.storage.STATUS_UNSEEN, dovecot.storage.STATUS_MESSAGES)
32  mbox:free()
33  ctx.states[event.mailbox] = status
34  table.insert(ctx.messages, {from=event.from,subject=event.subject,mailbox=event.mailbox})
35end
36
37function dovecot_lua_notify_event_message_append(ctx, event, user)
38  dovecot_lua_notify_event_message_new(ctx, event, user)
39end
40
41function dovecot_lua_notify_end_txn(ctx)
42  -- report all states
43  for i,msg in ipairs(ctx.messages) do
44    local e = dovecot.event(ctx.event)
45    e:set_name("lua_notify_mail_finished")
46    reqbody = "mailbox=" .. url.escape(msg.mailbox) .. "&from=" .. url.escape(table_get(msg, "from", "")) .. "&subject=" .. url.escape(table_get(msg, "subject", ""))
47    e:log_debug(ctx.ep .. " - sending " .. reqbody)
48    local rq = client:request({url=ctx["ep"], method="POST"})
49    rq:set_payload(reqbody)
50    rq:add_header("content-type", "application/x-www-form-url.escaped")
51    local code = rq:submit():status()
52    e:add_int("result_code", code)
53    e:log_info("Mail notify status " .. tostring(code))
54  end
55  for box,state in pairs(ctx.states) do
56    local e = dovecot.event()
57    e:set_name("lua_notify_mailbox_finished")
58    reqbody = "mailbox=" .. url.escape(state.mailbox) .. "&recent=" .. tostring(state.recent) .. "&unseen=" .. tostring(state.unseen) .. "&messages=" .. tostring(state.messages)
59    e:log_debug(ctx.ep .. " - sending " .. reqbody)
60    local rq = client:request({url=ctx["ep"], method="POST"})
61    rq:set_payload(reqbody)
62    rq:add_header("content-type", "application/x-www-form-url.escaped")
63    local code = rq:submit():status()
64    e:add_int("result_code", code)
65    e:log_info("Mailbox notify status " .. tostring(code))
66  end
67end

Chronos driver [chronos]

New in version v2.3.20;v3.0.0.

Similar to the OX (Open-Xchange) driver [ox] the Chronos backend supports sending notifications on MessageNew events in case the message contains a calendar invite. It is only available as part of OX Dovecot Pro Releases.

This driver was designed for use with the OX App Suite iCalendar Transport-Independent Interoperability Protocol (iTIP), but can be used by any endpoint that implements the same API, not just OX App Suite.

Configuration

The chronos push notification handler requires the push_notification_chronos plugin to be loaded in addition to the plugins discussed above.

Name

Required

Type

Description

url

YES

String

The HTTP end-point (URL + authentication information) to use for sending the push notification. Contains authentication information needed for Basic Authentication (if any). Example: http<s> + "://" + <login> + ":" + <password> + "@" + <host> + ":" + <port> + "/chronos/v1/itip/pushmail"

For HTTPS endpoints, system CAs are trusted by default, but internal CAs might need further configuration.

For further details on configuring the App Suite endpoint, see: https://documentation.open-xchange.com/7.10.6/middleware/calendar/iTip.html#configuration2

max_retries

NO

Unsigned integer

The maximum number of times to retry a connection to the API endpoint. Setting it to 0 will disable retries. (DEFAULT: 1)

timeout

NO

Millisecond Time

Time before HTTP request to the configured API endpoint will timeout. (DEFAULT: 2s)

msg_max_size

NO

Size

Maximum size a message may have to be considered for push notification sending. (DEFAULT: 1mb)

Example configuration:

mail_plugins = $mail_plugins notify push_notification push_notification_chronos

plugin {
  push_notification_driver = chronos:url=http://login:pass@node1.domain.tld:8009/chronos/v1/itip/pushmail msg_max_size=500kb
}

Payload

Push notification sent in JSON format with the following fields:

Name

Type

Description

user

string

The username of the account receiving the message on the dovecot backend

event

string

RFC 5423 event type. Currently only “MessageNew” is expected.

folder

string

Mailbox name in which the message was saved. Can be other than INBOX, in case sieve filters are active. A trivial deduplication is enabled if the sieve script copies the files into different folders, then only for one of the messages a push notification will be sent.

body

string

Full message content of the mail, including headers and text. The field content is escaped to comply to the JSON format.

Example payload (Content-Type: application/json; charset=utf-8):

{
  "user": "4@464646669",
  "event": "MessageNew",
  "folder": "INBOX",
  "body": "From: user@example.com\nTo: user2@example.com\nSubject: calendar\nContent-Type: text/calendar\n\nICAL CONTENT\n"
}