From 6db4e904be14b287093c029e418adc5c4b993482 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Sat, 22 Nov 2014 19:15:50 +0000 Subject: [PATCH] Clean up --- bindata.go | 8 ++--- mailhog/smtp/{server => protocol}/protocol.go | 2 +- mailhog/smtp/{server => protocol}/reply.go | 34 +++++++++++++++++-- mailhog/smtp/{server => protocol}/state.go | 2 +- mailhog/smtp/server/session.go | 26 +++++--------- mailhog/smtp/server/session_test.go | 5 +-- 6 files changed, 49 insertions(+), 28 deletions(-) rename mailhog/smtp/{server => protocol}/protocol.go (99%) rename mailhog/smtp/{server => protocol}/reply.go (76%) rename mailhog/smtp/{server => protocol}/state.go (96%) diff --git a/bindata.go b/bindata.go index b9d590d..b1021e1 100644 --- a/bindata.go +++ b/bindata.go @@ -1593,10 +1593,6 @@ type _bintree_t struct { var _bintree = &_bintree_t{nil, map[string]*_bintree_t{ "assets": &_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{}}, - "layout.html": &_bintree_t{assets_templates_layout_html, 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{}}, @@ -1606,5 +1602,9 @@ var _bintree = &_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{}}, }}, + "templates": &_bintree_t{nil, map[string]*_bintree_t{ + "index.html": &_bintree_t{assets_templates_index_html, map[string]*_bintree_t{}}, + "layout.html": &_bintree_t{assets_templates_layout_html, map[string]*_bintree_t{}}, + }}, }}, }} diff --git a/mailhog/smtp/server/protocol.go b/mailhog/smtp/protocol/protocol.go similarity index 99% rename from mailhog/smtp/server/protocol.go rename to mailhog/smtp/protocol/protocol.go index 6f9eada..bcdf2c5 100644 --- a/mailhog/smtp/server/protocol.go +++ b/mailhog/smtp/protocol/protocol.go @@ -1,4 +1,4 @@ -package smtp +package protocol // http://www.rfc-editor.org/rfc/rfc5321.txt diff --git a/mailhog/smtp/server/reply.go b/mailhog/smtp/protocol/reply.go similarity index 76% rename from mailhog/smtp/server/reply.go rename to mailhog/smtp/protocol/reply.go index c2c6ea4..5907ea6 100644 --- a/mailhog/smtp/server/reply.go +++ b/mailhog/smtp/protocol/reply.go @@ -1,13 +1,43 @@ -package smtp +package protocol + +import ( + "strconv" + "strings" +) // http://www.rfc-editor.org/rfc/rfc5321.txt // Reply is a struct representing an SMTP reply (status code + lines) type Reply struct { - status int + Status int lines []string } +// Lines returns the formatted SMTP reply +func (r Reply) Lines() []string { + var lines []string + + if len(r.lines) == 0 { + l := strconv.Itoa(r.Status) + lines = append(lines, l) + return lines + } + + for i, line := range r.lines { + l := "" + if i == len(r.lines)-1 { + l = strconv.Itoa(r.Status) + " " + line + "\n" + } else { + l = strconv.Itoa(r.Status) + "-" + line + "\n" + } + logText := strings.Replace(l, "\n", "\\n", -1) + logText = strings.Replace(logText, "\r", "\\r", -1) + lines = append(lines, l) + } + + return lines +} + // ReplyIdent creates a 220 welcome reply func ReplyIdent(ident string) *Reply { return &Reply{220, []string{ident}} } diff --git a/mailhog/smtp/server/state.go b/mailhog/smtp/protocol/state.go similarity index 96% rename from mailhog/smtp/server/state.go rename to mailhog/smtp/protocol/state.go index f450443..cbb98e7 100644 --- a/mailhog/smtp/server/state.go +++ b/mailhog/smtp/protocol/state.go @@ -1,4 +1,4 @@ -package smtp +package protocol // http://www.rfc-editor.org/rfc/rfc5321.txt diff --git a/mailhog/smtp/server/session.go b/mailhog/smtp/server/session.go index c6872b3..59f84b6 100644 --- a/mailhog/smtp/server/session.go +++ b/mailhog/smtp/server/session.go @@ -1,4 +1,4 @@ -package smtp +package server // http://www.rfc-editor.org/rfc/rfc5321.txt @@ -6,18 +6,18 @@ import ( "errors" "log" "net" - "strconv" "strings" "github.com/ian-kent/Go-MailHog/mailhog/config" "github.com/ian-kent/Go-MailHog/mailhog/data" + "github.com/ian-kent/Go-MailHog/mailhog/smtp/protocol" "github.com/ian-kent/Go-MailHog/mailhog/storage" ) // Session represents a SMTP session using net.TCPConn type Session struct { conn *net.TCPConn - proto *Protocol + proto *protocol.Protocol conf *config.Config isTLS bool line string @@ -25,7 +25,7 @@ type Session struct { // Accept starts a new SMTP session using net.TCPConn func Accept(conn *net.TCPConn, conf *config.Config) { - proto := NewProtocol() + proto := protocol.NewProtocol() session := &Session{conn, proto, conf, false, ""} proto.LogHandler = session.logf proto.MessageReceivedHandler = session.acceptMessageHandler @@ -84,7 +84,7 @@ func (c *Session) Read() bool { if reply != nil { c.Write(reply) - if reply.status == 221 { + if reply.Status == 221 { c.conn.Close() } } @@ -93,19 +93,9 @@ func (c *Session) Read() bool { } // Write writes a reply to the underlying net.TCPConn -func (c *Session) Write(reply *Reply) { - if len(reply.lines) == 0 { - l := strconv.Itoa(reply.status) - c.logf("Sent %d bytes: '%s'", len(l), l) - c.conn.Write([]byte(l)) - } - for i, line := range reply.lines { - l := "" - if i == len(reply.lines)-1 { - l = strconv.Itoa(reply.status) + " " + line + "\n" - } else { - l = strconv.Itoa(reply.status) + "-" + line + "\n" - } +func (c *Session) Write(reply *protocol.Reply) { + lines := reply.Lines() + for _, l := range lines { logText := strings.Replace(l, "\n", "\\n", -1) logText = strings.Replace(logText, "\r", "\\r", -1) c.logf("Sent %d bytes: '%s'", len(l), logText) diff --git a/mailhog/smtp/server/session_test.go b/mailhog/smtp/server/session_test.go index 48da6c1..0ab4b4b 100644 --- a/mailhog/smtp/server/session_test.go +++ b/mailhog/smtp/server/session_test.go @@ -1,8 +1,9 @@ -package smtp +package server import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestMAILParsing(t *testing.T) {