diodemail/smtp/server.go

70 lines
1.6 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"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type PlainListener struct {
listener net.Listener
}
func handle(connection Connection) {
log.Info().Msgf("New connection %v", connection.RemoteAddr())
defer connection.Close()
err := connection.Chain()
if err != nil {
log.Error().Msgf("Failed to serve %v: %v", connection.RemoteAddr(), err)
} else {
log.Info().Msgf("Successfully served %v", 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(NewConnection(connection))
}
}
func (self PlainListener) Close() error {
return self.listener.Close()
}