-
Notifications
You must be signed in to change notification settings - Fork 2
/
ded.go
71 lines (60 loc) · 1.5 KB
/
ded.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ded
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"sync"
)
//ErrEmailMalformed error returns when email is not valid.
var ErrEmailMalformed = errors.New("ded: email malformed")
//IsDisposableEmail checks whether the given email is disposable.
func IsDisposableEmail(email string) (bool, error) {
parts := strings.SplitN(email, "@", 2)
if len(parts) != 2 {
return false, ErrEmailMalformed
}
return IsDisposableDomain(parts[1])
}
//IsDisposableDomain checks whether the given domain is disposable.
func IsDisposableDomain(domain string) (bool, error) {
domains.once.Do(func() { domains.loadFromFile("domains.txt") })
if domains.err != nil {
return false, domains.err
}
domain = strings.TrimSpace(domain)
return domains.has(normalizeString(domain)), nil
}
func normalizeString(str string) string {
return strings.ToLower(strings.TrimSpace(str))
}
var domains = new(collection)
type collection struct {
items map[string]struct{}
err error
once sync.Once
}
func (c *collection) has(item string) bool {
_, ok := c.items[item]
return ok
}
func (c *collection) loadFromFile(filename string) {
f, err := os.Open(filename)
if err != nil {
c.err = fmt.Errorf("ded: could not open %q file: %v", filename, err)
return
}
c.items = make(map[string]struct{})
s := bufio.NewScanner(f)
for s.Scan() {
domain := normalizeString(s.Text())
if domain == "" {
continue
}
c.items[domain] = struct{}{}
}
if err := s.Err(); err != nil {
c.err = fmt.Errorf("ded: scanner error: %v", err)
}
}