Documentation
¶
Overview ¶
Package mail gives Outpost's apps email through one Gmail account, each at its own plus address: mail to you+yarr@gmail.com is Yarr's, and so is mail to you+k7q2@gmail.com when k7q2 is an alias Yarr was given. Outpost reads the inbox over IMAP, keeps each routed message for its app, labels it Outpost/<app> in Gmail and archives it. Mail to no route is never touched.
Apps don't hold the account's password. Each is started with OUTPOST_MAIL_URL, a loopback endpoint this package serves, and OUTPOST_MAIL_TOKEN, its own, and reads its messages and sends there. Each route also has an Atom feed, so a feed reader can subscribe to the mail sent to it. See docs/design/mail.md.
Index ¶
- Constants
- Variables
- type Addr
- type Held
- type Keys
- type Message
- type Options
- type Outgoing
- type Route
- type Service
- func (s *Service) AddAlias(service, note, by string) (Route, error)
- func (s *Service) CanSend(service string) bool
- func (s *Service) CheckNow()
- func (s *Service) CheckWait(ctx context.Context) error
- func (s *Service) Close() error
- func (s *Service) Held() (map[string]Held, error)
- func (s *Service) Messages(service string, after int64, limit int) ([]Message, error)
- func (s *Service) PasswordSecret() string
- func (s *Service) Regenerate(service, by string) error
- func (s *Service) RemoveAlias(service, route, by string) error
- func (s *Service) Resolve(service string) (map[string]string, string)
- func (s *Service) Routes(service string) []Route
- func (s *Service) Run(ctx context.Context)
- func (s *Service) Send(ctx context.Context, service string, o Outgoing) (string, error)
- func (s *Service) Serve(ctx context.Context, ln net.Listener) error
- func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Service) SetAccount(address, password, by string) error
- func (s *Service) SetSend(service string, allowed bool, by string) error
- func (s *Service) SetServices(f func() []string)
- func (s *Service) Status() Status
- func (s *Service) TestAccount(ctx context.Context, address, password string) error
- func (s *Service) URL() string
- type Status
Constants ¶
const ( EnvURL = "OUTPOST_MAIL_URL" EnvToken = "OUTPOST_MAIL_TOKEN" )
Environment variables every app is started with.
Variables ¶
var ( ErrNotAllowed = errors.New("this app isn't allowed to send mail; an admin can allow it under the app's Manage menu, in Mail") ErrNotReady = errors.New("no Gmail account is set up; an admin sets one on the portal's Mail page") ErrLimit = fmt.Errorf("this app has sent %d messages in the last 24 hours, its limit", dailyLimit) )
Errors Send returns, which the endpoint turns into statuses.
Functions ¶
This section is empty.
Types ¶
type Keys ¶
Keys is where the account's app password is kept: secrets.db, as a secret bound to no app.
type Message ¶
type Message struct {
ID int64 `json:"id"`
// Route is the plus part it was sent to, and To that whole address.
Route string `json:"route"`
To string `json:"to"`
From Addr `json:"from"`
ReplyTo string `json:"reply_to,omitempty"`
Subject string `json:"subject"`
// MessageID is its Message-ID header, without the angle brackets.
MessageID string `json:"message_id,omitempty"`
Date time.Time `json:"date"`
Received time.Time `json:"received"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
// Attachments counts the parts that weren't kept: files, images.
Attachments int `json:"attachments,omitempty"`
// contains filtered or unexported fields
}
Message is one message routed to an app.
type Options ¶
type Options struct {
// Path is mail.db.
Path string
// Listen is the loopback address the endpoint serves on, which apps are
// told as OUTPOST_MAIL_URL.
Listen string
// Address is config.toml's account, used until the portal sets one.
Address string
// Poll is how often the inbox is checked.
Poll time.Duration
// IMAP and SMTP are the servers, as host:port.
IMAP, SMTP string
// Keys holds the app password, under PasswordSecret.
Keys Keys
PasswordSecret string
// Services lists the declared apps, whose names are routes.
Services func() []string
// Logf receives one line per change and per routed message.
Logf func(format string, v ...any)
// Logger is where the service logs, at levels and with fields. It
// supersedes Logf.
Logger *log.Logger
}
Options configures a Service.
type Outgoing ¶
type Outgoing struct {
To []string `json:"to"`
Cc []string `json:"cc,omitempty"`
Bcc []string `json:"bcc,omitempty"`
// Subject, Text and HTML; at least one of Text and HTML.
Subject string `json:"subject"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
// FromName is the name shown beside the address; unset is the app's.
FromName string `json:"from_name,omitempty"`
// Route is which of the app's addresses it's from; unset is the one
// named after the app.
Route string `json:"route,omitempty"`
// InReplyTo is the id of a message in the app's inbox this answers. It
// threads the reply, and with no To, goes to whoever sent that one.
InReplyTo int64 `json:"in_reply_to,omitempty"`
}
Outgoing is a message an app asks to send.
type Route ¶
type Route struct {
Route string `json:"route"`
Service string `json:"-"`
// Alias says it's a random one, which can be removed; otherwise it's
// the app's name.
Alias bool `json:"alias"`
Note string `json:"note,omitempty"`
// Address is the whole address, "" until there's an account.
Address string `json:"address,omitempty"`
// Feed is the Atom feed of the mail sent to it.
Feed string `json:"feed"`
}
Route is one address an app gets mail at.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the mail endpoint, the inbox reader and their settings. It is safe for concurrent use.
func (*Service) AddAlias ¶
AddAlias gives service a new random address. The note says what it's for, such as the newsletter it was given to.
func (*Service) CheckNow ¶
func (s *Service) CheckNow()
CheckNow asks Run to check the inbox without waiting for the next poll.
func (*Service) CheckWait ¶
CheckWait asks Run to check the inbox now, and waits until it has, or until ctx ends.
func (*Service) PasswordSecret ¶
PasswordSecret is the secret the app password is kept in.
func (*Service) Regenerate ¶
Regenerate gives service a new token. The old one stops working at once.
func (*Service) RemoveAlias ¶
RemoveAlias takes an alias away from service. Mail to it is then left in the inbox, like any other mail to no route.
func (*Service) Resolve ¶
Resolve gives an app its OUTPOST_MAIL_URL and OUTPOST_MAIL_TOKEN, and a revision that changes when its token does, so the supervisor restarts it with the new one.
func (*Service) Routes ¶
Routes lists the addresses service gets mail at: its name, when that can be one, then its aliases, oldest first.
func (*Service) Send ¶
Send sends a message from one of service's addresses. It returns the new message's Message-ID.
func (*Service) ServeHTTP ¶
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the endpoint apps call, and the feeds feed readers fetch.
func (*Service) SetAccount ¶
SetAccount sets the Gmail account. An empty password keeps the one saved. Gmail shows an app password in groups of four, so spaces are taken out.
func (*Service) SetServices ¶
SetServices tells the Service how to list the declared apps, once the supervisor that knows them exists.
func (*Service) TestAccount ¶
TestAccount logs in to address with password, or with the saved one when password is empty, and logs out: what SetAccount would save, tried first.
type Status ¶
type Status struct {
Address string `json:"address,omitempty"`
FromPortal bool `json:"from_portal,omitempty"`
// HasPassword says the app password is saved.
HasPassword bool `json:"has_password"`
// Checked is the last check that finished, and Error why it failed.
Checked time.Time `json:"checked,omitzero"`
Error string `json:"error,omitempty"`
// LastRouted is when a message was last routed to an app.
LastRouted time.Time `json:"last_routed,omitzero"`
// Routed counts messages routed since the daemon started.
Routed int `json:"routed"`
}
Status is how reading the inbox is going.