about summary refs log tree commit diff stats
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to 'config')
-rw-r--r--config/aerc.conf.in11
-rw-r--r--config/config.go11
-rw-r--r--config/triggers.go49
3 files changed, 71 insertions, 0 deletions
diff --git a/config/aerc.conf.in b/config/aerc.conf.in
index 4219042..5b080e9 100644
--- a/config/aerc.conf.in
+++ b/config/aerc.conf.in
@@ -96,3 +96,14 @@ subject,~^\[PATCH=awk -f @SHAREDIR@/filters/hldiff
 #text/html=@SHAREDIR@/filters/html
 text/*=awk -f @SHAREDIR@/filters/plaintext
 #image/*=catimg -w $(tput cols) -
+
+[triggers]
+#
+# Triggers specify commands to execute when certain events occur.
+#
+# Example:
+# new-email=exec notify-send "New email from %n" "%s"<Enter>
+
+#
+# Executed when a new email arrives in the selected folder
+new-email=
diff --git a/config/config.go b/config/config.go
index 4a049fa..f863729 100644
--- a/config/config.go
+++ b/config/config.go
@@ -84,6 +84,11 @@ type ViewerConfig struct {
 	HeaderLayout   [][]string `ini:"-"`
 }
 
+type TriggersConfig struct {
+	NewEmail       string `ini:"new-email"`
+	ExecuteCommand func(command []string) error
+}
+
 type AercConfig struct {
 	Bindings BindingConfig
 	Compose  ComposeConfig
@@ -91,6 +96,7 @@ type AercConfig struct {
 	Accounts []AccountConfig `ini:"-"`
 	Filters  []FilterConfig  `ini:"-"`
 	Viewer   ViewerConfig    `ini:"-"`
+	Triggers TriggersConfig  `ini:"-"`
 	Ui       UIConfig
 	General  GeneralConfig
 }
@@ -278,6 +284,11 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
 			return err
 		}
 	}
+	if triggers, err := file.GetSection("triggers"); err == nil {
+		if err := triggers.MapTo(&config.Triggers); err != nil {
+			return err
+		}
+	}
 	return nil
 }
 
diff --git a/config/triggers.go b/config/triggers.go
new file mode 100644
index 0000000..d31f267
--- /dev/null
+++ b/config/triggers.go
@@ -0,0 +1,49 @@
+package config
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/google/shlex"
+
+	"git.sr.ht/~sircmpwn/aerc/lib/format"
+	"git.sr.ht/~sircmpwn/aerc/models"
+)
+
+func (trig *TriggersConfig) ExecTrigger(triggerCmd string,
+	triggerFmt func(string) (string, error)) error {
+
+	if len(triggerCmd) == 0 {
+		return errors.New("Trigger command empty")
+	}
+	triggerCmdParts, err := shlex.Split(triggerCmd)
+	if err != nil {
+		return err
+	}
+
+	var command []string
+	for _, part := range triggerCmdParts {
+		formattedPart, err := triggerFmt(part)
+		if err != nil {
+			return err
+		}
+		command = append(command, formattedPart)
+	}
+	return trig.ExecuteCommand(command)
+}
+
+func (trig *TriggersConfig) ExecNewEmail(account *AccountConfig,
+	conf *AercConfig, msg *models.MessageInfo) {
+	err := trig.ExecTrigger(trig.NewEmail,
+		func(part string) (string, error) {
+			formatstr, args, err := format.ParseMessageFormat(part,
+				conf.Ui.TimestampFormat, account.Name, 0, msg)
+			if err != nil {
+				return "", err
+			}
+			return fmt.Sprintf(formatstr, args...), nil
+		})
+	if err != nil {
+		fmt.Printf("Error from the new-email trigger: %s\n", err)
+	}
+}