mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
Many linter fixes for smtpd pkg
This commit is contained in:
@@ -72,7 +72,7 @@ func (m *FileMessage) From() string {
|
|||||||
return m.Ffrom
|
return m.Ffrom
|
||||||
}
|
}
|
||||||
|
|
||||||
// From returns the value of the Message To header
|
// To returns the value of the Message To header
|
||||||
func (m *FileMessage) To() []string {
|
func (m *FileMessage) To() []string {
|
||||||
return m.Fto
|
return m.Fto
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,10 +150,12 @@ type FileMailbox struct {
|
|||||||
messages []*FileMessage
|
messages []*FileMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name of the mailbox
|
||||||
func (mb *FileMailbox) Name() string {
|
func (mb *FileMailbox) Name() string {
|
||||||
return mb.name
|
return mb.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String renders the name and directory path of the mailbox
|
||||||
func (mb *FileMailbox) String() string {
|
func (mb *FileMailbox) String() string {
|
||||||
return mb.name + "[" + mb.dirName + "]"
|
return mb.name + "[" + mb.dirName + "]"
|
||||||
}
|
}
|
||||||
@@ -184,11 +186,11 @@ func (mb *FileMailbox) GetMessage(id string) (datastore.Message, error) {
|
|||||||
|
|
||||||
if id == "latest" && len(mb.messages) != 0 {
|
if id == "latest" && len(mb.messages) != 0 {
|
||||||
return mb.messages[len(mb.messages)-1], nil
|
return mb.messages[len(mb.messages)-1], nil
|
||||||
} else {
|
}
|
||||||
for _, m := range mb.messages {
|
|
||||||
if m.Fid == id {
|
for _, m := range mb.messages {
|
||||||
return m, nil
|
if m.Fid == id {
|
||||||
}
|
return m, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -496,7 +496,7 @@ func TestGetLatestMessage(t *testing.T) {
|
|||||||
assert.True(t, msg.ID() == id3, "Expected %q to be equal to %q", msg.ID(), id3)
|
assert.True(t, msg.ID() == id3, "Expected %q to be equal to %q", msg.ID(), id3)
|
||||||
|
|
||||||
// Test wrong id
|
// Test wrong id
|
||||||
msg, err = mb.GetMessage("wrongid")
|
_, err = mb.GetMessage("wrongid")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
if t.Failed() {
|
if t.Failed() {
|
||||||
|
|||||||
@@ -511,20 +511,16 @@ func (ss *Session) send(msg string) {
|
|||||||
|
|
||||||
// readByteLine reads a line of input into the provided buffer. Does
|
// readByteLine reads a line of input into the provided buffer. Does
|
||||||
// not reset the Buffer - please do so prior to calling.
|
// not reset the Buffer - please do so prior to calling.
|
||||||
func (ss *Session) readByteLine(buf *bytes.Buffer) error {
|
func (ss *Session) readByteLine(buf io.Writer) error {
|
||||||
if err := ss.conn.SetReadDeadline(ss.nextDeadline()); err != nil {
|
if err := ss.conn.SetReadDeadline(ss.nextDeadline()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for {
|
line, err := ss.reader.ReadBytes('\n')
|
||||||
line, err := ss.reader.ReadBytes('\n')
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err = buf.Write(line); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
_, err = buf.Write(line)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads a line of input
|
// Reads a line of input
|
||||||
@@ -573,7 +569,7 @@ func (ss *Session) parseCmd(line string) (cmd string, arg string, ok bool) {
|
|||||||
// The leading space is mandatory.
|
// The leading space is mandatory.
|
||||||
func (ss *Session) parseArgs(arg string) (args map[string]string, ok bool) {
|
func (ss *Session) parseArgs(arg string) (args map[string]string, ok bool) {
|
||||||
args = make(map[string]string)
|
args = make(map[string]string)
|
||||||
re := regexp.MustCompile(" (\\w+)=(\\w+)")
|
re := regexp.MustCompile(` (\w+)=(\w+)`)
|
||||||
pm := re.FindAllStringSubmatch(arg, -1)
|
pm := re.FindAllStringSubmatch(arg, -1)
|
||||||
if pm == nil {
|
if pm == nil {
|
||||||
ss.logWarn("Failed to parse arg string: %q")
|
ss.logWarn("Failed to parse arg string: %q")
|
||||||
|
|||||||
@@ -31,10 +31,8 @@ func TestGreetState(t *testing.T) {
|
|||||||
server, logbuf, teardown := setupSMTPServer(mds)
|
server, logbuf, teardown := setupSMTPServer(mds)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
var script []scriptStep
|
|
||||||
|
|
||||||
// Test out some mangled HELOs
|
// Test out some mangled HELOs
|
||||||
script = []scriptStep{
|
script := []scriptStep{
|
||||||
{"HELO", 501},
|
{"HELO", 501},
|
||||||
{"EHLO", 501},
|
{"EHLO", 501},
|
||||||
{"HELLO", 500},
|
{"HELLO", 500},
|
||||||
@@ -90,10 +88,8 @@ func TestReadyState(t *testing.T) {
|
|||||||
server, logbuf, teardown := setupSMTPServer(mds)
|
server, logbuf, teardown := setupSMTPServer(mds)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
var script []scriptStep
|
|
||||||
|
|
||||||
// Test out some mangled READY commands
|
// Test out some mangled READY commands
|
||||||
script = []scriptStep{
|
script := []scriptStep{
|
||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
{"FOOB", 500},
|
{"FOOB", 500},
|
||||||
{"HELO", 503},
|
{"HELO", 503},
|
||||||
@@ -165,10 +161,8 @@ func TestMailState(t *testing.T) {
|
|||||||
server, logbuf, teardown := setupSMTPServer(mds)
|
server, logbuf, teardown := setupSMTPServer(mds)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
var script []scriptStep
|
|
||||||
|
|
||||||
// Test out some mangled READY commands
|
// Test out some mangled READY commands
|
||||||
script = []scriptStep{
|
script := []scriptStep{
|
||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
{"MAIL FROM:<john@gmail.com>", 250},
|
{"MAIL FROM:<john@gmail.com>", 250},
|
||||||
{"FOOB", 500},
|
{"FOOB", 500},
|
||||||
|
|||||||
@@ -131,10 +131,8 @@ func (s *Server) Start(ctx context.Context) {
|
|||||||
go s.serve(ctx)
|
go s.serve(ctx)
|
||||||
|
|
||||||
// Wait for shutdown
|
// Wait for shutdown
|
||||||
select {
|
<-ctx.Done()
|
||||||
case <-ctx.Done():
|
log.Tracef("SMTP shutdown requested, connections will be drained")
|
||||||
log.Tracef("SMTP shutdown requested, connections will be drained")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Closing the listener will cause the serve() go routine to exit
|
// Closing the listener will cause the serve() go routine to exit
|
||||||
if err := s.listener.Close(); err != nil {
|
if err := s.listener.Close(); err != nil {
|
||||||
@@ -186,7 +184,7 @@ func (s *Server) serve(ctx context.Context) {
|
|||||||
func (s *Server) emergencyShutdown() {
|
func (s *Server) emergencyShutdown() {
|
||||||
// Shutdown Inbucket
|
// Shutdown Inbucket
|
||||||
select {
|
select {
|
||||||
case _ = <-s.globalShutdown:
|
case <-s.globalShutdown:
|
||||||
default:
|
default:
|
||||||
close(s.globalShutdown)
|
close(s.globalShutdown)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,15 +130,24 @@ LOOP:
|
|||||||
switch {
|
switch {
|
||||||
case ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'):
|
case ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'):
|
||||||
// Letters are OK
|
// Letters are OK
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
case '0' <= c && c <= '9':
|
case '0' <= c && c <= '9':
|
||||||
// Numbers are OK
|
// Numbers are OK
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
case bytes.IndexByte([]byte("!#$%&'*+-/=?^_`{|}~"), c) >= 0:
|
case bytes.IndexByte([]byte("!#$%&'*+-/=?^_`{|}~"), c) >= 0:
|
||||||
// These specials can be used unquoted
|
// These specials can be used unquoted
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
case c == '.':
|
case c == '.':
|
||||||
// A single period is OK
|
// A single period is OK
|
||||||
@@ -146,13 +155,19 @@ LOOP:
|
|||||||
// Sequence of periods is not permitted
|
// Sequence of periods is not permitted
|
||||||
return "", "", fmt.Errorf("Sequence of periods is not permitted")
|
return "", "", fmt.Errorf("Sequence of periods is not permitted")
|
||||||
}
|
}
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
case c == '\\':
|
case c == '\\':
|
||||||
inCharQuote = true
|
inCharQuote = true
|
||||||
case c == '"':
|
case c == '"':
|
||||||
if inCharQuote {
|
if inCharQuote {
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
} else if inStringQuote {
|
} else if inStringQuote {
|
||||||
inStringQuote = false
|
inStringQuote = false
|
||||||
@@ -165,7 +180,10 @@ LOOP:
|
|||||||
}
|
}
|
||||||
case c == '@':
|
case c == '@':
|
||||||
if inCharQuote || inStringQuote {
|
if inCharQuote || inStringQuote {
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
} else {
|
} else {
|
||||||
// End of local-part
|
// End of local-part
|
||||||
@@ -182,7 +200,10 @@ LOOP:
|
|||||||
return "", "", fmt.Errorf("Characters outside of US-ASCII range not permitted")
|
return "", "", fmt.Errorf("Characters outside of US-ASCII range not permitted")
|
||||||
default:
|
default:
|
||||||
if inCharQuote || inStringQuote {
|
if inCharQuote || inStringQuote {
|
||||||
_ = buf.WriteByte(c)
|
err = buf.WriteByte(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
inCharQuote = false
|
inCharQuote = false
|
||||||
} else {
|
} else {
|
||||||
return "", "", fmt.Errorf("Character %q must be quoted", c)
|
return "", "", fmt.Errorf("Character %q must be quoted", c)
|
||||||
|
|||||||
Reference in New Issue
Block a user