| 1 |
package lib |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
"unicode" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/rfc822" |
| 11 |
"git.sr.ht/~rjarry/aerc/models" |
| 12 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 13 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 14 |
) |
| 15 |
|
| 16 |
func Search(messages []rfc822.RawMessage, criteria *types.SearchCriteria) ([]models.UID, error) { |
| 17 |
criteria.PrepareHeader() |
| 18 |
requiredParts := GetRequiredParts(criteria) |
| 19 |
|
| 20 |
var matchedUids []models.UID |
| 21 |
for _, m := range messages { |
| 22 |
success, err := SearchMessage(m, criteria, requiredParts) |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} else if success { |
| 26 |
matchedUids = append(matchedUids, m.UID()) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
return matchedUids, nil |
| 31 |
} |
| 32 |
|
| 33 |
// searchMessage executes the search criteria for the given RawMessage, |
| 34 |
// returns true if search succeeded |
| 35 |
func SearchMessage( |
| 36 |
message rfc822.RawMessage, criteria *types.SearchCriteria, parts MsgParts, |
| 37 |
) (bool, error) { |
| •38 |
if criteria == nil { |
| 39 |
return true, nil |
| 40 |
} |
| 41 |
if criteria.Exclude != nil { |
| 42 |
excluded, err := searchMessage(message, criteria.Exclude, parts) |
| 43 |
if err != nil || excluded { |
| 44 |
return false, err |
| 45 |
} |
| 46 |
} |
| 47 |
return searchMessage(message, criteria.Match, parts) |
| 48 |
} |
| 49 |
|
| 50 |
func searchMessage( |
| 51 |
message rfc822.RawMessage, criteria *types.SearchCriteriaPart, parts MsgParts, |
| 52 |
) (bool, error) { |
| 53 |
if criteria == nil { |
| 54 |
return true, nil |
| 55 |
} |
| 56 |
// setup parts of the message to use in the search |
| 57 |
// this is so that we try to minimise reading unnecessary parts |
| 58 |
var ( |
| 59 |
flags models.Flags |
| 60 |
info *models.MessageInfo |
| 61 |
text string |
| 62 |
err error |
| 63 |
) |
| 64 |
|
| 65 |
if parts&FLAGS > 0 { |
| 66 |
flags, err = message.ModelFlags() |
| 67 |
if err != nil { |
| 68 |
return false, err |
| 69 |
} |
| 70 |
} |
| 71 |
info, err = rfc822.MessageInfo(message) |
| 72 |
if err != nil { |
| 73 |
return false, err |
| 74 |
} |
| 75 |
switch { |
| 76 |
case parts&BODY > 0: |
| 77 |
path := lib.FindFirstNonMultipart(info.BodyStructure, nil) |
| 78 |
reader, err := message.NewReader() |
| 79 |
if err != nil { |
| 80 |
return false, err |
| 81 |
} |
| 82 |
defer reader.Close() |
| 83 |
msg, err := rfc822.ReadMessage(reader) |
| 84 |
if err != nil { |
| 85 |
return false, err |
| 86 |
} |
| 87 |
part, err := rfc822.FetchEntityPartReader(msg, path) |
| 88 |
if err != nil { |
| 89 |
return false, err |
| 90 |
} |
| 91 |
bytes, err := io.ReadAll(part) |
| 92 |
if err != nil { |
| 93 |
return false, err |
| 94 |
} |
| 95 |
text = string(bytes) |
| 96 |
case parts&ALL > 0: |
| 97 |
reader, err := message.NewReader() |
| 98 |
if err != nil { |
| 99 |
return false, err |
| 100 |
} |
| 101 |
defer reader.Close() |
| 102 |
bytes, err := io.ReadAll(reader) |
| 103 |
if err != nil { |
| 104 |
return false, err |
| 105 |
} |
| 106 |
text = string(bytes) |
| 107 |
default: |
| 108 |
text = info.Envelope.Subject |
| 109 |
} |
| 110 |
|
| 111 |
// now search through the criteria |
| 112 |
// implicit AND at the moment so fail fast |
| 113 |
if criteria.Headers != nil { |
| 114 |
for k, v := range criteria.Headers { |
| 115 |
headerValue := info.RFC822Headers.Get(k) |
| 116 |
for _, text := range v { |
| 117 |
if !containsSmartCase(headerValue, text) { |
| 118 |
return false, nil |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
args := opt.LexArgs(strings.Join(criteria.Terms, " ")) |
| 125 |
for _, searchTerm := range args.Args() { |
| 126 |
if !containsSmartCase(text, searchTerm) { |
| 127 |
return false, nil |
| 128 |
} |
| 129 |
} |
| 130 |
if criteria.WithFlags != 0 { |
| 131 |
if !flags.Has(criteria.WithFlags) { |
| 132 |
return false, nil |
| 133 |
} |
| 134 |
} |
| 135 |
if criteria.WithoutFlags != 0 { |
| 136 |
if flags.Has(criteria.WithoutFlags) { |
| 137 |
return false, nil |
| 138 |
} |
| 139 |
} |
| 140 |
if parts&DATE > 0 { |
| 141 |
if date, err := info.RFC822Headers.Date(); err != nil { |
| 142 |
log.Errorf("Failed to get date from header: %v", err) |
| 143 |
} else { |
| 144 |
if !criteria.StartDate.IsZero() { |
| 145 |
if date.Before(criteria.StartDate) { |
| 146 |
return false, nil |
| 147 |
} |
| 148 |
} |
| 149 |
if !criteria.EndDate.IsZero() { |
| 150 |
if date.After(criteria.EndDate) { |
| 151 |
return false, nil |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return true, nil |
| 157 |
} |
| 158 |
|
| 159 |
// containsSmartCase is a smarter version of strings.Contains for searching. |
| 160 |
// Is case-insensitive unless substr contains an upper case character |
| 161 |
func containsSmartCase(s string, substr string) bool { |
| 162 |
if hasUpper(substr) { |
| 163 |
return strings.Contains(s, substr) |
| 164 |
} |
| 165 |
return strings.Contains(strings.ToLower(s), strings.ToLower(substr)) |
| 166 |
} |
| 167 |
|
| 168 |
func hasUpper(s string) bool { |
| 169 |
for _, r := range s { |
| 170 |
if unicode.IsUpper(r) { |
| 171 |
return true |
| 172 |
} |
| 173 |
} |
| 174 |
return false |
| 175 |
} |
| 176 |
|
| 177 |
// The parts of a message, kind of |
| 178 |
type MsgParts int |
| 179 |
|
| 180 |
const NONE MsgParts = 0 |
| 181 |
const ( |
| 182 |
FLAGS MsgParts = 1 << iota |
| 183 |
HEADER |
| 184 |
DATE |
| 185 |
BODY |
| 186 |
ALL |
| 187 |
) |
| 188 |
|
| 189 |
// Returns a bitmask of the parts of the message required to be loaded for the |
| 190 |
// given criteria |
| 191 |
func GetRequiredParts(criteria *types.SearchCriteria) MsgParts { |
| •192 |
if criteria == nil { |
| 193 |
return NONE |
| 194 |
} |
| 195 |
return getRequiredParts(criteria.Match) | getRequiredParts(criteria.Exclude) |
| 196 |
} |
| 197 |
|
| 198 |
func getRequiredParts(criteria *types.SearchCriteriaPart) MsgParts { |
| 199 |
required := NONE |
| 200 |
if criteria == nil { |
| 201 |
return required |
| 202 |
} |
| 203 |
if len(criteria.Headers) > 0 { |
| 204 |
required |= HEADER |
| 205 |
} |
| 206 |
if !criteria.StartDate.IsZero() || !criteria.EndDate.IsZero() { |
| 207 |
required |= DATE |
| 208 |
} |
| 209 |
if criteria.SearchBody { |
| 210 |
required |= BODY |
| 211 |
} |
| 212 |
if criteria.SearchAll { |
| 213 |
required |= ALL |
| 214 |
} |
| 215 |
if criteria.WithFlags != 0 { |
| 216 |
required |= FLAGS |
| 217 |
} |
| 218 |
if criteria.WithoutFlags != 0 { |
| 219 |
required |= FLAGS |
| 220 |
} |
| 221 |
|
| 222 |
return required |
| 223 |
} |