<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">Making progress :)</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">I'll document some obtracles I found,
      to make it easier for the next one implementing push with Dovecot
      and Lua.</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">First I tried with my usual Alpine
      based container, but Alpine seems not to build the Lua stuff for
      Dovecot :(</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">So I moved to an Ubuntu 18.04 based
      container and the official Dovecot CE repo:</div>
    <div class="moz-cite-prefix">
      <pre style="background-color:#ffffff;color:#000000;font-family:'JetBrains Mono',monospace;font-size:9,8pt;">FROM ubuntu:<span style="color:#0000ff;">18.04
</span>
RUN apt-get update && \
   apt-get install -y apt-transport-https gpg curl && \
   curl <a class="moz-txt-link-freetext" href="https://repo.dovecot.org/DOVECOT-REPO-GPG">https://repo.dovecot.org/DOVECOT-REPO-GPG</a> | gpg <span style="color:#808080;font-style:italic;">--import && \
</span><span style="color:#808080;font-style:italic;">   </span>gpg <span style="color:#808080;font-style:italic;">--export ED409DA1 > /etc/apt/trusted.gpg.d/dovecot.gpg && \
</span><span style="color:#808080;font-style:italic;">   </span>echo <span style="color:#008000;font-weight:bold;">"deb <a class="moz-txt-link-freetext" href="https://repo.dovecot.org/ce-2.3-latest/ubuntu/bionic">https://repo.dovecot.org/ce-2.3-latest/ubuntu/bionic</a> bionic main" </span>> /etc/apt/sources.list.d/dovecot.list && \
   apt-get update && \
   bash -c <span style="color:#008000;font-weight:bold;">"apt-get install -y dovecot-{core,imapd,sqlite,managesieved,sieve,pop3d,lmtpd,submissiond,lua} lua-socket"
</span>
CMD [ <span style="color:#008000;font-weight:bold;">"/usr/sbin/dovecot"</span>,<span style="color:#008000;font-weight:bold;">"-F"</span>,<span style="color:#008000;font-weight:bold;">"-c"</span>,<span style="color:#008000;font-weight:bold;">"/etc/dovecot/dovecot.conf" </span>]

I had to install lua-socket, which is used by the example script and not required by dovecot-lua, which is ok, you just need to know.

Using Aki's code snippet as user=<metadata> lead to an other error:

Aug 03 14:54:15 Error: doveadm: lua: /usr/share/lua/5.2/socket/url.lua:31: bad argument #1 to 'gsub' (string expected, got nil)
Aug 03 14:54:15 Error: lmtp(<username> 38): lmtp-server: conn 10.9.94.14:42092 [1]: rcpt <username>: lua: /usr/share/lua/5.2/socket/url.lua:31: bad argument #1 to 'gsub' (string expected, got nil)
</pre>
    </div>
    <div class="moz-cite-prefix">I'm now skipping the notification, if
      no metadata is set, like the OX driver does:<br>
    </div>
    <div class="moz-cite-prefix">
      <pre style="background-color:#ffffff;color:#000000;font-family:'JetBrains Mono',monospace;font-size:9,8pt;"><span style="color:#000080;font-weight:bold;">function </span><span style="color:#660e7a;font-style:italic;">dovecot_lua_notify_event_message_new</span>(<span style="color:#006666;font-weight:bold;">ctx</span>, <span style="color:#006666;font-weight:bold;">event</span>)
  <span style="color:#808080;font-style:italic;">-- check if there is a push token registered
</span><span style="color:#808080;font-style:italic;">  </span><span style="color:#000080;font-weight:bold;">if </span>(<span style="color:#006666;font-weight:bold;">ctx</span>.meta == <span style="color:#d700ff;">nil </span><span style="color:#000080;font-weight:bold;">or </span><span style="color:#006666;font-weight:bold;">ctx</span>.meta == <span style="color:#008000;font-weight:bold;">''</span>) <span style="color:#000080;font-weight:bold;">then
</span><span style="color:#000080;font-weight:bold;">    return
</span><span style="color:#000080;font-weight:bold;">  end
</span></pre>
    </div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Currently looking into the following
      questions:</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">- can I get the rfc 5423 type of event
      somehow (obviously I can set it on the event myself depending of
      the function called)</div>
    <div class="moz-cite-prefix">- looking at the example code, it looks
      like it can be called for multiple messages, when does that happen
      (LMTP send more then one)<br>
    </div>
    <div class="moz-cite-prefix">- why is the mailbox status put into an
      other structure and send with a different notifiction</div>
    <div class="moz-cite-prefix">- does anyone have a code snippet to
      send a JSON encoded message (probably easy to figure out looking
      at Lua docu)</div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Ralf<br>
    </div>
    <div class="moz-cite-prefix"><br>
    </div>
    <div class="moz-cite-prefix">Am 03.08.20 um 11:56 schrieb Ralf
      Becker:<br>
    </div>
    <blockquote type="cite"
      cite="mid:18f2cd72-0056-6f03-0430-8f7858b576b8@egroupware.org">
      <pre class="moz-quote-pre" wrap="">Thanks Aki, I'll check it out :)


Am 03.08.20 um 11:40 schrieb Aki Tuomi:
</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">On 03/08/2020 12:31 Ralf Becker <a class="moz-txt-link-rfc2396E" href="mailto:rb@egroupware.org"><rb@egroupware.org></a> wrote:

 
We're currently using the OX push driver, which is straight forward
(simple web hook) and allows to store (multiple) push tokens of our
webmailer direct in mailbox metadata.

Only drawback is that it only supports new arriving mails in the INBOX,
even mails moved via Sieve to other folders are NOT reported.

Therefore we updated now to Dovecot 2.3(.10.1) to also get mails moved
by user or Sieve scripts, deleted mails or flag changes.

As far as I read the example Lua scripts and (a little) the Dovecot C
code, the nice indirection of using mailbox metadata to a) enable push
and b) store push tokens (modify reported user attribute with them) does
NOT exist in the Lua driver by default.

