<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [

<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC5162 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5162.xml">
<!ENTITY RFC3501 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3501.xml">
<!ENTITY RFC5234 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5234.xml">
]>
<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
<?rfc strict="yes" ?>
<?rfc toc="yes"?>
<?rfc tocdepth="4"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes" ?>
<?rfc compact="yes" ?>
<?rfc subcompact="no" ?>
<rfc category="std" docName="draft-kundrat-qresync-arrived-00" ipr="trust200902">

  <front>
    <title>IMAP QRESYNC-ARRIVED Extension</title>

    <author fullname="Jan Kundrat" initials="J." surname="Kundrat">
      <address>
        <postal>
          <street>Eledrova 558</street>
          <city>Prague</city>
          <code>181 00</code>
          <country>CZ</country>
        </postal>
        <email>jkt@flaska.net</email>
      </address>
    </author>

    <date year="2012" month="August" day="18"/>

    <area>General</area>
    <workgroup>Internet Engineering Task Force</workgroup>

    <keyword>IMAP</keyword>
    <keyword>QRESYNC</keyword>
    <keyword>EXISTS</keyword>
    <keyword>EXPUNGE</keyword>
    <keyword>VANISHED</keyword>
    <keyword>ARRIVED</keyword>

    <abstract>
        <t>This document updates the QRESYNC extension of the IMAP protocol to use a new untagged response, the ARRIVED
            one, to inform about the UIDs of newly arriving messages.  Deprecating the EXISTS response, this extension
            prevents a possible race condition where clients can lose synchronization of message UIDs in a selected
            mailbox when new arrivals are immediately expunged.</t>
        <t><vspace blankLines="2" /></t>
    </abstract>
  </front>

  <middle>
    <section title="Introduction">
        <t>The QRESYNC extension <xref target="RFC5162"/> introduces the VANISHED response, a UID-based supplement to
            the sequence-number-based EXPUNGED <xref target="RFC3501"/>.  The VANISHED response is also explicitly
            allowed to refer to non-existing message UIDs.  Such a situation present a possible race condition where
            clients could lose track of the sequence -&gt; UID mapping where new arrivals are removed in a parallel
            session.
        </t>

        <t>This document updates the QRESYNC extension with a new response to be used instead of EXISTS, the ARRIVED
            one.  It also adds a mechanism for activating this extension and explains backward compatibility
            concerns.</t>

      <section title="Requirements Language">
        <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
        "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
        document are to be interpreted as described in <xref
        target="RFC2119">RFC 2119</xref>.</t>
      </section>
    </section>

    <section title="Race Condition in QRESYNC">
        <t>This section illustrates an example where a fully-compliant QRESYNC client loses track of UIDs of some
            messages in its mailbox.</t>

        <t>Suppose a client has opened a mailbox using the SELECT ... QRESYNC command.  The mailbox contains a single
            message with UID 5; UIDNEXT is 11.  The client is fully synchronized with the server.  In this situation,
            the server informs client about new arrival:</t>

        <figure align="center">
            <artwork align="left"><![CDATA[
S: * 3 EXISTS]]></artwork>
        </figure>

        <t>Upon receiving the EXISTS reponse, IMAP client would typically proceed towards discovering various message metadata like theirs UIDs and flags, either through the UID SEARCH ALL command, or via UID FETCH (FLAGS).  However, when the new arrivals are immediately expunged (perhaps as a result of an activity in a parallel session), the server can possibly process the request for additinal metadata only after the expunge has been reported to the client.  The VANISHED response actually complicates matter in this situation:</t>

        <figure align="center">
            <artwork align="left"><![CDATA[
S: * VANISHED 12:20]]></artwork>
        </figure>

        <t>This response informs about permanent removal of messages with UIDs between 12 and 20, inclusively.
            Unfortunately, as the VANISHED response is explicitly allowed to reference non-existing UIDs, the client
            doesn't know whether this VANISHED response affects two newly arriving messages.  Immediately after having
            received these two responses, the client cannot make any assumptions about the rest of the mailbox (i.e.
            messages with UIDs &gt; 6).  Specifically, any of the following can be true:</t>

        <t><list style="symbols">
                <t>The mailbox now contains only one message with UID 5</t>
                <t>The mailbox contains two messages; first of them has UID 5 and nothing is known about the other one</t>
                <t>The mailbox contains three messages; first of them has UID 5 and nothing is known about the rest of them</t>
            </list>
        </t>

        <t>In absence of the QRESYNC-ARRIVED extension or when talking to servers not offering that, clients MUST be
            prepared to work around the described race condition.  An example of such a behavior is issuing an explicit
            UID SEARCH command to rediscover the unclear portion of the sequence -&gt; UID mapping:</t>

        <figure align="center">
            <artwork align="left"><![CDATA[
S: * 3 EXISTS
S: * VANISHED 12:20
C: x UID SEARCH UID 11:*
S: * SEARCH
S: x OK Search completed]]></artwork>
        </figure>

        <t>A single search operation is enough to recover the complete UID mapping, but it induces unnecessary round trip before a full mapping can be established again.  In addition, clients would have to ignore any unsolicited FETCH responses not containing UID of the target message:</t>

        <figure align="center">
            <artwork align="left"><![CDATA[
S: * 3 EXISTS
S: * 2 FETCH (FLAGS (foo))
S: * 3 FETCH (FLAGS (bar))
S: * VANISHED 12:20
C: x UID SEARCH UID 11:*
S: * SEARCH
S: x OK Search completed
]]></artwork>
        </figure>

        <t>Clients have no chance but to ignore any FETCH response which affects messages whose UID is not yet known.</t>

        <t>This race condition can be avoided by switching to UID-based equivalent of EXISTS, the ARRIVED response.
            Alternatively, the same goal would be achieved if the QRESYNC specification mandated that VANISHED responses
            MUST NOT refer to UIDs which were not present in the mailbox at the time immediately before the VANISHED
            response was generated.</t>
    </section>

    <section title="IMAP Protocol Changes">
        <section title="QRESYNC-ARRIVED Parameter to SELECT/EXAMINE">
            <t>The QRESYNC-ARRIVED parameter to SELECT/EXAMINE commands shares syntax with the QRESYNC parameter defined
                in <xref target="RFC5162"/>; the only exception is that it is identified by the "QRESYNC-ARRIVED"
                atom.</t>

            <t>For backward compatibility, IMAP servers supporting this extension MUST support QRESYNC as defined by
                <xref target="RFC5162"/>.  In addition, such servers SHOULD NOT sent VANISHED responses containing UIDs
                of messages which were expunged before, and SHOULD NOT send the untagged EXPUNGED instead of
                VANISHED.</t>
        </section>

        <section title="ARRIVED Response">
            <t>Contents:  ordered list of UIDs</t>

            <t>The ARRIVED response reports that the specified UIDs have been delivered to the mailbox.  It is similar in
                functionality to the EXISTS response <xref target="RFC3501"/>; however, it can return information about
                multiple messages, and it returns UIDs instead of message numbers.  The first benefit saves bandwidth,
                while the second elliminates the potential of client losing track of assigned UIDs in a mailbox.</t>

            <t>If the client has opened the mailbox with the QRESYNC-ARRIVED parameter to SELECT/EXAMINE command, the
                EXISTS response MUST be still sent during the initial mailbox synchronization, i.e. between having
                received the SELECT or EXAMINE command and the corresponding tagged response, as in <xref
                    target="RFC3501"/>.  Servers MAY send the EXISTS response several times, but they SHOULD send it
                only once to conserve resources.  The number given in the EXISTS response MUST NOT shrink.  The clients
                MUST treat the last occurence as the final data.</t>

            <t>Servers MUST NOT issue ARRIVED prior to issuing a tagged OK for the SELECT/EXAMINE command to prevent
                uncertanities about the HIGHESTMODSEQ value.  New arrivals since the last time the mailbox has been
                selected are reported using FETCH responses with embedded UID, as in regular QRESYNC.</t>

            <t>After the mailbox has been selected, all further updates
                about new arrivals MUST use the ARRIVED response.  If servers were allowed to fall back to untagged
                EXISTS, the possibility of race conditions would return.</t>

            <t>UIDs sent in the ARRIVED response must be always presented in a sorted order, lowest UID first.  The
                sequence MUST NOT contain duplicate UIDs.  Servers SHOULD take advantage of the compression capabilities
                of the sequence-set syntax and use the range syntax if possible.</t>

            <t>The ARRIVED response MUST NOT be sent unless the client has passed the QRESYNC-ARRIVED option to the last
                SELECT or EXAMINE command.</t>
        </section>
    </section>

    <section anchor="Acknowledgements" title="Acknowledgements">
        <t>This text builds upon the QRESYNC IMAP extension <xref target="RFC5162" />.</t>
    </section>

    <section anchor="IANA" title="IANA Considerations">
      <t>IMAP4 capabilities are registered by publishing a standards track or IESG approved experimental RFC.  The
          registry is currently located at:</t>
      <t>http://www.iana.org/assignments/imap4-capabilities</t>
      <t>This document defines the QRESYNC-ARRIVED IMAP capability.  IANA will be asked to add this capability to the
          registry.</t>
    </section>

    <section title="Formal Syntax">
        <t>The following syntax specification uses the Augmented Backus-Naur
            Form (ABNF) notation as specified in <xref target="RFC5234"/>.</t>

        <t>Non-terminals referenced but not defined below are as defined by
            <xref target="RFC3501"/>, <xref target="RFC5162"/>, or <xref target="RFC5234"/>.</t>

        <figure align="center">
            <artwork align="left" type="abnf"><![CDATA[
capability          =/ "QRESYNC-ARRIVED"

select-param        =/ "QRESYNC-ARRIVED" SP "(" uidvalidity SP
                       mod-sequence-value [SP known-uids]
                       [SP seq-match-data] ")"
                       ;; conforms to the generic select-param
                       ;; syntax defined in [IMAPABNF]

message-data        =/ arrived-resp

arrived-resp        = "ARRIVED" SP sorted-sequence-set

sorted-sequence-set = sequence-set
                    ;; sequence of UIDs, "*" is not allowed
                    ;; UIDs must be sorted, lowest first, no duplicates

]]></artwork>
        </figure>
    </section>

    <section anchor="Security" title="Security Considerations">
        <t>This extensions adds no functionality on top of what is already defined in the QRESYNC extension,
            and therefore we believe that no additional security implications have to be considered.</t>
    </section>
  </middle>

  <back>
    <references title="Normative References">
      &RFC2119;

      &RFC5234;

      &RFC5162;

      &RFC3501;
    </references>

    <section title="Changelog">
        <section title="Changes in 00 since private-02">
            <t><list style="symbols">
                    <t>Fixed ABNF syntax for the select-param</t>
                    <t>Better abstract</t>
                    <t>Clearer formatting</t>
            </list></t>
        </section>
        <section title="Changes in private-02 since private-01">
            <t><list style="symbols">
                    <t>ENABLE is still required, otherwise clients have no way of activating VANISHED</t>
            </list></t>
        </section>
        <section title="Changes in private-01 sice private-00">
            <t><list style="symbols">
                    <t>Clarified that UIDs presented in the ARRIVED response MUST be always presented in a sorted order</t>
                    <t>Used a special non-terminal in the ABNF grammar</t>
                    <t>Used correct format for FETCH responses</t>
                    <t>Clarify that EXISTS must be sent at least once and SHOULD be sent only once</t>
                    <t>Clarify that ARRIVED MUST NOT occur before the tagged response to SELECT/EXAMINE</t>
                    <t>Clarify that arrivals since the last time are to be reported through usual UID FETCH, as in old QRESYNC</t>
            </list></t>
        </section>
    </section>
  </back>
</rfc>
