2012-12-11 04:59:23 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
2012-12-17 21:38:33 +03:00
|
|
|
"log"
|
|
|
|
"os"
|
2012-12-11 04:59:23 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-05-22 04:47:42 +03:00
|
|
|
func NewValidator(domains []string, usersFile string) func(string) bool {
|
2012-12-11 04:59:23 +03:00
|
|
|
validUsers := make(map[string]bool)
|
|
|
|
|
|
|
|
if usersFile != "" {
|
2014-11-10 04:07:02 +02:00
|
|
|
log.Printf("using authenticated emails file %s", usersFile)
|
2012-12-11 04:59:23 +03:00
|
|
|
r, err := os.Open(usersFile)
|
|
|
|
if err != nil {
|
2014-11-10 04:07:02 +02:00
|
|
|
log.Fatalf("failed opening authenticated-emails-file=%q, %s", usersFile, err)
|
2012-12-11 04:59:23 +03:00
|
|
|
}
|
|
|
|
csv_reader := csv.NewReader(r)
|
|
|
|
csv_reader.Comma = ','
|
|
|
|
csv_reader.Comment = '#'
|
|
|
|
csv_reader.TrimLeadingSpace = true
|
|
|
|
records, err := csv_reader.ReadAll()
|
|
|
|
for _, r := range records {
|
|
|
|
validUsers[r[0]] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := func(email string) bool {
|
2014-05-22 04:47:42 +03:00
|
|
|
valid := false
|
|
|
|
for _, domain := range domains {
|
|
|
|
emailSuffix := fmt.Sprintf("@%s", domain)
|
|
|
|
valid = valid || strings.HasSuffix(email, emailSuffix)
|
2012-12-11 04:59:23 +03:00
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
_, valid = validUsers[email]
|
|
|
|
}
|
|
|
|
log.Printf("validating: is %s valid? %v", email, valid)
|
|
|
|
return valid
|
|
|
|
}
|
|
|
|
return validator
|
|
|
|
}
|