/* 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)([]Command, 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 { commands, err := Greet(self) if err != nil { return err } for { message, err := self.Read("\r\n") if err != nil { return err } command_found := false for _, command := range commands { if command.Check(message) { commands, err = command.Exec(self, message[len(command.Name()):]) if err != nil { return err } command_found = true break } } if !command_found { expected := make([]string, len(commands)) for index, command := range commands { expected[index] = command.Name() } return fmt.Errorf("Expected one of %v, but got: %v", expected, message) } if len(commands) == 0 { break } } return nil } /* --- GREETING RESPONSE --- */ func Greet(connection Connection) ([]Command, error) { err := connection.Write( "220 localhost ESMTP diodemail -- Service ready" + "\n", ) if err != nil { return []Command{}, err } return []Command{ HELO, EHLO, }, nil } /* --- HELO/EHLO RESPONSE --- */ var HELO = Command{ "HELO", Hello } var EHLO = Command{ "EHLO", Hello } func Hello(connection Connection, message string) ([]Command, error) { err := connection.Write( fmt.Sprintf( "250 %v is shy" + "\r\n", connection.connection.LocalAddr(), ), ) if err != nil { return []Command{}, err } return []Command{ MAIL_FROM, }, nil } /* --- MAIL FROM RESPONSE --- */ var MAIL_FROM = Command{ "MAIL FROM:", MailFrom } func MailFrom(connection Connection, message string) ([]Command, error) { match := ReversePath.FindStringSubmatch(message) if match == nil { return []Command{}, fmt.Errorf("Invalid forward-path: %v", message) } else if len(match) > 1 { connection.ReversePathBuffer = &match[1] } err := connection.Write( "250 OK\n", ) if err != nil { return []Command{}, err } return []Command{ RCPT_TO, }, nil } var RCPT_TO = Command{ "RCPT TO:", RcptTo } func RcptTo(connection Connection, message string) ([]Command, error) { match := ForwardPath.FindStringSubmatch(message) if match == nil { return []Command{}, fmt.Errorf("Invalid forward-path: %v", message) } else { connection.ForwardPathBuffer = &match[1] } err := connection.Write( "250 OK\n", ) if err != nil { return []Command{}, err } return []Command{ DATA, }, nil } var DATA = Command { "DATA", Data } func Data(connection Connection, message string) ([]Command, error) { err := connection.Write( "354 Start Input\n", ) if err != nil { return []Command{}, err } data, err := connection.Read("\r\n.\r\n") connection.MailBuffer = &data err = connection.Write( "250 OK\n", ) if err != nil { return nil, err } return []Command{ QUIT, }, nil } var QUIT = Command { "QUIT", Quit } func Quit(connection Connection, message string) ([]Command, error) { err := connection.Write( "221 OK\n", ) if err != nil { return nil, err } return []Command{}, nil }