Fix a nil Envelope when getting the format

When getting the format for a message the envelope can be nil and this
shouldn't crash the program.
This commit is contained in:
Andrew Jeffery 2020-07-02 18:54:17 +01:00 committed by Reto Brunner
parent e1c2b596dc
commit 0acd6d0770
1 changed files with 48 additions and 0 deletions

View File

@ -76,6 +76,10 @@ func ParseMessageFormat(
case '%': case '%':
retval = append(retval, '%') retval = append(retval, '%')
case 'a': case 'a':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")
@ -85,6 +89,10 @@ func ParseMessageFormat(
args = append(args, args = append(args,
fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host)) fmt.Sprintf("%s@%s", addr.Mailbox, addr.Host))
case 'A': case 'A':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
var addr *models.Address var addr *models.Address
if len(msg.Envelope.ReplyTo) == 0 { if len(msg.Envelope.ReplyTo) == 0 {
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
@ -111,6 +119,10 @@ func ParseMessageFormat(
args = append(args, args = append(args,
msg.InternalDate.Local().Format(timestampformat)) msg.InternalDate.Local().Format(timestampformat))
case 'f': case 'f':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")
@ -119,6 +131,10 @@ func ParseMessageFormat(
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addr) args = append(args, addr)
case 'F': case 'F':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")
@ -143,9 +159,17 @@ func ParseMessageFormat(
args = append(args, strings.Join(msg.Labels, ", ")) args = append(args, strings.Join(msg.Labels, ", "))
case 'i': case 'i':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, msg.Envelope.MessageId) args = append(args, msg.Envelope.MessageId)
case 'n': case 'n':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")
@ -160,17 +184,33 @@ func ParseMessageFormat(
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, val) args = append(args, val)
case 'r': case 'r':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
addrs := models.FormatAddresses(msg.Envelope.To) addrs := models.FormatAddresses(msg.Envelope.To)
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addrs) args = append(args, addrs)
case 'R': case 'R':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
addrs := models.FormatAddresses(msg.Envelope.Cc) addrs := models.FormatAddresses(msg.Envelope.Cc)
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addrs) args = append(args, addrs)
case 's': case 's':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, msg.Envelope.Subject) args = append(args, msg.Envelope.Subject)
case 't': case 't':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.To) == 0 { if len(msg.Envelope.To) == 0 {
return "", nil, return "", nil,
errors.New("found no address for recipient") errors.New("found no address for recipient")
@ -183,6 +223,10 @@ func ParseMessageFormat(
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, accountName) args = append(args, accountName)
case 'u': case 'u':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")
@ -191,6 +235,10 @@ func ParseMessageFormat(
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, addr.Mailbox) args = append(args, addr.Mailbox)
case 'v': case 'v':
if msg.Envelope == nil {
return "", nil,
errors.New("no envelope available for this message")
}
if len(msg.Envelope.From) == 0 { if len(msg.Envelope.From) == 0 {
return "", nil, return "", nil,
errors.New("found no address for sender") errors.New("found no address for sender")