Skip to content

Commit 14205d5

Browse files
authored
🔀 Merge pull request #347 from ruby/search-docs
📚 Update `#search` documentation
2 parents d4968b8 + a366a0d commit 14205d5

File tree

1 file changed

+213
-35
lines changed

1 file changed

+213
-35
lines changed

lib/net/imap.rb

Lines changed: 213 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,70 +1930,248 @@ def uid_expunge(uid_set)
19301930
end
19311931

19321932
# Sends a {SEARCH command [IMAP4rev1 §6.4.4]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4]
1933-
# to search the mailbox for messages that match the given searching
1934-
# criteria, and returns message sequence numbers. +keys+ can either be a
1935-
# string holding the entire search string, or a single-dimension array of
1936-
# search keywords and arguments.
1937-
#
1938-
# Returns a SearchResult object. SearchResult inherits from Array (for
1933+
# to search the mailbox for messages that match the given search +criteria+,
1934+
# and returns a SearchResult. SearchResult inherits from Array (for
19391935
# backward compatibility) but adds SearchResult#modseq when the +CONDSTORE+
19401936
# capability has been enabled.
19411937
#
1938+
# +criteria+ is one or more search keys and their arguments, which may be
1939+
# provided as an array or a string.
1940+
# See {"Search criteria"}[rdoc-ref:#search@Search+criteria], below.
1941+
#
1942+
# * When +criteria+ is an array, each member is a +SEARCH+ command argument:
1943+
# * Any SequenceSet sends SequenceSet#valid_string.
1944+
# +Range+, <tt>-1</tt>, and nested +Array+ elements are converted to
1945+
# SequenceSet.
1946+
# * Any +String+ is sent verbatim when it is a valid \IMAP atom,
1947+
# and encoded as an \IMAP quoted or literal string otherwise.
1948+
# * Any other +Integer+ (besides <tt>-1</tt>) will be sent as +#to_s+.
1949+
# * +Date+ objects will be encoded as an \IMAP date (see ::encode_date).
1950+
#
1951+
# * When +criteria+ is a string, it will be sent directly to the server
1952+
# <em>without any validation or encoding</em>. *WARNING:* This is
1953+
# vulnerable to injection attacks when external inputs are used.
1954+
#
1955+
# +charset+ is the name of the {registered character
1956+
# set}[https://www.iana.org/assignments/character-sets/character-sets.xhtml]
1957+
# used by strings in the search +criteria+. When +charset+ isn't specified,
1958+
# either <tt>"US-ASCII"</tt> or <tt>"UTF-8"</tt> is assumed, depending on
1959+
# the server's capabilities. +charset+ may be sent inside +criteria+
1960+
# instead of as a separate argument.
1961+
#
19421962
# Related: #uid_search
19431963
#
1944-
# ===== Search criteria
1964+
# ===== For example:
19451965
#
1946-
# For a full list of search criteria,
1966+
# p imap.search(["SUBJECT", "hello", "NOT", "SEEN"])
1967+
# #=> [1, 6, 7, 8]
1968+
#
1969+
# The following searches send the exact same command to the server:
1970+
#
1971+
# # criteria array, charset arg
1972+
# imap.search(%w[OR UNSEEN FLAGGED SUBJECT foo], "UTF-8")
1973+
# # criteria string, charset arg
1974+
# imap.search("OR UNSEEN FLAGGED SUBJECT foo", "UTF-8")
1975+
# # criteria array contains charset arg
1976+
# imap.search(%w[CHARSET UTF-8 OR UNSEEN FLAGGED SUBJECT foo])
1977+
# # criteria string contains charset arg
1978+
# imap.search("CHARSET UTF-8 OR UNSEEN FLAGGED SUBJECT foo")
1979+
#
1980+
# ===== Search keys
1981+
#
1982+
# For full definitions of the standard search +criteria+,
19471983
# see [{IMAP4rev1 §6.4.4}[https://www.rfc-editor.org/rfc/rfc3501.html#section-6.4.4]],
19481984
# or [{IMAP4rev2 §6.4.4}[https://www.rfc-editor.org/rfc/rfc9051.html#section-6.4.4]],
19491985
# in addition to documentation for
1950-
# any [CAPABILITIES[https://www.iana.org/assignments/imap-capabilities/imap-capabilities.xhtml]]
1951-
# reported by #capabilities which may define additional search filters, e.g:
1986+
# any #capabilities which may define additional search filters, such as
19521987
# +CONDSTORE+, +WITHIN+, +FILTERS+, <tt>SEARCH=FUZZY</tt>, +OBJECTID+, or
1953-
# +SAVEDATE+. The following are some common search criteria:
1988+
# +SAVEDATE+.
1989+
#
1990+
# With the exception of <em>sequence-set</em> and <em>parenthesized
1991+
# list</em>, all search keys are composed of prefix label with zero or more
1992+
# arguments. The number and type of arguments is specific to each search
1993+
# key.
1994+
#
1995+
# +ALL+::
1996+
# Matches every message in the mailbox.
1997+
#
1998+
# (_search-key_ _search-key_...)::
1999+
# Combines one or more _search-key_ arguments to match
2000+
# messages which match all contained search keys. Useful for +OR+, +NOT+,
2001+
# and other search keys with _search-key_ arguments.
2002+
#
2003+
# _Note:_ this search key has no label.
2004+
#
2005+
# +OR+ _search-key_ _search-key_::
2006+
# Matches messages which match either _search-key_ argument.
2007+
#
2008+
# +NOT+ _search-key_::
2009+
# Matches messages which do not match _search-key_.
2010+
#
2011+
# _sequence-set_::
2012+
# Matches messages with message sequence numbers in _sequence-set_.
2013+
#
2014+
# _Note:_ this search key has no label.
2015+
#
2016+
# <em>+UIDONLY+ must *not* be enabled.</em>
2017+
# {[RFC9586]}[https://www.rfc-editor.org/rfc/rfc9586.html]
2018+
#
2019+
# +UID+ _sequence-set_::
2020+
# Matches messages with a UID in _sequence-set_.
2021+
#
2022+
# +ANSWERED+::
2023+
# +UNANSWERED+::
2024+
# Matches messages with or without the <tt>\\Answered</tt> flag.
2025+
# +DELETED+::
2026+
# +UNDELETED+::
2027+
# Matches messages with or without the <tt>\\Deleted</tt> flag.
2028+
# +DRAFT+::
2029+
# +UNDRAFT+::
2030+
# Matches messages with or without the <tt>\\Draft</tt> flag.
2031+
# +FLAGGED+::
2032+
# +UNFLAGGED+::
2033+
# Matches messages with or without the <tt>\\Flagged</tt> flag.
2034+
# +SEEN+::
2035+
# +UNSEEN+::
2036+
# Matches messages with or without the <tt>\\Seen</tt> flag.
2037+
#
2038+
# +KEYWORD+ _keyword_::
2039+
# +UNKEYWORD+ _keyword_::
2040+
# Matches messages with or without the specified _keyword_.
2041+
#
2042+
# +BCC+ _substring_::
2043+
# Matches when _substring_ is in the envelope's BCC field.
2044+
# +CC+ _substring_::
2045+
# Matches when _substring_ is in the envelope's CC field.
2046+
# +FROM+ _substring_::
2047+
# Matches when _substring_ is in the envelope's FROM field.
2048+
# +SUBJECT+ _substring_::
2049+
# Matches when _substring_ is in the envelope's SUBJECT field.
2050+
# +TO+ _substring_::
2051+
# Matches when _substring_ is in the envelope's TO field.
2052+
#
2053+
# +HEADER+ _field_ _substring_::
2054+
# Matches when _substring_ is in the specified header _field_.
2055+
#
2056+
# +BODY+ _string_::
2057+
# Matches when _string_ is in the body of the message.
2058+
# Does not match on header fields.
2059+
#
2060+
# The server _may_ use flexible matching, rather than simple substring
2061+
# matches. For example, this may use stemming or match only full words.
2062+
#
2063+
# +TEXT+ _string_::
2064+
# Matches when _string_ is in the header or body of the message.
2065+
#
2066+
# The server _may_ use flexible matching, rather than simple substring
2067+
# matches. For example, this may use stemming or match only full words.
2068+
#
2069+
# +BEFORE+ _date_::
2070+
# +ON+ _date_::
2071+
# +SINCE+ _date_::
2072+
# Matches when the +INTERNALDATE+ is earlier than, on, or later than
2073+
# _date_.
2074+
#
2075+
# +SENTBEFORE+ _date_::
2076+
# +SENTON+ _date_::
2077+
# +SENTSINCE+ _date_::
2078+
# Matches when the +Date+ header is earlier than, on, or later than _date_.
2079+
#
2080+
# +SMALLER+ _bytes_::
2081+
# +LARGER+ _bytes_::
2082+
# Matches when +RFC822.SIZE+ is smaller/larger than _bytes_.
2083+
#
2084+
# ====== Removed from +IMAP4rev2+
2085+
#
2086+
# The <tt>\\Recent</tt> flag has been removed from +IMAP4rev2+. So these
2087+
# search keys require the +IMAP4rev1+ capability.
2088+
#
2089+
# +RECENT+::
2090+
# +UNRECENT+::
2091+
# Matches messages with or without the <tt>\\Recent</tt> flag.
19542092
#
1955-
# <message set>:: a set of message sequence numbers. "<tt>,</tt>" indicates
1956-
# an interval, "+:+" indicates a range. For instance,
1957-
# "<tt>2,10:12,15</tt>" means "<tt>2,10,11,12,15</tt>".
2093+
# +NEW+::
2094+
# Equivalent to <tt>(RECENT UNSEEN)</tt>.
19582095
#
1959-
# BEFORE <date>:: messages with an internal date strictly before
1960-
# <b><date></b>. The date argument has a format similar
1961-
# to <tt>8-Aug-2002</tt>, and can be formatted using
1962-
# Net::IMAP.format_date.
2096+
# ====== Extension search keys
19632097
#
1964-
# BODY <string>:: messages that contain <string> within their body.
2098+
# The search keys described below are defined by standard \IMAP extensions.
19652099
#
1966-
# CC <string>:: messages containing <string> in their CC field.
2100+
# +OLDER+ _interval_::
2101+
# +YOUNGER+ _interval_::
2102+
# Matches when +INTERNALDATE+ is more/less than _interval_ seconds ago.
19672103
#
1968-
# FROM <string>:: messages that contain <string> in their FROM field.
2104+
# <em>Requires the +WITHIN+ capability</em>.
2105+
# {[RFC5032]}[https://www.rfc-editor.org/rfc/rfc5032.html]
19692106
#
1970-
# NEW:: messages with the \Recent, but not the \Seen, flag set.
2107+
# +ANNOTATION+ _entry_ _attr_ _value_::
2108+
# Matches messages that have annotations with entries matching _entry_,
2109+
# attributes matching _attr_, and _value_ in the attribute's values.
19712110
#
1972-
# NOT <search-key>:: negate the following search key.
2111+
# <em>Requires the +ANNOTATE-EXPERIMENT-1+ capability</em>.
2112+
# {[RFC5257]}[https://www.rfc-editor.org/rfc/rfc5257.html].
19732113
#
1974-
# OR <search-key> <search-key>:: "or" two search keys together.
2114+
# +FILTER+ _filter_::
2115+
# References a _filter_ that is stored on the server and matches all
2116+
# messages which would be matched by that filter's search criteria.
19752117
#
1976-
# ON <date>:: messages with an internal date exactly equal to <date>,
1977-
# which has a format similar to 8-Aug-2002.
2118+
# <em>Requires the +FILTERS+ capability</em>.
2119+
# {[RFC5466]}[https://www.rfc-editor.org/rfc/rfc5466.html#section-3.1]
19782120
#
1979-
# SINCE <date>:: messages with an internal date on or after <date>.
2121+
# +FUZZY+ _search-key_::
2122+
# Uses fuzzy matching for the specified search key.
19802123
#
1981-
# SUBJECT <string>:: messages with <string> in their subject.
2124+
# <em>Requires the <tt>SEARCH=FUZZY</tt> capability.</em>
2125+
# {[RFC6203]}[https://www.rfc-editor.org/rfc/rfc6203.html#section-6].
19822126
#
1983-
# TO <string>:: messages with <string> in their TO field.
2127+
# +MODSEQ+ _modseq_::
2128+
# Matches when +MODSEQ+ is greater than or equal to _modseq_.
19842129
#
1985-
# ===== For example:
2130+
# <em>Requires the +CONDSTORE+ capability</em>.
2131+
# {[RFC7162]}[https://www.rfc-editor.org/rfc/rfc7162.html#section-3.1.5].
19862132
#
1987-
# p imap.search(["SUBJECT", "hello", "NOT", "NEW"])
1988-
# #=> [1, 6, 7, 8]
2133+
# +MODSEQ+ _entry_ _entry-type_ _modseq_::
2134+
# Matches when a specific metadata _entry_ has been updated since
2135+
# _modseq_.
2136+
#
2137+
# For flags, the corresponding _entry_ name is
2138+
# <tt>"/flags/#{flag_name}"</tt>, where _flag_name_ includes the
2139+
# <tt>\\</tt> prefix. _entry-type_ can be one of <tt>"shared"</tt>,
2140+
# <tt>"priv"</tt> (private), or <tt>"all"</tt>.
2141+
#
2142+
# <em>Requires the +CONDSTORE+ capability</em>.
2143+
# {[RFC7162]}[https://www.rfc-editor.org/rfc/rfc7162.html#section-3.1.5].
2144+
#
2145+
# +EMAILID+ _objectid_::
2146+
# +THREADID+ _objectid_::
2147+
# Matches when +EMAILID+/+THREADID+ is equal to _objectid_
2148+
# (substring matches are not supported).
2149+
#
2150+
# <em>Requires the +OBJECTID+ capability</em>.
2151+
# {[RFC8474]}[https://www.rfc-editor.org/rfc/rfc8474.html#section-6]
2152+
#
2153+
# +SAVEDATESUPPORTED+::
2154+
# Matches every message in the mailbox when the mailbox supports the save
2155+
# date attribute. Otherwise, it matches no messages.
2156+
#
2157+
# <em>Requires the +SAVEDATE+ capability</em>.
2158+
# {[RFC8514]}[https://www.rfc-editor.org/rfc/rfc8514.html#section-4.3]
2159+
#
2160+
# +SAVEDBEFORE+ _date_::
2161+
# +SAVEDON+ _date_::
2162+
# +SAVEDSINCE+ _date_::
2163+
# Matches when the save date is earlier than, on, or later than _date_.
2164+
#
2165+
# <em>Requires the +SAVEDATE+ capability.</em>
2166+
# {[RFC8514]}[https://www.rfc-editor.org/rfc/rfc8514.html#section-4.3]
19892167
#
19902168
# ===== Capabilities
19912169
#
1992-
# If [CONDSTORE[https://www.rfc-editor.org/rfc/rfc7162.html]] is supported
2170+
# If CONDSTORE[https://www.rfc-editor.org/rfc/rfc7162.html] is supported
19932171
# and enabled for the selected mailbox, a non-empty SearchResult will
19942172
# include a +MODSEQ+ value.
19952173
# imap.select("mbox", condstore: true)
1996-
# result = imap.search(["SUBJECT", "hi there", "not", "new")
2174+
# result = imap.search(["SUBJECT", "hi there", "not", "new"])
19972175
# #=> Net::IMAP::SearchResult[1, 6, 7, 8, modseq: 5594]
19982176
# result.modseq # => 5594
19992177
def search(keys, charset = nil)
@@ -2008,7 +2186,7 @@ def search(keys, charset = nil)
20082186
# backward compatibility) but adds SearchResult#modseq when the +CONDSTORE+
20092187
# capability has been enabled.
20102188
#
2011-
# See #search for documentation of search criteria.
2189+
# See #search for documentation of parameters.
20122190
def uid_search(keys, charset = nil)
20132191
return search_internal("UID SEARCH", keys, charset)
20142192
end

0 commit comments

Comments
 (0)