<?xml version="1.0"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
  <!ENTITY rfc2119 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'>
  <!ENTITY rfc3501 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3501.xml'>
  <!ENTITY rfc3502 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3502.xml'>
  <!ENTITY rfc5182 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5182.xml'>
  <!ENTITY rfc5256 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5256.xml'>
  <!ENTITY rfc7162 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.7162.xml'>
  <!ENTITY rfc8174 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.8174.xml'>
  <!ENTITY rfc9051 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.9051.xml'>
  <!ENTITY rfc9394 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.9394.xml'>

]>
<?xml-stylesheet type="text/xsl" href="rfc2629.xslt"?>
<rfc category="exp" ipr="pre5378Trust200902" docName="draft-ietf-extra-imap-messagelimit-08">
	<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
	<?rfc toc="yes" ?>
	<?rfc symrefs="yes" ?>
	<?rfc sortrefs="yes"?>
	<?rfc iprnotified="no" ?>
	<?rfc strict="yes" ?>
	<?rfc comments="yes" ?>
	<?rfc inline="yes" ?>
	<?rfc compact="yes"?>
	<?rfc subcompact="no"?>
	<front>
		<title abbrev="IMAP MESSAGELIMIT">
      IMAP MESSAGELIMIT Extension
    </title>
		<author initials="A." surname="Melnikov" fullname="Alexey Melnikov">
			<organization abbrev="Isode">
				Isode Limited
			</organization>
			<address>
				<email>alexey.melnikov@isode.com</email>
				<uri>https://www.isode.com</uri>
			</address>
		</author>

    <author initials="A. P." surname="Achuthan" fullname="Arun Prakash Achuthan">
      <organization abbrev="Yahoo!">
        Yahoo!
      </organization>
      <address>
        <email>arunprakash@myyahoo.com</email>
      </address>
    </author>

    <author initials="V." surname="Nagulakonda" fullname="Vikram Nagulakonda">
      <organization abbrev="Yahoo!">
        Yahoo!
      </organization>
      <address>
        <email>nvikram_imap@yahoo.com</email>
      </address>
    </author>

    <author initials="L." surname="Alves" fullname="Luis Alves">
      <address>
        <email>luis.alves@lafaspot.com</email>
      </address>
    </author>

    <date year="2024"/>
		<abstract>
			<t>
        The MESSAGELIMIT extension of the Internet Message Access Protocol (RFC 3501/RFC 9051)
        allows servers to announce a limit on the number of
        messages that can be processed in a single FETCH/SEARCH/STORE/COPY/MOVE/APPEND/EXPUNGE command.
        This helps servers to control resource usage when performing various IMAP operations.
        This helps clients to know the message limit enforced by corresponding IMAP server
        and avoid issuing commands that would exceed such limit.
      </t>
      
    </abstract>
	</front>
	<middle>
    
        <section title="Introduction and Overview">

      <t>This document defines an extension to the Internet Message Access Protocol <xref target="RFC3501"/>
      for announcing a server limit on the number of
      messages that can be processed in a single FETCH/SEARCH/STORE/COPY/MOVE/APPEND/EXPUNGE command.
      This extension is compatible with both IMAP4rev1 <xref target="RFC3501"/> and IMAP4rev2 <xref target="RFC9051"/>.</t>

        </section>

		<section title="Document Conventions">
			<t>In protocol examples, this document uses a prefix of "C: " to denote lines sent by the client to the server, and
			"S: " for lines sent by the server to the client. Lines prefixed with "// " are comments explaining the previous protocol line.
			These prefixes and comments are not part of the protocol. Lines without any of these prefixes are continuations of the previous line,
			and no line break is present in the protocol unless specifically mentioned.</t>
      
			<t>
            The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
            "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
            "OPTIONAL" in this document are to be interpreted as described in BCP
            14 <xref target="RFC2119"/> <xref target="RFC8174"/>
            when, and only when, they appear in all capitals, as shown here.
            </t>

            <t>Other capitalised words are IMAP key words <xref target="RFC3501"/><xref target="RFC9051"/>
            or key words from this document.</t>
      
		</section>

    <section title="The MESSAGELIMIT extension" anchor="imap-messagelimit">

        <t>An IMAP server advertises support for the MESSAGELIMIT extension
        by including "MESSAGELIMIT=&lt;limit>" capability in the CAPABILITY response/response code,
        where "&lt;limit>" is a positive integer that conveys the maximum number of messages
        that can be processed in a single [UID] SEARCH/FETCH/STORE/COPY/MOVE/APPEND or UID EXPUNGE command.</t>

        <t>An IMAP server that only enforces message limit on [UID] COPY/APPEND commands would include
        the "SAVELIMIT=&lt;limit>" capability (instead of the "MESSAGELIMIT=&lt;limit>") in
        the CAPABILITY response/response code.</t>

        <t>The limit advertised in the MESSAGELIMIT or SAVELIMIT capability SHOULD NOT be lower than 1000 messages.</t>
      
        <!--///
            The server MUST NOT impose lower limit on a supporting client than on any other client.
            Put diffently, if code in a client worked in the past, then adding support for this extension
            MUST NOT break it due to lower server limit.
            
        Is the above only meaningful if ENABLE is used?
        -->

   <section title="Returning limits on the number of messages processed in a single SEARCH/FETCH/STORE/COPY/MOVE/APPEND/EXPUNGE command" anchor="messagelimit-commands">

      <t>
      <!--Reason: don't want to break COPY atomicity guarantee from IMAP4rev1/IMAP4rev2.-->
      If a server implementation doesn't allow more than &lt;N&gt; messages to be operated on
      by a single COPY/UID COPY command, it MUST fail the command by returning a tagged NO response
      with the MESSAGELIMIT response code defined below.

      If a server implementation doesn't allow more than &lt;N&gt; messages to be operated on
      by a single SEARCH/FETCH/STORE/MOVE/EXPUNGE command (or their UID variants), it MUST return the MESSAGELIMIT response code defined below:

      <list style='hanging'>
      <t hangText='MESSAGELIMIT'>
        <iref item='MESSAGELIMIT (response code)'/>

        <list>
          <t>
          The server doesn't allow more than &lt;N&gt; messages to be operated on
          by a single SEARCH/FETCH/STORE/COPY/MOVE command (or their UID variants).
          The lowest processed UID is &lt;LastUID&gt;. 
          The client needs to repeat the operation for remaining messages, if required.
          </t>

          <t>
          The server doesn't allow more than &lt;N&gt; \Deleted messages to be operated on
          by a single UID EXPUNGE command.
          The lowest processed UID is &lt;LastUID&gt;. 
          The client needs to repeat the operation for remaining messages, if required.
          </t>

          <t>
          Note that when the MESSAGELIMIT response code is returned,
          the server is REQUIRED to process messages from highest to lowest UIDs.
          </t>
          
          <t>
          Note that when the MESSAGELIMIT response code is similar to the LIMIT (<xref target="RFC9051"/>) response code,
          but it provides more details on the exact type of the limit and how to resume the command
          once the limit is exceeded.
          </t>

          <t>In the following example the &lt;N&gt; value is 1000 and the lowest processed UID &lt;LastUID&gt; is 23221.</t>

          <t>
