-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_entry.go
96 lines (84 loc) · 1.88 KB
/
header_entry.go
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
package gomail
import (
"fmt"
"strings"
)
type HeaderEntry struct {
Key string
Value []string
}
// NewHeaderEntry generates a new HeaderEntry after format validation
//
// Validation is based on RFC-5322: https://tools.ietf.org/html/rfc5322
//
// fieldName - The name of the field (like Date/From/Subject)
// value - The value for the field (use commas to signal multiple values)
func NewHeaderEntry(fieldName, value string) (HeaderEntry, error) {
// ensure it's a valid field
if !validField(fieldName) {
return HeaderEntry{}, fmt.Errorf("invalid field name %s", fieldName)
}
// ensure we're not setting multiple values when only one value can be used
splitValue := strings.Split(value, ",")
if !multipleFieldsAllowed(fieldName) && len(splitValue) > 1 {
return HeaderEntry{}, fmt.Errorf("only one value allowed for field %s (%d values given)", fieldName, len(splitValue))
}
return HeaderEntry{Key: fieldName, Value: []string{value}}, nil
}
func validField(fieldName string) bool {
return existsInStringSlice(validFields(), fieldName)
}
func multipleFieldsAllowed(fieldName string) bool {
return !existsInStringSlice(singleValueOnly(), fieldName)
}
func validFields() []string {
return []string{
"Date",
"From",
"Sender",
"Reply-To",
"To",
"Cc",
"Bcc",
"Message-ID",
"In-Reply-To",
"References",
"Subject",
"Comments",
"Keywords",
"Resent-Date",
"Resent-From",
"Resent-Sender",
"Resent-To",
"Resent-Cc",
"Resent-Bcc",
"Resent-Message-ID",
"Return-Path",
"Received",
}
}
func singleValueOnly() []string {
return []string{
"Date",
"From",
"Sender",
"Message-ID",
"In-Reply-To",
"References",
"Subject",
}
}
func requiredFields() []string {
return []string{
"Date",
"From",
}
}
func existsInStringSlice(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}