aboutsummaryrefslogtreecommitdiffstats
path: root/config/hooks.go
blob: b1b71a25b9489fe6a3c6ca14355bf227d8ecfa87 (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
62
63
64
65
66
package config

import (
	"strings"

	"git.sr.ht/~rjarry/aerc/log"
	"github.com/go-ini/ini"
)

type HooksConfig struct {
	AercStartup  string `ini:"aerc-startup"`
	AercShutdown string `ini:"aerc-shutdown"`
	MailReceived string `ini:"mail-received"`
	MailDeleted  string `ini:"mail-deleted"`
	MailAdded    string `ini:"mail-added"`
}

var Hooks HooksConfig

func parseHooks(file *ini.File) error {
	err := MapToStruct(file.Section("hooks"), &Hooks, true)
	if err != nil {
		return err
	}

	newEmail := file.Section("triggers").Key("new-email").String()
	if Hooks.MailReceived == "" && newEmail != "" {
		Hooks.MailReceived = convertNewEmailTrigger(newEmail)
		Warnings = append(Warnings, Warning{
			Title: "DEPRECATION NOTICE: [triggers].new-email",
			Body: `
The new-email trigger has been replaced by [hooks].email-received.

Your configuration in this instance was automatically converted to:

[hooks]
mail-received = ` + Hooks.MailReceived + `

Please verify the accuracy of the above translation.

Your configuration file was not changed. To make this change permanent and to
dismiss this deprecation warning on launch, copy the above lines into aerc.conf
and remove new-email from it. See aerc-config(5) for more details.
`,
		})
	}

	log.Debugf("aerc.conf: [hooks] %#v", Hooks)
	return nil
}

func convertNewEmailTrigger(old string) string {
	translations := map[string]string{
		"%a": "$AERC_FROM_ADDRESS",
		"%n": "$AERC_FROM_NAME",
		"%s": "$AERC_SUBJECT",
		"%f": "$AERC_FROM_NAME <$AERC_FROM_ADDRESS>",
		"%u": `$(echo "$AERC_FROM_ADDRESS" | cut -d@ -f1)`,
		"%v": `$(echo "$AERC_FROM_NAME" | cut -d' ' -f1)`,
	}
	for replace, with := range translations {
		old = strings.ReplaceAll(old, replace, with)
	}
	old = strings.TrimPrefix(old, "exec ")
	return strings.ReplaceAll(old, "%%", "%")
}