smtpd

package
v0.0.0-...-f00139d Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2019 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package smtpd contains source code of the SMTP server of Mailmock.

Index

Constants

This section is empty.

Variables

View Source
var Responses = map[Resp]Response{
	Ready:                 Response{CodeReady, []string{"<domain> Service ready"}},
	Closing:               Response{CodeClosing, []string{"<domain> Service closing transmission channel"}},
	Success:               Response{CodeSuccess, []string{"OK"}},
	Data:                  Response{CodeAskForData, []string{"Start mail input; end with <CRLF>.<CRLF>"}},
	NotAvailable:          Response{CodeNotAvailable, []string{"<domain> Service not available, closing transmission channel"}},
	ShuttingDown:          Response{CodeNotAvailable, []string{"<domain> Service shutting down and closing transmission channel"}},
	SessionTimeout:        Response{CodeNotAvailable, []string{"Your session timed out due to inactivity"}},
	Abort:                 Response{CodeAbort, []string{"Requested action aborted: error in processing"}},
	CommandUnrecognized:   Response{CodeCommandUnrecognized, []string{"Syntax error, command unrecognized"}},
	ParameterSyntax:       Response{CodeParameterSyntax, []string{"Syntax error in parameters or arguments"}},
	CommandNotImplemented: Response{CodeNotImplemented, []string{"Command not implemented"}},
	BadSequence:           Response{CodeBadSequence, []string{"Bad sequence of commands"}},
	NoValidRecipients:     Response{CodeTransactionFailed, []string{"No valid recipients"}},
	Misconfiguration:      Response{CodeTransactionFailed, []string{"Server is unable to reply to the requested action"}},
	Help:                  Response{CodeHelp, []string{""}},
	Status:                Response{CodeStatus, []string{""}},
	Extensions:            Response{CodeSuccess, []string{"<domain>", "HELP"}},
}

Responses returned by the SMTP server

Functions

func ParseCommand

func ParseCommand(input string) (*Command, *Response)

ParseCommand parses a SMTP command, returns appropriate response if the command is malformed If the command is well formed, returned response is nil.

func SetReply

func SetReply(resp Resp, s ...string)

SetReply set reply text of given code.

Types

type Code

type Code uint16

Code is an alias for the type uint16

const (
	CodeStatus                  Code = 211 // System status, or system help reply
	CodeHelp                    Code = 214 // Help message (Information on how to use the receiver or the meaning of a particular non-standard command; this reply is useful only to the human user)
	CodeReady                   Code = 220 // <domain> Service ready
	CodeClosing                 Code = 221 // <domain> Service closing transmission channel
	CodeSuccess                 Code = 250 // Requested mail action okay, completed
	CodeUserNotLocalTemp        Code = 251 // User not local; will forward to <forward-path>
	CodeCannotVerify            Code = 252 // Cannot VRFY user, but will accept message and attempt delivery
	CodeAskForData              Code = 354 // Start mail input; end with <CRLF>.<CRLF>
	CodeNotAvailable            Code = 421 // <domain> Service not available, closing transmission channel
	CodeMailboxUnavailableTemp  Code = 450 // Requested mail action not taken: mailbox unavailable (e.g., mailbox busy or temporarily blocked for policy reasons)
	CodeAbort                   Code = 451 // Requested action aborted: local error in processing
	CodeInsufficientStorageTemp Code = 452 // Requested action not taken: insufficient system storage
	CodeUnableAccomodateParam   Code = 455 // Server unable to accommodate parameters
	CodeCommandUnrecognized     Code = 500 // Syntax error, command unrecognized
	CodeParameterSyntax         Code = 501 // Syntax error in parameters or arguments
	CodeNotImplemented          Code = 502 // Command not implemented
	CodeBadSequence             Code = 503 // Bad sequence of commands
	CodeParameterNotImplemented Code = 504 // Command parameter not implemented
	CodeMailboxUnavailablePerm  Code = 550 // Requested action not taken: mailbox unavailable (e.g., mailbox not found, no access, or command rejected for policy reasons)
	CodeUserNotLocalPerm        Code = 551 // User not local; please try <forward-path>
	CodeInsufficientStoragePerm Code = 552 // Requested mail action aborted: exceeded storage allocation
	CodeMailboxNotAllowed       Code = 553 // Requested action not taken: mailbox name not allowed (e.g., mailbox syntax incorrect)
	CodeTransactionFailed       Code = 554 // Transaction failed
	CodeMailFromRcptToParam     Code = 555 // MAIL FROM/RCPT TO parameters not recognized or not implemented
)

