Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/vim/syntax/maddy-conf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ syn keyword maddyModDir
\ enforce_testing
\ entry
\ error_resp_action
\ expand_replaceholders
\ expand_placeholders
\ fail_action
\ fail_open
\ file
Expand Down
36 changes: 34 additions & 2 deletions internal/table/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -38,26 +39,57 @@ 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
}

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 {
Expand Down
89 changes: 89 additions & 0 deletions internal/table/regexp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, 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 <https://www.gnu.org/licenses/>.
*/

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)
}
}
}
Loading