Repository: emersion/go-imap
Pull request #330 (2020-01-05 17:17:35)

Support ALL criteria in SEARCH
Signed-off-by: Arnold Bechtoldt <arnold.bechtoldt@inovex.de>

```
criteria := imap.NewSearchCriteria()
c.UidSearch(criteria) // returns: []  Error in IMAP command UID SEARCH: Missing search parameters (0.001 + 0.000 secs).
```


Current behaviour:

```
Z12hwA EXAMINE INBOX
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS ()] Read-only mailbox.
* 6 EXISTS
* 6 RECENT
* OK [UNSEEN 1] First unseen.
* OK [UIDVALIDITY 1578248986] UIDs valid
* OK [UIDNEXT 7] Predicted next UID
Z12hwA OK [READ-ONLY] Examine completed (0.001 + 0.000 secs).
hw_TVQ UID SEARCH CHARSET UTF-8
hw_TVQ BAD Error in IMAP command UID SEARCH: Missing search parameters (0.001 + 0.000 secs).
IximMg LOGOUT
* BYE Logging out
IximMg OK Logout completed (0.001 + 0.000 secs).
```

codecov[bot]: # [Codecov](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=h1) Report
> Merging [#330](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=desc) into [master](https://codecov.io/gh/emersion/go-imap/commit/55c8f189db82e9dd4e21136ddc72b77b7be4b420?src=pr&el=desc) will **increase** coverage by `0.07%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/emersion/go-imap/pull/330/graphs/tree.svg?width=650&token=toR3ygEJFD&height=150&src=pr)](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #330      +/-   ##
==========================================
+ Coverage   73.39%   73.46%   +0.07%     
==========================================
  Files          32       32              
  Lines        3514     3516       +2     
==========================================
+ Hits         2579     2583       +4     
+ Misses        642      640       -2     
  Partials      293      293
```


| [Impacted Files](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [search.go](https://codecov.io/gh/emersion/go-imap/pull/330/diff?src=pr&el=tree#diff-c2VhcmNoLmdv) | `62.97% <100%> (+1.17%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=footer). Last update [55c8f18...7777440](https://codecov.io/gh/emersion/go-imap/pull/330?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/client/cmd_selected.go b/client/cmd_selected.go
index 0607247..ed0e189 100644
--- a/client/cmd_selected.go
+++ b/client/cmd_selected.go
@@ -122,7 +122,8 @@ func (c *Client) search(uid bool, criteria *imap.SearchCriteria) (ids []uint32,
 // match the searching criteria. When multiple keys are specified, the result is
 // the intersection (AND function) of all the messages that match those keys.
 // Criteria must be UTF-8 encoded. See RFC 3501 section 6.4.4 for a list of
-// searching criteria.
+// searching criteria. When no criteria has been set, all messages in the mailbox
+// will be searched using ALL criteria.
 func (c *Client) Search(criteria *imap.SearchCriteria) (seqNums []uint32, err error) {
 	return c.search(false, criteria)
 }
diff --git a/search.go b/search.go
index aedc01d..0ecb24d 100644
--- a/search.go
+++ b/search.go
@@ -362,5 +362,10 @@ func (c *SearchCriteria) Format() []interface{} {
 		fields = append(fields, RawString("OR"), or[0].Format(), or[1].Format())
 	}
 
+	// Not a single criteria given, add ALL criteria as fallback
+	if len(fields) == 0 {
+		fields = append(fields, RawString("ALL"))
+	}
+
 	return fields
 }


Test patch:
diff --git a/search_test.go b/search_test.go
index 07205a1..81c3e5e 100644
--- a/search_test.go
+++ b/search_test.go
@@ -65,6 +65,10 @@ var searchCriteriaTests = []struct {
 			}},
 		},
 	},
+	{
+		expected: "(ALL)",
+		criteria: &SearchCriteria{},
+	},
 }
 
 func TestSearchCriteria_Format(t *testing.T) {