| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"maps" |
| 5 |
"net/textproto" |
| 6 |
"time" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/models" |
| 9 |
) |
| 10 |
|
| 11 |
type SearchCriteria struct { |
| 12 |
Match *SearchCriteriaPart |
| 13 |
Exclude *SearchCriteriaPart |
| 14 |
UseExtension bool |
| 15 |
} |
| 16 |
|
| 17 |
type SearchCriteriaPart struct { |
| 18 |
WithFlags models.Flags |
| 19 |
WithoutFlags models.Flags |
| 20 |
From []string |
| 21 |
To []string |
| 22 |
Cc []string |
| 23 |
Headers textproto.MIMEHeader |
| 24 |
StartDate time.Time |
| 25 |
EndDate time.Time |
| 26 |
SearchBody bool |
| 27 |
SearchAll bool |
| 28 |
Terms []string |
| 29 |
Invert bool |
| 30 |
} |
| 31 |
|
| 32 |
func (c *SearchCriteria) PrepareHeader() { |
| 33 |
if c == nil { |
| 34 |
return |
| 35 |
} |
| 36 |
c.Match.PrepareHeader() |
| 37 |
c.Exclude.PrepareHeader() |
| 38 |
} |
| 39 |
|
| 40 |
func (c *SearchCriteriaPart) PrepareHeader() { |
| 41 |
if c == nil { |
| 42 |
return |
| 43 |
} |
| 44 |
if c.Headers == nil { |
| 45 |
c.Headers = make(textproto.MIMEHeader) |
| 46 |
} |
| 47 |
for _, from := range c.From { |
| 48 |
c.Headers.Add("From", from) |
| 49 |
} |
| 50 |
for _, to := range c.To { |
| 51 |
c.Headers.Add("To", to) |
| 52 |
} |
| 53 |
for _, cc := range c.Cc { |
| 54 |
c.Headers.Add("Cc", cc) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func (c *SearchCriteria) Combine(other *SearchCriteriaPart) *SearchCriteria { |
| 59 |
if c == nil { |
| 60 |
c = &SearchCriteria{} |
| 61 |
} |
| 62 |
if other.Invert { |
| 63 |
c.Exclude = c.Exclude.Combine(other) |
| 64 |
} else { |
| 65 |
c.Match = c.Match.Combine(other) |
| 66 |
} |
| 67 |
return c |
| 68 |
} |
| 69 |
|
| 70 |
func (c *SearchCriteriaPart) Combine(other *SearchCriteriaPart) *SearchCriteriaPart { |
| 71 |
if c == nil { |
| 72 |
return other |
| 73 |
} |
| 74 |
headers := make(textproto.MIMEHeader) |
| 75 |
maps.Copy(headers, c.Headers) |
| 76 |
maps.Copy(headers, other.Headers) |
| 77 |
start := c.StartDate |
| 78 |
if !other.StartDate.IsZero() { |
| 79 |
start = other.StartDate |
| 80 |
} |
| 81 |
end := c.EndDate |
| 82 |
if !other.EndDate.IsZero() { |
| 83 |
end = other.EndDate |
| 84 |
} |
| 85 |
from := make([]string, len(c.From)+len(other.From)) |
| 86 |
copy(from[:len(c.From)], c.From) |
| 87 |
copy(from[len(c.From):], other.From) |
| 88 |
to := make([]string, len(c.To)+len(other.To)) |
| 89 |
copy(to[:len(c.To)], c.To) |
| 90 |
copy(to[len(c.To):], other.To) |
| 91 |
cc := make([]string, len(c.Cc)+len(other.Cc)) |
| 92 |
copy(cc[:len(c.Cc)], c.Cc) |
| 93 |
copy(cc[len(c.Cc):], other.Cc) |
| 94 |
return &SearchCriteriaPart{ |
| 95 |
WithFlags: c.WithFlags | other.WithFlags, |
| 96 |
WithoutFlags: c.WithoutFlags | other.WithoutFlags, |
| 97 |
From: from, |
| 98 |
To: to, |
| 99 |
Cc: cc, |
| 100 |
Headers: headers, |
| 101 |
StartDate: start, |
| 102 |
EndDate: end, |
| 103 |
SearchBody: c.SearchBody || other.SearchBody, |
| 104 |
SearchAll: c.SearchAll || other.SearchAll, |
| 105 |
Terms: append(c.Terms, other.Terms...), |
| 106 |
Invert: c.Invert, |
| 107 |
} |
| 108 |
} |