refactor mappings getter
This commit is contained in:
11
bot/bot.go
11
bot/bot.go
@@ -76,8 +76,8 @@ func (b *Bot) Start() error {
|
|||||||
|
|
||||||
// Send email to matrix room
|
// Send email to matrix room
|
||||||
func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files []*utils.File) error {
|
func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files []*utils.File) error {
|
||||||
roomID, ok := b.rooms[utils.Mailbox(to)]
|
roomID, ok := b.GetMapping(ctx, utils.Mailbox(to))
|
||||||
if !ok || roomID == "" {
|
if !ok {
|
||||||
return errors.New("room not found")
|
return errors.New("room not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,15 +117,16 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files []
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetMappings returns mapping of mailbox = room
|
// GetMappings returns mapping of mailbox = room
|
||||||
func (b *Bot) GetMappings(ctx context.Context) (map[string]id.RoomID, error) {
|
func (b *Bot) GetMapping(ctx context.Context, mailbox string) (id.RoomID, bool) {
|
||||||
if len(b.rooms) == 0 {
|
if len(b.rooms) == 0 {
|
||||||
err := b.syncRooms(ctx)
|
err := b.syncRooms(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.rooms, nil
|
roomID, ok := b.rooms[mailbox]
|
||||||
|
return roomID, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the bot
|
// Stop the bot
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (b *Bot) setMailbox(ctx context.Context, evt *event.Event, mailbox string)
|
|||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
mailbox = utils.Mailbox(mailbox)
|
mailbox = utils.Mailbox(mailbox)
|
||||||
existingID, ok := b.rooms[mailbox]
|
existingID, ok := b.GetMapping(ctx, mailbox)
|
||||||
if ok && existingID != "" && existingID != evt.RoomID {
|
if ok && existingID != "" && existingID != evt.RoomID {
|
||||||
content := format.RenderMarkdown("Mailbox "+mailbox+"@"+b.domain+" already taken", true, true)
|
content := format.RenderMarkdown("Mailbox "+mailbox+"@"+b.domain+" already taken", true, true)
|
||||||
content.MsgType = event.MsgNotice
|
content.MsgType = event.MsgNotice
|
||||||
|
|||||||
@@ -30,23 +30,18 @@ func (s *session) Mail(from string, opts smtp.MailOptions) error {
|
|||||||
|
|
||||||
func (s *session) Rcpt(to string) error {
|
func (s *session) Rcpt(to string) error {
|
||||||
sentry.GetHubFromContext(s.ctx).Scope().SetTag("to", to)
|
sentry.GetHubFromContext(s.ctx).Scope().SetTag("to", to)
|
||||||
mappings, err := s.client.GetMappings(s.ctx)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error("cannot get mappings: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.log.Debug("mappings: %v", mappings)
|
|
||||||
_, ok := mappings[utils.Mailbox(to)]
|
|
||||||
if !ok {
|
|
||||||
s.log.Debug("mapping for %s not found", to)
|
|
||||||
return smtp.ErrAuthRequired
|
|
||||||
}
|
|
||||||
|
|
||||||
if utils.Hostname(to) != s.domain {
|
if utils.Hostname(to) != s.domain {
|
||||||
s.log.Debug("wrong domain of %s", to)
|
s.log.Debug("wrong domain of %s", to)
|
||||||
return smtp.ErrAuthRequired
|
return smtp.ErrAuthRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, ok := s.client.GetMapping(s.ctx, utils.Mailbox(to))
|
||||||
|
if !ok {
|
||||||
|
s.log.Debug("mapping for %s not found", to)
|
||||||
|
return smtp.ErrAuthRequired
|
||||||
|
}
|
||||||
|
|
||||||
s.to = to
|
s.to = to
|
||||||
s.log.Debug("mail to %s", to)
|
s.log.Debug("mail to %s", to)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ import (
|
|||||||
|
|
||||||
// Client interface to send emails
|
// Client interface to send emails
|
||||||
type Client interface {
|
type Client interface {
|
||||||
GetMappings(context.Context) (map[string]id.RoomID, error)
|
GetMapping(context.Context, string) (id.RoomID, bool)
|
||||||
Send(ctx context.Context, from, mailbox, subject, body string, files []*utils.File) error
|
Send(ctx context.Context, from, mailbox, subject, body string, files []*utils.File) error
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user