bugfixes; email parsing; send emails into matrix rooms; e2e tests
This commit is contained in:
@@ -2,6 +2,7 @@ package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-smtp"
|
||||
@@ -46,6 +47,9 @@ func Start(domain, port, loglevel string, client Client) error {
|
||||
s.ReadTimeout = 10 * time.Second
|
||||
s.WriteTimeout = 10 * time.Second
|
||||
s.MaxMessageBytes = 63 * 1024
|
||||
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
|
||||
s.Debug = os.Stdout
|
||||
}
|
||||
|
||||
log.Info("Starting SMTP server on %s:%s", domain, port)
|
||||
return s.ListenAndServe()
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
|
||||
"github.com/emersion/go-smtp"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/jhillyerd/enmime"
|
||||
"gitlab.com/etke.cc/go/logger"
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
type session struct {
|
||||
@@ -33,13 +35,14 @@ func (s *session) Rcpt(to string) error {
|
||||
s.log.Error("cannot get mappings: %v", err)
|
||||
return err
|
||||
}
|
||||
_, ok := mappings[to]
|
||||
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 Domain(to) != s.domain {
|
||||
if utils.Hostname(to) != s.domain {
|
||||
s.log.Debug("wrong domain of %s", to)
|
||||
return smtp.ErrAuthRequired
|
||||
}
|
||||
@@ -50,12 +53,16 @@ func (s *session) Rcpt(to string) error {
|
||||
}
|
||||
|
||||
func (s *session) Data(r io.Reader) error {
|
||||
b, err := io.ReadAll(r)
|
||||
parser := enmime.NewParser()
|
||||
env, err := parser.ReadEnvelope(r)
|
||||
if err != nil {
|
||||
s.log.Error("cannot read data: %v", err)
|
||||
return err
|
||||
}
|
||||
return s.client.Send(s.from, s.to, "", string(b))
|
||||
text := env.Text
|
||||
if env.HTML != "" {
|
||||
text = env.HTML
|
||||
}
|
||||
return s.client.Send(s.from, s.to, env.GetHeader("Subject"), text)
|
||||
}
|
||||
|
||||
func (s *session) Reset() {}
|
||||
|
||||
@@ -9,5 +9,5 @@ import (
|
||||
// Client interface to send emails
|
||||
type Client interface {
|
||||
GetMappings(context.Context) (map[string]id.RoomID, error)
|
||||
Send(from, to, subject, body string) error
|
||||
Send(from, mailbox, subject, body string) error
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package smtp
|
||||
|
||||
import "strings"
|
||||
|
||||
func Domain(email string) string {
|
||||
return email[strings.LastIndex(email, "@")+1:]
|
||||
}
|
||||
Reference in New Issue
Block a user