SMTP reply codes as defined by RFC 5321, 4.2.3

type Command

type Command struct {
	FullCmd        string
	Name           string
	PositionalArgs []string
	NamedArgs      map[string]string
}

Command is a parsed SMTP command.

type Envelope

type Envelope struct {
	Sender     string   `json:"sender"`
	Recipients []string `json:"recipients"`
}

Envelope contains the sender address (originator or return-path) and the recipients address (or forward-paths).

type Mail

type Mail struct {
	Envelope Envelope `json:"envelope"`
	Content  []string `json:"content"`
}

Mail object contains an envelope and content as described in RFC 5321 §2.3.1.

func (Mail) String

func (m Mail) String() string

type Resp

type Resp uint16

Resp is an alias for the type uint16

const (
	Ready                 Resp = iota // First response
	Closing                           // Service closing
	Success                           // Requested action completed
	Abort                             // Requested action aborted
	Data                              // Ask for data input
	NotAvailable                      // Service is not available
	ShuttingDown                      // Service is shutting down
	SessionTimeout                    // Session timeout
	CommandUnrecognized               // Syntax error, command unrecognized
	ParameterSyntax                   // Syntax error in parameters or arguments
	CommandNotImplemented             // Command not implemented
	BadSequence                       // Bad sequence of commands
	NoValidRecipients                 // Transaction failed : no valid recipients
	Help                              // Help response
	Status                            // Server status
	Misconfiguration                  // Unable to reply because of misconfiguration
	Extensions                        // Reply to EHLO with supported extensions
)

Responses

type Response

type Response struct {
	Code Code     `json:"code"`
	Msg  []string `json:"message"`
}

Response holds a 3 digit code and a messsage.

func (Response) IsError

func (e Response) IsError() bool

IsError returns true if the response is an error.

func (Response) IsSuccess

func (e Response) IsSuccess() bool

IsSuccess returns true if the response is an success.

func (Response) String

func (e Response) String() string

type Server

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

Server is holding the SMTP server properties.

func NewServer

func NewServer(name string, host string, port string, th *TransactionHandler, logger log.Logger) *Server

NewServer creates a SMTP server.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(stop <-chan struct{}) error

ListenAndServe starts listening for clients connection and serves SMTP commands.

type Session

type Session struct {
	State    SessionState `json:"state"`
	Client   string       `json:"client"`
	Tr       *Transaction `json:"transaction"`
	Extended bool
	// contains filtered or unexported fields
}

Session represents a SMTP session of a client.

func NewSession

func NewSession(c *textproto.Conn, th *TransactionHandler, logger log.Logger) *Session

NewSession return a new Session.

func (*Session) Serve

func (s *Session) Serve(stop <-chan struct{})

Serve will reponds to any request until a QUIT command is received or connection is broken.

func (*Session) String

func (s *Session) String() string

type SessionState

type SessionState string

SessionState is the state of a Session.

const (
	SSInitiated SessionState = "initiated"
	SSReady     SessionState = "ready"
	SSBusy      SessionState = "busy"
	SSClosed    SessionState = "closed"
)

Session States

type Transaction

type Transaction struct {
	Mail    Mail             `json:"mail"`
	State   TransactionState `json:"state"`
	History []string         `json:"history"`
}

Transaction represents either a successful, ongoing or aborted SMTP transaction.

func NewTransaction

func NewTransaction() *Transaction

NewTransaction creates a new SMTP transaction with initial state set to TSInitiated.

func (*Transaction) Abort

func (tr *Transaction) Abort() error

Abort sets transaction's state to TSAborted.

func (*Transaction) Data

func (tr *Transaction) Data(data []string) (*Response, error)

Data sets full data, this method can only be user during TSData phase.

func (*Transaction) Process

func (tr *Transaction) Process(cmd *Command) (*Response, error)

Process reads the given command, updates the transaction and returns appropriate response.

func (Transaction) String

func (tr Transaction) String() string

type TransactionHandler

type TransactionHandler func(*Transaction)

TransactionHandler will be called each time a transaction reach TSCompleted or TSAborted status.

type TransactionState

type TransactionState string

TransactionState is the state of a Transaction.

const (
	TSInitiated  TransactionState = "initiated"    // no command was received yet
	TSInProgress TransactionState = "in progress"  // sender address is set, and recipients are being filled
	TSData       TransactionState = "reading data" // sender and recipients are set, waiting for data to complete
	TSCompleted  TransactionState = "completed"    // data is received, the transaction is successfully completed
	TSAborted    TransactionState = "aborted"      // the transaction is not complete
)

Transaction States

Jump to

Keyboard shortcuts

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