fix: custom message bad pgp using template

This commit is contained in:
James Houlahan
2020-05-20 10:59:00 +02:00
parent f64cb4b56d
commit d718720b29

View File

@ -29,6 +29,7 @@ import (
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
"text/template"
"time" "time"
pmcrypto "github.com/ProtonMail/gopenpgp/crypto" pmcrypto "github.com/ProtonMail/gopenpgp/crypto"
@ -488,23 +489,43 @@ func (im *imapMailbox) fetchMessage(m *pmapi.Message) (err error) {
return return
} }
func (im *imapMailbox) customMessage(m *pmapi.Message, err error, attachBody bool) { const customMessageTemplate = `
// Assuming quoted-printable. <html>
origBody := strings.Replace(m.Body, "=", "=3D", -1) <head></head>
m.Body = "Content-Type: text/html\r\n" <body style="font-family: Arial,'Helvetica Neue',Helvetica,sans-serif; font-size: 14px;">
m.Body = "\n<html><head></head><body style=3D\"font-family: Arial,'Helvetica Neue',Helvetica,sans-serif; font-size: 14px; \">\n" <div style="color:#555; background-color:#cf9696; padding:20px; border-radius: 4px;">
m.Body += "<div style=3D\"color:#555; background-color:#cf9696; padding:20px; border-radius: 4px;\" >\n<strong>Decryption error</strong><br/>Decryption of this message's encrypted content failed.<pre>\n" <strong>Decryption error</strong><br/>
m.Body += err.Error() Decryption of this message's encrypted content failed.
m.Body += "\n</pre></div>\n" <pre>{{.Error}}</pre>
</div>
if attachBody { {{if .AttachBody}}
m.Body += "<div style=3D\"color:#333; background-color:#f4f4f4; border: 1px solid #acb0bf; border-radius: 2px; padding:1rem; margin:1rem 0; font-family:monospace; font-size: 1em;\" ><pre>\n" <div style="color:#333; background-color:#f4f4f4; border: 1px solid #acb0bf; border-radius: 2px; padding:1rem; margin:1rem 0; font-family:monospace; font-size: 1em;">
m.Body += origBody <pre>{{.Body}}</pre>
m.Body += "\n</pre></div>\n" </div>
{{- end}}
</body>
</html>
`
type customMessageData struct {
Error string
AttachBody bool
Body string
} }
m.Body += "</body></html>" func (im *imapMailbox) customMessage(m *pmapi.Message, err error, attachBody bool) {
m.MIMEType = "text/html" t := template.Must(template.New("customMessage").Parse(customMessageTemplate))
b := new(bytes.Buffer)
t.Execute(b, customMessageData{
Error: err.Error(),
AttachBody: attachBody,
Body: m.Body,
})
m.MIMEType = pmapi.ContentTypeHTML
m.Body = b.String()
// NOTE: we need to set header in custom message header, so we check that is non-nil. // NOTE: we need to set header in custom message header, so we check that is non-nil.
if m.Header == nil { if m.Header == nil {