| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
"sync/atomic" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 8 |
"git.sr.ht/~rjarry/aerc/models" |
| 9 |
"github.com/emersion/go-message/mail" |
| 10 |
"github.com/go-ini/ini" |
| 11 |
) |
| 12 |
|
| 13 |
type ViewerConfig struct { |
| 14 |
Pager string `ini:"pager" default:"less -Rc"` |
| 15 |
Alternatives []string `ini:"alternatives" default:"text/plain,text/html" delim:","` |
| 16 |
ShowHeaders bool `ini:"show-headers"` |
| 17 |
AlwaysShowMime bool `ini:"always-show-mime"` |
| 18 |
MaxMimeHeight int `ini:"max-mime-height" default:"0"` |
| 19 |
ParseHttpLinks bool `ini:"parse-http-links" default:"true"` |
| 20 |
HtmlInlineImages bool `ini:"html-inline-images"` |
| 21 |
HeaderLayout [][]string `ini:"header-layout" parse:"ParseLayout" default:"From|To,Cc|Bcc,Date,Subject"` |
| 22 |
KeyPassthrough bool |
| 23 |
|
| 24 |
// private |
| 25 |
contextualViewers []*ViewerConfigContext |
| 26 |
contextualCounts map[viewerContextType]int |
| 27 |
contextualCache map[viewerContextKey]*ViewerConfig |
| 28 |
} |
| 29 |
|
| 30 |
type viewerContextType int |
| 31 |
|
| 32 |
const ( |
| 33 |
viewerContextSender viewerContextType = iota |
| 34 |
viewerContextFrom |
| 35 |
viewerContextSubject |
| 36 |
) |
| 37 |
|
| 38 |
type ViewerConfigContext struct { |
| 39 |
ContextType viewerContextType |
| 40 |
Value string |
| 41 |
Regex *regexp.Regexp |
| 42 |
ViewerConfig *ViewerConfig |
| 43 |
Section ini.Section |
| 44 |
} |
| 45 |
|
| 46 |
type viewerContextKey struct { |
| 47 |
ctxType viewerContextType |
| 48 |
value string |
| 49 |
} |
| 50 |
|
| 51 |
var viewerConfig atomic.Pointer[ViewerConfig] |
| 52 |
|
| 53 |
func Viewer() *ViewerConfig { |
| 54 |
return viewerConfig.Load() |
| 55 |
} |
| 56 |
|
| 57 |
var viewerContextualSectionRe = regexp.MustCompile(`^viewer:(sender|from|subject)([~=])(.+)$`) |
| 58 |
|
| 59 |
func parseViewer(file *ini.File) (*ViewerConfig, error) { |
| 60 |
conf := &ViewerConfig{ |
| 61 |
contextualCounts: make(map[viewerContextType]int), |
| 62 |
contextualCache: make(map[viewerContextKey]*ViewerConfig), |
| 63 |
} |
| 64 |
if err := conf.parse(file.Section("viewer"), true); err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
|
| 68 |
for _, section := range file.Sections() { |
| 69 |
var err error |
| 70 |
groups := viewerContextualSectionRe.FindStringSubmatch(section.Name()) |
| 71 |
if groups == nil { |
| 72 |
continue |
| 73 |
} |
| 74 |
ctx, separator, value := groups[1], groups[2], groups[3] |
| 75 |
|
| 76 |
viewerSubConfig := ViewerConfig{} |
| 77 |
if err = viewerSubConfig.parse(section, false); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
contextualViewer := ViewerConfigContext{ |
| 81 |
ViewerConfig: &viewerSubConfig, |
| 82 |
Section: *section, |
| 83 |
} |
| 84 |
|
| 85 |
switch ctx { |
| 86 |
case "sender": |
| 87 |
contextualViewer.ContextType = viewerContextSender |
| 88 |
case "from": |
| 89 |
contextualViewer.ContextType = viewerContextFrom |
| 90 |
case "subject": |
| 91 |
contextualViewer.ContextType = viewerContextSubject |
| 92 |
} |
| 93 |
if separator == "=" { |
| 94 |
contextualViewer.Value = value |
| 95 |
} else { |
| 96 |
contextualViewer.Regex, err = regexp.Compile(value) |
| 97 |
if err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
conf.contextualViewers = append(conf.contextualViewers, &contextualViewer) |
| 103 |
conf.contextualCounts[contextualViewer.ContextType]++ |
| 104 |
} |
| 105 |
|
| 106 |
log.Debugf("aerc.conf: [viewer] %#v", conf) |
| 107 |
return conf, nil |
| 108 |
} |
| 109 |
|
| 110 |
func (config *ViewerConfig) parse(section *ini.Section, useDefaults bool) error { |
| 111 |
return MapToStruct(section, config, useDefaults) |
| 112 |
} |
| 113 |
|
| 114 |
func (v *ViewerConfig) ParseLayout(sec *ini.Section, key *ini.Key) ([][]string, error) { |
| 115 |
layout := parseLayout(key.String()) |
| 116 |
return layout, nil |
| 117 |
} |
| 118 |
|
| 119 |
func (base *ViewerConfig) mergeContextual( |
| 120 |
contextType viewerContextType, matcher func(*ViewerConfigContext) bool, |
| 121 |
) *ViewerConfig { |
| 122 |
for _, contextualViewer := range base.contextualViewers { |
| 123 |
if contextualViewer.ContextType != contextType { |
| 124 |
continue |
| 125 |
} |
| 126 |
if !matcher(contextualViewer) { |
| 127 |
continue |
| 128 |
} |
| 129 |
viewer := *base |
| 130 |
err := viewer.parse(&contextualViewer.Section, false) |
| 131 |
if err != nil { |
| 132 |
log.Warnf("merge viewer failed: %v", err) |
| 133 |
} |
| 134 |
viewer.contextualCache = make(map[viewerContextKey]*ViewerConfig) |
| 135 |
viewer.contextualCounts = base.contextualCounts |
| 136 |
viewer.contextualViewers = base.contextualViewers |
| 137 |
return &viewer |
| 138 |
} |
| 139 |
return base |
| 140 |
} |
| 141 |
|
| 142 |
func (base *ViewerConfig) contextual( |
| 143 |
ctxType viewerContextType, ctxKey string, matcher func(*ViewerConfigContext) bool, |
| 144 |
) *ViewerConfig { |
| 145 |
if base.contextualCounts[ctxType] == 0 { |
| 146 |
// shortcut if no contextual viewer for that type |
| 147 |
return base |
| 148 |
} |
| 149 |
key := viewerContextKey{ctxType: ctxType, value: ctxKey} |
| 150 |
ctx, found := base.contextualCache[key] |
| 151 |
if !found { |
| 152 |
ctx = base.mergeContextual(ctxType, matcher) |
| 153 |
base.contextualCache[key] = ctx |
| 154 |
} |
| 155 |
return ctx |
| 156 |
} |
| 157 |
|
| 158 |
func makeMatcher( |
| 159 |
valueTarget string, regexTarget string, |
| 160 |
) func(ctx *ViewerConfigContext) bool { |
| 161 |
return func(ctx *ViewerConfigContext) bool { |
| 162 |
if ctx.Value != "" && ctx.Value == valueTarget { |
| 163 |
return true |
| 164 |
} |
| 165 |
if ctx.Regex != nil && ctx.Regex.Match([]byte(regexTarget)) { |
| 166 |
return true |
| 167 |
} |
| 168 |
return false |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func (base *ViewerConfig) forAddresses( |
| 173 |
ctxType viewerContextType, addresses []*mail.Address, |
| 174 |
) *ViewerConfig { |
| 175 |
for _, address := range addresses { |
| 176 |
base = base.contextual( |
| 177 |
ctxType, address.String(), |
| 178 |
makeMatcher(address.Address, address.String()), |
| 179 |
) |
| 180 |
} |
| 181 |
return base |
| 182 |
} |
| 183 |
|
| 184 |
func (base *ViewerConfig) ForEnvelope(envelope *models.Envelope) *ViewerConfig { |
| 185 |
if envelope == nil { |
| 186 |
return base |
| 187 |
} |
| 188 |
base = base.forAddresses(viewerContextSender, envelope.Sender) |
| 189 |
base = base.forAddresses(viewerContextFrom, envelope.From) |
| 190 |
return base.contextual( |
| 191 |
viewerContextSubject, envelope.Subject, |
| 192 |
makeMatcher(envelope.Subject, envelope.Subject), |
| 193 |
) |
| 194 |
} |