diff --git a/dist/vim/syntax/maddy-conf.vim b/dist/vim/syntax/maddy-conf.vim index 9e56cdd8..a97984b6 100644 --- a/dist/vim/syntax/maddy-conf.vim +++ b/dist/vim/syntax/maddy-conf.vim @@ -134,7 +134,7 @@ syn keyword maddyModDir \ enforce_testing \ entry \ error_resp_action - \ expand_replaceholders + \ expand_placeholders \ fail_action \ fail_open \ file diff --git a/internal/table/regexp.go b/internal/table/regexp.go index 1cf214a9..b1709b38 100644 --- a/internal/table/regexp.go +++ b/internal/table/regexp.go @@ -26,6 +26,7 @@ import ( "github.com/foxcpp/maddy/framework/config" "github.com/foxcpp/maddy/framework/container" + "github.com/foxcpp/maddy/framework/log" "github.com/foxcpp/maddy/framework/module" "github.com/foxcpp/maddy/framework/module/modules" ) @@ -38,12 +39,15 @@ type Regexp struct { replacements []string expandPlaceholders bool + + log *log.Logger } -func NewRegexp(_ *container.C, modName, instName string) (module.Module, error) { +func NewRegexp(c *container.C, modName, instName string) (module.Module, error) { return &Regexp{ modName: modName, instName: instName, + log: c.DefaultLogger.Sublogger(modName), }, nil } @@ -51,13 +55,41 @@ func (r *Regexp) Configure(inlineArgs []string, cfg *config.Map) error { var ( fullMatch bool caseInsensitive bool + oldExpand *bool ) cfg.Bool("full_match", false, true, &fullMatch) cfg.Bool("case_insensitive", false, true, &caseInsensitive) - cfg.Bool("expand_replaceholders", false, true, &r.expandPlaceholders) + cfg.Bool("expand_placeholders", false, true, &r.expandPlaceholders) + // Old misspelled name, kept for compatibility. + cfg.Callback("expand_replaceholders", func(_ *config.Map, node config.Node) error { + r.log.Msg("expand_replaceholders is deprecated, use expand_placeholders instead") + if oldExpand != nil { + return config.NodeErr(node, "duplicate directive: %s", node.Name) + } + if len(node.Children) != 0 { + return config.NodeErr(node, "can't declare block here") + } + val := true + switch len(node.Args) { + case 0: + case 1: + b, err := config.ParseBool(node.Args[0]) + if err != nil { + return config.NodeErr(node, "bool argument should be 'yes' or 'no'") + } + val = b + default: + return config.NodeErr(node, "expected exactly 1 argument") + } + oldExpand = &val + return nil + }) if _, err := cfg.Process(); err != nil { return err } + if oldExpand != nil { + r.expandPlaceholders = *oldExpand + } regex := inlineArgs[0] if len(inlineArgs) > 1 { diff --git a/internal/table/regexp_test.go b/internal/table/regexp_test.go new file mode 100644 index 00000000..810066c8 --- /dev/null +++ b/internal/table/regexp_test.go @@ -0,0 +1,89 @@ +/* +Maddy Mail Server - Composable all-in-one email server. +Copyright © 2019-2020 Max Mazurov , Maddy Mail Server contributors + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package table + +import ( + "context" + "testing" + + "github.com/foxcpp/maddy/framework/config" + "github.com/foxcpp/maddy/framework/container" + "github.com/foxcpp/maddy/internal/testutils" +) + +func TestRegexpExpandPlaceholders(t *testing.T) { + test := func(directives []config.Node, expected string) { + t.Helper() + + mod, err := NewRegexp(container.New(), "table.regexp", "") + if err != nil { + t.Fatal(err) + } + r := mod.(*Regexp) + r.log = testutils.Logger(t, "table.regexp") + + cfg := &config.Map{Block: config.Node{Children: directives}} + if err := r.Configure([]string{"(.+)@example.org", "$1@example.com"}, cfg); err != nil { + t.Fatal(err) + } + + val, ok, err := r.Lookup(context.Background(), "user@example.org") + if err != nil { + t.Fatal(err) + } + if !ok { + t.Fatal("no match") + } + if val != expected { + t.Errorf("expected %q, got %q", expected, val) + } + } + + dir := func(name string, args ...string) []config.Node { + return []config.Node{{Name: name, Args: args}} + } + + test(nil, "user@example.com") + test(dir("expand_placeholders"), "user@example.com") + test(dir("expand_placeholders", "no"), "$1@example.com") + test(dir("expand_replaceholders"), "user@example.com") + test(dir("expand_replaceholders", "yes"), "user@example.com") + test(dir("expand_replaceholders", "no"), "$1@example.com") +} + +func TestRegexpExpandPlaceholdersInvalid(t *testing.T) { + for _, directives := range [][]config.Node{ + {{Name: "expand_replaceholders", Args: []string{"maybe"}}}, + {{Name: "expand_replaceholders", Args: []string{"yes", "no"}}}, + {{Name: "expand_replaceholders", Children: []config.Node{{Name: "x"}}}}, + {{Name: "expand_replaceholders", Args: []string{"yes"}}, {Name: "expand_replaceholders", Args: []string{"no"}}}, + } { + mod, err := NewRegexp(container.New(), "table.regexp", "") + if err != nil { + t.Fatal(err) + } + r := mod.(*Regexp) + r.log = testutils.Logger(t, "table.regexp") + + cfg := &config.Map{Block: config.Node{Children: directives}} + if err := r.Configure([]string{"(.+)@example.org", "$1@example.com"}, cfg); err == nil { + t.Errorf("expected error for %+v", directives) + } + } +}