1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-01-28 20:56:03 +00:00

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.
This commit is contained in:
Alberto Bertogli
2015-11-06 02:03:21 +00:00
parent e5c2676f83
commit 77d547288f
10 changed files with 639 additions and 25 deletions

View File

@@ -0,0 +1,48 @@
// 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
}