2019-07-21 22:01:51 +02:00
|
|
|
package format
|
2019-06-07 21:35:23 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-19 12:01:45 +02:00
|
|
|
"mime"
|
2019-11-12 12:50:00 +01:00
|
|
|
gomail "net/mail"
|
2019-06-07 21:35:23 +02:00
|
|
|
"strings"
|
2020-07-27 01:48:22 +02:00
|
|
|
"time"
|
2019-06-07 21:35:23 +02:00
|
|
|
"unicode"
|
|
|
|
|
2019-07-08 04:43:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2020-08-19 12:01:45 +02:00
|
|
|
"github.com/emersion/go-message"
|
2019-06-07 21:35:23 +02:00
|
|
|
)
|
|
|
|
|
2020-08-19 12:01:45 +02:00
|
|
|
func ParseAddress(address string) (*models.Address, error) {
|
2019-11-12 12:50:00 +01:00
|
|
|
addrs, err := gomail.ParseAddress(address)
|
|
|
|
if err != nil {
|
2020-08-19 12:01:45 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*models.Address)(addrs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseAddressList(s string) ([]*models.Address, error) {
|
2020-08-24 23:01:18 +02:00
|
|
|
if len(s) == 0 {
|
2020-09-10 07:33:28 +02:00
|
|
|
// we don't consider an empty list to be an error
|
2020-08-24 23:01:18 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-08-19 12:01:45 +02:00
|
|
|
parser := gomail.AddressParser{
|
|
|
|
&mime.WordDecoder{message.CharsetReader},
|
|
|
|
}
|
|
|
|
list, err := parser.ParseList(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-12 12:50:00 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 12:01:45 +02:00
|
|
|
addrs := make([]*models.Address, len(list))
|
|
|
|
for i, a := range list {
|
|
|
|
addrs[i] = (*models.Address)(a)
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatAddresses(l []*models.Address) string {
|
|
|
|
formatted := make([]string, len(l))
|
|
|
|
for i, a := range l {
|
|
|
|
formatted[i] = a.Format()
|
|
|
|
}
|
|
|
|
return strings.Join(formatted, ", ")
|
2019-11-12 12:50:00 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 08:42:26 +02:00
|
|
|
type Ctx struct {
|
|
|
|
FromAddress string
|
|
|
|
AccountName string
|
|
|
|
MsgNum int
|
|
|
|
MsgInfo *models.MessageInfo
|
|
|
|
MsgIsMarked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (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{}
|
|
|
|
|
2020-10-14 08:42:26 +02:00
|
|
|
accountFromAddress, err := ParseAddress(ctx.FromAddress)
|
2020-08-19 12:01:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2019-11-12 12:50:00 +01:00
|
|
|
|
2020-10-14 08:42:26 +02:00
|
|
|
envelope := ctx.MsgInfo.Envelope
|
|
|
|
|
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-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2020-08-19 12:01:45 +02:00
|
|
|
args = append(args, addr.Address)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'A':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-07-08 04:43:58 +02:00
|
|
|
var addr *models.Address
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(envelope.ReplyTo) == 0 {
|
|
|
|
if len(envelope.From) == 0 {
|
2019-06-07 21:35:23 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for sender or reply-to")
|
|
|
|
} else {
|
2020-10-14 08:42:26 +02:00
|
|
|
addr = envelope.From[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-14 08:42:26 +02:00
|
|
|
addr = envelope.ReplyTo[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
retval = append(retval, 's')
|
2020-08-19 12:01:45 +02:00
|
|
|
args = append(args, addr.Address)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'C':
|
|
|
|
retval = append(retval, 'd')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, ctx.MsgNum)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'd':
|
2020-10-14 08:42:26 +02:00
|
|
|
date := envelope.Date
|
2020-07-27 01:48:22 +02:00
|
|
|
if date.IsZero() {
|
2020-10-14 08:42:26 +02:00
|
|
|
date = ctx.MsgInfo.InternalDate
|
2020-07-27 01:48:22 +02:00
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
2020-10-14 08:42:26 +02:00
|
|
|
dummyIfZeroDate(date.Local(), timeFmt))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'D':
|
2020-10-14 08:42:26 +02:00
|
|
|
date := envelope.Date
|
2020-07-27 01:48:22 +02:00
|
|
|
if date.IsZero() {
|
2020-10-14 08:42:26 +02:00
|
|
|
date = ctx.MsgInfo.InternalDate
|
2020-07-27 01:48:22 +02:00
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2019-07-21 22:01:51 +02:00
|
|
|
args = append(args,
|
2020-10-14 08:42:26 +02:00
|
|
|
dummyIfZeroDate(date.Local(), timeFmt))
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'f':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0].Format()
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addr)
|
|
|
|
case 'F':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
var val string
|
2019-11-12 12:50:00 +01:00
|
|
|
|
2020-10-14 08:42:26 +02:00
|
|
|
if addr.Name == accountFromAddress.Name && len(envelope.To) != 0 {
|
|
|
|
addr = envelope.To[0]
|
2019-11-12 12:50:00 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 04:43:58 +02:00
|
|
|
if addr.Name != "" {
|
|
|
|
val = addr.Name
|
2019-06-07 21:35:23 +02:00
|
|
|
} else {
|
2020-08-19 12:01:45 +02:00
|
|
|
val = addr.Address
|
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')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, strings.Join(ctx.MsgInfo.Labels, ", "))
|
2019-12-23 12:51:58 +01:00
|
|
|
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'i':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, envelope.MessageId)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'n':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
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 {
|
2020-08-19 12:01:45 +02:00
|
|
|
val = addr.Address
|
2019-06-07 21:35:23 +02:00
|
|
|
}
|
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, val)
|
|
|
|
case 'r':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addrs := FormatAddresses(envelope.To)
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addrs)
|
|
|
|
case 'R':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addrs := FormatAddresses(envelope.Cc)
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
|
|
|
args = append(args, addrs)
|
|
|
|
case 's':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, envelope.Subject)
|
2019-07-21 22:01:51 +02:00
|
|
|
case 't':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(envelope.To) == 0 {
|
2019-07-21 22:01:51 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("found no address for recipient")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.To[0]
|
2019-07-21 22:01:51 +02:00
|
|
|
retval = append(retval, 's')
|
2020-08-19 12:01:45 +02:00
|
|
|
args = append(args, addr.Address)
|
2019-07-21 22:01:51 +02:00
|
|
|
case 'T':
|
|
|
|
retval = append(retval, 's')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, ctx.AccountName)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'u':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0]
|
2020-08-19 12:01:45 +02:00
|
|
|
mailbox := addr.Address // fallback if there's no @ sign
|
|
|
|
if split := strings.SplitN(addr.Address, "@", 2); len(split) == 2 {
|
|
|
|
mailbox = split[1]
|
|
|
|
}
|
2019-06-07 21:35:23 +02:00
|
|
|
retval = append(retval, 's')
|
2020-08-19 12:01:45 +02:00
|
|
|
args = append(args, mailbox)
|
2019-06-07 21:35:23 +02:00
|
|
|
case 'v':
|
2020-10-14 08:42:26 +02:00
|
|
|
if envelope == nil {
|
2020-07-02 19:54:17 +02:00
|
|
|
return "", nil,
|
|
|
|
errors.New("no envelope available for this message")
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if len(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
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
addr := envelope.From[0]
|
2019-06-07 21:35:23 +02:00
|
|
|
// 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
|
2020-10-14 08:42:26 +02:00
|
|
|
for _, flag := range ctx.MsgInfo.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
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 08:42:26 +02:00
|
|
|
if ctx.MsgIsMarked {
|
2019-12-18 06:34:07 +01:00
|
|
|
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')
|
2020-10-14 08:42:26 +02:00
|
|
|
args = append(args, ctx.MsgInfo.Size)
|
2019-06-07 21:35:23 +02:00
|
|
|
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
|
|
|
}
|
2020-07-27 01:48:22 +02:00
|
|
|
|
|
|
|
func dummyIfZeroDate(date time.Time, format string) string {
|
|
|
|
if date.IsZero() {
|
|
|
|
return strings.Repeat("?", len(format))
|
|
|
|
}
|
|
|
|
return date.Format(format)
|
|
|
|
}
|