Skip to content

Commit

Permalink
imapserver: simplify readSearchKeyWithAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 30, 2023
1 parent 56b0ebc commit 7578263
Showing 1 changed file with 12 additions and 40 deletions.
52 changes: 12 additions & 40 deletions imapserver/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,24 @@ func readSearchKeyWithAtom(criteria *imap.SearchCriteria, dec *imapwire.Decoder,
if err != nil {
return err
}
var dateCriteria imap.SearchCriteria
switch key {
case "SINCE":
criteria.Since = intersectSince(criteria.Since, t)
dateCriteria.Since = t
case "BEFORE":
criteria.Before = intersectBefore(criteria.Before, t)
dateCriteria.Before = t
case "ON":
criteria.Since = intersectSince(criteria.Since, t)
criteria.Before = intersectBefore(criteria.Before, t.Add(24*time.Hour))
dateCriteria.Since = t
dateCriteria.Before = t.Add(24 * time.Hour)
case "SENTSINCE":
criteria.SentSince = intersectSince(criteria.SentSince, t)
dateCriteria.SentSince = t
case "SENTBEFORE":
criteria.SentBefore = intersectBefore(criteria.SentBefore, t)
dateCriteria.SentBefore = t
case "SENTON":
criteria.SentSince = intersectSince(criteria.SentSince, t)
criteria.SentBefore = intersectBefore(criteria.SentBefore, t.Add(24*time.Hour))
dateCriteria.SentSince = t
dateCriteria.SentBefore = t.Add(24 * time.Hour)
}
criteria.And(&dateCriteria)
case "BODY":
var body string
if !dec.ExpectSP() || !dec.ExpectAString(&body) {
Expand All @@ -271,13 +273,9 @@ func readSearchKeyWithAtom(criteria *imap.SearchCriteria, dec *imapwire.Decoder,
}
switch key {
case "LARGER":
if criteria.Larger == 0 || n > criteria.Larger {
criteria.Larger = n
}
criteria.And(&imap.SearchCriteria{Larger: n})
case "SMALLER":
if criteria.Smaller == 0 || n < criteria.Smaller {
criteria.Smaller = n
}
criteria.And(&imap.SearchCriteria{Smaller: n})
}
case "NOT":
if !dec.ExpectSP() {
Expand Down Expand Up @@ -316,29 +314,3 @@ func readSearchKeyWithAtom(criteria *imap.SearchCriteria, dec *imapwire.Decoder,
func searchKeyFlag(key string) imap.Flag {
return imap.Flag("\\" + strings.Title(strings.ToLower(key)))
}

func intersectSince(t1, t2 time.Time) time.Time {
switch {
case t1.IsZero():
return t2
case t2.IsZero():
return t1
case t1.After(t2):
return t1
default:
return t2
}
}

func intersectBefore(t1, t2 time.Time) time.Time {
switch {
case t1.IsZero():
return t2
case t2.IsZero():
return t1
case t1.Before(t2):
return t1
default:
return t2
}
}

0 comments on commit 7578263

Please sign in to comment.