Make ident configurable

This commit is contained in:
Ian Kent 2014-11-23 00:42:15 +00:00
parent 1a5fd826ec
commit 559c2a5146
2 changed files with 16 additions and 10 deletions

View file

@ -19,9 +19,11 @@ type Command struct {
// Protocol is a state machine representing an SMTP session // Protocol is a state machine representing an SMTP session
type Protocol struct { type Protocol struct {
state State state State
message *data.SMTPMessage message *data.SMTPMessage
hostname string
Hostname string
Ident string
// LogHandler is called for each log message. If nil, log messages will // LogHandler is called for each log message. If nil, log messages will
// be output using log.Printf instead. // be output using log.Printf instead.
@ -46,8 +48,10 @@ type Protocol struct {
// handler is called when a message is received and should return a message ID // handler is called when a message is received and should return a message ID
func NewProtocol() *Protocol { func NewProtocol() *Protocol {
return &Protocol{ return &Protocol{
state: INVALID, state: INVALID,
message: &data.SMTPMessage{}, message: &data.SMTPMessage{},
Hostname: "",
Ident: "ESMTP Go-MailHog",
} }
} }
@ -64,10 +68,10 @@ func (proto *Protocol) logf(message string, args ...interface{}) {
// Start begins an SMTP conversation with a 220 reply, placing the state // Start begins an SMTP conversation with a 220 reply, placing the state
// machine in ESTABLISH state. // machine in ESTABLISH state.
func (proto *Protocol) Start(hostname string) *Reply { func (proto *Protocol) Start() *Reply {
proto.logf("Started session, switching to ESTABLISH state")
proto.state = ESTABLISH proto.state = ESTABLISH
proto.hostname = hostname return ReplyIdent(proto.Hostname + " " + proto.Ident)
return ReplyIdent(hostname + " ESMTP Go-MailHog")
} }
// Parse parses a line string and returns any remaining line string // Parse parses a line string and returns any remaining line string
@ -90,6 +94,7 @@ func (proto *Protocol) Parse(line string) (string, *Reply) {
line = "" line = ""
} }
// TODO collapse AUTH states into separate processing
if proto.state == DATA { if proto.state == DATA {
reply = proto.ProcessData(parts[0]) reply = proto.ProcessData(parts[0])
} else { } else {
@ -111,7 +116,7 @@ func (proto *Protocol) ProcessData(line string) (reply *Reply) {
proto.message.Data = strings.TrimSuffix(proto.message.Data, "\r\n.\r\n") proto.message.Data = strings.TrimSuffix(proto.message.Data, "\r\n.\r\n")
proto.state = MAIL proto.state = MAIL
msg := proto.message.Parse(proto.hostname) msg := proto.message.Parse(proto.Hostname)
if proto.MessageReceivedHandler == nil { if proto.MessageReceivedHandler == nil {
return ReplyStorageFailed("No storage backend") return ReplyStorageFailed("No storage backend")

View file

@ -24,6 +24,7 @@ type Session struct {
// Accept starts a new SMTP session using net.TCPConn // Accept starts a new SMTP session using net.TCPConn
func Accept(conn *net.TCPConn, conf *config.Config) { func Accept(conn *net.TCPConn, conf *config.Config) {
proto := protocol.NewProtocol() proto := protocol.NewProtocol()
proto.Hostname = conf.Hostname
session := &Session{conn, proto, conf, false, ""} session := &Session{conn, proto, conf, false, ""}
proto.LogHandler = session.logf proto.LogHandler = session.logf
proto.MessageReceivedHandler = session.acceptMessage proto.MessageReceivedHandler = session.acceptMessage
@ -32,7 +33,7 @@ func Accept(conn *net.TCPConn, conf *config.Config) {
proto.ValidateAuthenticationHandler = session.validateAuthentication proto.ValidateAuthenticationHandler = session.validateAuthentication
session.logf("Starting session") session.logf("Starting session")
session.Write(proto.Start(conf.Hostname)) session.Write(proto.Start())
for session.Read() == true { for session.Read() == true {
} }
session.logf("Session ended") session.logf("Session ended")