Remove protocol dependency on config

This commit is contained in:
Ian Kent 2014-11-22 18:47:31 +00:00
parent 2fe16ec68e
commit b174a42877
3 changed files with 12 additions and 13 deletions

View file

@ -1594,13 +1594,13 @@ type _bintree_t struct {
var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{
"assets": &_bintree_t{nil, map[string]*_bintree_t{ "assets": &_bintree_t{nil, map[string]*_bintree_t{
"images": &_bintree_t{nil, map[string]*_bintree_t{ "images": &_bintree_t{nil, map[string]*_bintree_t{
"ajax-loader.gif": &_bintree_t{assets_images_ajax_loader_gif, map[string]*_bintree_t{}},
"github.png": &_bintree_t{assets_images_github_png, map[string]*_bintree_t{}}, "github.png": &_bintree_t{assets_images_github_png, map[string]*_bintree_t{}},
"hog.png": &_bintree_t{assets_images_hog_png, map[string]*_bintree_t{}}, "hog.png": &_bintree_t{assets_images_hog_png, map[string]*_bintree_t{}},
"ajax-loader.gif": &_bintree_t{assets_images_ajax_loader_gif, map[string]*_bintree_t{}},
}}, }},
"js": &_bintree_t{nil, map[string]*_bintree_t{ "js": &_bintree_t{nil, map[string]*_bintree_t{
"controllers.js": &_bintree_t{assets_js_controllers_js, map[string]*_bintree_t{}},
"strutil.js": &_bintree_t{assets_js_strutil_js, map[string]*_bintree_t{}}, "strutil.js": &_bintree_t{assets_js_strutil_js, map[string]*_bintree_t{}},
"controllers.js": &_bintree_t{assets_js_controllers_js, map[string]*_bintree_t{}},
}}, }},
"templates": &_bintree_t{nil, map[string]*_bintree_t{ "templates": &_bintree_t{nil, map[string]*_bintree_t{
"index.html": &_bintree_t{assets_templates_index_html, map[string]*_bintree_t{}}, "index.html": &_bintree_t{assets_templates_index_html, map[string]*_bintree_t{}},

View file

@ -8,15 +8,14 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/ian-kent/Go-MailHog/mailhog/config"
"github.com/ian-kent/Go-MailHog/mailhog/data" "github.com/ian-kent/Go-MailHog/mailhog/data"
) )
// Protocol is a state machine representing an SMTP session // Protocol is a state machine representing an SMTP session
type Protocol struct { type Protocol struct {
conf *config.Config
state State state State
message *data.SMTPMessage message *data.SMTPMessage
hostname string
MessageIDHandler func() (string, error) MessageIDHandler func() (string, error)
LogHandler func(message string, args ...interface{}) LogHandler func(message string, args ...interface{})
@ -84,9 +83,8 @@ var StateMap = map[State]string{
// NewProtocol returns a new SMTP state machine in INVALID state // NewProtocol returns a new SMTP state machine in INVALID state
// 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(cfg *config.Config) *Protocol { func NewProtocol() *Protocol {
return &Protocol{ return &Protocol{
conf: cfg,
state: INVALID, state: INVALID,
message: &data.SMTPMessage{}, message: &data.SMTPMessage{},
} }
@ -104,11 +102,12 @@ func (proto *Protocol) logf(message string, args ...interface{}) {
} }
// Start begins an SMTP conversation with a 220 reply // Start begins an SMTP conversation with a 220 reply
func (proto *Protocol) Start() *Reply { func (proto *Protocol) Start(hostname string) *Reply {
proto.state = ESTABLISH proto.state = ESTABLISH
proto.hostname = hostname
return &Reply{ return &Reply{
status: 220, status: 220,
lines: []string{proto.conf.Hostname + " ESMTP Go-MailHog"}, lines: []string{hostname + " ESMTP Go-MailHog"},
} }
} }
@ -151,7 +150,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.conf.Hostname) msg := proto.message.Parse(proto.hostname)
if proto.MessageReceivedHandler != nil { if proto.MessageReceivedHandler != nil {
id, err := proto.MessageReceivedHandler(msg) id, err := proto.MessageReceivedHandler(msg)

View file

@ -25,13 +25,13 @@ 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 := NewProtocol(conf) proto := NewProtocol()
session := &Session{conn, proto, conf, false, ""} session := &Session{conn, proto, conf, false, ""}
proto.LogHandler = session.logf proto.LogHandler = session.logf
proto.MessageReceivedHandler = session.acceptMessageHandler proto.MessageReceivedHandler = session.acceptMessageHandler
session.logf("Starting session") session.logf("Starting session")
session.Write(proto.Start()) session.Write(proto.Start(conf.Hostname))
for session.Read() == true { for session.Read() == true {
} }
session.logf("Session ended") session.logf("Session ended")