zangtumb/backend.go

79 lines
1.5 KiB
Go
Raw Normal View History

2020-10-08 22:36:17 +02:00
package main
import (
"bufio"
"log"
"os"
"strings"
2021-07-30 23:20:49 +02:00
"sync"
2020-10-08 22:36:17 +02:00
)
// The Backend implements SMTP server methods.
type Backend struct {
MailBaseFolder string
ValidRecipientsFile string
ValidRecipientsMap map[string]struct{}
MaxRecipients int
}
var SmtpBackend *Backend
2021-07-30 23:20:49 +02:00
var mutex = &sync.Mutex{}
2020-10-08 22:36:17 +02:00
// Load Valid Recipients
func (bkd *Backend) LoadValidRecipients() error {
bkd.MaxRecipients = 0
var void struct{}
file, err := os.Open(bkd.ValidRecipientsFile)
if err != nil {
log.Println(err)
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Trim(scanner.Text(), " ")
bkd.ValidRecipientsMap[s] = void
log.Printf("RCPT: <%s>", s)
bkd.MaxRecipients++
}
return nil
}
2021-07-30 23:20:49 +02:00
// Checks if Mail is a valid local recipient
2020-10-08 22:36:17 +02:00
func (bkd *Backend) CheckValidRcpt(to string) bool {
2021-07-30 23:20:49 +02:00
defer mutex.Unlock()
mutex.Lock()
2020-10-08 22:36:17 +02:00
_, isValid := bkd.ValidRecipientsMap[to]
return isValid
}
func init() {
SmtpBackend = new(Backend)
SmtpBackend.ValidRecipientsMap = make(map[string]struct{})
SmtpBackend.ValidRecipientsFile = os.Getenv("RECIPIENTS")
if SmtpBackend.ValidRecipientsFile == "" {
SmtpBackend.ValidRecipientsFile = "./recipients.conf"
}
SmtpBackend.MailBaseFolder = os.Getenv("MAILFOLDER")
if SmtpBackend.MailBaseFolder == "" {
SmtpBackend.MailBaseFolder = "./mail"
}
if err := SmtpBackend.LoadValidRecipients(); err != nil {
log.Println("Failed to load Recipients", err)
} else {
log.Println("Recipients Loaded")
}
}