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

@ -21,7 +21,9 @@ type Command struct {
type Protocol struct {
state State
message *data.SMTPMessage
hostname string
Hostname string
Ident string
// LogHandler is called for each log message. If nil, log messages will
// be output using log.Printf instead.
@ -48,6 +50,8 @@ func NewProtocol() *Protocol {
return &Protocol{
state: INVALID,
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
// 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.hostname = hostname
return ReplyIdent(hostname + " ESMTP Go-MailHog")
return ReplyIdent(proto.Hostname + " " + proto.Ident)
}
// Parse parses a line string and returns any remaining line string
@ -90,6 +94,7 @@ func (proto *Protocol) Parse(line string) (string, *Reply) {
line = ""
}
// TODO collapse AUTH states into separate processing
if proto.state == DATA {
reply = proto.ProcessData(parts[0])
} 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.state = MAIL
msg := proto.message.Parse(proto.hostname)
msg := proto.message.Parse(proto.Hostname)
if proto.MessageReceivedHandler == nil {
return ReplyStorageFailed("No storage backend")

View file

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