<figure><artwork><![CDATA[
  C: 03 FETCH 10000:14589 (UID FLAGS)
  S: * 14589 FETCH (FLAGS (\Seen) UID 25000)
  S: * 14588 FETCH (FLAGS (\Answered) UID 24998)
  S: ... further 997 fetch responses
  S: * 13590 FETCH (FLAGS () UID 23221)
  S: 03 OK [MESSAGELIMIT 1000 23221] FETCH completed with 1000 partial
      results
]]></artwork></figure>
          </t>

          <t>In the following example the client searches for UNDELETED UIDs between 22000:25000.
          The total number of searched messages (note, NOT the number of matched messages) exceeds the server's published 1000 messages limit.</t>

          <t>
<figure><artwork><![CDATA[
  C: 04 UID SEARCH UID 22000:25000 UNDELETED
  S: * SEARCH 25000 24998 (... UIDs ...) 23221
  S: 04 OK [MESSAGELIMIT 1000 23221] SEARCH completed with 1000 partial results 
]]></artwork></figure>
          </t>

          <t>The following example demonstrates copy of messages with UIDs between 18000:21000.
          The total message count exceeds the server's published 1000 messages limit.
          As COPY/UID COPY needs to atomic (as per <xref target="RFC3501"/>/<xref target="RFC9051"/>),
          no messages are copied.</t>

          <t>
