// Package tequat implements parsing of mail messages // into categories of "Text, Quotes, Attachment". package tequat import ( "mime" "net/textproto" ) // "Text, Quotes, Attachments" type TQA []Atom type Atom interface { isatom() } type Text struct { Raw string } func (_ Text) isatom() {} type Quote struct { Raw string } func (_ Quote) isatom() {} type SignatureBlock struct { // leading "-- \n" is stripped Raw string } func (_ SignatureBlock) isatom() {} type DroppedAlternatives struct { MediatypeTs []string } func (_ DroppedAlternatives) isatom() {} // type Attachment struct { Inline bool // Header should contain a singular, non-empty value keyed "Content-Type". // Header may be nil. Header textproto.MIMEHeader Data []byte } func (_ Attachment) isatom() {} func (attachment Attachment) ContentType() string { return GetUnique(attachment.Header, "Content-Type") } // may be empty // TODO This is not the whole story. (See: "Content-Type: application/pdf; name=..." or "Content-Disposition: attachment; filename=...") func (attachment Attachment) Ext() string { exts, err := mime.ExtensionsByType(attachment.ContentType()) if err == nil && len(exts) > 0 { return exts[0] } return "" } // func GetUnique(header textproto.MIMEHeader, key string) string { if header == nil || len(header.Values(key)) != 1 { return "" } return header.Get(key) }