diodemail/smtp/server.go
2024-09-29 21:51:03 +00:00

81 lines
1.7 KiB
Go

/* 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"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type PlainListener struct {
listener net.Listener
}
func handle(connection net.Conn) {
log.Info().Msgf(
"New connection %v. Starting session.",
connection.RemoteAddr(),
)
defer connection.Close()
session := MakeSMTPSession(connection)
err := session.Run()
if err != nil {
log.Error().Msgf(
"Session %v exited with error: %v",
connection.RemoteAddr(),
err,
)
} else {
log.Info().Msgf(
"Session %v exited successfully",
connection.RemoteAddr(),
)
}
}
func Run(host string, implicit_tls bool) error {
log.Logger = zerolog.
New(zerolog.ConsoleWriter{Out: os.Stderr}).
With().
Timestamp().
Logger().
Level(zerolog.TraceLevel)
listener, err := net.Listen("tcp", host)
if err != nil {
return err
}
for {
connection, err := listener.Accept()
if err != nil {
return err
}
go handle(connection)
}
}
func (self PlainListener) Close() error {
return self.listener.Close()
}