chore(GODT-2799): Move SMTP backend to SMTP service module

This commit is contained in:
Leander Beernaert
2023-07-18 15:19:34 +02:00
parent 8d028966c7
commit bc66841cdc
3 changed files with 27 additions and 20 deletions

View File

@ -50,16 +50,6 @@ func (bridge *Bridge) addIMAPUser(ctx context.Context, user *user.User) error {
return bridge.serverManager.AddIMAPUser(ctx, user)
}
// addSMTPUser connects the given user to gluon.
func (bridge *Bridge) addSMTPUser(ctx context.Context, user *user.User) error {
return bridge.serverManager.AddSMTPAccount(ctx, user.GetSMTPService())
}
// removeSMTPUser connects the given user to gluon.
func (bridge *Bridge) removeSMTPUser(ctx context.Context, user *user.User) error {
return bridge.serverManager.RemoveSMTPAccount(ctx, user.GetSMTPService())
}
// removeIMAPUser disconnects the given user from gluon, optionally also removing its files.
func (bridge *Bridge) removeIMAPUser(ctx context.Context, user *user.User, withData bool) error {
return bridge.serverManager.RemoveIMAPUser(ctx, user, withData)

View File

@ -23,7 +23,8 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/ProtonMail/proton-bridge/v3/internal/logging"
bridgesmtp "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
smtpservice "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
"github.com/ProtonMail/proton-bridge/v3/internal/user"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"github.com/sirupsen/logrus"
@ -33,10 +34,10 @@ func (bridge *Bridge) restartSMTP(ctx context.Context) error {
return bridge.serverManager.RestartSMTP(ctx)
}
func newSMTPServer(bridge *Bridge, accounts *bridgesmtp.Accounts, tlsConfig *tls.Config, logSMTP bool) *smtp.Server {
func newSMTPServer(bridge *Bridge, accounts *smtpservice.Accounts, tlsConfig *tls.Config, logSMTP bool) *smtp.Server {
logrus.WithField("logSMTP", logSMTP).Info("Creating SMTP server")
smtpServer := smtp.NewServer(&smtpBackend{userAgent: &bridgeUserAgentUpdater{Bridge: bridge}, accounts: accounts})
smtpServer := smtp.NewServer(smtpservice.NewBackend(accounts, &bridgeUserAgentUpdater{Bridge: bridge}))
smtpServer.TLSConfig = tlsConfig
smtpServer.Domain = constants.Host
@ -62,3 +63,13 @@ func newSMTPServer(bridge *Bridge, accounts *bridgesmtp.Accounts, tlsConfig *tls
return smtpServer
}
// addSMTPUser connects the given user to the smtp server.
func (bridge *Bridge) addSMTPUser(ctx context.Context, user *user.User) error {
return bridge.serverManager.AddSMTPAccount(ctx, user.GetSMTPService())
}
// removeSMTPUser disconnects the given user from the smtp server.
func (bridge *Bridge) removeSMTPUser(ctx context.Context, user *user.User) error {
return bridge.serverManager.RemoveSMTPAccount(ctx, user.GetSMTPService())
}

View File

@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package bridge
package smtp
import (
"context"
@ -25,19 +25,25 @@ import (
"strings"
"github.com/ProtonMail/proton-bridge/v3/internal/identifier"
smtpservice "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
"github.com/ProtonMail/proton-bridge/v3/internal/useragent"
"github.com/emersion/go-smtp"
"github.com/sirupsen/logrus"
)
type smtpBackend struct {
accounts *smtpservice.Accounts
type Backend struct {
accounts *Accounts
userAgent identifier.UserAgentUpdater
}
func NewBackend(accounts *Accounts, userAgent identifier.UserAgentUpdater) *Backend {
return &Backend{
accounts: accounts,
userAgent: userAgent,
}
}
type smtpSession struct {
accounts *smtpservice.Accounts
accounts *Accounts
userAgent identifier.UserAgentUpdater
userID string
@ -47,14 +53,14 @@ type smtpSession struct {
to []string
}
func (be *smtpBackend) NewSession(*smtp.Conn) (smtp.Session, error) {
func (be *Backend) NewSession(*smtp.Conn) (smtp.Session, error) {
return &smtpSession{accounts: be.accounts, userAgent: be.userAgent}, nil
}
func (s *smtpSession) AuthPlain(username, password string) error {
userID, authID, err := s.accounts.CheckAuth(username, []byte(password))
if err != nil {
if !errors.Is(err, smtpservice.ErrNoSuchUser) {
if !errors.Is(err, ErrNoSuchUser) {
return fmt.Errorf("unknown error")
}
logrus.WithFields(logrus.Fields{