| 1 | // Package coordinatormail contains offline policy groundwork, not a mail |
| 2 | // adapter. Nothing here provisions a mailbox, sends mail or authorizes actions. |
| 3 | package coordinatormail |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "net/mail" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | var ErrPolicy = errors.New("coordinator mail: policy refused") |
| 12 | |
| 13 | // Scope is resolved from trusted Equipo storage, never from email headers. |
| 14 | // Address must be the authenticated person's currently verified bare address. |
| 15 | type Scope struct { |
| 16 | Team, Person, Address, Coordinator, Project, Task, Thread string |
| 17 | } |
| 18 | |
| 19 | func (s Scope) valid() bool { |
| 20 | return s.Team != "" && s.Person != "" && bareAddress(s.Address) && |
| 21 | s.Coordinator != "" && s.Project != "" && s.Task != "" && s.Thread != "" |
| 22 | } |
| 23 | |
| 24 | func bareAddress(s string) bool { |
| 25 | if strings.ContainsAny(s, "\r\n") { |
| 26 | return false |
| 27 | } |
| 28 | a, err := mail.ParseAddress(s) |
| 29 | return err == nil && a.Name == "" && a.Address == s |
| 30 | } |
| 31 | |
| 32 | // Outbound is a policy snapshot. Re-resolve it immediately before dispatch. |
| 33 | // Recipient is singular by design: no Cc, Bcc, arbitrary recipients or aliases. |
| 34 | type Outbound struct { |
| 35 | Scope |
| 36 | Recipient, Sender, SenderLabel string |
| 37 | Configured, Authorized bool |
| 38 | Verified, Active, OptedIn bool |
| 39 | Suppressed bool |
| 40 | } |
| 41 | |
| 42 | func CheckOutbound(o Outbound) error { |
| 43 | if !o.Scope.valid() || !o.Configured || !o.Authorized || !o.Verified || |
| 44 | !o.Active || !o.OptedIn || o.Suppressed || o.Recipient != o.Address || |
| 45 | !bareAddress(o.Sender) || o.Sender == o.Address || |
| 46 | o.SenderLabel != "Equipo coordinator" { |
| 47 | return ErrPolicy |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // Reply contains ONLY server-resolved facts. An adapter must authenticate the |
| 53 | // transport and verify the sender binding before constructing it. These flags |
| 54 | // are not evidence by themselves and must never be decoded from an HTTP body. |
| 55 | // A signed delivery webhook proves delivery, not the author's identity. |
| 56 | type Reply struct { |
| 57 | Expected, Bound Scope |
| 58 | EventID string |
| 59 | TransportVerified, SenderBound, RouteVerified bool |
| 60 | Active, Automated, FromCoordinator bool |
| 61 | } |
| 62 | |
| 63 | // CheckReply permits importing untrusted discussion into an existing thread. |
| 64 | // It never permits creating tasks, routing instructions or approving actions. |
| 65 | // Quoted text, forwarding and attachments cannot alter the resolved scope. |
| 66 | func CheckReply(r Reply) error { |
| 67 | if !r.Expected.valid() || r.Expected != r.Bound || r.EventID == "" || |
| 68 | !r.TransportVerified || !r.SenderBound || !r.RouteVerified || |
| 69 | !r.Active || r.Automated || r.FromCoordinator { |
| 70 | return ErrPolicy |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | |