send emails
This commit is contained in:
@@ -10,14 +10,15 @@ import (
|
||||
"gitlab.com/etke.cc/go/logger"
|
||||
)
|
||||
|
||||
type backend struct {
|
||||
// msa is mail submission agent
|
||||
type msa struct {
|
||||
log *logger.Logger
|
||||
domain string
|
||||
client Client
|
||||
}
|
||||
|
||||
func (b *backend) newSession() *session {
|
||||
return &session{
|
||||
func (b *msa) newSession() *msasession {
|
||||
return &msasession{
|
||||
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
|
||||
log: b.log,
|
||||
domain: b.domain,
|
||||
@@ -25,28 +26,30 @@ func (b *backend) newSession() *session {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
||||
func (b *msa) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
||||
return nil, smtp.ErrAuthUnsupported
|
||||
}
|
||||
|
||||
func (b *backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
||||
func (b *msa) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
||||
return b.newSession(), nil
|
||||
}
|
||||
|
||||
func Start(domain, port, loglevel string, maxSize int, client Client) error {
|
||||
log := logger.New("smtp.", loglevel)
|
||||
be := &backend{
|
||||
sender := NewMTA(loglevel)
|
||||
receiver := &msa{
|
||||
log: log,
|
||||
domain: domain,
|
||||
client: client,
|
||||
}
|
||||
s := smtp.NewServer(be)
|
||||
receiver.client.SetMTA(sender)
|
||||
s := smtp.NewServer(receiver)
|
||||
s.Addr = ":" + port
|
||||
s.Domain = domain
|
||||
s.AuthDisabled = true
|
||||
s.ReadTimeout = 10 * time.Second
|
||||
s.WriteTimeout = 10 * time.Second
|
||||
s.MaxMessageBytes = maxSize * 1024 * 1024
|
||||
s.AllowInsecureAuth = true
|
||||
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
|
||||
s.Debug = os.Stdout
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
type session struct {
|
||||
type msasession struct {
|
||||
log *logger.Logger
|
||||
domain string
|
||||
client Client
|
||||
@@ -22,14 +22,14 @@ type session struct {
|
||||
from string
|
||||
}
|
||||
|
||||
func (s *session) Mail(from string, opts smtp.MailOptions) error {
|
||||
func (s *msasession) Mail(from string, opts smtp.MailOptions) error {
|
||||
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
|
||||
s.from = from
|
||||
s.log.Debug("mail from %s, options: %+v", from, opts)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) Rcpt(to string) error {
|
||||
func (s *msasession) Rcpt(to string) error {
|
||||
sentry.GetHubFromContext(s.ctx).Scope().SetTag("to", to)
|
||||
|
||||
if utils.Hostname(to) != s.domain {
|
||||
@@ -48,7 +48,7 @@ func (s *session) Rcpt(to string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) parseAttachments(parts []*enmime.Part) []*utils.File {
|
||||
func (s *msasession) parseAttachments(parts []*enmime.Part) []*utils.File {
|
||||
files := make([]*utils.File, 0, len(parts))
|
||||
for _, attachment := range parts {
|
||||
for _, err := range attachment.Errors {
|
||||
@@ -61,7 +61,7 @@ func (s *session) parseAttachments(parts []*enmime.Part) []*utils.File {
|
||||
return files
|
||||
}
|
||||
|
||||
func (s *session) Data(r io.Reader) error {
|
||||
func (s *msasession) Data(r io.Reader) error {
|
||||
parser := enmime.NewParser()
|
||||
eml, err := parser.ReadEnvelope(r)
|
||||
if err != nil {
|
||||
@@ -84,11 +84,11 @@ func (s *session) Data(r io.Reader) error {
|
||||
eml.HTML,
|
||||
files)
|
||||
|
||||
return s.client.Send(s.ctx, email)
|
||||
return s.client.Send2Matrix(s.ctx, email)
|
||||
}
|
||||
|
||||
func (s *session) Reset() {}
|
||||
func (s *msasession) Reset() {}
|
||||
|
||||
func (s *session) Logout() error {
|
||||
func (s *msasession) Logout() error {
|
||||
return nil
|
||||
}
|
||||
116
smtp/mta.go
Normal file
116
smtp/mta.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/etke.cc/go/logger"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
// Client interface to send emails into matrix
|
||||
type Client interface {
|
||||
GetMapping(string) (id.RoomID, bool)
|
||||
Send2Matrix(ctx context.Context, email *utils.Email) error
|
||||
SetMTA(mta utils.MTA)
|
||||
}
|
||||
|
||||
// mta is Mail Transfer Agent
|
||||
type mta struct {
|
||||
log *logger.Logger
|
||||
}
|
||||
|
||||
func NewMTA(loglevel string) utils.MTA {
|
||||
return &mta{
|
||||
log: logger.New("smtp/mta.", loglevel),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mta) Send(from, to, data string) error {
|
||||
m.log.Debug("Sending email from %s to %s", from, to)
|
||||
conn, err := m.connect(from, to)
|
||||
if err != nil {
|
||||
m.log.Error("cannot connect to SMTP server of %s: %v", to, err)
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
err = conn.Mail(from)
|
||||
if err != nil {
|
||||
m.log.Error("cannot call MAIL command: %v", err)
|
||||
return err
|
||||
}
|
||||
err = conn.Rcpt(to)
|
||||
if err != nil {
|
||||
m.log.Error("cannot send RCPT command: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var w io.WriteCloser
|
||||
w, err = conn.Data()
|
||||
if err != nil {
|
||||
m.log.Error("cannot send DATA command: %v", err)
|
||||
return err
|
||||
}
|
||||
defer w.Close()
|
||||
m.log.Debug("sending DATA:\n%s", data)
|
||||
_, err = strings.NewReader(data).WriteTo(w)
|
||||
if err != nil {
|
||||
m.log.Debug("cannot write DATA: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log.Debug("email has been sent")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mta) tryServer(localname, mxhost string) *smtp.Client {
|
||||
m.log.Debug("trying SMTP connection to %s", mxhost)
|
||||
conn, err := smtp.Dial(mxhost + ":smtp")
|
||||
if err != nil {
|
||||
m.log.Warn("cannot connect to the %s: %v", mxhost, err)
|
||||
return nil
|
||||
}
|
||||
err = conn.Hello(localname)
|
||||
if err != nil {
|
||||
m.log.Warn("cannot call HELLO command of the %s: %v", mxhost, err)
|
||||
return nil
|
||||
}
|
||||
if ok, _ := conn.Extension("STARTTLS"); ok {
|
||||
m.log.Debug("%s supports STARTTLS", mxhost)
|
||||
config := &tls.Config{ServerName: mxhost}
|
||||
err = conn.StartTLS(config)
|
||||
if err != nil {
|
||||
m.log.Warn("STARTTLS connection to the %s failed: %v", mxhost, err)
|
||||
}
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
func (m *mta) connect(from, to string) (*smtp.Client, error) {
|
||||
localname := strings.SplitN(from, "@", 2)[1]
|
||||
hostname := strings.SplitN(to, "@", 2)[1]
|
||||
|
||||
m.log.Debug("performing MX lookup of %s", hostname)
|
||||
mxs, err := net.LookupMX(hostname)
|
||||
if err != nil {
|
||||
m.log.Error("cannot perform MX lookup: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, mx := range mxs {
|
||||
client := m.tryServer(localname, mx.Host)
|
||||
if client != nil {
|
||||
return client, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("target SMTP server not found")
|
||||
}
|
||||
15
smtp/smtp.go
15
smtp/smtp.go
@@ -1,15 +0,0 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
// Client interface to send emails
|
||||
type Client interface {
|
||||
GetMapping(string) (id.RoomID, bool)
|
||||
Send(ctx context.Context, email *utils.Email) error
|
||||
}
|
||||
Reference in New Issue
Block a user