MCPcopy Create free account
hub / github.com/foxcpp/maddy / ExtractFromDomain

Function ExtractFromDomain

internal/dmarc/evaluate.go:226–256  ·  view source on GitHub ↗
(hdr textproto.Header)

Source from the content-addressed store, hash-verified

224}
225
226func ExtractFromDomain(hdr textproto.Header) (string, error) {
227 // TODO(GH emersion/go-message#75): Add textproto.Header.Count method.
228 var firstFrom string
229 for fields := hdr.FieldsByKey("From"); fields.Next(); {
230 if firstFrom == "" {
231 firstFrom = fields.Value()
232 } else {
233 return "", errors.New("dmarc: multiple From header fields are not allowed")
234 }
235 }
236 if firstFrom == "" {
237 return "", errors.New("dmarc: missing From header field")
238 }
239
240 hdrFromList, err := mail.ParseAddressList(firstFrom)
241 if err != nil {
242 return "", fmt.Errorf("dmarc: malformed From header field: %s", strings.TrimPrefix(err.Error(), "mail: "))
243 }
244 if len(hdrFromList) > 1 {
245 return "", errors.New("dmarc: multiple addresses in From field are not allowed")
246 }
247 if len(hdrFromList) == 0 {
248 return "", errors.New("dmarc: missing address in From field")
249 }
250 _, domain, err := address.Split(hdrFromList[0].Address)
251 if err != nil {
252 return "", fmt.Errorf("dmarc: malformed From header field: %w", err)
253 }
254
255 return domain, nil
256}

Callers 2

FetchRecordMethod · 0.85
TestExtractDomainsFunction · 0.85

Calls 3

SplitFunction · 0.92
NextMethod · 0.45
ErrorMethod · 0.45

Tested by 1

TestExtractDomainsFunction · 0.68