add alias decision logic

This commit is contained in:
Adora Laura Kalb 2024-07-25 21:45:29 +02:00
parent c7f6f45086
commit b3273545b5
Signed by: adoralaura
SSH key fingerprint: SHA256:3XrkbR8ikAZJVtYfaUliX1MhmJYVAe/ocIb/MiDHBJ8
3 changed files with 37 additions and 22 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}