From b3273545b55621ace932c39265a969d61b3ad53a Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Thu, 25 Jul 2024 21:45:29 +0200 Subject: [PATCH] add alias decision logic --- cmd/mailcow-admin-aliases/main.go | 28 ++++++++++++++-------------- internal/mailcow/aliases.go | 21 +++++++++++++-------- internal/misc/misc.go | 10 ++++++++++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/cmd/mailcow-admin-aliases/main.go b/cmd/mailcow-admin-aliases/main.go index abd8662..61d2c2c 100644 --- a/cmd/mailcow-admin-aliases/main.go +++ b/cmd/mailcow-admin-aliases/main.go @@ -13,6 +13,7 @@ import ( "code.lila.network/adoralaura/mailcow-admin-aliases/internal/configuration" "code.lila.network/adoralaura/mailcow-admin-aliases/internal/logging" "code.lila.network/adoralaura/mailcow-admin-aliases/internal/mailcow" + "code.lila.network/adoralaura/mailcow-admin-aliases/internal/misc" "github.com/spf13/pflag" ) @@ -39,26 +40,25 @@ func main() { os.Exit(1) } - for _, domain := range domains { - for _, prefix := range cfg.MailPrefixes { - address := prefix + "@" + domain.DomainName - if address != cfg.AdminEmail { - wantedAliases = append(wantedAliases, address) - } - } - } - - aliases, err := mailcow.LoadAliases(cfg) + existingAliases, existingAliasesSlice, err := mailcow.LoadAliases(cfg) if err != nil { slog.Error("failed to get aliases", "error", err.Error()) os.Exit(1) } - slog.Info(fmt.Sprintf("Found %v domains, %v aliases.", len(domains), len(aliases))) - - for _, alias := range aliases { - for _, wantedPrefix := range cfg.MailPrefixes { + slog.Info(fmt.Sprintf("Found %v domains, %v aliases.", len(domains), len(existingAliases))) + for _, domain := range domains { + for _, prefix := range cfg.MailPrefixes { + address := prefix + "@" + domain.DomainName + if address != cfg.AdminEmail && !misc.StringIsInStringSlice(existingAliasesSlice, address) { + slog.Info(fmt.Sprintf("%v: Adding to wanted alias list", address)) + wantedAliases = append(wantedAliases, address) + } else { + slog.Info(fmt.Sprintf("%v: Ignoring alias", address)) + } } } + // TODO: create aliases + } diff --git a/internal/mailcow/aliases.go b/internal/mailcow/aliases.go index 1be7cc3..8608de7 100644 --- a/internal/mailcow/aliases.go +++ b/internal/mailcow/aliases.go @@ -36,8 +36,9 @@ func NewAlias(alias string, destination string) MailcowAlias { return a } -func LoadAliases(cfg configuration.Config) ([]MailcowAlias, error) { - var domains []MailcowAlias +func LoadAliases(cfg configuration.Config) ([]MailcowAlias, []string, error) { + var aliases []MailcowAlias + var aliasSlice []string url := cfg.ApiEndpoint + configuration.AllAliasesApiEndpoint method := "GET" @@ -48,14 +49,14 @@ func LoadAliases(cfg configuration.Config) ([]MailcowAlias, error) { slog.Debug("alias request", "method", method, "url", url) if err != nil { - return []MailcowAlias{}, fmt.Errorf("failed to create alias http request: %w", err) + return []MailcowAlias{}, []string{}, fmt.Errorf("failed to create alias http request: %w", err) } req.Header.Add("accept", "application/json") req.Header.Add("X-API-Key", cfg.ApiKey) res, err := client.Do(req) if err != nil { - return []MailcowAlias{}, fmt.Errorf("failed to request aliases from server: %w", err) + return []MailcowAlias{}, []string{}, fmt.Errorf("failed to request aliases from server: %w", err) } defer res.Body.Close() @@ -63,13 +64,17 @@ func LoadAliases(cfg configuration.Config) ([]MailcowAlias, error) { body, err := io.ReadAll(res.Body) if err != nil { - return []MailcowAlias{}, fmt.Errorf("failed to read alias request body: %w", err) + return []MailcowAlias{}, []string{}, fmt.Errorf("failed to read alias request body: %w", err) } - err = json.Unmarshal(body, &domains) + err = json.Unmarshal(body, &aliases) if err != nil { - return []MailcowAlias{}, fmt.Errorf("failed to unmarshal alias request body: %w", err) + return []MailcowAlias{}, []string{}, fmt.Errorf("failed to unmarshal alias request body: %w", err) } - return domains, nil + for _, alias := range aliases { + aliasSlice = append(aliasSlice, alias.Address) + } + + return aliases, aliasSlice, nil } diff --git a/internal/misc/misc.go b/internal/misc/misc.go index 55d1195..ffba75c 100644 --- a/internal/misc/misc.go +++ b/internal/misc/misc.go @@ -9,3 +9,13 @@ func StringIsInStringSlice(slice []string, s string) bool { return false } + +func RemoveStringFromStringSlice(slice []string, s string) []string { + a := []string{} + for _, item := range slice { + if item != s { + a = append(a, item) + } + } + return a +}