mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
8d763f450d
This is a follow-up on the `go fix` run in commit
0210c5fd92.
That commit recorded the automated updates.
This one applies further simplifications that are mechanical and do not
involve logic changes, but are outside of what `go fix` did.
It also removes some now-obsolete comments and statements.
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
// Package envelope implements functions related to handling email envelopes
|
|
// (basically tuples of (from, to, data).
|
|
package envelope
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"blitiri.com.ar/go/chasquid/internal/set"
|
|
)
|
|
|
|
// Split an user@domain address into user and domain.
|
|
func Split(addr string) (string, string) {
|
|
user, domain, _ := strings.Cut(addr, "@")
|
|
return user, domain
|
|
}
|
|
|
|
// UserOf user@domain returns user.
|
|
func UserOf(addr string) string {
|
|
user, _ := Split(addr)
|
|
return user
|
|
}
|
|
|
|
// DomainOf user@domain returns domain.
|
|
func DomainOf(addr string) string {
|
|
_, domain := Split(addr)
|
|
return domain
|
|
}
|
|
|
|
// DomainIn checks that the domain of the address is on the given set.
|
|
func DomainIn(addr string, locals *set.String) bool {
|
|
domain := DomainOf(addr)
|
|
if domain == "" {
|
|
return true
|
|
}
|
|
|
|
return locals.Has(domain)
|
|
}
|
|
|
|
// AddHeader adds (prepends) a MIME header to the message.
|
|
func AddHeader(data []byte, k, v string) []byte {
|
|
if len(v) > 0 {
|
|
// If the value contains newlines, indent them properly.
|
|
if v[len(v)-1] == '\n' {
|
|
v = v[:len(v)-1]
|
|
}
|
|
v = strings.ReplaceAll(v, "\n", "\n\t")
|
|
}
|
|
|
|
header := []byte(fmt.Sprintf("%s: %s\n", k, v))
|
|
return append(header, data...)
|
|
}
|