1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-20 15:07:03 +00:00
Files
go-chasquid-smtp/internal/courier/courier.go
Alberto Bertogli 77d547288f Implement couriers
This patch introduces the couriers, which the queue uses to deliver mail.

We have a local courier (using procmail), a remote courier (uses SMTP), and a
router courier that decides which of the two to use based on a list of local
domains.

There are still a few things pending, but they all have their basic
functionality working and tested.
2015-11-13 03:41:06 +00:00

49 lines
1.0 KiB
Go

// Package courier implements various couriers for delivering messages.
package courier
import "strings"
// Courier delivers mail to a single recipient.
// It is implemented by different couriers, for both local and remote
// recipients.
type Courier interface {
Deliver(from string, to string, data []byte) error
}
// Router decides if the destination is local or remote, and delivers
// accordingly.
type Router struct {
Local Courier
Remote Courier
LocalDomains map[string]bool
}
func (r *Router) Deliver(from string, to string, data []byte) error {
d := 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
}