diff --git a/channels/email/main.go b/channels/email/main.go index 723e9d1..bf7ccd7 100644 --- a/channels/email/main.go +++ b/channels/email/main.go @@ -4,7 +4,7 @@ // STARTTLS), username, password_ref ("engine:key"), allowed_senders // (comma-separated addresses), authserv_id (the mail provider's // Authentication-Results id, such as "mx.google.com") and poll_interval -// (default 30s). +// (default 30s). An smtp_addr on port 465 uses implicit TLS. package main import ( @@ -13,6 +13,7 @@ import ( "fmt" "io" "log/slog" + "net" "slices" "strings" "time" @@ -23,6 +24,8 @@ import ( "github.com/emersion/go-msgauth/authres" "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "github.com/samcharles93/archipelago/sdk/channel" gatewayv1 "github.com/samcharles93/archipelago/sdk/gateway/v1" @@ -100,9 +103,21 @@ func parseSettings(cfg channel.Config) (settings, error) { return s, nil } +// rejected is a message the channel will never route: it is skipped for the +// life of the process. Any other failure leaves the message unread for the +// next poll. +type rejected struct{ reason string } + +func (r rejected) Error() string { return r.reason } + +func reject(format string, args ...any) error { return rejected{fmt.Sprintf(format, args...)} } + // poll routes each unseen message once. Only routed mail is marked seen, so -// mail the channel ignores keeps its unread state; skipped remembers it for the -// life of the process instead. +// mail the channel ignores keeps its unread state; skipped remembers rejected +// mail for the life of the process instead. Mail that failed for another +// reason, such as the gateway being down, is tried again on the next poll; its +// Message-ID makes the gateway replay a reply it already produced rather than +// run the turn twice. func poll(ctx context.Context, client *imapclient.Client, s settings, chat gatewayv1.ChatServiceClient, skipped map[imap.UID]bool, log *slog.Logger) error { found, err := client.UIDSearch(&imap.SearchCriteria{NotFlag: []imap.Flag{imap.FlagSeen}}, nil).Wait() if err != nil { @@ -120,8 +135,13 @@ func poll(ctx context.Context, client *imapclient.Client, s settings, chat gatew var routed []imap.UID for _, msg := range messages { if err := handle(ctx, s, chat, msg.FindBodySection(section)); err != nil { - log.Warn("email message not routed", "uid", msg.UID, "err", err) - skipped[msg.UID] = true + var r rejected + if errors.As(err, &r) { + log.Warn("email message ignored", "uid", msg.UID, "reason", r.reason) + skipped[msg.UID] = true + } else { + log.Warn("email message not routed; retrying on the next poll", "uid", msg.UID, "err", err) + } continue } routed = append(routed, msg.UID) @@ -140,39 +160,63 @@ func poll(ctx context.Context, client *imapclient.Client, s settings, chat gatew func handle(ctx context.Context, s settings, chat gatewayv1.ChatServiceClient, raw []byte) error { reader, err := mail.CreateReader(strings.NewReader(string(raw))) if err != nil { - return fmt.Errorf("parse: %w", err) + return reject("unparseable message: %v", err) } from, err := reader.Header.AddressList("From") if err != nil || len(from) != 1 { - return errors.New("message needs exactly one From address") + return reject("message needs exactly one From address") } sender := strings.ToLower(from[0].Address) if !s.allowed[sender] { - return fmt.Errorf("sender %q is not allowed", sender) + return reject("sender %q is not allowed", sender) } if !authenticated(reader.Header, s.authservID, sender) { - return fmt.Errorf("sender %q failed DMARC/DKIM at %s", sender, s.authservID) + return reject("sender %q failed DMARC/DKIM at %s", sender, s.authservID) + } + if automatic(reader.Header) { + return reject("message from %q was sent automatically", sender) } text, err := plainText(reader) if err != nil { - return err + return reject("%v", err) } + messageID, _ := reader.Header.MessageID() reply, err := chat.Route(ctx, &gatewayv1.RouteRequest{Message: &gatewayv1.Message{ + MessageId: messageID, + SourceId: messageID, ChannelId: s.username, From: sender, SenderId: sender, Text: text, Platform: "email", }}) - if err != nil { + switch status.Code(err) { + case codes.OK: + case codes.InvalidArgument, codes.PermissionDenied: + return reject("gateway refused the message: %s", status.Convert(err).Message()) + default: return fmt.Errorf("route: %w", err) } if reply.GetText() == "" { return nil } subject, _ := reader.Header.Subject() - messageID, _ := reader.Header.MessageID() - return sendReply(s, sender, subject, messageID, reply.GetText()) + references, _ := reader.Header.MsgIDList("References") + return sendReply(s, sender, subject, messageID, references, reply.GetText()) +} + +// automatic reports whether a message was generated by software rather than +// written to the channel, such as an out-of-office reply or a list digest. +// Answering one can start a loop with another auto-responder. +func automatic(header mail.Header) bool { + if v := strings.ToLower(strings.TrimSpace(header.Get("Auto-Submitted"))); v != "" && v != "no" { + return true + } + switch strings.ToLower(strings.TrimSpace(header.Get("Precedence"))) { + case "bulk", "junk", "list", "auto_reply": + return true + } + return header.Get("List-Id") != "" } // authenticated reports whether the provider's own Authentication-Results @@ -226,7 +270,7 @@ func plainText(reader *mail.Reader) (string, error) { } } -func sendReply(s settings, to, subject, inReplyTo, text string) error { +func sendReply(s settings, to, subject, inReplyTo string, references []string, text string) error { if !strings.HasPrefix(strings.ToLower(subject), "re:") { subject = "Re: " + subject } @@ -240,7 +284,11 @@ func sendReply(s settings, to, subject, inReplyTo, text string) error { } if inReplyTo != "" { header.SetMsgIDList("In-Reply-To", []string{inReplyTo}) + header.SetMsgIDList("References", append(references, inReplyTo)) } + // Marks the reply as automatic so a well-behaved auto-responder does not + // answer it. + header.Set("Auto-Submitted", "auto-replied") header.SetContentType("text/plain", map[string]string{"charset": "utf-8"}) var body strings.Builder @@ -255,7 +303,11 @@ func sendReply(s settings, to, subject, inReplyTo, text string) error { return err } auth := sasl.NewPlainClient("", s.username, s.password) - if err := smtp.SendMail(s.smtpAddr, auth, s.username, []string{to}, strings.NewReader(body.String())); err != nil { + send := smtp.SendMail // STARTTLS, required + if _, port, _ := net.SplitHostPort(s.smtpAddr); port == "465" { + send = smtp.SendMailTLS + } + if err := send(s.smtpAddr, auth, s.username, []string{to}, strings.NewReader(body.String())); err != nil { return fmt.Errorf("send reply: %w", err) } return nil diff --git a/channels/email/main_test.go b/channels/email/main_test.go index 8f61fb4..af42325 100644 --- a/channels/email/main_test.go +++ b/channels/email/main_test.go @@ -30,3 +30,27 @@ func TestAuthenticated(t *testing.T) { }) } } + +func TestAutomatic(t *testing.T) { + for _, tc := range []struct { + name string + headers map[string]string + want bool + }{ + {"written by a person", nil, false}, + {"explicitly not automatic", map[string]string{"Auto-Submitted": "no"}, false}, + {"out-of-office reply", map[string]string{"Auto-Submitted": "auto-replied"}, true}, + {"bulk precedence", map[string]string{"Precedence": "bulk"}, true}, + {"mailing list", map[string]string{"List-Id": ""}, true}, + } { + t.Run(tc.name, func(t *testing.T) { + var h mail.Header + for k, v := range tc.headers { + h.Set(k, v) + } + if got := automatic(h); got != tc.want { + t.Fatalf("automatic = %v, want %v", got, tc.want) + } + }) + } +} diff --git a/channels/email/package.yaml b/channels/email/package.yaml index e9ac3f8..b3d2d95 100644 --- a/channels/email/package.yaml +++ b/channels/email/package.yaml @@ -1,7 +1,7 @@ apiVersion: dev.archie.package.v1 displayName: Email description: "Email chat channel: polls a mailbox over IMAP, routes mail from allowed, DMARC/DKIM-authenticated senders, replies over SMTP. The mail hosts are named by the operator's egress acceptance." -version: 1.0.0 +version: 1.1.0 contributes: extensions: - surface: channel