mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
Minor style and simplification cleanups
This patch does various minor style and simplification cleanups, fixing things detected by tools such as go vet, gofmt -s, and golint. There are no functional changes, this change is purely cosmetic, but will enable us to run those tools more regularly now that their output is clean.
This commit is contained in:
30
chasquid.go
30
chasquid.go
@@ -325,9 +325,9 @@ type Conn struct {
|
|||||||
tlsConfig *tls.Config
|
tlsConfig *tls.Config
|
||||||
|
|
||||||
// Envelope.
|
// Envelope.
|
||||||
mail_from string
|
mailFrom string
|
||||||
rcpt_to []string
|
rcptTo []string
|
||||||
data []byte
|
data []byte
|
||||||
|
|
||||||
// Are we using TLS?
|
// Are we using TLS?
|
||||||
onTLS bool
|
onTLS bool
|
||||||
@@ -536,7 +536,7 @@ func (c *Conn) MAIL(params string) (code int, msg string) {
|
|||||||
// but that's not according to the RFC. We reset the envelope instead.
|
// but that's not according to the RFC. We reset the envelope instead.
|
||||||
c.resetEnvelope()
|
c.resetEnvelope()
|
||||||
|
|
||||||
c.mail_from = e.Address
|
c.mailFrom = e.Address
|
||||||
return 250, "You feel like you are being watched"
|
return 250, "You feel like you are being watched"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,30 +557,30 @@ func (c *Conn) RCPT(params string) (code int, msg string) {
|
|||||||
return 501, "malformed address"
|
return 501, "malformed address"
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.mail_from == "" {
|
if c.mailFrom == "" {
|
||||||
return 503, "sender not yet given"
|
return 503, "sender not yet given"
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC says 100 is the minimum limit for this, but it seems excessive.
|
// RFC says 100 is the minimum limit for this, but it seems excessive.
|
||||||
if len(c.rcpt_to) > 100 {
|
if len(c.rcptTo) > 100 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: do we allow receivers without a domain?
|
// TODO: do we allow receivers without a domain?
|
||||||
// TODO: check the case:
|
// TODO: check the case:
|
||||||
// - local recipient, always ok
|
// - local recipient, always ok
|
||||||
// - external recipient, only ok if mail_from is local (needs auth)
|
// - external recipient, only ok if mailFrom is local (needs auth)
|
||||||
|
|
||||||
c.rcpt_to = append(c.rcpt_to, e.Address)
|
c.rcptTo = append(c.rcptTo, e.Address)
|
||||||
return 250, "You have an eerie feeling..."
|
return 250, "You have an eerie feeling..."
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) DATA(params string, tr *trace.Trace) (code int, msg string) {
|
func (c *Conn) DATA(params string, tr *trace.Trace) (code int, msg string) {
|
||||||
if c.mail_from == "" {
|
if c.mailFrom == "" {
|
||||||
return 503, "sender not yet given"
|
return 503, "sender not yet given"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.rcpt_to) == 0 {
|
if len(c.rcptTo) == 0 {
|
||||||
return 503, "need an address to send to"
|
return 503, "need an address to send to"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -606,7 +606,7 @@ func (c *Conn) DATA(params string, tr *trace.Trace) (code int, msg string) {
|
|||||||
|
|
||||||
// There are no partial failures here: we put it in the queue, and then if
|
// There are no partial failures here: we put it in the queue, and then if
|
||||||
// individual deliveries fail, we report via email.
|
// individual deliveries fail, we report via email.
|
||||||
msgID, err := c.queue.Put(c.mail_from, c.rcpt_to, c.data)
|
msgID, err := c.queue.Put(c.mailFrom, c.rcptTo, c.data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tr.LazyPrintf(" error queueing: %v", err)
|
tr.LazyPrintf(" error queueing: %v", err)
|
||||||
tr.SetError()
|
tr.SetError()
|
||||||
@@ -724,14 +724,14 @@ func (c *Conn) AUTH(params string, tr *trace.Trace) (code int, msg string) {
|
|||||||
c.authDomain = domain
|
c.authDomain = domain
|
||||||
c.completedAuth = true
|
c.completedAuth = true
|
||||||
return 235, ""
|
return 235, ""
|
||||||
} else {
|
|
||||||
return 535, "Incorrect user or password"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 535, "Incorrect user or password"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) resetEnvelope() {
|
func (c *Conn) resetEnvelope() {
|
||||||
c.mail_from = ""
|
c.mailFrom = ""
|
||||||
c.rcpt_to = nil
|
c.rcptTo = nil
|
||||||
c.data = nil
|
c.data = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ func TestWrongMailParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Mail("from@from"); err != nil {
|
if err := c.Mail("from@from"); err != nil {
|
||||||
t.Errorf("Mail:", err)
|
t.Errorf("Mail: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
timeoutError = fmt.Errorf("Operation timed out")
|
errTimeout = fmt.Errorf("Operation timed out")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Procmail delivers local mail via procmail.
|
// Procmail delivers local mail via procmail.
|
||||||
@@ -79,7 +79,7 @@ func (p *Procmail) Deliver(from string, to string, data []byte) error {
|
|||||||
timedOut := !timer.Stop()
|
timedOut := !timer.Stop()
|
||||||
|
|
||||||
if timedOut {
|
if timedOut {
|
||||||
return tr.Error(timeoutError)
|
return tr.Error(errTimeout)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tr.Errorf("Procmail failed: %v - %q", err, output.String())
|
return tr.Errorf("Procmail failed: %v - %q", err, output.String())
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func TestProcmailTimeout(t *testing.T) {
|
|||||||
p := Procmail{}
|
p := Procmail{}
|
||||||
|
|
||||||
err := p.Deliver("from", "to@local", []byte("data"))
|
err := p.Deliver("from", "to@local", []byte("data"))
|
||||||
if err != timeoutError {
|
if err != errTimeout {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,19 +86,19 @@ func TestSMTPErrors(t *testing.T) {
|
|||||||
|
|
||||||
responses := []map[string]string{
|
responses := []map[string]string{
|
||||||
// First test: hang response, should fail due to timeout.
|
// First test: hang response, should fail due to timeout.
|
||||||
map[string]string{
|
{
|
||||||
"_welcome": "220 no newline",
|
"_welcome": "220 no newline",
|
||||||
},
|
},
|
||||||
|
|
||||||
// MAIL FROM not allowed.
|
// MAIL FROM not allowed.
|
||||||
map[string]string{
|
{
|
||||||
"_welcome": "220 mail from not allowed\n",
|
"_welcome": "220 mail from not allowed\n",
|
||||||
"EHLO localhost": "250 ehlo ok\n",
|
"EHLO localhost": "250 ehlo ok\n",
|
||||||
"MAIL FROM:<me@me>": "501 mail error\n",
|
"MAIL FROM:<me@me>": "501 mail error\n",
|
||||||
},
|
},
|
||||||
|
|
||||||
// RCPT TO not allowed.
|
// RCPT TO not allowed.
|
||||||
map[string]string{
|
{
|
||||||
"_welcome": "220 rcpt to not allowed\n",
|
"_welcome": "220 rcpt to not allowed\n",
|
||||||
"EHLO localhost": "250 ehlo ok\n",
|
"EHLO localhost": "250 ehlo ok\n",
|
||||||
"MAIL FROM:<me@me>": "250 mail ok\n",
|
"MAIL FROM:<me@me>": "250 mail ok\n",
|
||||||
@@ -106,7 +106,7 @@ func TestSMTPErrors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// DATA error.
|
// DATA error.
|
||||||
map[string]string{
|
{
|
||||||
"_welcome": "220 data error\n",
|
"_welcome": "220 data error\n",
|
||||||
"EHLO localhost": "250 ehlo ok\n",
|
"EHLO localhost": "250 ehlo ok\n",
|
||||||
"MAIL FROM:<me@me>": "250 mail ok\n",
|
"MAIL FROM:<me@me>": "250 mail ok\n",
|
||||||
@@ -115,7 +115,7 @@ func TestSMTPErrors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// DATA response error.
|
// DATA response error.
|
||||||
map[string]string{
|
{
|
||||||
"_welcome": "220 data response error\n",
|
"_welcome": "220 data response error\n",
|
||||||
"EHLO localhost": "250 ehlo ok\n",
|
"EHLO localhost": "250 ehlo ok\n",
|
||||||
"MAIL FROM:<me@me>": "250 mail ok\n",
|
"MAIL FROM:<me@me>": "250 mail ok\n",
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
queueFullError = fmt.Errorf("Queue size too big, try again later")
|
errQueueFull = fmt.Errorf("Queue size too big, try again later")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Channel used to get random IDs for items in the queue.
|
// Channel used to get random IDs for items in the queue.
|
||||||
@@ -90,7 +90,7 @@ func (q *Queue) Len() int {
|
|||||||
// Put an envelope in the queue.
|
// Put an envelope in the queue.
|
||||||
func (q *Queue) Put(from string, to []string, data []byte) (string, error) {
|
func (q *Queue) Put(from string, to []string, data []byte) (string, error) {
|
||||||
if q.Len() >= maxQueueSize {
|
if q.Len() >= maxQueueSize {
|
||||||
return "", queueFullError
|
return "", errQueueFull
|
||||||
}
|
}
|
||||||
|
|
||||||
item := &Item{
|
item := &Item{
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ func TestFullQueue(t *testing.T) {
|
|||||||
|
|
||||||
// This one should fail due to the queue being too big.
|
// This one should fail due to the queue being too big.
|
||||||
id, err := q.Put("from", []string{"to"}, []byte("data"))
|
id, err := q.Put("from", []string{"to"}, []byte("data"))
|
||||||
if err != queueFullError {
|
if err != errQueueFull {
|
||||||
t.Errorf("Not failed as expected: %v - %v", id, err)
|
t.Errorf("Not failed as expected: %v - %v", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
// Error to return when $LISTEN_PID does not refer to us.
|
// Error to return when $LISTEN_PID does not refer to us.
|
||||||
PIDMismatch = errors.New("$LISTEN_PID != our PID")
|
ErrPIDMismatch = errors.New("$LISTEN_PID != our PID")
|
||||||
|
|
||||||
// First FD for listeners.
|
// First FD for listeners.
|
||||||
// It's 3 by definition, but using a variable simplifies testing.
|
// It's 3 by definition, but using a variable simplifies testing.
|
||||||
@@ -36,7 +36,7 @@ func Listeners() ([]net.Listener, error) {
|
|||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"error converting $LISTEN_PID=%q: %v", pidStr, err)
|
"error converting $LISTEN_PID=%q: %v", pidStr, err)
|
||||||
} else if pid != os.Getpid() {
|
} else if pid != os.Getpid() {
|
||||||
return nil, PIDMismatch
|
return nil, ErrPIDMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS"))
|
nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS"))
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func TestWrongPID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setenv(strconv.Itoa(pid), "4")
|
setenv(strconv.Itoa(pid), "4")
|
||||||
if _, err := Listeners(); err != PIDMismatch {
|
if _, err := Listeners(); err != ErrPIDMismatch {
|
||||||
t.Errorf("Did not fail with PID mismatch: %v", err)
|
t.Errorf("Did not fail with PID mismatch: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,8 +75,8 @@ type DB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MissingHeaderErr = errors.New("missing '#chasquid-userdb-v1' header")
|
ErrMissingHeader = errors.New("missing '#chasquid-userdb-v1' header")
|
||||||
InvalidUsernameErr = errors.New("username contains invalid characters")
|
ErrInvalidUsername = errors.New("username contains invalid characters")
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(fname string) *DB {
|
func New(fname string) *DB {
|
||||||
@@ -114,7 +114,7 @@ func Load(fname string) (*DB, []error, error) {
|
|||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
if scanner.Text() != "#chasquid-userdb-v1" {
|
if scanner.Text() != "#chasquid-userdb-v1" {
|
||||||
return nil, nil, MissingHeaderErr
|
return nil, nil, ErrMissingHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
var warnings []error
|
var warnings []error
|
||||||
@@ -194,7 +194,7 @@ func (db *DB) Write() error {
|
|||||||
// TODO: Sort the usernames, just to be friendlier.
|
// TODO: Sort the usernames, just to be friendlier.
|
||||||
for _, user := range db.users {
|
for _, user := range db.users {
|
||||||
if strings.ContainsAny(user.name, illegalUsernameChars) {
|
if strings.ContainsAny(user.name, illegalUsernameChars) {
|
||||||
return InvalidUsernameErr
|
return ErrInvalidUsername
|
||||||
}
|
}
|
||||||
fmt.Fprintf(buf, "%s %s %s\n",
|
fmt.Fprintf(buf, "%s %s %s\n",
|
||||||
user.name, user.scheme.String(),
|
user.name, user.scheme.String(),
|
||||||
@@ -247,7 +247,7 @@ const illegalUsernameChars = "\t\n\v\f\r \xa0\x85"
|
|||||||
// Add a user to the database. If the user is already present, override it.
|
// Add a user to the database. If the user is already present, override it.
|
||||||
func (db *DB) AddUser(name, plainPassword string) error {
|
func (db *DB) AddUser(name, plainPassword string) error {
|
||||||
if !ValidUsername(name) {
|
if !ValidUsername(name) {
|
||||||
return InvalidUsernameErr
|
return ErrInvalidUsername
|
||||||
}
|
}
|
||||||
|
|
||||||
s := scryptScheme{
|
s := scryptScheme{
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func TestLoad(t *testing.T) {
|
|||||||
{"header \\r\\n", "#chasquid-userdb-v1\r\n", false, nil, false},
|
{"header \\r\\n", "#chasquid-userdb-v1\r\n", false, nil, false},
|
||||||
{"header EOF", "#chasquid-userdb-v1", false, nil, false},
|
{"header EOF", "#chasquid-userdb-v1", false, nil, false},
|
||||||
{"missing header", "this is not the header",
|
{"missing header", "this is not the header",
|
||||||
true, MissingHeaderErr, false},
|
true, ErrMissingHeader, false},
|
||||||
{"invalid user", "#chasquid-userdb-v1\nnam\xa0e PLAIN pass\n",
|
{"invalid user", "#chasquid-userdb-v1\nnam\xa0e PLAIN pass\n",
|
||||||
false, nil, true},
|
false, nil, true},
|
||||||
{"too few fields", "#chasquid-userdb-v1\nfield1 field2\n",
|
{"too few fields", "#chasquid-userdb-v1\nfield1 field2\n",
|
||||||
@@ -131,7 +131,7 @@ func testOneLoad(t *testing.T, desc, content string, fatal bool, fatalErr error,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if db != nil && !dbEquals(db, emptyDB) {
|
if db != nil && !dbEquals(db, emptyDB) {
|
||||||
t.Errorf("case %q: DB not empty: %#v", db)
|
t.Errorf("case %q: DB not empty: %#v", desc, db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user