Add new-email trigger

This patch sets up the trigger config section of aerc.conf.

Each trigger has its own function which is called from the place where
it is triggered. Currently only the new-email trigger is implemented.

The triggers make use of format strings. For instance, in the new-email
trigger this allows the user to select the trigger command and also the
information extracted from the command and placed into their command.

To actually execute the trigger commands the keypresses are simulated.

Further triggers can be implemented in the future.

Formatting of the command is moved to a new package.
This commit is contained in:
Jeffas 2019-07-21 21:01:51 +01:00 committed by Drew DeVault
parent 0950e39f53
commit dc4c36adbf
11 changed files with 175 additions and 35 deletions

12
aerc.go
View file

@ -52,7 +52,7 @@ func getCommands(selected libui.Drawable) []*commands.Commands {
} }
} }
func execCommand(aerc *widgets.Aerc, ui *libui.UI, cmd string) error { func execCommand(aerc *widgets.Aerc, ui *libui.UI, cmd []string) error {
cmds := getCommands((*aerc).SelectedTab()) cmds := getCommands((*aerc).SelectedTab())
for i, set := range cmds { for i, set := range cmds {
err := set.ExecuteCommand(aerc, cmd) err := set.ExecuteCommand(aerc, cmd)
@ -144,11 +144,11 @@ func main() {
ui *libui.UI ui *libui.UI
) )
aerc = widgets.NewAerc(conf, logger, func(cmd string) error { aerc = widgets.NewAerc(conf, logger, func(cmd []string) error {
return execCommand(aerc, ui, cmd) return execCommand(aerc, ui, cmd)
}, func(cmd string) []string { }, func(cmd string) []string {
return getCompletions(aerc, cmd) return getCompletions(aerc, cmd)
}) })
ui, err = libui.Initialize(conf, aerc) ui, err = libui.Initialize(conf, aerc)
if err != nil { if err != nil {

View file

@ -56,11 +56,7 @@ type CommandSource interface {
Commands() *Commands Commands() *Commands
} }
func (cmds *Commands) ExecuteCommand(aerc *widgets.Aerc, cmd string) error { func (cmds *Commands) ExecuteCommand(aerc *widgets.Aerc, args []string) error {
args, err := shlex.Split(cmd)
if err != nil {
return err
}
if len(args) == 0 { if len(args) == 0 {
return errors.New("Expected a command.") return errors.New("Expected a command.")
} }

View file

@ -96,3 +96,14 @@ subject,~^\[PATCH=awk -f @SHAREDIR@/filters/hldiff
#text/html=@SHAREDIR@/filters/html #text/html=@SHAREDIR@/filters/html
text/*=awk -f @SHAREDIR@/filters/plaintext text/*=awk -f @SHAREDIR@/filters/plaintext
#image/*=catimg -w $(tput cols) - #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=

View file

@ -84,6 +84,11 @@ type ViewerConfig struct {
HeaderLayout [][]string `ini:"-"` HeaderLayout [][]string `ini:"-"`
} }
type TriggersConfig struct {
NewEmail string `ini:"new-email"`
ExecuteCommand func(command []string) error
}
type AercConfig struct { type AercConfig struct {
Bindings BindingConfig Bindings BindingConfig
Compose ComposeConfig Compose ComposeConfig
@ -91,6 +96,7 @@ type AercConfig struct {
Accounts []AccountConfig `ini:"-"` Accounts []AccountConfig `ini:"-"`
Filters []FilterConfig `ini:"-"` Filters []FilterConfig `ini:"-"`
Viewer ViewerConfig `ini:"-"` Viewer ViewerConfig `ini:"-"`
Triggers TriggersConfig `ini:"-"`
Ui UIConfig Ui UIConfig
General GeneralConfig General GeneralConfig
} }
@ -278,6 +284,11 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
return err return err
} }
} }
if triggers, err := file.GetSection("triggers"); err == nil {
if err := triggers.MapTo(&config.Triggers); err != nil {
return err
}
}
return nil return nil
} }

49
config/triggers.go Normal file
View file

@ -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)
}
}

View file

@ -68,6 +68,10 @@ These options are configured in the *[ui]* section of aerc.conf.
: comma-separated list of formatted CC names and addresses : comma-separated list of formatted CC names and addresses
| %s | %s
: subject : subject
| %t
: the (first) address the new email was sent to
| %T
: the account name which received the email
| %u | %u
: sender mailbox name (e.g. "smith" in "smith@example.net") : sender mailbox name (e.g. "smith" in "smith@example.net")
| %v | %v
@ -164,6 +168,22 @@ aerc ships with some default filters installed in the share directory (usually
_/usr/share/aerc/filters_). Note that these may have additional dependencies _/usr/share/aerc/filters_). Note that these may have additional dependencies
that aerc does not have alone. that aerc does not have alone.
## TRIGGERS
Triggers specify commands to execute when certain events occur.
They are configured in the *[triggers]* section of aerc.conf.
*new-email*
Executed when a new email arrives in the selected folder.
e.g. new-email=exec notify-send "New email from %n" "%s"
Default: ""
Format specifiers from *index-format* are expanded with respect to the new
message.
# ACCOUNTS.CONF # ACCOUNTS.CONF
This file is used for configuring each mail account used for aerc. Each section This file is used for configuring each mail account used for aerc. Each section

View file

@ -1,4 +1,4 @@
package lib package format
import ( import (
"errors" "errors"
@ -6,14 +6,12 @@ import (
"strings" "strings"
"unicode" "unicode"
"git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/models"
) )
func ParseIndexFormat(conf *config.AercConfig, number int, func ParseMessageFormat(format string, timestampformat string,
msg *models.MessageInfo) (string, []interface{}, error) { accountName string, number int, msg *models.MessageInfo) (string,
[]interface{}, error) {
format := conf.Ui.IndexFormat
retval := make([]byte, 0, len(format)) retval := make([]byte, 0, len(format))
var args []interface{} var args []interface{}
@ -64,11 +62,13 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
retval = append(retval, '%') retval = append(retval, '%')
case 'a': case 'a':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0] addr := msg.Envelope.From[0]
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)) args = append(args,
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
case 'A': case 'A':
var addr *models.Address var addr *models.Address
if len(msg.Envelope.ReplyTo) == 0 { if len(msg.Envelope.ReplyTo) == 0 {
@ -82,26 +82,31 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
addr = msg.Envelope.ReplyTo[0] addr = msg.Envelope.ReplyTo[0]
} }
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)) args = append(args,
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
case 'C': case 'C':
retval = append(retval, 'd') retval = append(retval, 'd')
args = append(args, number) args = append(args, number)
case 'd': case 'd':
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, msg.InternalDate.Format(conf.Ui.TimestampFormat)) args = append(args,
msg.InternalDate.Format(timestampformat))
case 'D': case 'D':
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, msg.InternalDate.Local().Format(conf.Ui.TimestampFormat)) args = append(args,
msg.InternalDate.Local().Format(timestampformat))
case 'f': case 'f':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0].Format() addr := msg.Envelope.From[0].Format()
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addr) args = append(args, addr)
case 'F': case 'F':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0] addr := msg.Envelope.From[0]
// TODO: handle case when sender is current user. Then // TODO: handle case when sender is current user. Then
@ -120,7 +125,8 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
args = append(args, msg.Envelope.MessageId) args = append(args, msg.Envelope.MessageId)
case 'n': case 'n':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0] addr := msg.Envelope.From[0]
var val string var val string
@ -142,22 +148,37 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
case 's': case 's':
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, msg.Envelope.Subject) args = append(args, msg.Envelope.Subject)
case 't':
if len(msg.Envelope.To) == 0 {
return "", nil,
errors.New("found no address for recipient")
}
addr := msg.Envelope.To[0]
retval = append(retval, 's')
args = append(args,
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
case 'T':
retval = append(retval, 's')
args = append(args, accountName)
case 'u': case 'u':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0] addr := msg.Envelope.From[0]
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addr.Mailbox) args = append(args, addr.Mailbox)
case 'v': case 'v':
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, errors.New("found no address for sender") return "", nil,
errors.New("found no address for sender")
} }
addr := msg.Envelope.From[0] addr := msg.Envelope.From[0]
// check if message is from current user // check if message is from current user
if addr.Name != "" { if addr.Name != "" {
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, strings.Split(addr.Name, " ")[0]) args = append(args,
strings.Split(addr.Name, " ")[0])
} }
case 'Z': case 'Z':
// calculate all flags // calculate all flags
@ -237,5 +258,6 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
return string(retval), args, nil return string(retval), args, nil
handle_end_error: handle_end_error:
return "", nil, errors.New("reached end of string while parsing index format") return "", nil,
errors.New("reached end of string while parsing message format")
} }

View file

@ -32,10 +32,13 @@ type MessageStore struct {
pendingBodies map[uint32]interface{} pendingBodies map[uint32]interface{}
pendingHeaders map[uint32]interface{} pendingHeaders map[uint32]interface{}
worker *types.Worker worker *types.Worker
triggerNewEmail func(*models.MessageInfo)
} }
func NewMessageStore(worker *types.Worker, func NewMessageStore(worker *types.Worker,
dirInfo *models.DirectoryInfo) *MessageStore { dirInfo *models.DirectoryInfo,
triggerNewEmail func(*models.MessageInfo)) *MessageStore {
return &MessageStore{ return &MessageStore{
Deleted: make(map[uint32]interface{}), Deleted: make(map[uint32]interface{}),
@ -48,6 +51,8 @@ func NewMessageStore(worker *types.Worker,
pendingBodies: make(map[uint32]interface{}), pendingBodies: make(map[uint32]interface{}),
pendingHeaders: make(map[uint32]interface{}), pendingHeaders: make(map[uint32]interface{}),
worker: worker, worker: worker,
triggerNewEmail: triggerNewEmail,
} }
} }
@ -165,6 +170,18 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
} else { } else {
store.Messages[msg.Info.Uid] = msg.Info store.Messages[msg.Info.Uid] = msg.Info
} }
seen := false
recent := false
for _, flag := range msg.Info.Flags {
if flag == models.RecentFlag {
recent = true
} else if flag == models.SeenFlag {
seen = true
}
}
if !seen && recent {
store.triggerNewEmail(msg.Info)
}
if _, ok := store.pendingHeaders[msg.Info.Uid]; msg.Info.Envelope != nil && ok { if _, ok := store.pendingHeaders[msg.Info.Uid]; msg.Info.Envelope != nil && ok {
delete(store.pendingHeaders, msg.Info.Uid) delete(store.pendingHeaders, msg.Info.Uid)
if cbs, ok := store.headerCallbacks[msg.Info.Uid]; ok { if cbs, ok := store.headerCallbacks[msg.Info.Uid]; ok {

View file

@ -203,7 +203,11 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
if store, ok := acct.msgStores[msg.Info.Name]; ok { if store, ok := acct.msgStores[msg.Info.Name]; ok {
store.Update(msg) store.Update(msg)
} else { } else {
store = lib.NewMessageStore(acct.worker, msg.Info) store = lib.NewMessageStore(acct.worker, msg.Info,
func(msg *models.MessageInfo) {
acct.conf.Triggers.ExecNewEmail(acct.acct,
acct.conf, msg)
})
acct.msgStores[msg.Info.Name] = store acct.msgStores[msg.Info.Name] = store
store.OnUpdate(func(_ *lib.MessageStore) { store.OnUpdate(func(_ *lib.MessageStore) {
store.OnUpdate(nil) store.OnUpdate(nil)

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/google/shlex"
"git.sr.ht/~sircmpwn/aerc/config" "git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/lib/ui" "git.sr.ht/~sircmpwn/aerc/lib/ui"
@ -16,7 +17,7 @@ import (
type Aerc struct { type Aerc struct {
accounts map[string]*AccountView accounts map[string]*AccountView
cmd func(cmd string) error cmd func(cmd []string) error
complete func(cmd string) []string complete func(cmd string) []string
conf *config.AercConfig conf *config.AercConfig
focused libui.Interactive focused libui.Interactive
@ -30,7 +31,7 @@ type Aerc struct {
} }
func NewAerc(conf *config.AercConfig, logger *log.Logger, func NewAerc(conf *config.AercConfig, logger *log.Logger,
cmd func(cmd string) error, complete func(cmd string) []string) *Aerc { cmd func(cmd []string) error, complete func(cmd string) []string) *Aerc {
tabs := libui.NewTabs() tabs := libui.NewTabs()
@ -62,6 +63,7 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger,
} }
statusline.SetAerc(aerc) statusline.SetAerc(aerc)
conf.Triggers.ExecuteCommand = cmd
for i, acct := range conf.Accounts { for i, acct := range conf.Accounts {
view := NewAccountView(conf, &conf.Accounts[i], logger, aerc) view := NewAccountView(conf, &conf.Accounts[i], logger, aerc)
@ -311,7 +313,12 @@ func (aerc *Aerc) focus(item libui.Interactive) {
func (aerc *Aerc) BeginExCommand() { func (aerc *Aerc) BeginExCommand() {
previous := aerc.focused previous := aerc.focused
exline := NewExLine(func(cmd string) { exline := NewExLine(func(cmd string) {
err := aerc.cmd(cmd) parts, err := shlex.Split(cmd)
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
}
err = aerc.cmd(parts)
if err != nil { if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second). aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed) Color(tcell.ColorDefault, tcell.ColorRed)

View file

@ -9,6 +9,7 @@ import (
"git.sr.ht/~sircmpwn/aerc/config" "git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/lib" "git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/lib/format"
"git.sr.ht/~sircmpwn/aerc/lib/ui" "git.sr.ht/~sircmpwn/aerc/lib/ui"
"git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/models"
) )
@ -95,7 +96,9 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
} }
ctx.Fill(0, row, ctx.Width(), 1, ' ', style) ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
fmtStr, args, err := lib.ParseIndexFormat(ml.conf, i, msg) fmtStr, args, err := format.ParseMessageFormat(
ml.conf.Ui.IndexFormat,
ml.conf.Ui.TimestampFormat, "", i, msg)
if err != nil { if err != nil {
ctx.Printf(0, row, style, "%v", err) ctx.Printf(0, row, style, "%v", err)
} else { } else {