Skip to content

Commit

Permalink
Refactor email domain blocklist config field in importer package.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Apr 9, 2023
1 parent 476d5be commit 98729f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type constants struct {
AllowExport bool `koanf:"allow_export"`
AllowWipe bool `koanf:"allow_wipe"`
Exportable map[string]bool `koanf:"-"`
DomainBlocklist map[string]bool `koanf:"-"`
DomainBlocklist []string `koanf:"-"`
} `koanf:"privacy"`
Security struct {
EnableCaptcha bool `koanf:"enable_captcha"`
Expand Down Expand Up @@ -370,7 +370,7 @@ func initConstants() *constants {
c.Lang = ko.String("app.lang")
c.Privacy.Exportable = maps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
c.MediaProvider = ko.String("upload.provider")
c.Privacy.DomainBlocklist = maps.StringSliceToLookupMap(ko.Strings("privacy.domain_blocklist"))
c.Privacy.DomainBlocklist = ko.Strings("privacy.domain_blocklist")

// Static URLS.
// url.com/subscription/{campaign_uuid}/{subscriber_uuid}
Expand Down
23 changes: 13 additions & 10 deletions internal/subimporter/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sync"

"github.com/gofrs/uuid"
"github.com/knadh/koanf/maps"
"github.com/knadh/listmonk/internal/i18n"
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
Expand Down Expand Up @@ -51,9 +52,10 @@ const (

// Importer represents the bulk CSV subscriber import system.
type Importer struct {
opt Options
db *sql.DB
i18n *i18n.I18n
opt Options
db *sql.DB
i18n *i18n.I18n
domainBlocklist map[string]bool

stop chan bool
status Status
Expand All @@ -68,7 +70,7 @@ type Options struct {
NotifCB models.AdminNotifCallback

// Lookup table for blocklisted domains.
DomainBlocklist map[string]bool
DomainBlocklist []string
}

// Session represents a single import session.
Expand Down Expand Up @@ -130,11 +132,12 @@ var (
// New returns a new instance of Importer.
func New(opt Options, db *sql.DB, i *i18n.I18n) *Importer {
im := Importer{
opt: opt,
db: db,
i18n: i,
status: Status{Status: StatusNone, logBuf: bytes.NewBuffer(nil)},
stop: make(chan bool, 1),
opt: opt,
db: db,
i18n: i,
domainBlocklist: maps.StringSliceToLookupMap(opt.DomainBlocklist),
status: Status{Status: StatusNone, logBuf: bytes.NewBuffer(nil)},
stop: make(chan bool, 1),
}
return &im
}
Expand Down Expand Up @@ -587,7 +590,7 @@ func (im *Importer) SanitizeEmail(email string) (string, error) {
// Check if the e-mail's domain is blocklisted.
d := strings.Split(em.Address, "@")
if len(d) == 2 {
_, ok := im.opt.DomainBlocklist[d[1]]
_, ok := im.domainBlocklist[d[1]]
if ok {
return "", errors.New(im.i18n.T("subscribers.domainBlocklisted"))
}
Expand Down

0 comments on commit 98729f6

Please sign in to comment.