diodemail/smtp/commands.go

169 lines
3.3 KiB
Go
Raw Normal View History

2024-09-29 21:51:03 +00:00
/* 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) Name() 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(
"220 localhost ESMTPSession diodemail -- Service ready" + "\n",
)
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.GetAddress(),
),
)
if err != nil {
return false, 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",
smtp_session.GetAddress(),
),
)
if err != nil {
return false, err
}
return false, nil
}
func MailFrom(smtp_session SMTPSession, message string) (bool, error) {
match := ReversePath.FindStringSubmatch(message)
if match == nil {
smtp_session.Write(
"501 Could not parse reverse-path",
)
} else if len(match) > 1 {
smtp_session.MailFrom(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
return false, err
}
}
return false, nil
}
func RcptTo(smtp_session SMTPSession, message string) (bool, error) {
match := ForwardPath.FindStringSubmatch(message)
if match == nil {
smtp_session.Write(
"501 Could not parse forward-path",
)
} else {
smtp_session.AddRecipient(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
return false, 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 false, err
}
data, err := smtp_session.Read("\r\n.\r\n")
smtp_session.SetMailBuffer(data)
err = smtp_session.Write(
"250 OK\n",
)
smtp_session.Reset()
if err != nil {
return false, 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 false, err
}
return false, nil
}
func Rset(smtp_session SMTPSession, message string) (bool, error) {
err := smtp_session.Write(
"250 Reset\n",
)
smtp_session.Reset()
if err != nil {
return false, err
}
return false, nil
}