1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package rfc822
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"mime/quotedprintable"
"github.com/sirupsen/logrus"
)
var ErrNoSuchPart = errors.New("no such parts exists")
type Section struct {
identifier []int
literal []byte
parsedHeader *Header
header int
body int
end int
children []*Section
}
func Parse(literal []byte) *Section {
return parse(literal, []int{}, 0, len(literal))
}
func (section *Section) Identifier() []int {
return section.identifier
}
func (section *Section) ContentType() (MIMEType, map[string]string, error) {
header, err := section.ParseHeader()
if err != nil {
return "", nil, err
}
contentType := header.Get("Content-Type")
mimeType, values, err := ParseMIMEType(contentType)
if err != nil {
logrus.Warnf("Message contains invalid mime type: %v", contentType)
return "", nil, nil //nolint:nilerr
}
return mimeType, values, nil
}
func (section *Section) Header() []byte {
return section.literal[section.header:section.body]
}
func (section *Section) ParseHeader() (*Header, error) {
if section.parsedHeader == nil {
h, err := NewHeader(section.Header())
if err != nil {
return nil, err
}
section.parsedHeader = h
}
return section.parsedHeader, nil
}
func (section *Section) Body() []byte {
return section.literal[section.body:section.end]
}
func (section *Section) DecodedBody() ([]byte, error) {
header, err := section.ParseHeader()
if err != nil {
return nil, err
}
switch header.Get("Content-Transfer-Encoding") {
case "base64":
return base64Decode(section.Body())
case "quoted-printable":
return quotedPrintableDecode(section.Body())
default:
return section.Body(), nil
}
}
func (section *Section) Literal() []byte {
return section.literal[section.header:section.end]
}
func (section *Section) Children() ([]*Section, error) {
if len(section.children) == 0 {
if err := section.load(); err != nil {
return nil, err
}
}
return section.children, nil
}
func (section *Section) Part(identifier ...int) (*Section, error) {
if len(identifier) > 0 {
children, err := section.Children()
if err != nil {
return nil, err
}
if identifier[0] <= 0 || identifier[0]-1 > len(children) {
return nil, ErrNoSuchPart
}
if len(children) != 0 {
childIndex := identifier[0] - 1
if childIndex >= len(children) {
return nil, fmt.Errorf("invalid part index")
}
return children[identifier[0]-1].Part(identifier[1:]...)
}
}
return section, nil
}
func (section *Section) Walk(f func(*Section) error) error {
if err := f(section); err != nil {
return err
}
children, err := section.Children()
if err != nil {
return err
}
for _, child := range children {
if err := child.Walk(f); err != nil {
return err
}
}
return nil
}
func (section *Section) load() error {
contentType, contentParams, err := section.ContentType()
if err != nil {
return err
}
if MIMEType(contentType) == MessageRFC822 {
child := parse(
section.literal[section.body:section.end],
section.identifier,
0,
section.end-section.body,
)
if err := child.load(); err != nil {
return err
}
section.children = append(section.children, child.children...)
} else if contentType.IsMultiPart() {
scanner, err := NewByteScanner(section.literal[section.body:section.end], []byte(contentParams["boundary"]))
if err != nil {
return err
}
res := scanner.ScanAll()
for idx, res := range res {
child := parse(
section.literal,
append(section.identifier, idx+1),
section.body+res.Offset,
section.body+res.Offset+len(res.Data),
)
section.children = append(section.children, child)
}
}
return nil
}
func Split(b []byte) ([]byte, []byte) {
remaining := b
splitIndex := int(0)
separator := []byte{'\n'}
for len(remaining) != 0 {
index := bytes.Index(remaining, separator)
if index < 0 {
splitIndex += len(remaining)
break
}
splitIndex += index + 1
if len(bytes.Trim(remaining[0:index], "\r\n")) == 0 {
break
}
remaining = remaining[index+1:]
}
return b[0:splitIndex], b[splitIndex:]
}
func parse(literal []byte, identifier []int, begin, end int) *Section {
header, _ := Split(literal[begin:end])
parsedHeader, err := NewHeader(header)
if err != nil {
header = nil
parsedHeader = nil
}
return &Section{
identifier: identifier,
literal: literal,
parsedHeader: parsedHeader,
header: begin,
body: begin + len(header),
end: end,
}
}
func base64Decode(b []byte) ([]byte, error) {
res := make([]byte, base64.StdEncoding.DecodedLen(len(b)))
n, err := base64.StdEncoding.Decode(res, b)
if err != nil {
return nil, err
}
return res[0:n], nil
}
func quotedPrintableDecode(b []byte) ([]byte, error) {
return io.ReadAll(quotedprintable.NewReader(bytes.NewReader(b)))
}
|