smtpServerProtocol

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2023 License: MIT-0 Imports: 10 Imported by: 1

README

SMTP server protocol

RFC spec you can read here

Authentication

Some informative doc can be found here

Usage

protocol := smtpServerProtocol.CreateProtocol(
    hostname,
    ipAddr,
    &smtpServerProtocol.Validation{
        MaximumLineLength: context.Config.Smtp.Validation.MaximumLineLength,
        MaximumReceivers:  context.Config.Smtp.Validation.MaximumReceivers,
    },
)

protocol.OnMessageReceived(func(message *smtpMessage.SmtpMessage) (string, error) {
    messageId, err := savemessage(message)
    
    return messageId, err
})

Development

go mod tidy
go mod verify
go mod vendor
go test --cover

Credits

  • Think Studio

Documentation

Index

Constants

View Source
const (
	CommandHelo = CommandName("HELO")
	CommandEhlo = CommandName("EHLO")
	CommandAuth = CommandName("AUTH")
	CommandMail = CommandName("MAIL")
	CommandRset = CommandName("RSET")
	CommandRcpt = CommandName("RCPT")
	CommandData = CommandName("DATA")
	CommandQuit = CommandName("QUIT")
)

List predefined command names

View Source
const (
	StateCommandsExchange = ConversationState("commands_exchange")
	StateWaitingAuth      = ConversationState("waiting_auth")
	StateData             = ConversationState("data")
	StateCustomScene      = ConversationState("custom_scene")
)
View Source
const (
	CODE_SYSTEM_STATUS             = 211
	CODE_HELP_MESSAGE              = 214
	CODE_SERVICE_READY             = 220
	CODE_SERVICE_CLOSING           = 221
	CODE_AUTHENTICATION_SUCCESS    = 235
	CODE_ACTION_OK                 = 250
	CODE_USER_IS_NOT_LOCAL         = 251 // will forward to <forward-path>
	CODE_USER_NOT_VERIFIED         = 252
	CODE_AUTH_CREDENTIALS          = 334
	CODE_MAIL_DATA                 = 354
	CODE_SERVICE_NOT_AVAILABLE     = 421
	CODE_MAILBOX_UNAVAILABLE       = 450
	CODE_LOCAL_ERROR               = 451
	CODE_EXCEEDED_SYSTEM_STORAGE   = 452
	CODE_COMMAND_SYNTAX_ERROR      = 500
	CODE_PARAMETER_SYNTAX_ERROR    = 501
	CODE_COMMAND_NOT_IMPLEMENTED   = 502
	CODE_COMMANDS_BAD_SEQUENCE     = 503
	CODE_PARAMETER_NOT_IMPLEMENTED = 504
	CODE_AUTH_FAILED               = 535
	CODE_MAILBOX_404               = 550
	CODE_USER_NOT_LOCAL            = 551 // please try <forward-path>
	CODE_EXCEEDED_STORAGE          = 552
	CODE__MAILBOX_NAME_INCORRECT   = 553
	CODE_TRANSACTION_FAILED        = 554
)

LIst of predefined by rfc5321 list of status codes.

View Source
const CommandEndSymbol = "\r\n"

CommandEndSymbol contains separator to understand where command is ended and need generate reply.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command is a struct representing an SMTP command (verb + arguments)

func CommandFromLine

func CommandFromLine(line string) *Command

CommandFromLine creates Command object form line string

type CommandName

type CommandName string

CommandName represent standard command name (sting) described in rfc5321

type ConversationState

type ConversationState string

ConversationState represents on what stage now current Client<->Server conversation.

type Protocol

type Protocol struct {
	Hostname string
	Ip       *net.TCPAddr
	// contains filtered or unexported fields
}

Protocol represents rfc5321 described protocol conversation

func CreateProtocol

func CreateProtocol(hostname string, ip *net.TCPAddr, validation *Validation) *Protocol

func (*Protocol) CreateCustomSceneUsing

func (protocol *Protocol) CreateCustomSceneUsing(callback func(sceneName string) Scene)

func (*Protocol) EHLO

func (protocol *Protocol) EHLO(command *Command) *Reply

func (*Protocol) HELO

func (protocol *Protocol) HELO(command *Command) *Reply

func (*Protocol) HandleReceivedLine

func (protocol *Protocol) HandleReceivedLine(receivedLine string) *Reply

func (*Protocol) MAIL

func (protocol *Protocol) MAIL(command *Command) *Reply

func (*Protocol) OnMessageReceived

func (protocol *Protocol) OnMessageReceived(callback func(message *smtpMessage.SmtpMessage) (string, error))

OnMessageReceived allow to provide custom success callback.

func (*Protocol) RCPT

func (protocol *Protocol) RCPT(command *Command) *Reply

func (*Protocol) RSET

func (protocol *Protocol) RSET(command *Command) *Reply

func (*Protocol) SayWelcome

func (protocol *Protocol) SayWelcome(identification string) *Reply

func (*Protocol) SetAuthMechanisms

func (protocol *Protocol) SetAuthMechanisms(authMechanisms []string)

func (*Protocol) SetStateCommandsExchange

func (protocol *Protocol) SetStateCommandsExchange()

type Reply

type Reply struct {
	Status int
	// contains filtered or unexported fields
}

Reply is a struct representing an SMTP reply (status code + lines)

func ReplyAuthCredentials

func ReplyAuthCredentials(response string) *Reply

ReplyAuthCredentials creates reply with a 334 code and requests a username

func ReplyAuthFailed

func ReplyAuthFailed(response string) *Reply

func ReplyAuthOk

func ReplyAuthOk() *Reply

ReplyAuthOk creates a authentication successful reply.

func ReplyBye

func ReplyBye() *Reply

ReplyBye used on close connection.

func ReplyCommandNotImplemented

func ReplyCommandNotImplemented() *Reply

func ReplyExceededStorage

func ReplyExceededStorage(response string) *Reply

func ReplyLineTooLong

func ReplyLineTooLong() *Reply

ReplyLineTooLong due to exceeding these limits

func ReplyMailData

func ReplyMailData() *Reply

func ReplyMailbox404

func ReplyMailbox404(response string) *Reply

func ReplyOk

func ReplyOk(message ...string) *Reply

ReplyOk represents generic success response.

func ReplyServiceReady

func ReplyServiceReady(identification string) *Reply

ReplyServiceReady creates a welcome reply.

func ReplyUnrecognisedCommand

func ReplyUnrecognisedCommand() *Reply

func (Reply) FormattedLines

func (r Reply) FormattedLines() []string

FormattedLines returns the formatted SMTP reply lines.

type Scene

type Scene interface {
	// Start scene by send specific message (reply) to client.
	Start(receivedLine string, protocol *Protocol) *Reply
	// ReadAndWriteReply reads client message and write reply
	ReadAndWriteReply(receivedLine string) *Reply
	// Finish scene, by notifying protocol to finish this scene
	Finish()
}

Scene represents custom logic flow (scene) for some specific set of commands, for example authentication.

type Validation

type Validation struct {
	MaximumLineLength int
	MaximumReceivers  int
}

Validation allows to send to package custom validation parameters what accepts server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL