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