2019-07-21 22:01:51 +02:00
|
|
|
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) {
|
2019-11-12 12:50:00 +01:00
|
|
|
formatstr, args, err := format.ParseMessageFormat(
|
|
|
|
account.From,
|
|
|
|
part,
|
2019-12-18 06:34:07 +01:00
|
|
|
conf.Ui.TimestampFormat, account.Name, 0, msg, false)
|
2019-07-21 22:01:51 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|