From dc4c36adbfbffd34319ddc007bad437ef802ee72 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Sun, 21 Jul 2019 21:01:51 +0100 Subject: 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. --- lib/format/format.go | 263 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/indexformat.go | 241 ---------------------------------------------- lib/msgstore.go | 19 +++- 3 files changed, 281 insertions(+), 242 deletions(-) create mode 100644 lib/format/format.go delete mode 100644 lib/indexformat.go (limited to 'lib') diff --git a/lib/format/format.go b/lib/format/format.go new file mode 100644 index 0000000..b403f2d --- /dev/null +++ b/lib/format/format.go @@ -0,0 +1,263 @@ +package format + +import ( + "errors" + "fmt" + "strings" + "unicode" + + "git.sr.ht/~sircmpwn/aerc/models" +) + +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{} + + var c rune + for i, ni := 0, 0; i < len(format); { + ni = strings.IndexByte(format[i:], '%') + if ni < 0 { + ni = len(format) + retval = append(retval, []byte(format[i:ni])...) + break + } + ni += i + 1 + // Check for fmt flags + if ni == len(format) { + goto handle_end_error + } + c = rune(format[ni]) + if c == '+' || c == '-' || c == '#' || c == ' ' || c == '0' { + ni++ + } + + // Check for precision and width + if ni == len(format) { + goto handle_end_error + } + c = rune(format[ni]) + for unicode.IsDigit(c) { + ni++ + c = rune(format[ni]) + } + if c == '.' { + ni++ + c = rune(format[ni]) + for unicode.IsDigit(c) { + ni++ + c = rune(format[ni]) + } + } + + retval = append(retval, []byte(format[i:ni])...) + // Get final format verb + if ni == len(format) { + goto handle_end_error + } + c = rune(format[ni]) + switch c { + case '%': + retval = append(retval, '%') + case 'a': + if len(msg.Envelope.From) == 0 { + 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)) + case 'A': + var addr *models.Address + if len(msg.Envelope.ReplyTo) == 0 { + if len(msg.Envelope.From) == 0 { + return "", nil, + errors.New("found no address for sender or reply-to") + } else { + addr = msg.Envelope.From[0] + } + } else { + addr = msg.Envelope.ReplyTo[0] + } + retval = append(retval, 's') + 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(timestampformat)) + case 'D': + retval = append(retval, 's') + 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") + } + 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") + } + addr := msg.Envelope.From[0] + // TODO: handle case when sender is current user. Then + // use recipient's name + var val string + if addr.Name != "" { + val = addr.Name + } else { + val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) + } + retval = append(retval, 's') + args = append(args, val) + + case 'i': + retval = append(retval, 's') + args = append(args, msg.Envelope.MessageId) + case 'n': + if len(msg.Envelope.From) == 0 { + return "", nil, + errors.New("found no address for sender") + } + addr := msg.Envelope.From[0] + var val string + if addr.Name != "" { + val = addr.Name + } else { + val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) + } + retval = append(retval, 's') + args = append(args, val) + case 'r': + addrs := models.FormatAddresses(msg.Envelope.To) + retval = append(retval, 's') + args = append(args, addrs) + case 'R': + addrs := models.FormatAddresses(msg.Envelope.Cc) + retval = append(retval, 's') + args = append(args, addrs) + 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") + } + 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") + } + 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]) + } + case 'Z': + // calculate all flags + var readReplyFlag = "" + var delFlag = "" + var flaggedFlag = "" + seen := false + recent := false + answered := false + for _, flag := range msg.Flags { + if flag == models.SeenFlag { + seen = true + } else if flag == models.RecentFlag { + recent = true + } else if flag == models.AnsweredFlag { + answered = true + } + if flag == models.DeletedFlag { + delFlag = "D" + // TODO: check if attachments + } + if flag == models.FlaggedFlag { + flaggedFlag = "!" + } + // TODO: check gpg stuff + } + if seen { + if answered { + readReplyFlag = "r" // message has been replied to + } + } else { + if recent { + readReplyFlag = "N" // message is new + } else { + readReplyFlag = "O" // message is old + } + } + retval = append(retval, '3', 's') + args = append(args, readReplyFlag+delFlag+flaggedFlag) + + // Move the below cases to proper alphabetical positions once + // implemented + case 'l': + // TODO: number of lines in the message + retval = append(retval, 'd') + args = append(args, msg.Size) + case 'e': + // TODO: current message number in thread + fallthrough + case 'E': + // TODO: number of messages in current thread + fallthrough + case 'H': + // TODO: spam attribute(s) of this message + fallthrough + case 'L': + // TODO: + fallthrough + case 'X': + // TODO: number of attachments + fallthrough + case 'y': + // TODO: X-Label field + fallthrough + case 'Y': + // TODO: X-Label field and some other constraints + fallthrough + default: + // Just ignore it and print as is + // so %k in index format becomes %%k to Printf + retval = append(retval, '%') + retval = append(retval, byte(c)) + } + i = ni + 1 + } + + return string(retval), args, nil + +handle_end_error: + return "", nil, + errors.New("reached end of string while parsing message format") +} diff --git a/lib/indexformat.go b/lib/indexformat.go deleted file mode 100644 index 34b4d77..0000000 --- a/lib/indexformat.go +++ /dev/null @@ -1,241 +0,0 @@ -package lib - -import ( - "errors" - "fmt" - "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 - retval := make([]byte, 0, len(format)) - var args []interface{} - - var c rune - for i, ni := 0, 0; i < len(format); { - ni = strings.IndexByte(format[i:], '%') - if ni < 0 { - ni = len(format) - retval = append(retval, []byte(format[i:ni])...) - break - } - ni += i + 1 - // Check for fmt flags - if ni == len(format) { - goto handle_end_error - } - c = rune(format[ni]) - if c == '+' || c == '-' || c == '#' || c == ' ' || c == '0' { - ni++ - } - - // Check for precision and width - if ni == len(format) { - goto handle_end_error - } - c = rune(format[ni]) - for unicode.IsDigit(c) { - ni++ - c = rune(format[ni]) - } - if c == '.' { - ni++ - c = rune(format[ni]) - for unicode.IsDigit(c) { - ni++ - c = rune(format[ni]) - } - } - - retval = append(retval, []byte(format[i:ni])...) - // Get final format verb - if ni == len(format) { - goto handle_end_error - } - c = rune(format[ni]) - switch c { - case '%': - retval = append(retval, '%') - case 'a': - if len(msg.Envelope.From) == 0 { - 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)) - case 'A': - var addr *models.Address - if len(msg.Envelope.ReplyTo) == 0 { - if len(msg.Envelope.From) == 0 { - return "", nil, - errors.New("found no address for sender or reply-to") - } else { - addr = msg.Envelope.From[0] - } - } else { - addr = msg.Envelope.ReplyTo[0] - } - retval = append(retval, 's') - 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)) - case 'D': - retval = append(retval, 's') - args = append(args, msg.InternalDate.Local().Format(conf.Ui.TimestampFormat)) - case 'f': - if len(msg.Envelope.From) == 0 { - 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") - } - addr := msg.Envelope.From[0] - // TODO: handle case when sender is current user. Then - // use recipient's name - var val string - if addr.Name != "" { - val = addr.Name - } else { - val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) - } - retval = append(retval, 's') - args = append(args, val) - - case 'i': - retval = append(retval, 's') - args = append(args, msg.Envelope.MessageId) - case 'n': - if len(msg.Envelope.From) == 0 { - return "", nil, errors.New("found no address for sender") - } - addr := msg.Envelope.From[0] - var val string - if addr.Name != "" { - val = addr.Name - } else { - val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host) - } - retval = append(retval, 's') - args = append(args, val) - case 'r': - addrs := models.FormatAddresses(msg.Envelope.To) - retval = append(retval, 's') - args = append(args, addrs) - case 'R': - addrs := models.FormatAddresses(msg.Envelope.Cc) - retval = append(retval, 's') - args = append(args, addrs) - case 's': - retval = append(retval, 's') - args = append(args, msg.Envelope.Subject) - case 'u': - if len(msg.Envelope.From) == 0 { - 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") - } - 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]) - } - case 'Z': - // calculate all flags - var readReplyFlag = "" - var delFlag = "" - var flaggedFlag = "" - seen := false - recent := false - answered := false - for _, flag := range msg.Flags { - if flag == models.SeenFlag { - seen = true - } else if flag == models.RecentFlag { - recent = true - } else if flag == models.AnsweredFlag { - answered = true - } - if flag == models.DeletedFlag { - delFlag = "D" - // TODO: check if attachments - } - if flag == models.FlaggedFlag { - flaggedFlag = "!" - } - // TODO: check gpg stuff - } - if seen { - if answered { - readReplyFlag = "r" // message has been replied to - } - } else { - if recent { - readReplyFlag = "N" // message is new - } else { - readReplyFlag = "O" // message is old - } - } - retval = append(retval, '3', 's') - args = append(args, readReplyFlag+delFlag+flaggedFlag) - - // Move the below cases to proper alphabetical positions once - // implemented - case 'l': - // TODO: number of lines in the message - retval = append(retval, 'd') - args = append(args, msg.Size) - case 'e': - // TODO: current message number in thread - fallthrough - case 'E': - // TODO: number of messages in current thread - fallthrough - case 'H': - // TODO: spam attribute(s) of this message - fallthrough - case 'L': - // TODO: - fallthrough - case 'X': - // TODO: number of attachments - fallthrough - case 'y': - // TODO: X-Label field - fallthrough - case 'Y': - // TODO: X-Label field and some other constraints - fallthrough - default: - // Just ignore it and print as is - // so %k in index format becomes %%k to Printf - retval = append(retval, '%') - retval = append(retval, byte(c)) - } - i = ni + 1 - } - - return string(retval), args, nil - -handle_end_error: - return "", nil, errors.New("reached end of string while parsing index 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 { -- cgit v1.2.3