Documentation
¶
Overview ¶
Package mailsync keeps each account's local store in step with its IMAP server: one actor per account runs the reconcile pass, watches INBOX with IDLE, polls the other mailboxes and fetches bodies on demand (docs/design/sync.md).
Index ¶
- Constants
- Variables
- func DraftMessage(d store.Draft, ident store.Identity, blobs *blob.Store, ...) compose.Message
- func QueueRemoveDraftCopy(ctx context.Context, tx *store.Tx, accountID int64, uid uint32) error
- type Check
- type Config
- type Manager
- func (m *Manager) ActOnAnnounced(ctx context.Context, id int64, action string) error
- func (m *Manager) ApplyRules(ctx context.Context, ids []int64) (int, error)
- func (m *Manager) Copy(ctx context.Context, ids []int64, to int64) error
- func (m *Manager) CreateMailbox(ctx context.Context, accountID int64, name string, parentID *int64) (store.Mailbox, error)
- func (m *Manager) Delete(ctx context.Context, ids []int64) error
- func (m *Manager) DeleteMailbox(ctx context.Context, id int64) error
- func (m *Manager) DraftsChanged(accountID int64)
- func (m *Manager) EraseMailbox(ctx context.Context, id int64) (int, error)
- func (m *Manager) EraseOld(ctx context.Context, now time.Time) error
- func (m *Manager) FetchBody(ctx context.Context, messageID int64) (string, error)
- func (m *Manager) FetchDisplay(ctx context.Context, messageID int64) (string, error)
- func (m *Manager) FireReminders(ctx context.Context, now time.Time) error
- func (m *Manager) Kick(accountID int64)
- func (m *Manager) MarkMailboxRead(ctx context.Context, id int64) (int, error)
- func (m *Manager) Move(ctx context.Context, ids []int64, from, to int64) error
- func (m *Manager) MoveMailbox(ctx context.Context, id int64, parentID *int64) (store.Mailbox, error)
- func (m *Manager) OutboxChanged(accountID int64)
- func (m *Manager) Reload(ctx context.Context) error
- func (m *Manager) RenameMailbox(ctx context.Context, id int64, name string) (store.Mailbox, error)
- func (m *Manager) Restart(ctx context.Context, accountID int64) error
- func (m *Manager) SetFlags(ctx context.Context, ids []int64, c store.FlagChange) error
- func (m *Manager) SetMailboxRole(ctx context.Context, id int64, role api.MailboxRole) (store.Mailbox, error)
- func (m *Manager) Start(ctx context.Context) error
- func (m *Manager) Status(accountID int64) []api.SyncStatus
- func (m *Manager) SyncNow(accountID int64) error
- func (m *Manager) Verify(ctx context.Context, accountID int64) ([]Check, error)
- func (m *Manager) Wait()
- type TokenSource
Constants ¶
const CodeApplyFailed = "sync.apply_failed"
CodeApplyFailed is the problem code of a store write that fails while it applies the server's state: the mailbox list, new headers, flag and label changes, removals and the sync state of a pass (ADR-0049). The record's MESSAGE is the error's text; the code names no mail.
Variables ¶
var ErrConflict = errors.New("mailsync: conflict")
ErrConflict is an operation the store's state does not allow now.
var ErrInvalid = errors.New("mailsync: invalid request")
ErrInvalid marks a request the caller must change.
var ErrNotSynced = errors.New("mailsync: the account has not synced yet")
ErrNotSynced means the account has no synced folder to compare yet.
var ErrOffline = errors.New("mailsync: account is offline")
ErrOffline means the account is not connected, so a body that is not stored locally cannot be fetched now.
var ErrReadOnly = errors.New("account is read-only")
ErrReadOnly refuses a change to a read-only account's mail.
Functions ¶
func DraftMessage ¶
func DraftMessage(d store.Draft, ident store.Identity, blobs *blob.Store, parts *render.PartsCache, date time.Time) compose.Message
DraftMessage turns a draft into what compose.Build writes (docs/design/send.md, Building a message): From and Reply-To come from the identity, attachments are read from blobs, and quoted inline images, which the draft's HTML shows as data: or mailpart: URLs, become cid: parts. A mailpart: image missing from the cache is left out. date is the Date header.
func QueueRemoveDraftCopy ¶
QueueRemoveDraftCopy queues, in tx, the deletion of a draft's server copy at uid in the account's Drafts mailbox. It does nothing for uid 0 or an account without a Drafts mailbox. Kick the account afterwards.
Types ¶
type Check ¶
type Check struct {
MailboxID int64
Path string
Server, Local int
MissingLocally []uint32 // server UIDs with no local message
MissingOnServer []uint32 // local UIDs the server no longer has
FlagDiffs int
LabelDiffs int // Gmail
}
Check is how one synced folder compares with the server.
type Config ¶
type Config struct {
PollInterval time.Duration // STATUS polling of mailboxes IDLE does not watch [5m]
// BodyIdle closes the body connection (C3) after this long without a
// request [2m].
BodyIdle time.Duration
// NoPrefetch turns off C3's prefetch of recent INBOX and VIP bodies
// (ADR-0037), for tests that watch the body connection.
NoPrefetch bool
// Now is the sync window's clock (ADR-0016): the day a pass searches
// SINCE and its sync state's time [time.Now]. Replay tests stop it at
// the recording's time.
Now func() time.Time
// GmailPoll is the Gmail accounts' polling interval [1m]: Gmail's IDLE
// reports new mail but not label or flag changes made elsewhere.
GmailPoll time.Duration
IdleMax time.Duration // re-issue IDLE before servers time it out [25m]
Chunk int // UIDs per header fetch [500]
MinBackoff time.Duration // first retry after a failure [2s]
MaxBackoff time.Duration // longest retry interval [5m]
// InsecureSkipVerify accepts any TLS certificate; for test servers only
// (FROSTMAIL_INSECURE_TLS=1).
InsecureSkipVerify bool
// Trace, if set, receives each account's raw IMAP exchange.
Trace func(accountID int64) io.Writer
// DraftQuiet is how long a draft must be unchanged before its server
// copy is written [5s].
DraftQuiet time.Duration
// Parts is the parts cache quoted inline images are read from when a
// draft is built; nil leaves them out.
Parts *render.PartsCache
// Tokens gives OAuth accounts their access tokens (oauth.Manager); nil
// leaves OAuth accounts unable to sign in.
Tokens TokenSource
// Announce shows an account's new inbox mail on the desktop
// (notify.Desktop); nil announces nothing.
Announce func(ctx context.Context, accountID int64, mail []notify.Mail) error
// SendRetry is the unit of the outbox's retry schedule [1m]: one unit
// while the server cannot be reached, else 1, 2, 5 and 15 units by
// attempt, then 60.
SendRetry time.Duration
// Problems, when set, records CodeApplyFailed in the local journal when
// a store write that applies the server's state fails (ADR-0049); nil
// records nothing.
Problems *problems.Recorder
// Activity, when set, records what sync does in the background
// (ADR-0055); nil records nothing.
Activity *activity.Hub
}
Config tunes sync. Zero fields take the defaults in brackets.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager runs one actor per account.
func New ¶
func New(db *store.DB, sec secrets.Store, blobs *blob.Store, log *slog.Logger, cfg Config, publish func([]api.EventEnvelope)) *Manager
New returns a Manager. publish delivers transient events (sync.progress); durable events go through the store as usual.
func (*Manager) ActOnAnnounced ¶ added in v0.8.0
ActOnAnnounced does what a notification's button asks of its message: notify.ActionArchive moves it to its account's archive and notify.ActionTrash to its Trash. It does nothing to a message that is gone or already in Trash, where a delete would erase it, and nothing on an account without the mailbox, where Trash would erase too. The check and the move are one transaction.
func (*Manager) ApplyRules ¶
ApplyRules runs the enabled rules on chosen messages now (rule.apply) and returns how many met a rule's conditions. Messages of a read-only account are refused with ErrReadOnly.
func (*Manager) Copy ¶
Copy copies messages to mailbox to, which must belong to their account. On Gmail it adds the label to messages in All Mail at once (only labels can be copied into); elsewhere it queues a COPY and changes nothing locally: the copies are new messages the destination's sync fetches after the replay. Messages already in to are skipped.
func (*Manager) CreateMailbox ¶
func (m *Manager) CreateMailbox(ctx context.Context, accountID int64, name string, parentID *int64) (store.Mailbox, error)
CreateMailbox makes a mailbox at the top level or inside parentID.
func (*Manager) Delete ¶
Delete moves messages to their account's Trash, or expunges those already there (and everything, on an account without a Trash mailbox).
func (*Manager) DeleteMailbox ¶
DeleteMailbox deletes a mailbox, those inside it, and their messages (on Gmail the labels; the messages stay in All Mail).
func (*Manager) DraftsChanged ¶
DraftsChanged tells an account's IMAP actor that a draft changed, so it saves the server copy once the draft is quiet.
func (*Manager) EraseMailbox ¶
EraseMailbox deletes every message of a trash or junk mailbox for good (Erase Deleted Items, Erase Junk Mail) and returns how many: they are hidden at once and expunged when the op replays.
func (*Manager) EraseOld ¶ added in v0.6.0
EraseOld erases the mail that has been at least the account's chosen number of days in its Trash or Junk mailbox (ADR-0051, Erase Deleted Items and Erase Junk Mail): as mailbox.erase does, the old members are hidden at once and one expunge op is queued for them, in one transaction per mailbox. It looks only at the mailbox that has the trash or junk role when it runs, and never at a read-only account's mail. One account's error is logged and the pass goes on; the first error is returned.
func (*Manager) FetchBody ¶
FetchBody makes sure a message's whole raw body is in the blob store, asking its account's actor to fetch it if needed, and returns the blob ID.
func (*Manager) FetchDisplay ¶
FetchDisplay makes sure what a reader shows of a message is in the blob store, its whole body or else its display copy (ADR-0037), asking its account's actor to fetch the display copy if needed, and returns the blob ID.
func (*Manager) FireReminders ¶
FireReminders fires the Remind Me reminders due by now (ADR-0025, docs/design/organize.md, Remind Me), including those that fell due while maild was stopped. For each account, one transaction drops the reminders, lists the messages at now, and moves those that left the inbox back to it (an op; a label edit on Gmail). The account's notifications then announce them with "Reminder" before the subject. A read-only account's messages come back to the top without moving.
func (*Manager) MarkMailboxRead ¶ added in v0.8.0
MarkMailboxRead marks every unread message of a mailbox read (Mail.app's Mark All Messages as Read), as SetFlags does for the messages it is given: at once in the store, with flags ops queued for the server. The unread messages are read in the transaction that marks them. It returns how many it marked.
func (*Manager) Move ¶
Move moves messages to mailbox to, which must belong to their account. from is the mailbox they leave on a Gmail account (0 for the default); elsewhere a message is in one mailbox and from is ignored.
func (*Manager) MoveMailbox ¶
func (m *Manager) MoveMailbox(ctx context.Context, id int64, parentID *int64) (store.Mailbox, error)
MoveMailbox puts a mailbox inside parentID, or at the top level.
func (*Manager) OutboxChanged ¶
OutboxChanged wakes an account's sender: a message was queued or retried.
func (*Manager) Reload ¶
Reload starts actors for accounts without one and stops actors whose account is gone.
func (*Manager) RenameMailbox ¶
RenameMailbox gives a mailbox a new name in the same place.
func (*Manager) Restart ¶
Restart stops an account's actor and starts a fresh one with the current settings and password.
func (*Manager) SetFlags ¶
SetFlags changes flags locally and queues the change for each account the messages belong to.
func (*Manager) SetMailboxRole ¶
func (m *Manager) SetMailboxRole(ctx context.Context, id int64, role api.MailboxRole) (store.Mailbox, error)
SetMailboxRole makes a mailbox its account's mailbox of role (Use This Mailbox For). Gmail's mailboxes keep their roles.
func (*Manager) Status ¶
func (m *Manager) Status(accountID int64) []api.SyncStatus
Status returns the sync state of one account, or of all when accountID is 0.