/* 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 . */ package smtp import ( "fmt" "strings" ) type Command struct { name string Exec func(Connection, string)(bool, error) } func (self Command) Name() string { return self.name } func (self Command) Check(message string) bool { return strings.HasPrefix(message, self.name) } func (self Connection) Chain() 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 { 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") } } return nil } /* --- GREETING RESPONSE --- */ func Greet(connection Connection) error { err := connection.Write( "220 localhost ESMTP diodemail -- Service ready" + "\n", ) if err != nil { return err } return nil } /* --- HELO/EHLO RESPONSE --- */ func Helo(connection Connection, message string) (bool, error) { err := connection.Write( fmt.Sprintf( "250 %v is shy" + "\r\n", connection.connection.LocalAddr(), ), ) if err != nil { return false, err } return false, nil } func Ehlo(connection Connection, message string) (bool, error) { err := connection.Write( fmt.Sprintf( "250 %v is shy" + "\r\n", connection.connection.LocalAddr(), ), ) if err != nil { return false, err } return false, nil } /* --- MAIL FROM RESPONSE --- */ func MailFrom(connection Connection, message string) (bool, error) { match := ReversePath.FindStringSubmatch(message) if match == nil { connection.Write( "501 Could not parse reverse-path", ) } else if len(match) > 1 { connection.ReversePathBuffer = &match[1] err := connection.Write( "250 OK\n", ) if err != nil { return false, err } } return false, nil } func RcptTo(connection Connection, message string) (bool, error) { match := ForwardPath.FindStringSubmatch(message) if match == nil { connection.Write( "501 Could not parse forward-path", ) } else { connection.AddRecipient(match[1]) err := connection.Write( "250 OK\n", ) if err != nil { return false, err } } return false, nil } func Data(connection Connection, message string) (bool, error) { err := connection.Write( "354 Start Input\n", ) if err != nil { return false, err } data, err := connection.Read("\r\n.\r\n") connection.MailBuffer = &data err = connection.Write( "250 OK\n", ) connection.Reset() if err != nil { return false, err } return false, nil } func Quit(connection Connection, message string) (bool, error) { err := connection.Write( "221 Goodbye :)\n", ) if err != nil { return true, err } return true, nil } func Noop(connection Connection, message string) (bool, error) { err := connection.Write( "250 OK\n", ) if err != nil { return false, err } return false, nil } func Rset(connection Connection, message string) (bool, error) { err := connection.Write( "250 Reset\n", ) connection.Reset() if err != nil { return false, err } return false, nil }