Skip to content

Commit f0d7c3d

Browse files
committed
📚 Update #search documentation
* Expanded criteria docs: * explain sequence-set coercion * explain encoding of string, integer, and date args * add warning for raw string data * Expanded charset docs * Link to IANA charset registry * Indicate defaults are "US-ASCII" or "UTF-8" * Demonstrate sending charset embedded inside criteria * Move `#search` example above search criteria. The list of search keys is long. And to be more complete, it will need to get even longer. It's nice to have some examples near the top. * Update `#search` example (`NEW` is deprecated) `NEW` and `RECENT` have been removed from `IMAP4rev2`, so I'm updating the example with a search key that isn't deprecated.
1 parent 5a0e5cb commit f0d7c3d

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

lib/net/imap.rb

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,17 +1930,53 @@ 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
#
1964+
# ===== For example:
1965+
#
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+
#
19441980
# ===== Search criteria
19451981
#
19461982
# For a full list of search criteria,
@@ -1982,18 +2018,13 @@ def uid_expunge(uid_set)
19822018
#
19832019
# TO <string>:: messages with <string> in their TO field.
19842020
#
1985-
# ===== For example:
1986-
#
1987-
# p imap.search(["SUBJECT", "hello", "NOT", "NEW"])
1988-
# #=> [1, 6, 7, 8]
1989-
#
19902021
# ===== Capabilities
19912022
#
1992-
# If [CONDSTORE[https://www.rfc-editor.org/rfc/rfc7162.html]] is supported
2023+
# If CONDSTORE[https://www.rfc-editor.org/rfc/rfc7162.html] is supported
19932024
# and enabled for the selected mailbox, a non-empty SearchResult will
19942025
# include a +MODSEQ+ value.
19952026
# imap.select("mbox", condstore: true)
1996-
# result = imap.search(["SUBJECT", "hi there", "not", "new")
2027+
# result = imap.search(["SUBJECT", "hi there", "not", "new"])
19972028
# #=> Net::IMAP::SearchResult[1, 6, 7, 8, modseq: 5594]
19982029
# result.modseq # => 5594
19992030
def search(keys, charset = nil)
@@ -2008,7 +2039,7 @@ def search(keys, charset = nil)
20082039
# backward compatibility) but adds SearchResult#modseq when the +CONDSTORE+
20092040
# capability has been enabled.
20102041
#
2011-
# See #search for documentation of search criteria.
2042+
# See #search for documentation of parameters.
20122043
def uid_search(keys, charset = nil)
20132044
return search_internal("UID SEARCH", keys, charset)
20142045
end

0 commit comments

Comments
 (0)