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"23 "fmt"2425 "github.com/foxcpp/maddy/framework/address"26 "github.com/foxcpp/maddy/framework/config"27 "github.com/foxcpp/maddy/framework/log"28 "github.com/foxcpp/maddy/framework/module"29)3031type EmailWithDomain struct {32 modName string33 instName string34 domains []string35 log log.Logger36}3738func NewEmailWithDomain(modName, instName string, _, inlineArgs []string) (module.Module, error) {39 return &EmailWithDomain{40 modName: modName,41 instName: instName,42 domains: inlineArgs,43 log: log.Logger{Name: modName},44 }, nil45}4647func (s *EmailWithDomain) Init(cfg *config.Map) error {48 for _, d := range s.domains {49 if !address.ValidDomain(d) {50 return fmt.Errorf("%s: invalid domain: %s", s.modName, d)51 }52 }53 if len(s.domains) == 0 {54 return fmt.Errorf("%s: at least one domain is required", s.modName)55 }5657 return nil58}5960func (s *EmailWithDomain) Name() string {61 return s.modName62}6364func (s *EmailWithDomain) InstanceName() string {65 return s.modName66}6768func (s *EmailWithDomain) Lookup(ctx context.Context, key string) (string, bool, error) {69 quotedMbox := address.QuoteMbox(key)7071 if len(s.domains) == 0 {72 s.log.Msg("only first domain is used when expanding key", "key", key, "domain", s.domains[0])73 }7475 return quotedMbox + "@" + s.domains[0], true, nil76}7778func (s *EmailWithDomain) LookupMulti(ctx context.Context, key string) ([]string, error) {79 quotedMbox := address.QuoteMbox(key)80 emails := make([]string, len(s.domains))81 for i, domain := range s.domains {82 emails[i] = quotedMbox + "@" + domain83 }84 return emails, nil85}8687func init() {88 module.Register("table.email_with_domain", NewEmailWithDomain)89}