Skip to content

Commit 2399044

Browse files
authored
🔀 Merge pull request #363 from ruby/search-charset-conflict-raise-argument_error
🥅 Raise ArgumentError on multiple search charset args
2 parents cd3ef80 + 8285049 commit 2399044

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

‎lib/net/imap.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,9 +3150,18 @@ def enforce_logindisabled?
31503150
end
31513151
end
31523152

3153-
def search_internal(cmd, keys, charset = nil)
3154-
keys = normalize_searching_criteria(keys)
3155-
args = charset ? ["CHARSET", charset, *keys] : keys
3153+
def search_args(keys, charset = nil)
3154+
# NOTE: not handling combined RETURN and CHARSET for raw strings
3155+
if charset && keys in /\ACHARSET\b/i | Array[/\ACHARSET\z/i, *]
3156+
raise ArgumentError, "multiple charset arguments"
3157+
end
3158+
args = normalize_searching_criteria(keys)
3159+
args.prepend("CHARSET", charset) if charset
3160+
args
3161+
end
3162+
3163+
def search_internal(cmd, ...)
3164+
args = search_args(...)
31563165
synchronize do
31573166
send_command(cmd, *args)
31583167
search_result = clear_responses("SEARCH").last
@@ -3223,7 +3232,7 @@ def thread_internal(cmd, algorithm, search_keys, charset)
32233232
end
32243233

32253234
def normalize_searching_criteria(criteria)
3226-
return RawData.new(criteria) if criteria.is_a?(String)
3235+
return [RawData.new(criteria)] if criteria.is_a?(String)
32273236
criteria.map {|i|
32283237
if coerce_search_arg_to_seqset?(i)
32293238
SequenceSet[i]

‎test/net/imap/test_imap.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,12 @@ def test_unselect
12291229
imap.search(["subject", "hello", Set[1, 2, 3, 4, 5, 8, *(10..100)]])
12301230
assert_equal "subject hello 1:5,8,10:100", server.commands.pop.args
12311231

1232+
imap.search('SUBJECT "Hello world"', "UTF-8")
1233+
assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args
1234+
1235+
imap.search('CHARSET UTF-8 SUBJECT "Hello world"')
1236+
assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args
1237+
12321238
imap.search([:*])
12331239
assert_equal "*", server.commands.pop.args
12341240

@@ -1259,6 +1265,24 @@ def seqset_coercible.to_sequence_set
12591265
end
12601266
end
12611267

1268+
test("#search/#uid_search with invalid arguments") do
1269+
with_fake_server do |server, imap|
1270+
server.on "SEARCH" do |cmd| cmd.fail_no "should fail before this" end
1271+
server.on "UID SEARCH" do |cmd| cmd.fail_no "should fail before this" end
1272+
1273+
assert_raise(ArgumentError) do
1274+
imap.search(["charset", "foo", "ALL"], "bar")
1275+
end
1276+
assert_raise(ArgumentError) do
1277+
imap.search("charset foo ALL", "bar")
1278+
end
1279+
# Parsing return opts is too complicated, for now.
1280+
# assert_raise(ArgumentError) do
1281+
# imap.search("return () charset foo ALL", "bar")
1282+
# end
1283+
end
1284+
end
1285+
12621286
test("missing server SEARCH response") do
12631287
with_fake_server do |server, imap|
12641288
server.on "SEARCH", &:done_ok

0 commit comments

Comments
 (0)