| 1 |
package lib |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"github.com/emersion/go-message/mail" |
| 7 |
) |
| 8 |
|
| 9 |
// LimitHeaders returns a new Header with the specified headers included or |
| 10 |
// excluded |
| 11 |
func LimitHeaders(hdr *mail.Header, fields []string, exclude bool) *mail.Header { |
| 12 |
fieldMap := make(map[string]struct{}, len(fields)) |
| 13 |
for _, f := range fields { |
| 14 |
fieldMap[strings.ToLower(f)] = struct{}{} |
| 15 |
} |
| 16 |
nh := &mail.Header{} |
| 17 |
curFields := hdr.Fields() |
| 18 |
for curFields.Next() { |
| 19 |
key := strings.ToLower(curFields.Key()) |
| 20 |
_, present := fieldMap[key] |
| 21 |
// XOR exclude and present. When they are equal, it means we |
| 22 |
// should not add the header to the new header struct |
| 23 |
if exclude == present { |
| 24 |
continue |
| 25 |
} |
| 26 |
nh.Add(key, curFields.Value()) |
| 27 |
} |
| 28 |
return nh |
| 29 |
} |