happyDomain/services/spf.go

68 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-12-24 10:18:08 +00:00
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
2020-09-23 19:57:51 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
2020-09-23 19:57:51 +00:00
//
2023-12-24 10:18:08 +00:00
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-09-23 19:57:51 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2020-09-23 19:57:51 +00:00
//
2023-12-24 10:18:08 +00:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-09-23 19:57:51 +00:00
package svcs
2021-01-22 14:52:29 +00:00
import (
2023-12-11 17:53:17 +00:00
"fmt"
2021-01-22 14:52:29 +00:00
"strings"
2023-12-11 17:53:17 +00:00
"github.com/StackExchange/dnscontrol/v4/pkg/spflib"
2021-01-22 14:52:29 +00:00
)
2020-09-23 19:57:51 +00:00
2023-12-11 17:53:17 +00:00
type SPF struct {
Version uint `json:"version" happydomain:"label=Version,placeholder=1,required,description=The version of SPF to use.,default=1,hidden"`
Directives []string `json:"directives" happydomain:"label=Directives,placeholder=ip4:203.0.113.12"`
2020-09-23 19:57:51 +00:00
}
2023-12-11 17:53:17 +00:00
func (t *SPF) Analyze(txt string) error {
_, err := spflib.Parse(txt, nil)
if err != nil {
return err
}
2020-09-23 19:57:51 +00:00
2023-12-11 17:53:17 +00:00
t.Version = 1
2020-09-23 19:57:51 +00:00
2023-12-11 17:53:17 +00:00
fields := strings.Fields(txt)
2020-09-23 19:57:51 +00:00
2023-12-11 17:53:17 +00:00
// Avoid doublon
for _, directive := range fields[1:] {
exists := false
for _, known := range t.Directives {
if known == directive {
exists = true
break
}
}
if !exists {
t.Directives = append(t.Directives, directive)
}
}
return nil
2020-09-23 19:57:51 +00:00
}
2020-10-10 16:44:17 +00:00
func (t *SPF) String() string {
2023-12-11 17:53:17 +00:00
directives := append([]string{fmt.Sprintf("v=spf%d", t.Version)}, t.Directives...)
return strings.Join(directives, " ")
2020-09-23 19:57:51 +00:00
}