1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package table2021import (22 "context"2324 "github.com/foxcpp/maddy/framework/config"25 modconfig "github.com/foxcpp/maddy/framework/config/module"26 "github.com/foxcpp/maddy/framework/module"27)2829type Chain struct {30 modName string31 instName string3233 chain []module.Table34 optional []bool35}3637func NewChain(modName, instName string, _, _ []string) (module.Module, error) {38 return &Chain{39 modName: modName,40 instName: instName,41 }, nil42}4344func (s *Chain) Init(cfg *config.Map) error {45 cfg.Callback("step", func(m *config.Map, node config.Node) error {46 var tbl module.Table47 err := modconfig.ModuleFromNode("table", node.Args, node, m.Globals, &tbl)48 if err != nil {49 return err50 }5152 s.chain = append(s.chain, tbl)53 s.optional = append(s.optional, false)54 return nil55 })56 cfg.Callback("optional_step", func(m *config.Map, node config.Node) error {57 var tbl module.Table58 err := modconfig.ModuleFromNode("table", node.Args, node, m.Globals, &tbl)59 if err != nil {60 return err61 }6263 s.chain = append(s.chain, tbl)64 s.optional = append(s.optional, true)65 return nil66 })6768 _, err := cfg.Process()69 return err70}7172func (s *Chain) Name() string {73 return s.modName74}7576func (s *Chain) InstanceName() string {77 return s.instName78}7980func (s *Chain) Lookup(ctx context.Context, key string) (string, bool, error) {81 newVal, err := s.LookupMulti(ctx, key)82 if err != nil {83 return "", false, err84 }85 if len(newVal) == 0 {86 return "", false, nil87 }8889 return newVal[0], true, nil90}9192func (s *Chain) LookupMulti(ctx context.Context, key string) ([]string, error) {93 result := []string{key}94STEP:95 for i, step := range s.chain {96 newResult := []string{}97 for _, key = range result {98 if step_multi, ok := step.(module.MultiTable); ok {99 val, err := step_multi.LookupMulti(ctx, key)100 if err != nil {101 return []string{}, err102 }103 if len(val) == 0 {104 if s.optional[i] {105 continue STEP106 }107 return []string{}, nil108 }109 newResult = append(newResult, val...)110 } else {111 val, ok, err := step.Lookup(ctx, key)112 if err != nil {113 return []string{}, err114 }115 if !ok {116 if s.optional[i] {117 continue STEP118 }119 return []string{}, nil120 }121 newResult = append(newResult, val)122 }123 }124 result = newResult125 }126 return result, nil127}128129func init() {130 module.Register("table.chain", NewChain)131}