1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-11-06 09:09:14 +02:00

Feature: Set tags via X-Tags message header

@see #119
This commit is contained in:
Ralph Slooten
2023-06-02 14:47:36 +12:00
parent 1b47716f5f
commit d4268b8ae1
6 changed files with 87 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
// Package tools provides various methods for variouws things
// Package tools provides various methods for various things
package tools
import (
@@ -23,7 +23,7 @@ func RemoveMessageHeaders(msg []byte, headers []string) ([]byte, error) {
reBlank := regexp.MustCompile(`^\s+`)
for _, hdr := range headers {
// case-insentitive
// case-insensitive
reHdr := regexp.MustCompile(`(?i)^` + regexp.QuoteMeta(hdr+":"))
// header := []byte(hdr + ":")

25
utils/tools/tags.go Normal file
View File

@@ -0,0 +1,25 @@
package tools
import (
"regexp"
"strings"
)
var (
// Invalid tag characters regex
tagsInvalidChars = regexp.MustCompile(`[^a-zA-Z0-9\-\ \_]`)
// Regex to catch multiple spaces
multiSpaceRe = regexp.MustCompile(`(\s+)`)
)
// CleanTag returns a clean tag, removing whitespace and invalid characters
func CleanTag(s string) string {
s = strings.TrimSpace(
multiSpaceRe.ReplaceAllString(
tagsInvalidChars.ReplaceAllString(s, " "),
" ",
),
)
return s
}