1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-08-13 20:04:49 +02:00

Feature: Add option to disable auto-tagging for plus-addresses & X-Tags (#323)

This commit is contained in:
Ralph Slooten
2024-06-28 22:35:07 +12:00
parent c7e0455479
commit 0dca8df29c
4 changed files with 57 additions and 14 deletions

View File

@@ -131,6 +131,7 @@ func init() {
rootCmd.Flags().StringVarP(&config.CLITagsArg, "tag", "t", config.CLITagsArg, "Tag new messages matching filters") rootCmd.Flags().StringVarP(&config.CLITagsArg, "tag", "t", config.CLITagsArg, "Tag new messages matching filters")
rootCmd.Flags().StringVar(&config.TagsConfig, "tags-config", config.TagsConfig, "Load tags filters from yaml configuration file") rootCmd.Flags().StringVar(&config.TagsConfig, "tags-config", config.TagsConfig, "Load tags filters from yaml configuration file")
rootCmd.Flags().BoolVar(&tools.TagsTitleCase, "tags-title-case", tools.TagsTitleCase, "TitleCase new tags generated from plus-addresses and X-Tags") rootCmd.Flags().BoolVar(&tools.TagsTitleCase, "tags-title-case", tools.TagsTitleCase, "TitleCase new tags generated from plus-addresses and X-Tags")
rootCmd.Flags().StringVar(&config.TagsDisable, "tags-disable", config.TagsDisable, "Disable auto-tagging, comma separated (eg: plus-addresses,x-tags)")
// Webhook // Webhook
rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages") rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages")
@@ -290,6 +291,7 @@ func initConfigFromEnv() {
config.CLITagsArg = os.Getenv("MP_TAG") config.CLITagsArg = os.Getenv("MP_TAG")
config.TagsConfig = os.Getenv("MP_TAGS_CONFIG") config.TagsConfig = os.Getenv("MP_TAGS_CONFIG")
tools.TagsTitleCase = getEnabledFromEnv("MP_TAGS_TITLE_CASE") tools.TagsTitleCase = getEnabledFromEnv("MP_TAGS_TITLE_CASE")
config.TagsDisable = os.Getenv("MP_TAGS_DISABLE")
// Webhook // Webhook
if len(os.Getenv("MP_WEBHOOK_URL")) > 0 { if len(os.Getenv("MP_WEBHOOK_URL")) > 0 {

View File

@@ -102,6 +102,10 @@ var (
// TagFilters are used to apply tags to new mail // TagFilters are used to apply tags to new mail
TagFilters []autoTag TagFilters []autoTag
// TagsDisable accepts a comma-separated list of tag types to disable
// including x-tags & plus-addresses
TagsDisable string
// SMTPRelayConfigFile to parse a yaml file and store config of relay SMTP server // SMTPRelayConfigFile to parse a yaml file and store config of relay SMTP server
SMTPRelayConfigFile string SMTPRelayConfigFile string
@@ -390,7 +394,7 @@ func VerifyConfig() error {
} }
} }
// load tag filters // load tag filters & options
TagFilters = []autoTag{} TagFilters = []autoTag{}
if err := loadTagsFromArgs(CLITagsArg); err != nil { if err := loadTagsFromArgs(CLITagsArg); err != nil {
return err return err
@@ -398,6 +402,9 @@ func VerifyConfig() error {
if err := loadTagsFromConfig(TagsConfig); err != nil { if err := loadTagsFromConfig(TagsConfig); err != nil {
return err return err
} }
if err := parseTagsDisable(TagsDisable); err != nil {
return err
}
if SMTPAllowedRecipients != "" { if SMTPAllowedRecipients != "" {
restrictRegexp, err := regexp.Compile(SMTPAllowedRecipients) restrictRegexp, err := regexp.Compile(SMTPAllowedRecipients)

View File

@@ -11,6 +11,14 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var (
// TagsDisablePlus disables message tagging using plus-addresses (user+tag@example.com) - set via verifyConfig()
TagsDisablePlus bool
// TagsDisableXTags disables message tagging via the X-Tags header - set via verifyConfig()
TagsDisableXTags bool
)
type yamlTags struct { type yamlTags struct {
Filters []yamlTag `yaml:"filters"` Filters []yamlTag `yaml:"filters"`
} }
@@ -79,3 +87,25 @@ func loadTagsFromArgs(c string) error {
return nil return nil
} }
func parseTagsDisable(s string) error {
s = strings.TrimSpace(s)
if s == "" {
return nil
}
parts := strings.Split(strings.ToLower(s), ",")
for _, p := range parts {
switch strings.TrimSpace(p) {
case "x-tags", "xtags":
TagsDisableXTags = true
case "plus-addresses", "plus-addressing":
TagsDisablePlus = true
default:
return fmt.Errorf("[tags] invalid --tags-disable option: %s", p)
}
}
return nil
}

View File

@@ -112,20 +112,24 @@ func Store(body *[]byte) (string, error) {
return "", err return "", err
} }
// extract tags from body matches // extract tags using pre-set tag filters, empty slice if not set
rawTags := findTagsInRawMessage(body) tags := findTagsInRawMessage(body)
// extract plus addresses tags from enmime.Envelope
plusTags := obj.tagsFromPlusAddresses()
// extract tags from X-Tags header
xTags := tools.SetTagCasing(strings.Split(strings.TrimSpace(env.Root.Header.Get("X-Tags")), ","))
// extract tags from search matches
searchTags := tagFilterMatches(id)
// combine all tags into one slice if !config.TagsDisableXTags {
tags := append(rawTags, plusTags...) xTagsHdr := env.Root.Header.Get("X-Tags")
tags = append(tags, xTags...) if xTagsHdr != "" {
// sort and extract only unique tags // extract tags from X-Tags header
tags = sortedUniqueTags(append(tags, searchTags...)) tags = append(tags, tools.SetTagCasing(strings.Split(strings.TrimSpace(xTagsHdr), ","))...)
}
}
if !config.TagsDisablePlus {
// get tags from plus-addresses
tags = append(tags, obj.tagsFromPlusAddresses()...)
}
// extract tags from search matches, and sort and extract unique tags
tags = sortedUniqueTags(append(tags, tagFilterMatches(id)...))
if len(tags) > 0 { if len(tags) > 0 {
if err := SetMessageTags(id, tags); err != nil { if err := SetMessageTags(id, tags); err != nil {