From a86ec0a236fd91a95eaec263e5e55e7d4c8e3fe9 Mon Sep 17 00:00:00 2001 From: nick evans Date: Thu, 7 Nov 2024 14:01:47 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20De-dup=20code=20in=20n?= =?UTF-8?q?ormalize=5Fsearching=5Fcriteria?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The exact same `RawData` code was used by `search_internal`, `sort_internal`, and `thread_internal`. This pulls that logic into `normalize_searching_criteria`. --- lib/net/imap.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index c52b2498..5e228174 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3118,11 +3118,7 @@ def enforce_logindisabled? end def search_internal(cmd, keys, charset) - if keys.instance_of?(String) - keys = [RawData.new(keys)] - else - normalize_searching_criteria(keys) - end + keys = normalize_searching_criteria(keys) synchronize do if charset send_command(cmd, "CHARSET", charset, *keys) @@ -3175,11 +3171,7 @@ def copy_internal(cmd, set, mailbox) end def sort_internal(cmd, sort_keys, search_keys, charset) - if search_keys.instance_of?(String) - search_keys = [RawData.new(search_keys)] - else - normalize_searching_criteria(search_keys) - end + search_keys = normalize_searching_criteria(search_keys) synchronize do send_command(cmd, sort_keys, charset, *search_keys) clear_responses("SORT").last || [] @@ -3187,19 +3179,16 @@ def sort_internal(cmd, sort_keys, search_keys, charset) end def thread_internal(cmd, algorithm, search_keys, charset) - if search_keys.instance_of?(String) - search_keys = [RawData.new(search_keys)] - else - normalize_searching_criteria(search_keys) - end + search_keys = normalize_searching_criteria(search_keys) synchronize do send_command(cmd, algorithm, charset, *search_keys) clear_responses("THREAD").last || [] end end - def normalize_searching_criteria(keys) - keys.collect! do |i| + def normalize_searching_criteria(criteria) + return RawData.new(criteria) if criteria.is_a?(String) + criteria.map do |i| case i when -1, Range, Array SequenceSet.new(i) From a90782dcc4a023fa42456aab4ea1695ce42f8db6 Mon Sep 17 00:00:00 2001 From: nick evans Date: Thu, 7 Nov 2024 14:03:24 -0500 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplify=20`CHARSET`?= =?UTF-8?q?=20args=20in=20`search=5Finternal`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 5e228174..fe6fc448 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3119,12 +3119,9 @@ def enforce_logindisabled? def search_internal(cmd, keys, charset) keys = normalize_searching_criteria(keys) + args = charset ? ["CHARSET", charset, *keys] : keys synchronize do - if charset - send_command(cmd, "CHARSET", charset, *keys) - else - send_command(cmd, *keys) - end + send_command(cmd, *args) clear_responses("SEARCH").last || [] end end From 8323ce312f47a63ea248fe184ec3f12f10b223f9 Mon Sep 17 00:00:00 2001 From: nicholas evans Date: Mon, 1 Jan 2024 21:49:10 -0500 Subject: [PATCH 3/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Forward=20all=20search?= =?UTF-8?q?/uid=5Fsearch=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forward all `#search` and `#uid_search` arguments to `#search_internal`. This simplifies future changes to search parameters. --- lib/net/imap.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index fe6fc448..b9e39762 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -1929,6 +1929,9 @@ def uid_expunge(uid_set) end end + # :call-seq: + # search(criteria, charset = nil) -> result + # # Sends a {SEARCH command [IMAP4rev1 §6.4.4]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4] # to search the mailbox for messages that match the given search +criteria+, # and returns a SearchResult. SearchResult inherits from Array (for @@ -2174,10 +2177,13 @@ def uid_expunge(uid_set) # result = imap.search(["SUBJECT", "hi there", "not", "new"]) # #=> Net::IMAP::SearchResult[1, 6, 7, 8, modseq: 5594] # result.modseq # => 5594 - def search(keys, charset = nil) - return search_internal("SEARCH", keys, charset) + def search(...) + search_internal("SEARCH", ...) end + # :call-seq: + # uid_search(criteria, charset = nil) -> result + # # Sends a {UID SEARCH command [IMAP4rev1 §6.4.8]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8] # to search the mailbox for messages that match the given searching # criteria, and returns unique identifiers (UIDs). @@ -2187,8 +2193,8 @@ def search(keys, charset = nil) # capability has been enabled. # # See #search for documentation of parameters. - def uid_search(keys, charset = nil) - return search_internal("UID SEARCH", keys, charset) + def uid_search(...) + search_internal("UID SEARCH", ...) end # :call-seq: @@ -3117,7 +3123,7 @@ def enforce_logindisabled? end end - def search_internal(cmd, keys, charset) + def search_internal(cmd, keys, charset = nil) keys = normalize_searching_criteria(keys) args = charset ? ["CHARSET", charset, *keys] : keys synchronize do