Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint: Format with gofmt from go 1.20 #273

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ func newBoundaryReader(reader *bufio.Reader, boundary string) *boundaryReader {

// Read returns a buffer containing the content up until boundary
//
// Excerpt from io package on io.Reader implementations:
// Excerpt from io package on io.Reader implementations:
//
// type Reader interface {
// Read(p []byte) (n int, err error)
// }
// type Reader interface {
// Read(p []byte) (n int, err error)
// }
//
// Read reads up to len(p) bytes into p. It returns the number of
// bytes read (0 <= n <= len(p)) and any error encountered. Even
// if Read returns n < len(p), it may use all of p as scratch space
// during the call. If some data is available but not len(p) bytes,
// Read conventionally returns what is available instead of waiting
// for more.
// Read reads up to len(p) bytes into p. It returns the number of
// bytes read (0 <= n <= len(p)) and any error encountered. Even
// if Read returns n < len(p), it may use all of p as scratch space
// during the call. If some data is available but not len(p) bytes,
// Read conventionally returns what is available instead of waiting
// for more.
//
// When Read encounters an error or end-of-file condition after
// successfully reading n > 0 bytes, it returns the number of bytes
// read. It may return the (non-nil) error from the same call or
// return the error (and n == 0) from a subsequent call. An instance
// of this general case is that a Reader returning a non-zero number
// of bytes at the end of the input stream may return either err == EOF
// or err == nil. The next Read should return 0, EOF.
// When Read encounters an error or end-of-file condition after
// successfully reading n > 0 bytes, it returns the number of bytes
// read. It may return the (non-nil) error from the same call or
// return the error (and n == 0) from a subsequent call. An instance
// of this general case is that a Reader returning a non-zero number
// of bytes at the end of the input stream may return either err == EOF
// or err == nil. The next Read should return 0, EOF.
//
// Callers should always process the n > 0 bytes returned before
// considering the error err. Doing so correctly handles I/O errors
// that happen after reading some bytes and also both of the allowed
// EOF behaviors.
// Callers should always process the n > 0 bytes returned before
// considering the error err. Doing so correctly handles I/O errors
// that happen after reading some bytes and also both of the allowed
// EOF behaviors.
func (b *boundaryReader) Read(dest []byte) (n int, err error) {
if b.buffer.Len() >= len(dest) {
// This read request can be satisfied entirely by the buffer.
Expand Down
6 changes: 3 additions & 3 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ 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"
// - 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))
if strings.ToLower(mtype) == cdAttachment ||
Expand Down
10 changes: 5 additions & 5 deletions enmime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// included mime/multipart support where possible, but is geared towards parsing MIME encoded
// emails.
//
// Overview
// # Overview
//
// The enmime API has two conceptual layers. The lower layer is a tree of Part structs,
// representing each component of a decoded MIME message. The upper layer, called an Envelope
// provides an intuitive way to interact with a MIME message.
//
// Part Tree
// # Part Tree
//
// Calling ReadParts causes enmime to parse the body of a MIME message into a tree of Part objects,
// each of which is aware of its content type, filename and headers. The content of a Part is
Expand All @@ -22,7 +22,7 @@
// DepthMatchFirst() methods to search the Part tree. BreadthMatchAll() and DepthMatchAll() will
// collect all Parts matching your criteria.
//
// Envelope
// # Envelope
//
// ReadEnvelope returns an Envelope struct. Behind the scenes a Part tree is constructed, and then
// sorted into the correct fields of the Envelope.
Expand All @@ -31,15 +31,15 @@
// text Part available, the HTML Part will be down-converted using the html2text library[1]. The
// root of the Part tree, as well as slices of the inline and attachment Parts are also available.
//
// Headers
// # Headers
//
// Every MIME Part has its own headers, accessible via the Part.Header field. The raw headers for
// an Envelope are available in Root.Header. Envelope also provides helper methods to fetch
// headers: GetHeader(key) will return the RFC 2047 decoded value of the specified header.
// AddressList(key) will convert the specified address header into a slice of net/mail.Address
// values.
//
// Errors
// # Errors
//
// enmime attempts to be tolerant of poorly encoded MIME messages. In situations where parsing is
// not possible, the ReadEnvelope and ReadParts functions will return a hard error. If enmime is
Expand Down
6 changes: 4 additions & 2 deletions inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ var defaultHeadersList = []string{

// DecodeHeaders returns a limited selection of mime headers for use by user agents
// Default header list:
// "Date", "Subject", "Sender", "From", "To", "CC" and "BCC"
//
// "Date", "Subject", "Sender", "From", "To", "CC" and "BCC"
//
// Additional headers provided will be formatted canonically:
// h, err := enmime.DecodeHeaders(b, "content-type", "user-agent")
//
// h, err := enmime.DecodeHeaders(b, "content-type", "user-agent")
func DecodeHeaders(b []byte, addtlHeaders ...string) (textproto.MIMEHeader, error) {
b = ensureHeaderBoundary(b)
tr := textproto.NewReader(bufio.NewReader(bytes.NewReader(b)))
Expand Down
32 changes: 20 additions & 12 deletions mediatype/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const (
// Parse is a more tolerant implementation of Go's mime.ParseMediaType function.
//
// Tolerances accounted for:
// * Missing ';' between content-type and media parameters
// * Repeating media parameters
// * Unquoted values in media parameters containing 'tspecials' characters
// * Newline characters
// - Missing ';' between content-type and media parameters
// - Repeating media parameters
// - Unquoted values in media parameters containing 'tspecials' characters
// - Newline characters
func Parse(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
mtype, params, err = mime.ParseMediaType(
fixNewlines(fixUnescapedQuotes(fixUnquotedSpecials(fixMangledMediaType(removeTrailingHTMLTags(ctype), ';')))))
Expand Down Expand Up @@ -156,16 +156,24 @@ func fixMangledMediaType(mtype string, sep rune) string {
// Content-Type header.
//
// Given this this header:
// `Content-Type: text/calendar; charset=utf-8; method=text/calendar`
//
// `Content-Type: text/calendar; charset=utf-8; method=text/calendar`
//
// `consumeParams` should be given this part:
// ` charset=utf-8; method=text/calendar`
//
// ` charset=utf-8; method=text/calendar`
//
// And returns (first pass):
// `consumed = "charset=utf-8;"`
// `rest = " method=text/calendar"`
//
// `consumed = "charset=utf-8;"`
// `rest = " method=text/calendar"`
//
// Capture the `consumed` value (to build a clean Content-Type header value) and pass the value of
// `rest` back to `consumeParam`. That second call will return:
// `consumed = " method=\"text/calendar\""`
// `rest = ""`
//
// `consumed = " method=\"text/calendar\""`
// `rest = ""`
//
// Again, use the value of `consumed` to build a clean Content-Type header value. Given that `rest`
// is empty, all of the parameters have been consumed successfully.
//
Expand Down Expand Up @@ -381,8 +389,8 @@ func fixUnquotedSpecials(s string) string {

// fixUnescapedQuotes inspects for unescaped quotes inside of a quoted string and escapes them
//
// Input: application/rtf; charset=iso-8859-1; name=""V047411.rtf".rtf"
// Output: application/rtf; charset=iso-8859-1; name="\"V047411.rtf\".rtf"
// Input: application/rtf; charset=iso-8859-1; name=""V047411.rtf".rtf"
// Output: application/rtf; charset=iso-8859-1; name="\"V047411.rtf\".rtf"
func fixUnescapedQuotes(hvalue string) string {
params := stringutil.SplitAfterUnquoted(hvalue, ';', '"')
sb := &strings.Builder{}
Expand Down