mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-01-09 17:55:57 +00:00
Introduce an "envelope" package
This patch introduces an "envelope" package which, for now, provides simple utilities for getting the user and domain of an address. It also changes the couriers to use it (but other implementations remain, will be moved over in subsequent patches).
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Package courier implements various couriers for delivering messages.
|
||||
package courier
|
||||
|
||||
import "strings"
|
||||
import "blitiri.com.ar/go/chasquid/internal/envelope"
|
||||
|
||||
// Courier delivers mail to a single recipient.
|
||||
// It is implemented by different couriers, for both local and remote
|
||||
@@ -19,30 +19,10 @@ type Router struct {
|
||||
}
|
||||
|
||||
func (r *Router) Deliver(from string, to string, data []byte) error {
|
||||
d := domainOf(to)
|
||||
d := envelope.DomainOf(to)
|
||||
if r.LocalDomains[d] {
|
||||
return r.Local.Deliver(from, to, data)
|
||||
} else {
|
||||
return r.Remote.Deliver(from, to, data)
|
||||
}
|
||||
}
|
||||
|
||||
// Split an user@domain address into user and domain.
|
||||
func split(addr string) (string, string) {
|
||||
ps := strings.SplitN(addr, "@", 2)
|
||||
if len(ps) != 2 {
|
||||
return addr, ""
|
||||
}
|
||||
|
||||
return ps[0], ps[1]
|
||||
}
|
||||
|
||||
func userOf(addr string) string {
|
||||
user, _ := split(addr)
|
||||
return user
|
||||
}
|
||||
|
||||
func domainOf(addr string) string {
|
||||
_, domain := split(addr)
|
||||
return domain
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/envelope"
|
||||
"blitiri.com.ar/go/chasquid/internal/trace"
|
||||
)
|
||||
|
||||
@@ -36,13 +37,17 @@ func (p *Procmail) Deliver(from string, to string, data []byte) error {
|
||||
defer tr.Finish()
|
||||
|
||||
// Get the user, and sanitize to be extra paranoid.
|
||||
user := sanitizeForProcmail(userOf(to))
|
||||
tr.LazyPrintf("%s -> %s (%s)", from, user, to)
|
||||
user := sanitizeForProcmail(envelope.UserOf(to))
|
||||
domain := sanitizeForProcmail(envelope.DomainOf(to))
|
||||
tr.LazyPrintf("%s -> %s (%s @ %s)", from, user, to, domain)
|
||||
|
||||
// Prepare the command, replacing the necessary arguments.
|
||||
replacer := strings.NewReplacer(
|
||||
"%user%", user,
|
||||
"%domain%", domain)
|
||||
args := []string{}
|
||||
for _, a := range MailDeliveryAgentArgs {
|
||||
args = append(args, strings.Replace(a, "%user%", user, -1))
|
||||
args = append(args, replacer.Replace(a))
|
||||
}
|
||||
cmd := exec.Command(MailDeliveryAgentBin, args...)
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ func TestProcmail(t *testing.T) {
|
||||
MailDeliveryAgentArgs = []string{dir + "/%user%"}
|
||||
|
||||
p := Procmail{}
|
||||
err = p.Deliver("from@x", "to@y", []byte("data"))
|
||||
|
||||
err = p.Deliver("from@x", "to@local", []byte("data"))
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver: %v", err)
|
||||
}
|
||||
@@ -36,7 +37,8 @@ func TestProcmailTimeout(t *testing.T) {
|
||||
procmailTimeout = 100 * time.Millisecond
|
||||
|
||||
p := Procmail{}
|
||||
err := p.Deliver("from", "to", []byte("data"))
|
||||
|
||||
err := p.Deliver("from", "to@local", []byte("data"))
|
||||
if err != timeoutError {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/envelope"
|
||||
"blitiri.com.ar/go/chasquid/internal/trace"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,7 @@ func (s *SMTP) Deliver(from string, to string, data []byte) error {
|
||||
defer tr.Finish()
|
||||
tr.LazyPrintf("%s -> %s", from, to)
|
||||
|
||||
mx, err := lookupMX(domainOf(to))
|
||||
mx, err := lookupMX(envelope.DomainOf(to))
|
||||
if err != nil {
|
||||
return tr.Errorf("Could not find mail server: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user