7Network Working Group A. Melnikov
8Request for Comments: 5182 Isode Ltd.
9Updates: 3501 March 2008
10Category: Standards Track
17 This document specifies an Internet standards track protocol for the
18 Internet community, and requests discussion and suggestions for
19 improvements. Please refer to the current edition of the "Internet
20 Official Protocol Standards" (STD 1) for the standardization state
21 and status of this protocol. Distribution of this memo is unlimited.
25 Many IMAP clients use the result of a SEARCH command as the input to
26 perform another operation, for example, fetching the found messages,
27 deleting them, or copying them to another mailbox.
29 This can be achieved using standard IMAP operations described in RFC
30 3501; however, this would be suboptimal. The server will send the
31 list of found messages to the client; after that, the client will
32 have to parse the list, reformat it, and send it back to the server.
33 The client can't pipeline the SEARCH command with the subsequent
34 command, and, as a result, the server might not be able to perform
37 This document proposes an IMAP extension that allows a client to tell
38 a server to use the result of a SEARCH (or Unique Identifier (UID)
39 SEARCH) command as an input to any subsequent command.
43 Many IMAP clients use the result of a SEARCH command as the input to
44 perform another operation, for example, fetching the found messages,
45 deleting them, or copying them to another mailbox.
47 This document proposes an IMAP extension that allows a client to tell
48 a server to use the result of a SEARCH (or UID SEARCH) command as an
49 input to any subsequent command.
51 The SEARCH result reference extension defines a new SEARCH result
52 option [IMAPABNF] "SAVE" that tells the server to remember the result
53 of the SEARCH or UID SEARCH command (as well as any command based on
54 SEARCH, e.g., SORT and THREAD [SORT]) and store it in an internal
58Melnikov Standards Track [Page 1]
60RFC 5182 Last SEARCH Result Reference March 2008
63 variable that we will reference as the "search result variable". The
64 client can use the "$" marker to reference the content of this
65 internal variable. The "$" marker can be used instead of message
66 sequence or UID sequence in order to indicate that the server should
67 substitute it with the list of messages from the search result
68 variable. Thus, the client can use the result of the latest
69 remembered SEARCH command as a parameter to another command. The
70 search result marker has several advantages:
72 * it avoids wasted bandwidth and associated delay;
74 * it allows the client to pipeline a SEARCH [IMAP4] command with a
75 subsequent FETCH/STORE/COPY/SEARCH [IMAP4] or UID EXPUNGE
78 * the client doesn't need to spend time reformatting the result of
79 a SEARCH command into a message set used in the subsequent
82 * it allows the server to perform optimizations. For example, if
83 the server can execute several pipelined commands in parallel
84 (or out of order), presence of the search result marker can
85 allow the server to decide which commands may or may not be
86 executed out of order.
88 In absence of any other SEARCH result option, the SAVE result option
89 also suppresses any SEARCH response that would have been otherwise
90 returned by the SEARCH command.
921.1. Conventions Used in This Document
94 In examples, "C:" indicates lines sent by a client that is connected
95 to a server. "S:" indicates lines sent by the server to the client.
97 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
98 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
99 document are to be interpreted as described in [KEYWORDS].
101 Explanatory comments in examples start with // and are not part of
114Melnikov Standards Track [Page 2]
116RFC 5182 Last SEARCH Result Reference March 2008
1212.1. Normative Description of the SEARCHRES Extension
123 The SEARCH result reference extension described in this document is
124 present in any IMAP4 server implementation that returns "SEARCHRES"
125 as one of the supported capabilities in the CAPABILITY command
126 response. Any such server MUST also implement the [ESEARCH]
129 Upon successful completion of a SELECT or an EXAMINE command (after
130 the tagged OK response), the current search result variable is reset
131 to the empty sequence.
133 A successful SEARCH command with the SAVE result option sets the
134 value of the search result variable to the list of messages found in
135 the SEARCH command. For example, if no messages were found, the
136 search result variable will contain the empty list.
138 Any of the following SEARCH commands MUST NOT change the search
141 o a SEARCH command that caused the server to return the BAD tagged
144 o a SEARCH command with no SAVE result option that caused the
145 server to return NO tagged response,
147 o a successful SEARCH command with no SAVE result option.
149 A SEARCH command with the SAVE result option that caused the server
150 to return the NO tagged response sets the value of the search result
151 variable to the empty sequence.
153 When a message listed in the search result variable is EXPUNGEd, it
154 is automatically removed from the list. Implementors are reminded
155 that if the server stores the list as a list of message numbers, it
156 MUST automatically adjust them when notifying the client about
157 expunged messages, as described in Section 7.4.1 of [IMAP4].
159 If the server decides to send a new UIDVALIDITY value while the
160 mailbox is opened, this causes resetting of the search variable to
170Melnikov Standards Track [Page 3]
172RFC 5182 Last SEARCH Result Reference March 2008
175 Note that even if the "$" marker contains the empty list of messages,
176 it must be treated by all commands accepting message sets as
177 parameters as a valid, but non-matching list of messages. For
178 example, the "FETCH $" command would return a tagged OK response and
179 no FETCH responses. See also the Example 5 below.
181 Note that even if the "$" marker contains the empty list of messages,
182 it must be treated as a valid but non-matching list of messages, by
183 all commands that accept message sets as parameters.
185 Implementation note: server implementors should note that "$" can
186 reference IMAP message sequences or UID sequences, depending on the
187 context where it is used. For example, the "$" marker can be set as
188 a result of a SEARCH (SAVE) command and used as a parameter to a UID
189 FETCH command (which accepts a UID sequence, not a message sequence),
190 or the "$" marker can be set as a result of a UID SEARCH (SAVE)
191 command and used as a parameter to a FETCH command (which accepts a
192 message sequence, not a UID sequence).
196 1) The following example demonstrates how the client can use the
197 result of a SEARCH command to FETCH headers of interesting
201 C: A282 SEARCH RETURN (SAVE) FLAGGED SINCE 1-Feb-1994
203 S: A282 OK SEARCH completed, result saved
204 C: A283 FETCH $ (UID INTERNALDATE FLAGS RFC822.HEADER)
205 S: * 2 FETCH (UID 14 ...
206 S: * 84 FETCH (UID 100 ...
207 S: * 882 FETCH (UID 1115 ...
210 The client can also pipeline the two commands:
213 C: A282 SEARCH RETURN (SAVE) FLAGGED SINCE 1-Feb-1994
215 C: A283 FETCH $ (UID INTERNALDATE FLAGS RFC822.HEADER)
216 S: A282 OK SEARCH completed
217 S: * 2 FETCH (UID 14 ...
218 S: * 84 FETCH (UID 100 ...
219 S: * 882 FETCH (UID 1115 ...
226Melnikov Standards Track [Page 4]
228RFC 5182 Last SEARCH Result Reference March 2008
231 2) The following example demonstrates that the result of one SEARCH
232 command can be used as input to another SEARCH command:
235 C: A300 SEARCH RETURN (SAVE) SINCE 1-Jan-2004
237 S: A300 OK SEARCH completed
238 C: A301 UID SEARCH UID $ SMALLER 4096
239 S: * SEARCH 17 900 901
242 Note that the second command in Example 3 can be replaced with:
243 C: A301 UID SEARCH $ SMALLER 4096
244 and the result of the command would be the same.
246 3) The following example shows that the "$"
247 marker can be combined with other message numbers using the OR
251 C: P282 SEARCH RETURN (SAVE) SINCE 1-Feb-1994
253 S: P282 OK SEARCH completed
254 C: P283 SEARCH CHARSET UTF-8 (OR $ 1,3000:3021) TEXT {8}
256 S: * SEARCH 882 1102 3003 3005 3006
259 Note: Since this document format is restricted to 7-bit ASCII text,
260 it is not possible to show actual UTF-8 data. The "YYYYYYYY" is a
261 placeholder for what would be 8 octets of 8-bit data in an actual
282Melnikov Standards Track [Page 5]
284RFC 5182 Last SEARCH Result Reference March 2008
287 4) The following example demonstrates that a failed SEARCH sets the
288 search result variable to the empty list.
291 C: B282 SEARCH RETURN (SAVE) SINCE 1-Feb-1994
293 S: B282 OK SEARCH completed
294 C: B283 SEARCH CHARSET KOI8-R (OR $ 1,3000:3021) TEXT {4}
296 S: B283 NO [BADCHARSET UTF-8] KOI8-R is not supported
297 //After this command the saved result variable contains
298 //no messages. A client that wants to reissue the B283
299 //SEARCH command with another CHARSET would have to reissue
300 //the B282 command as well. One possible workaround for
301 //this is to include the desired CHARSET parameter
302 //in the earliest SEARCH RETURN (SAVE) command in a
303 //sequence of related SEARCH commands.
304 //A better approach might be to always use CHARSET UTF-8
307 Note: Since this document format is restricted to 7-bit ASCII text,
308 it is not possible to show actual KOI8-R data. The "XXXX" is a
309 placeholder for what would be 4 octets of 8-bit data in an actual
312 5) The following example demonstrates that it is not an error to use
313 the "$" marker when it contains no messages.
316 C: E282 SEARCH RETURN (SAVE) SINCE 28-Oct-2006
318 C: E283 COPY $ "Other Messages"
319 //The "$" contains no messages
320 S: E282 OK SEARCH completed
321 S: E283 OK COPY completed, nothing copied
3232.3. Multiple Commands in Progress
325 Use of a SEARCH RETURN (SAVE) command followed by a command using the
326 "$" marker creates direct dependency between the two commands. As
327 directed by Section 5.5 of [IMAP4], a server MUST execute the two
328 commands in the order they were received. (A server capable of
329 out-of-order execution can in some cases execute the two commands in
330 parallel, for example, if a SEARCH RETURN (SAVE) is followed by
331 "SEARCH $", the search criteria from the first command can be
332 directly substituted into the second command.)
338Melnikov Standards Track [Page 6]
340RFC 5182 Last SEARCH Result Reference March 2008
343 A client supporting this extension MAY pipeline a SEARCH RETURN
344 (SAVE) command with one or more command using the "$" marker, as long
345 as this doesn't create an ambiguity, as described in Section 5.5 of
349 C: F282 SEARCH RETURN (SAVE) KEYWORD $Junk
350 C: F283 COPY $ "Junk"
351 C: F284 STORE $ +FLAGS.Silent (\Deleted)
352 S: F282 OK SEARCH completed
353 S: F283 OK COPY completed
354 S: F284 OK STORE completed
357 C: G282 SEARCH RETURN (SAVE) KEYWORD $Junk
358 C: G283 SEARCH RETURN (ALL) SINCE 28-Oct-2006
360 //The server can execute the two SEARCH commands
361 //in any order, as they don't have any dependency.
362 //Note that the second command is making use of
363 //the [ESEARCH] extension.
364 S: * ESEARCH (TAG "G283") ALL 3:15,27,29:103
365 S: G283 OK SEARCH completed
366 S: G282 OK SEARCH completed
368 The following example demonstrates that the result of the second
369 SEARCH always overrides the result of the first.
372 C: H282 SEARCH RETURN (SAVE) KEYWORD $Junk
373 C: H283 SEARCH RETURN (SAVE) SINCE 28-Oct-2006
375 S: H282 OK SEARCH completed
376 S: H283 OK SEARCH completed
3782.4. Interaction with ESEARCH Extension
380 Servers that implement the extension defined in this document MUST
381 implement [ESEARCH] and conform to additional requirements listed in
384 The SAVE result option doesn't change whether the server would return
385 items corresponding to MIN, MAX, ALL, or COUNT [ESEARCH] result
394Melnikov Standards Track [Page 7]
396RFC 5182 Last SEARCH Result Reference March 2008
399 When the SAVE result option is combined with the MIN or MAX [ESEARCH]
400 result option, and none of the other ESEARCH result options are
401 present, the corresponding MIN/MAX is returned (if the search result
402 is not empty), but the "$" marker would contain a single message as
403 returned in the MIN/MAX return item.
405 If the SAVE result option is combined with both MIN and MAX result
406 options, and none of the other ESEARCH result options are present,
407 the "$" marker would contain one or two messages as returned in the
408 MIN/MAX return items.
410 If the SAVE result option is combined with the ALL and/or COUNT
411 result option(s), the "$" marker would always contain all messages
412 found by the SEARCH or UID SEARCH command. (Note that the last rule
413 might affect ESEARCH implementations that optimize how the COUNT
414 result is constructed.)
416 The following table summarizes the additional requirement on ESEARCH
417 server implementations described in this section.
419 +----------------+-------------------+
420 | Combination of | "$" marker value |
422 +----------------+-------------------+
424 +----------------+-------------------+
426 +----------------+-------------------+
427 | SAVE MIN MAX | MIN & MAX |
428 +----------------+-------------------+
429 | SAVE * [m] | all found messages|
430 +----------------+-------------------+
432 where '*' means "ALL" and/or "COUNT"
433 '[m]' means optional "MIN" and/or "MAX"
450Melnikov Standards Track [Page 8]
452RFC 5182 Last SEARCH Result Reference March 2008
455 The following example demonstrates behavioral difference for
456 different combinations of ESEARCH result options. Explanatory
457 comments start with // and are not part of the protocol:
460 C: C282 SEARCH RETURN (ALL) SINCE 12-Feb-2006
462 S: * ESEARCH (TAG "C283") ALL 2,10:15,21
463 //$ value hasn't changed
464 S: C282 OK SEARCH completed
466 C: C283 SEARCH RETURN (ALL SAVE) SINCE 12-Feb-2006
468 S: * ESEARCH (TAG "C283") ALL 2,10:15,21
469 //$ value is 2,10:15,21
470 S: C283 OK SEARCH completed
472 C: C284 SEARCH RETURN (SAVE MIN) SINCE 12-Feb-2006
474 S: * ESEARCH (TAG "C284") MIN 2
476 S: C284 OK SEARCH completed
478 C: C285 SEARCH RETURN (MAX SAVE MIN) SINCE
479 12-Feb-2006 NOT FROM "Smith"
480 S: * ESEARCH (TAG "C285") MIN 2 MAX 21
482 S: C285 OK SEARCH completed
484 C: C286 SEARCH RETURN (MAX SAVE MIN COUNT)
485 SINCE 12-Feb-2006 NOT FROM "Smith"
486 S: * ESEARCH (TAG "C286") MIN 2 MAX 21 COUNT 8
487 //$ value is 2,10:15,21
488 S: C286 OK SEARCH completed
490 C: C286 SEARCH RETURN (ALL SAVE MIN) SINCE
491 12-Feb-2006 NOT FROM "Smith"
492 S: * ESEARCH (TAG "C286") MIN 2 ALL 2,10:15,21
493 //$ value is 2,10:15,21
494 S: C286 OK SEARCH completed
506Melnikov Standards Track [Page 9]
508RFC 5182 Last SEARCH Result Reference March 2008
5112.5. Refusing to Save Search Results
513 In some cases, the server MAY refuse to save a SEARCH (SAVE) result,
514 for example, if an internal limit on the number of saved results is
517 In this case, the server MUST return a tagged NO response containing
518 the NOTSAVED response code and set the search result variable to the
519 empty sequence, as described in Section 2.1.
523 The following syntax specification uses the Augmented Backus-Naur
524 Form (ABNF) notation as specified in [ABNF]. Non-terminals
525 referenced but not defined below are as defined in [IMAP4] or
528 Except as noted otherwise, all alphabetic characters are
529 case-insensitive. The use of upper- or lower-case characters to
530 define token strings is for editorial clarity only. Implementations
531 MUST accept these strings in a case-insensitive fashion.
533 capability =/ "SEARCHRES"
534 ;; capability is defined in [IMAP4]
536 sequence-set =/ seq-last-command
537 ;; extends sequence-set to allow for
538 ;; "result of the last command" indicator.
540 seq-last-command = "$"
542 search-return-opt = "SAVE"
543 ;; conforms to generic search-return-opt
544 ;; syntax defined in [IMAPABNF]
546 resp-text-code =/ "NOTSAVED"
547 ;; <resp-text-code> from [IMAP4]
562Melnikov Standards Track [Page 10]
564RFC 5182 Last SEARCH Result Reference March 2008
5674. Security Considerations
569 This extension requires the server to keep additional state, that may
570 be used to simplify Denial of Service attacks. In order to minimize
571 damage from such attacks, server implementations MAY limit the number
572 of saved searches they allow across all connections at any given time
573 and return the tagged NO response containing the NOTSAVED response
574 code (see Section 2.5) to a SEARCH RETURN (SAVE) command when this
577 Apart from that, it is believed that this extension doesn't raise any
578 additional security concerns not already discussed in [IMAP4].
5805. IANA Considerations
582 This document defines the "SEARCHRES" IMAP capability. IANA has
583 added it to the IMAP4 Capabilities Registry, which is currently
586 http://www.iana.org/assignments/imap4-capabilities
590 The author would like to thank Mark Crispin, Cyrus Daboo, and Curtis
591 King for remembering that this document had to be written, as well as
592 for comments and corrections received.
594 The author would also like to thank Dave Cridland, Mark Crispin,
595 Chris Newman, Dan Karp, and Spencer Dawkins for comments and
596 corrections received.
598 Valuable comments, both in agreement and in dissent, were received
599 from Arnt Gulbrandsen.
618Melnikov Standards Track [Page 11]
620RFC 5182 Last SEARCH Result Reference March 2008
6257.1. Normative References
627 [KEYWORDS] Bradner, S., "Key words for use in RFCs to Indicate
628 Requirement Levels", BCP 14, RFC 2119, March 1997.
630 [ABNF] Crocker, D., Ed., and P. Overell, "Augmented BNF for
631 Syntax Specifications: ABNF", STD 68, RFC 5234, January
634 [IMAP4] Crispin, M., "INTERNET MESSAGE ACCESS PROTOCOL - VERSION
635 4rev1", RFC 3501, March 2003.
637 [IMAPABNF] Melnikov, A. and C. Daboo, "Collected Extensions to IMAP4
638 ABNF", RFC 4466, April 2006.
640 [ESEARCH] Melnikov, A. and D. Cridland, "IMAP4 Extension to SEARCH
641 Command for Controlling What Kind of Information Is
642 Returned", RFC 4731, November 2006.
6447.2. Informative References
646 [UIDPLUS] Crispin, M., "Internet Message Access Protocol (IMAP) -
647 UIDPLUS extension", RFC 4315, December 2005.
649 [SORT] Crispin, M. and K. Murchison, "INTERNET MESSAGE ACCESS
650 PROTOCOL - SORT AND THREAD EXTENSIONS", Work in Progress,
657 5 Castle Business Village,
660 TW12 2BX, United Kingdom
662 EMail: Alexey.Melnikov@isode.com
674Melnikov Standards Track [Page 12]
676RFC 5182 Last SEARCH Result Reference March 2008
679Full Copyright Statement
681 Copyright (C) The IETF Trust (2008).
683 This document is subject to the rights, licenses and restrictions
684 contained in BCP 78, and except as set forth therein, the authors
685 retain all their rights.
687 This document and the information contained herein are provided on an
688 "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS
689 OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY, THE IETF TRUST AND
690 THE INTERNET ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS
691 OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF
692 THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
693 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
697 The IETF takes no position regarding the validity or scope of any
698 Intellectual Property Rights or other rights that might be claimed to
699 pertain to the implementation or use of the technology described in
700 this document or the extent to which any license under such rights
701 might or might not be available; nor does it represent that it has
702 made any independent effort to identify any such rights. Information
703 on the procedures with respect to rights in RFC documents can be
704 found in BCP 78 and BCP 79.
706 Copies of IPR disclosures made to the IETF Secretariat and any
707 assurances of licenses to be made available, or the result of an
708 attempt made to obtain a general license or permission for the use of
709 such proprietary rights by implementers or users of this
710 specification can be obtained from the IETF on-line IPR repository at
711 http://www.ietf.org/ipr.
713 The IETF invites any interested party to bring to its attention any
714 copyrights, patents or patent applications, or other proprietary
715 rights that may cover technology that may be required to implement
716 this standard. Please address the information to the IETF at
730Melnikov Standards Track [Page 13]