about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-07-21 21:01:51 +0100
committerDrew DeVault <sir@cmpwn.com>2019-07-26 14:00:24 -0400
commitdc4c36adbfbffd34319ddc007bad437ef802ee72 (patch)
tree1742b507d27a9564c3f51623c01266cca8f9a267 /lib
parent0950e39f538610172858a5e3b7582a7f5cb1fd64 (diff)
downloadaerc-dc4c36adbfbffd34319ddc007bad437ef802ee72.tar.gz
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/format/format.go (renamed from lib/indexformat.go)58
-rw-r--r--lib/msgstore.go19
2 files changed, 58 insertions, 19 deletions
diff --git a/lib/indexformat.go b/lib/format/format.go
index 34b4d77..b403f2d 100644
--- a/lib/indexformat.go
+++ b/lib/format/format.go
@@ -1,4 +1,4 @@
-package lib
+package format
 
 import (
 	"errors"
@@ -6,14 +6,12 @@ import (
 	"strings"
 	"unicode"
 
-	"git.sr.ht/~sircmpwn/aerc/config"
 	"git.sr.ht/~sircmpwn/aerc/models"
 )
 
-func ParseIndexFormat(conf *config.AercConfig, number int,
-	msg *models.MessageInfo) (string, []interface{}, error) {
-
-	format := conf.Ui.IndexFormat
+func ParseMessageFormat(format string, timestampformat string,
+	accountName string, number int, msg *models.MessageInfo) (string,
+	[]interface{}, error) {
 	retval := make([]byte, 0, len(format))
 	var args []interface{}
 
@@ -64,11 +62,13 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
 			retval = append(retval, '%')
 		case 'a':
 			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]
 			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':
 			var addr *models.Address
 			if len(msg.Envelope.ReplyTo) == 0 {
@@ -82,26 +82,31 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
 				addr = msg.Envelope.ReplyTo[0]
 			}
 			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':
 			retval = append(retval, 'd')
 			args = append(args, number)
 		case 'd':
 			retval = append(retval, 's')
-			args = append(args, msg.InternalDate.Format(conf.Ui.TimestampFormat))
+			args = append(args,
+				msg.InternalDate.Format(timestampformat))
 		case 'D':
 			retval = append(retval, 's')
-			args = append(args, msg.InternalDate.Local().Format(conf.Ui.TimestampFormat))
+			args = append(args,
+				msg.InternalDate.Local().Format(timestampformat))
 		case 'f':
 			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()
 			retval = append(retval, 's')
 			args = append(args, addr)
 		case 'F':
 			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]
 			// 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)
 		case 'n':
 			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]
 			var val string
@@ -142,22 +148,37 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
 		case 's':
 			retval = append(retval, 's')
 			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':
 			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]
 			retval = append(retval, 's')
 			args = append(args, addr.Mailbox)
 		case 'v':
 			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]
 			// check if message is from current user
 			if addr.Name != "" {
 				retval = append(retval, 's')
-				args = append(args, strings.Split(addr.Name, " ")[0])
+				args = append(args,
+					strings.Split(addr.Name, " ")[0])
 			}
 		case 'Z':
 			// calculate all flags
@@ -237,5 +258,6 @@ func ParseIndexFormat(conf *config.AercConfig, number int,
 	return string(retval), args, nil
 
 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")
 }
diff --git a/lib/msgstore.go b/lib/msgstore.go
index 736217e..53faaac 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -32,10 +32,13 @@ type MessageStore struct {
 	pendingBodies  map[uint32]interface{}
 	pendingHeaders map[uint32]interface{}
 	worker         *types.Worker
+
+	triggerNewEmail func(*models.MessageInfo)
 }
 
 func NewMessageStore(worker *types.Worker,
-	dirInfo *models.DirectoryInfo) *MessageStore {
+	dirInfo *models.DirectoryInfo,
+	triggerNewEmail func(*models.MessageInfo)) *MessageStore {
 
 	return &MessageStore{
 		Deleted: make(map[uint32]interface{}),
@@ -48,6 +51,8 @@ func NewMessageStore(worker *types.Worker,
 		pendingBodies:  make(map[uint32]interface{}),
 		pendingHeaders: make(map[uint32]interface{}),
 		worker:         worker,
+
+		triggerNewEmail: triggerNewEmail,
 	}
 }
 
@@ -165,6 +170,18 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
 		} else {
 			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 {
 			delete(store.pendingHeaders, msg.Info.Uid)
 			if cbs, ok := store.headerCallbacks[msg.Info.Uid]; ok {