So my questions is: how can I access mailbox metadata from within the
Lua script, to make eg. the example script behave like the OX driver
with user_from_metadata set?

I'm happy to update the Lua examples, if that's any help ...

Ralf

-- 
Ralf Becker
EGroupware GmbH [<a class="moz-txt-link-abbreviated" href="http://www.egroupware.org">www.egroupware.org</a>]
Handelsregister HRB Kaiserslautern 3587
Geschäftsführer Birgit und Ralf Becker
Leibnizstr. 17, 67663 Kaiserslautern, Germany
Telefon +49 631 31657-0
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">Actually it does exist:

<a class="moz-txt-link-freetext" href="https://doc.dovecot.org/admin_manual/lua/#mail_user.metadata_get">https://doc.dovecot.org/admin_manual/lua/#mail_user.metadata_get</a>

or

<a class="moz-txt-link-freetext" href="https://doc.dovecot.org/admin_manual/lua/#object-mailbox">https://doc.dovecot.org/admin_manual/lua/#object-mailbox</a>

<a class="moz-txt-link-freetext" href="mailbox:metadata_get()">mailbox:metadata_get()</a>

You get both of these objects from the push notification data, you just have to keep them in the context state. (See the example scripts)

function dovecot_lua_notify_begin_txn(user)
   local meta = user:metadata_get("/private/key")
   return {messages={}, ep=user:plugin_getenv("push_lua_url"), username=user.username, meta=meta}
end

Aki
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
</pre>
    </blockquote>
    <p><br>
    </p>
    <pre class="moz-signature" cols="72">-- 
Ralf Becker
EGroupware GmbH [<a class="moz-txt-link-abbreviated" href="http://www.egroupware.org">www.egroupware.org</a>]
Handelsregister HRB Kaiserslautern 3587
Geschäftsführer Birgit und Ralf Becker
Leibnizstr. 17, 67663 Kaiserslautern, Germany
Telefon +49 631 31657-0
</pre>
  </body>
</html>