aboutsummaryrefslogtreecommitdiffstats
path: root/config/triggers.go
blob: 82750c2d8fb0df306bcded168f2ed1402969f44d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package config

import (
	"github.com/go-ini/ini"
	"github.com/google/shlex"

	"git.sr.ht/~rjarry/aerc/lib/format"
	"git.sr.ht/~rjarry/aerc/log"
)

type TriggersConfig struct {
	NewEmail []string `ini:"new-email" parse:"ParseNewEmail"`
}

var Triggers = new(TriggersConfig)

func parseTriggers(file *ini.File) error {
	if err := MapToStruct(file.Section("triggers"), Triggers, true); err != nil {
		return err
	}
	log.Debugf("aerc.conf: [triggers] %#v", Triggers)
	return nil
}

func (t *TriggersConfig) ParseNewEmail(_ *ini.Section, key *ini.Key) ([]string, error) {
	cmd := indexFmtRegexp.ReplaceAllStringFunc(
		key.String(),
		func(s string) string {
			runes := []rune(s)
			t, _ := indexVerbToTemplate(runes[len(runes)-1])
			return t
		},
	)
	args, err := shlex.Split(cmd)
	if err != nil {
		return nil, err
	}
	if cmd != key.String() {
		log.Warnf("%s %s",
			"The new-email trigger now uses templates instead of %-based placeholders.",
			"Backward compatibility will be removed in aerc 0.17.")
		Warnings = append(Warnings, Warning{
			Title: "FORMAT CHANGED: [triggers].new-email",
			Body: `
The new-email trigger now uses templates instead of %-based placeholders.

Your configuration in this instance was automatically converted to:

[triggers]
new-email = ` + format.ShellQuote(args) + `

Your configuration file was not changed. To make this change permanent and to
dismiss this warning on launch, replace the above line into aerc.conf. See
aerc-config(5) for more details.

The automatic conversion of new-email will be removed in aerc 0.17.
`,
		})
	}
	return args, nil
}