Skip to content

Commit 3d2493d

Browse files
committed
✨ Add changedsince kwarg to #fetch, #uid_fetch
Fixes #132.
1 parent 647a745 commit 3d2493d

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/net/imap.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ module Net
511511
# search criterion, and adds SearchResult#modseq to the search response.
512512
# - Updates #thread and #uid_thread with the +MODSEQ+ search criterion
513513
# <em>(but thread responses are unchanged)</em>.
514+
# - Updates #fetch and #uid_fetch with the +changedsince+ modifier and
515+
# +MODSEQ+ FetchData attribute.
514516
#
515517
# ==== RFC8438: <tt>STATUS=SIZE</tt>
516518
# - Updates #status with the +SIZE+ status attribute.
@@ -1986,6 +1988,9 @@ def uid_search(keys, charset = nil)
19861988
return search_internal("UID SEARCH", keys, charset)
19871989
end
19881990

1991+
# :call-seq:
1992+
# fetch(set, attr, changedsince: nil) -> array of FetchData
1993+
#
19891994
# Sends a {FETCH command [IMAP4rev1 §6.4.5]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.5]
19901995
# to retrieve data associated with a message in the mailbox.
19911996
#
@@ -2001,6 +2006,9 @@ def uid_search(keys, charset = nil)
20012006
# +attr+ is a list of attributes to fetch; see the documentation
20022007
# for FetchData for a list of valid attributes.
20032008
#
2009+
# +changedsince+ is an optional integer mod-sequence. It limits results to
2010+
# messages with a mod-sequence greater than +changedsince+.
2011+
#
20042012
# The return value is an array of FetchData.
20052013
#
20062014
# Related: #uid_search, FetchData
@@ -2022,10 +2030,23 @@ def uid_search(keys, charset = nil)
20222030
# #=> "12-Oct-2000 22:40:59 +0900"
20232031
# p data.attr["UID"]
20242032
# #=> 98
2025-
def fetch(set, attr, mod = nil)
2026-
return fetch_internal("FETCH", set, attr, mod)
2033+
#
2034+
# ===== Capabilities
2035+
#
2036+
# Many extensions define new message +attr+ names. See FetchData for a list
2037+
# of supported extension fields.
2038+
#
2039+
# The server's capabilities must include +CONDSTORE+
2040+
# {[RFC7162]}[https://tools.ietf.org/html/rfc7162] in order to use the
2041+
# +changedsince+ argument. Using +changedsince+ implicitly enables the
2042+
# +CONDSTORE+ extension.
2043+
def fetch(set, attr, mod = nil, changedsince: nil)
2044+
fetch_internal("FETCH", set, attr, mod, changedsince: changedsince)
20272045
end
20282046

2047+
# :call-seq:
2048+
# uid_fetch(set, attr, changedsince: nil) -> array of FetchData
2049+
#
20292050
# Sends a {UID FETCH command [IMAP4rev1 §6.4.8]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8]
20302051
# to retrieve data associated with a message in the mailbox.
20312052
#
@@ -2038,8 +2059,11 @@ def fetch(set, attr, mod = nil)
20382059
# whether a +UID+ was specified as a message data item to the +FETCH+.
20392060
#
20402061
# Related: #fetch, FetchData
2041-
def uid_fetch(set, attr, mod = nil)
2042-
return fetch_internal("UID FETCH", set, attr, mod)
2062+
#
2063+
# ===== Capabilities
2064+
# Same as #fetch.
2065+
def uid_fetch(set, attr, mod = nil, changedsince: nil)
2066+
fetch_internal("UID FETCH", set, attr, mod, changedsince: changedsince)
20432067
end
20442068

20452069
# Sends a {STORE command [IMAP4rev1 §6.4.6]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.6]
@@ -2760,7 +2784,11 @@ def search_internal(cmd, keys, charset)
27602784
end
27612785
end
27622786

2763-
def fetch_internal(cmd, set, attr, mod = nil)
2787+
def fetch_internal(cmd, set, attr, mod = nil, changedsince: nil)
2788+
if changedsince
2789+
mod ||= []
2790+
mod << "CHANGEDSINCE" << Integer(changedsince)
2791+
end
27642792
case attr
27652793
when String then
27662794
attr = RawData.new(attr)

test/net/imap/test_imap.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,24 @@ def test_clear_responses
11511151
end
11521152
end
11531153

1154+
test "#fetch with changedsince" do
1155+
with_fake_server select: "inbox" do |server, imap|
1156+
server.on("FETCH", &:done_ok)
1157+
imap.fetch 1..-1, %w[FLAGS], changedsince: 12345
1158+
assert_equal("RUBY0002 FETCH 1:* (FLAGS) (CHANGEDSINCE 12345)",
1159+
server.commands.pop.raw.strip)
1160+
end
1161+
end
1162+
1163+
test "#uid_fetch with changedsince" do
1164+
with_fake_server select: "inbox" do |server, imap|
1165+
server.on("UID FETCH", &:done_ok)
1166+
imap.uid_fetch 1..-1, %w[FLAGS], changedsince: 12345
1167+
assert_equal("RUBY0002 UID FETCH 1:* (FLAGS) (CHANGEDSINCE 12345)",
1168+
server.commands.pop.raw.strip)
1169+
end
1170+
end
1171+
11541172
def test_close
11551173
with_fake_server(select: "inbox") do |server, imap|
11561174
resp = imap.close

0 commit comments

Comments
 (0)