2019-07-21 22:01:51 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/shlex"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/format"
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2019-07-21 22:01:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (trig *TriggersConfig) ExecTrigger(triggerCmd string,
|
2022-07-31 22:16:40 +02:00
|
|
|
triggerFmt func(string) (string, error),
|
|
|
|
) error {
|
2019-07-21 22:01:51 +02:00
|
|
|
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,
|
2022-07-31 22:16:40 +02:00
|
|
|
conf *AercConfig, msg *models.MessageInfo,
|
|
|
|
) {
|
2019-07-21 22:01:51 +02:00
|
|
|
err := trig.ExecTrigger(trig.NewEmail,
|
|
|
|
func(part string) (string, error) {
|
2019-11-12 12:50:00 +01:00
|
|
|
formatstr, args, err := format.ParseMessageFormat(
|
2020-10-14 08:42:26 +02:00
|
|
|
part, conf.Ui.TimestampFormat,
|
2021-10-26 17:24:45 +02:00
|
|
|
conf.Ui.ThisDayTimeFormat,
|
2021-11-06 17:32:38 +01:00
|
|
|
conf.Ui.ThisWeekTimeFormat,
|
2021-10-26 17:24:45 +02:00
|
|
|
conf.Ui.ThisYearTimeFormat,
|
2020-10-14 08:42:26 +02:00
|
|
|
format.Ctx{
|
|
|
|
FromAddress: account.From,
|
|
|
|
AccountName: account.Name,
|
2022-07-31 22:16:40 +02:00
|
|
|
MsgInfo: msg,
|
|
|
|
},
|
2020-10-14 08:42:26 +02:00
|
|
|
)
|
2019-07-21 22:01:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(formatstr, args...), nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("failed to run new-email trigger: %v", err)
|
2019-07-21 22:01:51 +02:00
|
|
|
}
|
|
|
|
}
|