103 lines
2.2 KiB
Go
103 lines
2.2 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 (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Command struct {
|
|
name string
|
|
Exec func(Connection)([]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()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
command_found := false
|
|
for _, command := range commands {
|
|
if command.Check(message) {
|
|
commands, err = command.Exec(self)
|
|
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 nil, err
|
|
}
|
|
|
|
return []Command{
|
|
Command{ "HELO", Hello },
|
|
Command{ "EHLO", Hello },
|
|
}, nil
|
|
}
|
|
|
|
/* --- HELO/EHLO RESPONSE --- */
|
|
|
|
func Hello(connection Connection) ([]Command, error) {
|
|
err := connection.Write(
|
|
fmt.Sprintf("250 %v is shy", connection.connection.LocalAddr()) + "\n",
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return []Command{}, nil
|
|
}
|