2019-07-21 22:01:51 +02:00
|
|
|
package format
|
2019-06-07 21:35:23 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-12 12:50:00 +01:00
|
|
|
gomail "net/mail"
|
2019-06-07 21:35:23 +02:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
2019-07-08 04:43:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-06-07 21:35:23 +02:00
|
|
|
)
|
|
|
|
|
2019-11-12 12:50:00 +01:00
|
|
|
func parseAddress(address string) *gomail.Address {
|
|
|
|
addrs, err := gomail.ParseAddress(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseMessageFormat(
|
|
|
|
fromAddress string,
|
|
|
|
format string, timestampformat string,
|
2019-12-18 06:34:07 +01:00
|
|
|
accountName string, number int, msg *models.MessageInfo,
|
|
|
|
marked bool) (string,
|
2019-07-21 22:01:51 +02:00
|
|
|
[]interface{}, error) {
|
2019-06-07 21:35:23 +02:00
|
|
|
retval := make([]byte, 0, len(format))
|
|
|
|
var args []interface{}
|
|
|
|
|
2019-11-12 12:50:00 +01:00
|
|
|
accountFromAddress := parseAddress(fromAddress)
|
|
|
|
|
2019-06-07 21:35:23 +02:00
|
|
|
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':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
addr := msg.Envelope.From[0]
|
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
|
|
|
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'A':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
var addr *models.Address
|
2019-06-07 21:35:23 +02:00
|
|
|
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')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
|
|
|
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'C':
|
|
|
|
retval = append(retval, 'd')
|
|
|
|
args = append(args, number)
|
|
|
|
case 'd':
|
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
|
|
|
msg.InternalDate.Format(timestampformat))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'D':
|
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
|
|
|
msg.InternalDate.Local().Format(timestampformat))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'f':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
addr := msg.Envelope.From[0].Format()
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addr)
|
|
|
|
case 'F':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
addr := msg.Envelope.From[0]
|
|
|
|
var val string
|
2019-11-12 12:50:00 +01:00
|
|
|
|
2019-11-22 13:33:05 +01:00
|
|
|
if addr.Name == accountFromAddress.Name && len(msg.Envelope.To) != 0 {
|
2019-11-12 12:50:00 +01:00
|
|
|
addr = msg.Envelope.To[0]
|
|
|
|
}
|
|
|
|
|
2019-07-08 04:43:58 +02:00
|
|
|
if addr.Name != "" {
|
|
|
|
val = addr.Name
|
2019-06-07 21:35:23 +02:00
|
|
|
} else {
|
2019-07-08 04:43:58 +02:00
|
|
|
val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, val)
|
|
|
|
|
2019-12-23 12:51:58 +01:00
|
|
|
case 'g':
|
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, strings.Join(msg.Labels, ", "))
|
|
|
|
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'i':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, msg.Envelope.MessageId)
|
|
|
|
case 'n':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
addr := msg.Envelope.From[0]
|
|
|
|
var val string
|
2019-07-08 04:43:58 +02:00
|
|
|
if addr.Name != "" {
|
|
|
|
val = addr.Name
|
2019-06-07 21:35:23 +02:00
|
|
|
} else {
|
2019-07-08 04:43:58 +02:00
|
|
|
val = fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, val)
|
|
|
|
case 'r':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
addrs := models.FormatAddresses(msg.Envelope.To)
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addrs)
|
|
|
|
case 'R':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
addrs := models.FormatAddresses(msg.Envelope.Cc)
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addrs)
|
|
|
|
case 's':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, msg.Envelope.Subject)
|
2019-07-21 22:01:51 +02:00
|
|
|
case 't':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-07-21 22:01:51 +02:00
|
|
|
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)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'u':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
addr := msg.Envelope.From[0]
|
|
|
|
retval = append(retval, 's')
|
2019-07-08 04:43:58 +02:00
|
|
|
args = append(args, addr.Mailbox)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'v':
|
2020-07-02 19:54:17 +02:00
|
|
|
if msg.Envelope == nil {
|
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
if len(msg.Envelope.From) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
addr := msg.Envelope.From[0]
|
|
|
|
// check if message is from current user
|
2019-07-08 04:43:58 +02:00
|
|
|
if addr.Name != "" {
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
|
|
|
strings.Split(addr.Name, " ")[0])
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
case 'Z':
|
|
|
|
// calculate all flags
|
2019-07-12 17:14:26 +02:00
|
|
|
var readReplyFlag = ""
|
2019-06-07 21:35:23 +02:00
|
|
|
var delFlag = ""
|
|
|
|
var flaggedFlag = ""
|
2019-12-18 06:34:07 +01:00
|
|
|
var markedFlag = ""
|
2019-07-12 17:14:26 +02:00
|
|
|
seen := false
|
|
|
|
recent := false
|
|
|
|
answered := false
|
2019-06-07 21:35:23 +02:00
|
|
|
for _, flag := range msg.Flags {
|
2019-07-08 04:43:58 +02:00
|
|
|
if flag == models.SeenFlag {
|
2019-07-12 17:14:26 +02:00
|
|
|
seen = true
|
2019-07-08 04:43:58 +02:00
|
|
|
} else if flag == models.RecentFlag {
|
2019-07-12 17:14:26 +02:00
|
|
|
recent = true
|
2019-07-08 04:43:58 +02:00
|
|
|
} else if flag == models.AnsweredFlag {
|
2019-07-12 17:14:26 +02:00
|
|
|
answered = true
|
2019-06-14 23:11:17 +02:00
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
if flag == models.DeletedFlag {
|
2019-06-07 21:35:23 +02:00
|
|
|
delFlag = "D"
|
|
|
|
// TODO: check if attachments
|
2019-06-14 23:11:17 +02:00
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
if flag == models.FlaggedFlag {
|
2019-06-07 21:35:23 +02:00
|
|
|
flaggedFlag = "!"
|
|
|
|
}
|
|
|
|
// TODO: check gpg stuff
|
|
|
|
}
|
2019-07-12 17:14:26 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 06:34:07 +01:00
|
|
|
if marked {
|
|
|
|
markedFlag = "*"
|
|
|
|
}
|
2019-12-22 13:08:23 +01:00
|
|
|
retval = append(retval, '4', 's')
|
2019-12-18 06:34:07 +01:00
|
|
|
args = append(args, readReplyFlag+delFlag+flaggedFlag+markedFlag)
|
2019-06-07 21:35:23 +02:00
|
|
|
|
|
|
|
// 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:
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("reached end of string while parsing message format")
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|