-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/tag-filters' into develop
- Loading branch information
Showing
11 changed files
with
279 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/axllent/mailpit/internal/logger" | ||
"github.com/axllent/mailpit/internal/tools" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type yamlTags struct { | ||
Filters []yamlTag `yaml:"filters"` | ||
} | ||
|
||
type yamlTag struct { | ||
Match string `yaml:"match"` | ||
Tags string `yaml:"tags"` | ||
} | ||
|
||
// Load tags from a configuration from a file, if set | ||
func loadTagsFromConfig(c string) error { | ||
if c == "" { | ||
return nil // not set, ignore | ||
} | ||
|
||
c = filepath.Clean(c) | ||
|
||
if !isFile(c) { | ||
return fmt.Errorf("[tags] configuration file not found or unreadable: %s", c) | ||
} | ||
|
||
data, err := os.ReadFile(c) | ||
if err != nil { | ||
return fmt.Errorf("[tags] %s", err.Error()) | ||
} | ||
|
||
conf := yamlTags{} | ||
|
||
if err := yaml.Unmarshal(data, &conf); err != nil { | ||
return err | ||
} | ||
|
||
if conf.Filters == nil { | ||
return fmt.Errorf("[tags] missing tag: array in %s", c) | ||
} | ||
|
||
for _, t := range conf.Filters { | ||
tags := strings.Split(t.Tags, ",") | ||
TagFilters = append(TagFilters, autoTag{Match: t.Match, Tags: tags}) | ||
} | ||
|
||
logger.Log().Debugf("[tags] loaded %s from config %s", tools.Plural(len(conf.Filters), "tag filter", "tag filters"), c) | ||
|
||
return nil | ||
} | ||
|
||
func loadTagsFromArgs(c string) error { | ||
if c == "" { | ||
return nil // not set, ignore | ||
} | ||
|
||
args := tools.ArgsParser(c) | ||
|
||
for _, a := range args { | ||
t := strings.Split(a, "=") | ||
if len(t) > 1 { | ||
match := strings.TrimSpace(strings.ToLower(strings.Join(t[1:], "="))) | ||
tags := strings.Split(t[0], ",") | ||
TagFilters = append(TagFilters, autoTag{Match: match, Tags: tags}) | ||
} else { | ||
return fmt.Errorf("[tag] error parsing tags (%s)", a) | ||
} | ||
} | ||
|
||
logger.Log().Debugf("[tags] loaded %s from CLI args", tools.Plural(len(args), "tag filter", "tag filters")) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,8 @@ func InitDB() error { | |
return err | ||
} | ||
|
||
LoadTagFilters() | ||
|
||
dbFile = p | ||
dbLastAction = time.Now() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"strings" | ||
|
||
"github.com/axllent/mailpit/config" | ||
"github.com/axllent/mailpit/internal/logger" | ||
"github.com/axllent/mailpit/internal/tools" | ||
"github.com/leporo/sqlf" | ||
) | ||
|
||
// TagFilter struct | ||
type TagFilter struct { | ||
Match string | ||
SQL *sqlf.Stmt | ||
Tags []string | ||
} | ||
|
||
var tagFilters = []TagFilter{} | ||
|
||
// LoadTagFilters loads tag filters from the config and pre-generates the SQL query | ||
func LoadTagFilters() { | ||
tagFilters = []TagFilter{} | ||
|
||
for _, t := range config.TagFilters { | ||
match := strings.TrimSpace(t.Match) | ||
if match == "" { | ||
logger.Log().Warnf("[tags] ignoring tag item with missing 'match'") | ||
continue | ||
} | ||
if t.Tags == nil || len(t.Tags) == 0 { | ||
logger.Log().Warnf("[tags] ignoring tag items with missing 'tags' array") | ||
continue | ||
} | ||
|
||
validTags := []string{} | ||
for _, tag := range t.Tags { | ||
tagName := tools.CleanTag(tag) | ||
if !config.ValidTagRegexp.MatchString(tagName) || len(tagName) == 0 { | ||
logger.Log().Warnf("[tags] invalid tag (%s) - can only contain spaces, letters, numbers, - & _", tagName) | ||
continue | ||
} | ||
validTags = append(validTags, tagName) | ||
} | ||
|
||
if len(validTags) == 0 { | ||
continue | ||
} | ||
|
||
tagFilters = append(tagFilters, TagFilter{Match: match, Tags: validTags, SQL: searchQueryBuilder(match, "")}) | ||
} | ||
} | ||
|
||
// TagFilterMatches returns a slice of matching tags from a message | ||
func tagFilterMatches(id string) []string { | ||
tags := []string{} | ||
|
||
if len(tagFilters) == 0 { | ||
return tags | ||
} | ||
|
||
for _, f := range tagFilters { | ||
var matchID string | ||
q := f.SQL.Clone().Where("ID = ?", id) | ||
if err := q.QueryAndClose(context.Background(), db, func(row *sql.Rows) { | ||
var ignore sql.NullString | ||
|
||
if err := row.Scan(&ignore, &matchID, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore); err != nil { | ||
logger.Log().Errorf("[db] %s", err.Error()) | ||
return | ||
} | ||
}); err != nil { | ||
logger.Log().Errorf("[db] %s", err.Error()) | ||
return tags | ||
} | ||
if matchID == id { | ||
tags = append(tags, f.Tags...) | ||
} | ||
} | ||
|
||
return tags | ||
} |
Oops, something went wrong.