diodemail/smtp/connection.go

143 lines
3.3 KiB
Go
Raw Normal View History

/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
* This file is part of SSHare.
*
* SSHare is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* SSHare. If not, see <https://www.gnu.org/licenses/>.
*/
package smtp
import (
"net"
2024-09-29 21:51:03 +00:00
"fmt"
"strings"
"github.com/rs/zerolog/log"
)
2024-09-29 21:51:03 +00:00
type SMTPSession struct {
connection net.Conn
ReversePathBuffer *string
2024-09-29 18:24:06 +00:00
ForwardPathBuffer []string
MailBuffer *string
}
2024-09-29 21:51:03 +00:00
func MakeSMTPSession(connection net.Conn) SMTPSession {
return SMTPSession{
connection,
nil,
2024-09-29 18:24:06 +00:00
[]string{},
nil,
}
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) Run() error {
err := Greet(self)
if err != nil {
return err
}
COMMANDS := []Command{
Command{ "HELO", Helo },
Command{ "EHLO", Ehlo },
Command{ "MAIL FROM:", MailFrom },
Command{ "RCPT TO:", RcptTo },
Command{ "DATA", Data },
Command{ "QUIT", Quit },
Command{ "NOOP", Noop },
Command{ "RSET", Rset },
}
quit := false
for !quit {
message, err := self.Read("\n")
if err != nil {
2024-09-29 21:51:03 +00:00
return err
}
command_found := false
for _, command := range COMMANDS {
if command.Check(message) {
command_found = true
quit, err = command.Exec(self, message[len(command.Name()):])
if err != nil {
return err
}
break
}
}
if !command_found {
self.Write("500 Unrecognized Command\n")
}
}
2024-09-29 21:51:03 +00:00
return nil
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) TraceSession(direction string, message string) {
lines := strings.Split(message, "\n")
for index, line := range lines {
if index != len(lines) - 1 || line != "" {
if index == 0 {
2024-09-29 21:51:03 +00:00
log.Trace().Msgf("%v %v %v", self.connection.RemoteAddr(), direction, line)
} else {
2024-09-29 21:51:03 +00:00
log.Trace().Msgf("%v %v", self.connection.RemoteAddr(), line)
}
}
}
}
2024-09-29 21:51:03 +00:00
var buffer = [1024]byte{}
func (self SMTPSession) Read(delimiter string) (string, error) {
var message string
for !strings.Contains(message, delimiter) {
num_read, err := self.connection.Read(buffer[:])
if err != nil {
return "", err
}
2024-09-29 21:51:03 +00:00
message += string(buffer[:num_read])
}
2024-09-29 21:51:03 +00:00
self.TraceSession(" ->", message)
return message[:len(message) - len(delimiter)], nil
}
func (self SMTPSession) Write(message string) error {
self.TraceSession("<- ", message)
_, err := self.connection.Write([]byte(message))
return err
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) GetAddress() string {
return fmt.Sprint(self.connection.LocalAddr())
2024-09-29 18:24:06 +00:00
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) MailFrom(from string) {
self.Reset()
self.ReversePathBuffer = &from
}
func (self SMTPSession) AddRecipient(recipient string) {
self.ForwardPathBuffer = append(self.ForwardPathBuffer, recipient)
2024-09-29 18:24:06 +00:00
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) SetMailBuffer(data string) {
self.MailBuffer = &data
}
2024-09-29 21:51:03 +00:00
func (self SMTPSession) Reset() {
self.ReversePathBuffer = nil
self.ForwardPathBuffer = []string{}
self.MailBuffer = nil
}