feat: use confirmer in smtp

This commit is contained in:
James Houlahan
2020-07-23 13:44:22 +02:00
parent c8f118a26b
commit 36ef9f20ae
7 changed files with 83 additions and 36 deletions

View File

@ -24,8 +24,10 @@ import (
"github.com/ProtonMail/proton-bridge/internal/bridge"
"github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/confirmer"
"github.com/ProtonMail/proton-bridge/pkg/listener"
goSMTPBackend "github.com/emersion/go-smtp"
"github.com/sirupsen/logrus"
)
type panicHandler interface {
@ -33,12 +35,12 @@ type panicHandler interface {
}
type smtpBackend struct {
panicHandler panicHandler
eventListener listener.Listener
preferences *config.Preferences
bridge bridger
shouldSendNoEncChannels map[string]chan bool
sendRecorder *sendRecorder
panicHandler panicHandler
eventListener listener.Listener
preferences *config.Preferences
bridge bridger
confirmer *confirmer.Confirmer
sendRecorder *sendRecorder
}
// NewSMTPBackend returns struct implementing go-smtp/backend interface.
@ -58,12 +60,12 @@ func newSMTPBackend(
bridge bridger,
) *smtpBackend {
return &smtpBackend{
panicHandler: panicHandler,
eventListener: eventListener,
preferences: preferences,
bridge: bridge,
shouldSendNoEncChannels: make(map[string]chan bool),
sendRecorder: newSendRecorder(),
panicHandler: panicHandler,
eventListener: eventListener,
preferences: preferences,
bridge: bridge,
confirmer: confirmer.New(),
sendRecorder: newSendRecorder(),
}
}
@ -103,7 +105,7 @@ func (sb *smtpBackend) shouldReportOutgoingNoEnc() bool {
}
func (sb *smtpBackend) ConfirmNoEncryption(messageID string, shouldSend bool) {
if ch, ok := sb.shouldSendNoEncChannels[messageID]; ok {
ch <- shouldSend
if err := sb.confirmer.SetResponse(messageID, shouldSend); err != nil {
logrus.WithError(err).Error("Failed to set confirmation value")
}
}

View File

@ -23,11 +23,9 @@ import (
"encoding/base64"
"fmt"
"io"
"math/rand"
"mime"
"net/mail"
"regexp"
"strconv"
"strings"
"time"
@ -38,6 +36,7 @@ import (
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
goSMTPBackend "github.com/emersion/go-smtp"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type smtpUser struct {
@ -497,28 +496,19 @@ func (su *smtpUser) continueSendingUnencryptedMail(subject string) bool {
return true
}
messageID := strconv.Itoa(rand.Int()) //nolint[gosec]
ch := make(chan bool)
su.backend.shouldSendNoEncChannels[messageID] = ch
su.eventListener.Emit(events.OutgoingNoEncEvent, messageID+":"+subject)
// GUI should always respond in 10 seconds, but let's have safety timeout
// in case GUI will not respond properly. If GUI didn't respond, we cannot
// be sure if user even saw the notice: better to not send the e-mail.
req := su.backend.confirmer.NewRequest(15 * time.Second)
log.Debug("Waiting for sendingUnencrypted confirmation for ", messageID)
su.eventListener.Emit(events.OutgoingNoEncEvent, req.ID()+":"+subject)
var res bool
select {
case res = <-ch:
// GUI should always respond in 10 seconds, but let's have safety timeout
// in case GUI will not respond properly. If GUI didn't respond, we cannot
// be sure if user even saw the notice: better to not send the e-mail.
log.Debug("Got sendingUnencrypted for ", messageID, ": ", res)
case <-time.After(15 * time.Second):
log.Debug("sendingUnencrypted timeout, not sending ", messageID)
res = false
res, err := req.Result()
if err != nil {
logrus.WithError(err).Error("Failed to determine whether to send unencrypted, assuming no")
return false
}
delete(su.backend.shouldSendNoEncChannels, messageID)
close(ch)
return res
}