upgrade deps; rewrite smtp session

This commit is contained in:
Aine
2024-02-19 22:55:14 +02:00
parent 10213cc7d7
commit a01720da00
277 changed files with 106832 additions and 7641 deletions

View File

@@ -1,17 +1,16 @@
package enmime
import (
"net/textproto"
"strings"
"github.com/jhillyerd/enmime/mediatype"
inttp "github.com/jhillyerd/enmime/internal/textproto"
)
// detectMultipartMessage returns true if the message has a recognized multipart Content-Type header
func detectMultipartMessage(root *Part, multipartWOBoundaryAsSinglepart bool) bool {
// Parse top-level multipart
ctype := root.Header.Get(hnContentType)
mtype, params, _, err := mediatype.Parse(ctype)
mtype, params, _, err := root.parseMediaType(ctype)
if err != nil {
return false
}
@@ -32,30 +31,30 @@ func detectMultipartMessage(root *Part, multipartWOBoundaryAsSinglepart bool) bo
//
// Valid Attachment-Headers:
//
// - Content-Disposition: attachment; filename="frog.jpg"
// - Content-Disposition: inline; filename="frog.jpg"
// - Content-Type: attachment; filename="frog.jpg"
func detectAttachmentHeader(header textproto.MIMEHeader) bool {
mtype, params, _, _ := mediatype.Parse(header.Get(hnContentDisposition))
// - Content-Disposition: attachment; filename="frog.jpg"
// - Content-Disposition: inline; filename="frog.jpg"
// - Content-Type: attachment; filename="frog.jpg"
func detectAttachmentHeader(root *Part, header inttp.MIMEHeader) bool {
mtype, params, _, _ := root.parseMediaType(header.Get(hnContentDisposition))
if strings.ToLower(mtype) == cdAttachment ||
(strings.ToLower(mtype) == cdInline && len(params) > 0) {
return true
}
mtype, _, _, _ = mediatype.Parse(header.Get(hnContentType))
mtype, _, _, _ = root.parseMediaType(header.Get(hnContentType))
return strings.ToLower(mtype) == cdAttachment
}
// detectTextHeader returns true, if the the MIME headers define a valid 'text/plain' or 'text/html'
// part. If the emptyContentTypeIsPlain argument is set to true, a missing Content-Type header will
// result in a positive plain part detection.
func detectTextHeader(header textproto.MIMEHeader, emptyContentTypeIsText bool) bool {
func detectTextHeader(root *Part, header inttp.MIMEHeader, emptyContentTypeIsText bool) bool {
ctype := header.Get(hnContentType)
if ctype == "" && emptyContentTypeIsText {
return true
}
if mtype, _, _, err := mediatype.Parse(ctype); err == nil {
if mtype, _, _, err := root.parseMediaType(ctype); err == nil {
switch mtype {
case ctTextPlain, ctTextHTML:
return true
@@ -67,23 +66,24 @@ func detectTextHeader(header textproto.MIMEHeader, emptyContentTypeIsText bool)
// detectBinaryBody returns true if the mail header defines a binary body.
func detectBinaryBody(root *Part) bool {
if detectTextHeader(root.Header, true) {
header := inttp.MIMEHeader(root.Header) // Use internal header methods.
if detectTextHeader(root, header, true) {
// It is text/plain, but an attachment.
// Content-Type: text/plain; name="test.csv"
// Content-Disposition: attachment; filename="test.csv"
// Check for attachment only, or inline body is marked
// as attachment, too.
mtype, _, _, _ := mediatype.Parse(root.Header.Get(hnContentDisposition))
mtype, _, _, _ := root.parseMediaType(header.Get(hnContentDisposition))
return strings.ToLower(mtype) == cdAttachment
}
isBin := detectAttachmentHeader(root.Header)
isBin := detectAttachmentHeader(root, header)
if !isBin {
// This must be an attachment, if the Content-Type is not
// 'text/plain' or 'text/html'.
// Example:
// Content-Type: application/pdf; name="doc.pdf"
mtype, _, _, _ := mediatype.Parse(root.Header.Get(hnContentType))
mtype, _, _, _ := root.parseMediaType(header.Get(hnContentType))
mtype = strings.ToLower(mtype)
if mtype != ctTextPlain && mtype != ctTextHTML {
return true