mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-22 04:30:43 +00:00
Only include CHARSET in search when there’s text (#723)
This commit is contained in:
1 parent
cab8fef8a7
commit
e8bb770d00
4 files changed
+100
-9
No files matched your search
@@ -585,8 +585,10 @@ extension CommandEncodeBuffer {
|
||||
self.buffer.writeSearchReturnOptions(options)
|
||||
} +
|
||||
self.buffer.writeSpace() +
|
||||
self.buffer.writeIfExists(charset) { (charset) -> Int in
|
||||
self.buffer.writeString("CHARSET \(charset) ")
|
||||
self.buffer.write(if: key.usesString) {
|
||||
self.buffer.writeIfExists(charset) { (charset) -> Int in
|
||||
self.buffer.writeString("CHARSET \(charset) ")
|
||||
}
|
||||
} +
|
||||
self.buffer.writeSearchKey(key)
|
||||
}
|
||||
|
||||
@@ -140,6 +140,64 @@ public indirect enum SearchKey: Hashable {
|
||||
case modificationSequence(SearchModificationSequence)
|
||||
}
|
||||
|
||||
extension SearchKey {
|
||||
/// Are there any (nested) search keys that use strings?
|
||||
///
|
||||
/// We use this to check if we need to specify `CHARSET UTF-8`
|
||||
var usesString: Bool {
|
||||
switch self {
|
||||
case .all,
|
||||
.answered,
|
||||
.deleted,
|
||||
.flagged,
|
||||
.new,
|
||||
.old,
|
||||
.recent,
|
||||
.seen,
|
||||
.unanswered,
|
||||
.undeleted,
|
||||
.unflagged,
|
||||
.unseen,
|
||||
.draft,
|
||||
.undraft,
|
||||
.before,
|
||||
.keyword,
|
||||
.on,
|
||||
.since,
|
||||
.unkeyword,
|
||||
.messageSizeLarger,
|
||||
.sentBefore,
|
||||
.sentOn,
|
||||
.sentSince,
|
||||
.messageSizeSmaller,
|
||||
.uid,
|
||||
.sequenceNumbers,
|
||||
.older,
|
||||
.younger,
|
||||
.modificationSequence,
|
||||
.filter:
|
||||
return false
|
||||
|
||||
case .bcc,
|
||||
.body,
|
||||
.cc,
|
||||
.from,
|
||||
.subject,
|
||||
.text,
|
||||
.to,
|
||||
.header:
|
||||
return true
|
||||
|
||||
case .not(let key):
|
||||
return key.usesString
|
||||
case .or(let keyA, let keyB):
|
||||
return keyA.usesString || keyB.usesString
|
||||
case .and(let keys):
|
||||
return keys.contains(where: \.usesString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SearchKey {
|
||||
fileprivate var count: Int {
|
||||
switch self {
|
||||
@@ -195,10 +253,6 @@ extension SearchKey {
|
||||
// MARK: - IMAP
|
||||
|
||||
extension EncodeBuffer {
|
||||
@discardableResult mutating func writeSearchKeys(_ keys: [SearchKey]) -> Int {
|
||||
writeSearchKey(.and(keys))
|
||||
}
|
||||
|
||||
@discardableResult mutating func writeSearchKey(_ key: SearchKey, encloseInParenthesisIfNeeded: Bool = false) -> Int {
|
||||
let encloseInParenthesis = encloseInParenthesisIfNeeded && key.count > 1
|
||||
if encloseInParenthesis {
|
||||
|
||||
@@ -89,9 +89,9 @@ final class RoundtripTests: XCTestCase {
|
||||
(.uidStore(.set(.all), [], .flags(.add(silent: true, list: [.draft, .deleted, .answered]))), #line),
|
||||
|
||||
(.search(key: .all), #line),
|
||||
(.search(key: .or(.deleted, .unseen), charset: "UTF-7"), #line),
|
||||
(.search(key: .or(.deleted, .unseen), charset: "UTF-7", returnOptions: [.min, .max]), #line),
|
||||
(.search(key: .and([.new, .deleted, .unseen]), charset: "UTF-7", returnOptions: [.min, .max]), #line),
|
||||
(.search(key: .or(.deleted, .from("example")), charset: "UTF-7"), #line),
|
||||
(.search(key: .or(.to("bar"), .unseen), charset: "UTF-8", returnOptions: [.min, .max]), #line),
|
||||
(.search(key: .and([.new, .deleted, .unseen]), charset: nil, returnOptions: [.min, .max]), #line),
|
||||
|
||||
(.extendedSearch(ExtendedSearchOptions(key: .all, sourceOptions: ExtendedSearchSourceOptions(sourceMailbox: [.inboxes]))), #line),
|
||||
]
|
||||
|
||||
@@ -32,6 +32,41 @@ extension CommandEncoder_Tests {
|
||||
(.tagged(.init(tag: "1", command: .noop)), "1 NOOP\r\n", #line),
|
||||
(.append(.start(tag: "2", appendingTo: .inbox)), "2 APPEND \"INBOX\"", #line),
|
||||
(.idleDone, "DONE\r\n", #line),
|
||||
|
||||
// SEARCH
|
||||
// We only want to include "CHARSET" if there’s a string in the search key / query.
|
||||
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .all, charset: "UTF-8"))),
|
||||
#"A1 SEARCH ALL\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .draft, charset: "UTF-8"))),
|
||||
#"A1 SEARCH DRAFT\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .uid(.set([2 ... 80])), charset: "UTF-8"))),
|
||||
#"A1 SEARCH UID 2:80\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .sequenceNumbers(.set([2 ... 80])), charset: "UTF-8"))),
|
||||
#"A1 SEARCH 2:80\#r\#n"#, #line),
|
||||
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .and([.draft, .to("foo")]), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 DRAFT TO "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .or(.draft, .to("foo")), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 OR DRAFT TO "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .not(.to("foo")), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 NOT TO "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .bcc("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 BCC "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .body("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 BODY "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .cc("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 CC "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .from("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 FROM "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .subject("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 SUBJECT "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .text("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 TEXT "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .to("foo"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 TO "foo"\#r\#n"#, #line),
|
||||
(.tagged(.init(tag: "A1", command: .search(key: .header("foo", "bar"), charset: "UTF-8"))),
|
||||
#"A1 SEARCH CHARSET UTF-8 HEADER "foo" "bar"\#r\#n"#, #line),
|
||||
]
|
||||
|
||||
for (command, expected, line) in inputs {
|
||||
|
||||
Reference in new issue
Block a user