maddy/config.go
fox.cpp ee553a4cc4 Rework logging
Implement debug log (can be enabled using `debug` config directive)
Remove errors directive for IMAP endpoint module.
2019-03-30 17:34:19 +02:00

121 lines
2.9 KiB
Go

package maddy
import (
"errors"
"fmt"
"github.com/emersion/maddy/config"
"github.com/emersion/maddy/module"
)
/*
Config matchers for module interfaces.
*/
func authProvider(modName string) (module.AuthProvider, error) {
modObj := module.GetInstance(modName)
if modObj == nil {
return nil, fmt.Errorf("unknown auth. provider instance: %s", modName)
}
provider, ok := modObj.(module.AuthProvider)
if !ok {
return nil, fmt.Errorf("module %s doesn't implements auth. provider interface", modObj.Name())
}
return provider, nil
}
func authDirective(m *config.Map, node *config.Node) (interface{}, error) {
if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument")
}
if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here")
}
modObj, err := authProvider(node.Args[0])
if err != nil {
return nil, m.MatchErr("%s", err.Error())
}
return modObj, nil
}
func storageBackend(modName string) (module.Storage, error) {
modObj := module.GetInstance(modName)
if modObj == nil {
return nil, fmt.Errorf("unknown storage backend instance: %s", modName)
}
backend, ok := modObj.(module.Storage)
if !ok {
return nil, fmt.Errorf("module %s doesn't implements storage interface", modObj.Name())
}
return backend, nil
}
func storageDirective(m *config.Map, node *config.Node) (interface{}, error) {
if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument")
}
if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here")
}
modObj, err := storageBackend(node.Args[0])
if err != nil {
return nil, m.MatchErr("%s", err.Error())
}
return modObj, nil
}
func deliveryTarget(modName string) (module.DeliveryTarget, error) {
mod := module.GetInstance(modName)
if mod == nil {
return nil, fmt.Errorf("unknown delivery target instance: %s", modName)
}
target, ok := mod.(module.DeliveryTarget)
if !ok {
return nil, fmt.Errorf("module %s doesn't implements delivery target interface", mod.Name())
}
return target, nil
}
func deliveryDirective(m *config.Map, node *config.Node) (interface{}, error) {
if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument")
}
if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here")
}
modObj, err := deliveryTarget(node.Args[0])
if err != nil {
return nil, m.MatchErr("%s", err.Error())
}
return modObj, nil
}
func defaultAuthProvider() (interface{}, error) {
res, err := authProvider("default_auth")
if err != nil {
res, err = authProvider("default")
if err != nil {
return nil, errors.New("missing default auth. provider, must set custom")
}
}
return res, nil
}
func defaultStorage() (interface{}, error) {
res, err := storageBackend("default_storage")
if err != nil {
res, err = storageBackend("default")
if err != nil {
return nil, errors.New("missing default storage backend, must set custom")
}
}
return res, nil
}