aboutsummaryrefslogtreecommitdiff
path: root/utils/smtp/session.go
blob: fe1f5c4e2e3c8aad569dfc1487f431e159331f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package smtp

import (
	"dove/config"
	"dove/messages"
	"dove/services"
	"dove/utils/errors"
	"dove/utils/logger"
	"io"

	"github.com/emersion/go-smtp"
)

func (self *session) AuthPlain(username string, password string) error {
	if !config.SMTP.AuthRequired {
		return nil
	}

	if username != config.SMTP.Username || password != config.SMTP.Password {
		logger.Warnf(LOG_PREFIX, messages.SMTPAuthFailed, username)
		return errors.Error(messages.SMTPInvalidCredentials)
	}

	return nil
}

func (self *session) Mail(senderAddress string, _ *smtp.MailOptions) error {
	logger.Debugf(LOG_PREFIX, messages.SMTPMailFrom, senderAddress)
	self.fromAddress = senderAddress
	return nil
}

func (self *session) Rcpt(recipientAddress string, _ *smtp.RcptOptions) error {
	logger.Debugf(LOG_PREFIX, messages.SMTPRecipient, recipientAddress)
	self.toAddresses = append(self.toAddresses, recipientAddress)
	return nil
}

func (self *session) Data(messageReader io.Reader) error {
	rawMessage, readError := io.ReadAll(messageReader)
	if readError != nil {
		return readError
	}

	logger.Infof(LOG_PREFIX, messages.SMTPMessageReceived, len(rawMessage))

	if processError := services.ProcessEmail(rawMessage, self.toAddresses); processError != nil {
		logger.Errorf(LOG_PREFIX, messages.SMTPMessageStoreFailed, processError)
		return processError
	}

	return nil
}

func (self *session) Reset() {
	self.fromAddress = ""
	self.toAddresses = nil
}

func (self *session) Logout() error {
	return nil
}