diodemail/smtp/commands.go

191 lines
3.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 (
"fmt"
"strings"
)
type Command struct {
name string
Exec func(*SMTPSession, string)(bool, error)
}
func (self Command) GetName() string {
return self.name
}
func (self Command) Check(message string) bool {
return strings.HasPrefix(message, self.name)
}
func Greet(smtp_session *SMTPSession) error {
err := smtp_session.Write(
fmt.Sprintf(
"220 %v ESMTP diodemail -- Service ready" + "\n",
smtp_session.GetHost(),
),
)
if err != nil {
return err
}
return nil
}
func Helo(smtp_session *SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
fmt.Sprintf(
"250 %v is shy" + "\r\n",
smtp_session.GetHost(),
),
)
if err != nil {
return true, err
}
return false, nil
}
func Ehlo(smtp_session *SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
fmt.Sprintf(
"250-%v is shy" + "\r\n" +
"250-8BITMIME" + "\r\n" +
"250 SMTPUTF8" + "\r\n",
smtp_session.GetHost(),
),
)
if err != nil {
return true, err
}
return false, nil
}
func MailFrom(smtp_session *SMTPSession, message string) (bool, error) {
match := ReversePath.FindStringSubmatch(message)
if match == nil {
err := smtp_session.Write(
"501 Could not parse reverse-path\n",
)
if err != nil {
return true, err
}
} else if len(match) > 1 {
smtp_session.MailFrom(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
return true, err
}
}
return false, nil
}
func RcptTo(smtp_session *SMTPSession, message string) (bool, error) {
match := ForwardPath.FindStringSubmatch(message)
if match == nil {
err := smtp_session.Write(
"501 Could not parse forward-path\n",
)
if err != nil {
return true, err
}
} else {
smtp_session.AddRecipient(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
return true, err
}
}
return false, nil
}
func Data(smtp_session *SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
"354 Start Input\n",
)
if err != nil {
return true, err
}
data, err := smtp_session.Read("\r\n.\r\n")
if err != nil {
smtp_session.Write(
"550 Action not taken\n",
)
return false, err
}
err = smtp_session.SendMail(data)
if err != nil {
smtp_session.Write(
"550 Action not taken\n",
)
return false, err
}
smtp_session.Reset()
err = smtp_session.Write(
"250 OK\n",
)
if err != nil {
return true, err
}
return false, nil
}
func Quit(smtp_session *SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
"221 Goodbye :)\n",
)
if err != nil {
return true, err
}
return true, nil
}
func Noop(smtp_session *SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
return true, err
}
return false, nil
}
func Rset(smtp_session *SMTPSession, message string) (bool, error) {
smtp_session.Reset()
err := smtp_session.Write(
"250 Reset\n",
)
if err != nil {
return true, err
}
return false, nil
}