| 1 |
//go:build notmuch |
| 2 |
// +build notmuch |
| 3 |
|
| 4 |
package notmuch |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/models" |
| 11 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 12 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 13 |
) |
| 14 |
|
| 15 |
type queryBuilder struct { |
| 16 |
s string |
| 17 |
} |
| 18 |
|
| 19 |
func (q *queryBuilder) and(s string) { |
| 20 |
if len(s) == 0 { |
| 21 |
return |
| 22 |
} |
| 23 |
if len(q.s) != 0 { |
| 24 |
q.s += " and " |
| 25 |
} |
| 26 |
q.s += "(" + s + ")" |
| 27 |
} |
| 28 |
|
| 29 |
func (q *queryBuilder) or(s string) { |
| 30 |
if len(s) == 0 { |
| 31 |
return |
| 32 |
} |
| 33 |
if len(q.s) != 0 { |
| 34 |
q.s += " or " |
| 35 |
} |
| 36 |
q.s += "(" + s + ")" |
| 37 |
} |
| 38 |
|
| 39 |
func invert(s string, invert bool) string { |
| 40 |
if len(s) == 0 || !invert { |
| 41 |
return s |
| 42 |
} |
| 43 |
return "not (" + s + ")" |
| 44 |
} |
| 45 |
|
| 46 |
func translate(criteria *types.SearchCriteria) string { |
| 47 |
if criteria == nil { |
| 48 |
return "" |
| 49 |
} |
| 50 |
var base queryBuilder |
| 51 |
translatePart(&base, criteria.Match) |
| 52 |
translatePart(&base, criteria.Exclude) |
| 53 |
return base.s |
| 54 |
} |
| 55 |
|
| 56 |
func translatePart(base *queryBuilder, crit *types.SearchCriteriaPart) { |
| 57 |
if crit == nil { |
| 58 |
return |
| 59 |
} |
| 60 |
|
| 61 |
// recipients |
| 62 |
var from queryBuilder |
| 63 |
for _, f := range crit.From { |
| 64 |
from.or("from:" + opt.QuoteArg(f)) |
| 65 |
} |
| 66 |
if from.s != "" { |
| 67 |
base.and(invert(from.s, crit.Invert)) |
| 68 |
} |
| 69 |
|
| 70 |
var to queryBuilder |
| 71 |
for _, t := range crit.To { |
| 72 |
to.or("to:" + opt.QuoteArg(t)) |
| 73 |
} |
| 74 |
if to.s != "" { |
| 75 |
base.and(invert(to.s, crit.Invert)) |
| 76 |
} |
| 77 |
|
| 78 |
var cc queryBuilder |
| 79 |
for _, c := range crit.Cc { |
| 80 |
cc.or("cc:" + opt.QuoteArg(c)) |
| 81 |
} |
| 82 |
if cc.s != "" { |
| 83 |
base.and(invert(cc.s, crit.Invert)) |
| 84 |
} |
| 85 |
|
| 86 |
// flags |
| 87 |
for f := range flagToTag { |
| 88 |
if crit.WithFlags.Has(f) { |
| 89 |
base.and(invert(getParsedFlag(f, false), crit.Invert)) |
| 90 |
} |
| 91 |
if crit.WithoutFlags.Has(f) { |
| 92 |
base.and(invert(getParsedFlag(f, true), crit.Invert)) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// dates |
| 97 |
switch { |
| 98 |
case !crit.StartDate.IsZero() && !crit.EndDate.IsZero(): |
| 99 |
base.and(invert(fmt.Sprintf("date:@%d..@%d", |
| 100 |
crit.StartDate.Unix(), crit.EndDate.Unix()), crit.Invert)) |
| 101 |
case !crit.StartDate.IsZero(): |
| 102 |
base.and(invert(fmt.Sprintf("date:@%d..", crit.StartDate.Unix()), crit.Invert)) |
| 103 |
case !crit.EndDate.IsZero(): |
| 104 |
base.and(invert(fmt.Sprintf("date:..@%d", crit.EndDate.Unix()), crit.Invert)) |
| 105 |
} |
| 106 |
|
| 107 |
// other terms |
| 108 |
if len(crit.Terms) > 0 { |
| 109 |
if crit.SearchBody { |
| 110 |
base.and(invert("body:"+opt.QuoteArg(strings.Join(crit.Terms, " ")), crit.Invert)) |
| 111 |
} else { |
| 112 |
for _, term := range crit.Terms { |
| 113 |
base.and(invert(term, crit.Invert)) |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func getParsedFlag(flag models.Flags, inverse bool) string { |
| 120 |
name := "tag:" + flagToTag[flag] |
| 121 |
if flagToInvert[flag] { |
| 122 |
name = "not " + name |
| 123 |
} |
| 124 |
return name |
| 125 |
} |