<figure><artwork><![CDATA[
  C: 05 UID COPY 18000:21000 "Trash"
  S: 05 NO [MESSAGELIMIT 1000 20001] Too many messages to copy, try a smaller subset
]]></artwork></figure>
          </t>

          <t>The following example shows MOVE of messages with UIDs between 18000:21000.
          The total message count exceeds the server's published 1000 messages limit.
          (Unlike COPY/UID COPY, MOVE/UID MOVE don't need to be atomic.)
          The client that wants to move all messages in the range and observes a MESSAGELIMIT response code,
          can repeat the UID MOVE command with the same parameter. (For the MOVE command, the message set parameter need to be updated before repeating the command.)
          The client needs to keep doing this until the MESSAGELIMIT response is not returned (or until a tagged NO/BAD is returned).
          </t>
          
          <t>
<figure><artwork><![CDATA[
  C: 06 UID MOVE 18000:21000 "Archive/2021/2021-12"
  S: * OK [COPYUID 1397597919 20001:21000 22363:23362] Some messages were not moved
  S: * 12336 EXPUNGE
  S: * 12335 EXPUNGE
  ...
  S: * 11337 EXPUNGE
  S: 06 OK [MESSAGELIMIT 1000 20001] MOVE completed for the last 1000 messages
]]></artwork></figure>
          </t>


          <t>The following example shows update of flags for messages with UIDs between 18000:20000. The total number of existing messages in the UID range exceeds the server's published 1000 messages limit.
          The client that wants to change flags for all messages in the range and observes a MESSAGELIMIT response code,          
          can repeat the UID STORE command with the updated UID range that doesn't include the UID returned in the MESSAGELIMIT response code. (For the STORE command, the message set parameter also need to be updated before repeating the command.)
          The client needs to keep doing this until the MESSAGELIMIT response is not returned (or until a tagged NO/BAD is returned).
          </t>

          <t>
<figure><artwork><![CDATA[
  C: 07 UID STORE 18000:20000 +FLAGS (\Seen)
  S: * 11215 FETCH (FLAGS (\Seen \Deleted) UID 20000)
  S: * 11214 FETCH (FLAGS (\Seen \Answered \Deleted) UID 19998)
  ...
  S: * 10216 FETCH (FLAGS (\Seen) UID 19578)
  S: 07 OK [MESSAGELIMIT 1000 19578] STORE completed for the last 1000 messages          
]]></artwork></figure>
          </t>


          <t>The following example shows removal of messages (using UID EXPUNGE) that have \Deleted flag set with UIDs between 11000:13000.
          The total message count of messages with \Deleted flag set exceeds the server's published 1000 messages limit.
          The client that wants to remove all messages marked as \Deleted in the range and observes a MESSAGELIMIT response code,          
          can repeat the UID EXPUNGE command with the same parameter.
          The client needs to keep doing this until the MESSAGELIMIT response is not returned (or until a tagged NO/BAD is returned).
          </t>

          <t>
<figure><artwork><![CDATA[
  C: 08 UID EXPUNGE 11000:13000
  S: * 4306 EXPUNGE
  S: * 4305 EXPUNGE
  ...
  S: * 3307 EXPUNGE
  S: 08 OK [MESSAGELIMIT 1000 11627] UID EXPUNGE completed for the last 1000 messages          
]]></artwork></figure>
          </t>

          <t>The following example shows removal of messages (using EXPUNGE) that have \Deleted flag set.
          Unlike UID EXPUNGE, the server MUST NOT impose any message limit when processing EXPUNGE.
         </t>

          <t>
<figure><artwork><![CDATA[
  C: 09 EXPUNGE
  S: * 4306 EXPUNGE
  S: * 4305 EXPUNGE
  ...
  S: * 3307 EXPUNGE
  S: * 112 EXPUNGE
  S: 09 OK EXPUNGE completed          
]]></artwork></figure>
          </t>

          <t>
          Similarly, the server MUST NOT impose any message limit when processing a CLOSE or a STATUS UNSEEN command.
         </t>


          <t>The following example shows use of MESSAGELIMIT response code together with the PARTIAL <xref target="RFC9394"/> extension.
          The total message count (as specified by the PARTIAL range) exceeds the server's published 1000 messages limit,
          so the server refuses to do any work in this case.</t>

          <t>
<figure><artwork><![CDATA[
  C: 10 UID FETCH 22000:25000 (UID FLAGS MODSEQ) (PARTIAL -1:-1500)
  S: 10 NO [MESSAGELIMIT 1000] FETCH exceeds the maximum 1000 message limit
]]></artwork></figure>
          </t>


          <t>Without the PARTIAL parameter the above UID FETCH can look
          like this:</t>

          <t>
<figure><artwork><![CDATA[
  C: 10 UID FETCH 22000:25000 (UID FLAGS MODSEQ)
  S: * 12367 FETCH (FLAGS (\Seen \Deleted) UID 23007)
  S: * 12366 FETCH (FLAGS (\Seen \Answered \Deleted) UID 23114)
  ...
  S: * 13366 FETCH (FLAGS (\Seen) UID 24598)
  S: 10 OK [MESSAGELIMIT 1000 23007] FETCH exceeds the maximum 1000 message limit
]]></artwork></figure>
          </t>


        </list>
      </t>
      </list>

      </t>

      <t>Note that when the server needs to return both EXPUNGEISSUED (<xref target="RFC9051"/>)
      and MESSAGELIMIT response codes, the former MUST be returned in the tagged OK response,
      while the latter MUST be returned in an untagged NO response. The following example demonstrates
      that:
      </t>

      <t>
<figure><artwork><![CDATA[
  C: 11 FETCH 10000:14589 (UID FLAGS)
  S: * 14589 FETCH (FLAGS (\Seen) UID 25000)
  S: * 14588 FETCH (FLAGS (\Answered) UID 24998)
  S: ... further 997 fetch responses
  S: * 13590 FETCH (FLAGS () UID 23221)
  S: * NO [MESSAGELIMIT 1000 23221] FETCH completed with 1000 partial
      results
  S: 11 OK [EXPUNGEISSUED] Some messages were also expunged
]]></artwork></figure>
      </t>

      <t>When IMAP MULTIAPPEND <xref target="RFC3502"/> extension is also supported by the server,
      the message limit also applies to the APPEND command.</t>

    </section>

    <section title="UIDAFTER and UIDBEFORE SEARCH criteria" anchor="search-criteria">

      <t>
        The MESSAGELIMIT extension also defines 2 extra SEARCH keys: UIDAFTER and UIDBEFORE,
        which make it easier to convert a single UID to a range of UIDs.
      </t>
      
      <t>
        "UIDAFTER &lt;uid&gt;" - Messages that have a UID greater than the specified UID.
        This is semantically the same as "UID &lt;uid&gt;+1:*".
      </t>

      <t>
        "UIDBEFORE &lt;uid&gt;" - Messages that have a UID less than the specified UID.
        This is semantically the same as "UID 1:&lt;uid&gt;-1" (or if &lt;uid&gt; has the value 1, then the empty set).
      </t>

      <t>
        These 2 SEARCH keys are particularly useful when the SEARCHRES <xref target="RFC5182"/> extension
        is also supported, but they can be used without it. For example, this allows a SEARCH that
        sets the "$" marker to be converted to a range of messages in a subsequent SEARCH, and both SEARCH requests
        can be pipelined.
      </t>
      
      <t>
<figure><artwork><![CDATA[
  C: 12 UID SEARCH UIDAFTER 25000 UNDELETED
  S: * SEARCH 27800 27798 (... 250 UIDs ...) 25001
  S: 12 OK SEARCH completed
]]></artwork></figure>
      </t>

    </section>
        
    <section title="Interaction with SORT and THREAD extensions">

      <t>
      Servers that advertise MESSAGELIMIT N will be unable to execute a THREAD <xref target="RFC5256"/> command in a mailbox with more than N messages.
      </t>

      <t>
      Servers that advertise MESSAGELIMIT N might be unable to execute a SORT <xref target="RFC5256"/> command in a mailbox with more than N messages,
      unless they maintain indices for different SORT orders they support. In absence of such indeces server implementors will need to decide whether
      their server advertises SORT or MESSAGELIMIT capability.
      </t>

    </section>

    <section title="Interaction with SEARCHRES extension and IMAP4rev2">

      <t>
      Servers that support both MESSAGELIMIT and SEARCHRES <xref target="RFC5182"/> extensions MUST truncate SEARCH SAVE result stored
      in the $ variable when the SEARCH command succeeds, but the MESSAGELIMIT response code is returned. For example, if the following
      SEARCH would have returned 1200 results in absence of MESSAGELIMIT, and the MESSAGELIMIT is 1000, only 1000 matching results
      will be saved in the $ variable:
      </t>

      <t>
<figure><artwork><![CDATA[
  C: D0004 UID SEARCH RETURN (SAVE) SINCE 1-Jan-2004 NOT FROM "Smith" UID 22000:25000 UNDELETED
  S: D0004 OK [MESSAGELIMIT 1000 1179] SEARCH completed with 1000 partial results saved
]]></artwork></figure>
      </t>

    </section>

    
    <section title="Effects of MESSAGELIMIT/SAVELIMIT extensions on non compliant clients">

      <t>
      A server that advertises the MESSAGELIMIT=N capability would have the following effect on clients
      that don't support this capability:
      
        <list>

          <t>Operations on a mailbox that has &lt;= N messages are not affected.</t>

          <t>In a mailbox with more than N messages:
          
            <list>

              <t>An attempt to COPY/UID COPY more than N messages will always fail.</t>
              <t>EXPUNGE and CLOSE will always operate on the full mailbox, so they are not affected.</t>
              <t>Other commands like FETCH, SEARCH and MOVE will be effectively restricted to the last N messages
              of the mailbox. In particular unextended SEARCHes intended for counting of messages with or without
              a particular set of flags would return incorrect counts.</t>
            
            </list>
          </t>
      
        </list>
        
      </t>

    </section>
    
    </section>

    <section title="Formal syntax">
			<t>The following syntax specification uses the Augmented Backus-Naur Form (ABNF) notation as specified in <xref target="ABNF"/>.</t>
			<t>Non-terminals referenced but not defined below are as defined by <xref target="RFC3501">IMAP4</xref>.</t>
			<t>Except as noted otherwise, all alphabetic characters are case-insensitive.
      The use of upper or lower case characters to define token strings is for editorial clarity only.
      Implementations MUST accept these strings in a case-insensitive fashion.</t>

<figure><artwork><![CDATA[
capability          =/ "MESSAGELIMIT=" message-limit /
                       "SAVELIMIT=" message-limit
                       ;; <capability> from [RFC3501]

message-limit       = nz-number

resp-text-code      =/ "MESSAGELIMIT" SP message-limit [SP uniqueid]
    ;; No more than nz-number messages can be processed
    ;; by any command at a time. The last (lowest) processed
    ;; UID is uniqueid.
    ;; The last parameter is omitted, when not known.
    
]]></artwork></figure>
      
		</section>

		<section title="Security Considerations">

      <t>
      This document defines an additional IMAP4 capability.  As such, it
      does not change the underlying security considerations of <xref target="RFC3501"/>
      and IMAP4rev2 <xref target="RFC9051"/>.
      </t>

      <t>
      This document defines an optimization that can both reduce the amount of work
      performed by the server, as well at the amount of data returned to the client.
      Use of this extension is likely to cause the server and the client to use less memory
      than when the extension is not used. However, as this is going
      to be new code in both the client and the server, rigorous testing of such code
      is required in order to avoid introducing of new implementation bugs.
      </t>
      
    <!--///Also misleading message counts? Or an attempt to calculate how much
        will be freed if \Deleted messages are EXPUNGEd from the mailbox?-->

		</section>

    <section title="IANA Considerations">
      
      <section title="Changes/additions to the IMAP4 capabilities registry">
        
			<t>
				IMAP4 capabilities are registered by publishing a standards track or
				IESG approved Informational or Experimental RFC.
        The registry is currently located at:
			</t>

			<figure><artwork><![CDATA[
   https://www.iana.org/assignments/imap4-capabilities
    ]]></artwork></figure>

			<t>
        IANA is requested to add registrations of "MESSAGELIMIT=" and "SAVELIMIT=" capabilities to
        this registry, both pointing to this document.
      </t>

      </section>

    </section>

    <section title="Acknowledgments">

      <t>This document was motivated by Yahoo! team and their questions
      about best client practices for dealing with large mailboxes.</t>

			<t>
			Editor of this document would like to thank the following people
			who provided useful comments or participated in discussions of
      this document: Timo Sirainen, Barry Leiba, Ken Murchison and Arnt Gulbrandsen.
			</t>

		</section>
		
	</middle>
	<back>
		<references title="Normative References">
			&rfc2119;
      &rfc8174;
      &rfc3501;
      &rfc3502;
      &rfc5182;
      &rfc5256;
      <reference anchor="ABNF">
				<front>
					<title>Augmented BNF for Syntax Specifications: ABNF</title>
					<author initials="D" surname="Crocker" fullname="Dave Crocker" role="editor">
						<organization />
					</author>
					<author initials="P" surname="Overell" fullname="Paul Overell" role="editor">
						<organization />
					</author>
					<date year="2008" month="January"/>
				</front>
				<seriesInfo name="RFC" value="5234" />
				<format type="TXT" target="https://www.rfc-editor.org/info/rfc5234" />
			</reference>

      &rfc9051;

    </references>

    <references title="Informative References">
      
      &rfc9394;
    
    </references>

	</back>
</rfc>
