mailsync

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

This section is empty.

Variables

View Source
var ErrConflict = errors.New("mailsync: conflict")

ErrConflict is an operation the store's state does not allow now.

View Source
var ErrInvalid = errors.New("mailsync: invalid request")

ErrInvalid marks a request the caller must change.

View Source
var ErrNotSynced = errors.New("mailsync: the account has not synced yet")

ErrNotSynced means the account has no synced folder to compare yet.

View Source
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.

View Source
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 from mailpart: URLs into parts, become cid: parts. A mailpart: image missing from the cache is left out. date is the Date header.

func QueueRemoveDraftCopy

func QueueRemoveDraftCopy(ctx context.Context, tx *store.Tx, accountID int64, uid uint32) error

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.

func (Check) OK

func (c Check) OK() bool

OK reports whether the folder matches 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
}

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) ApplyRules

func (m *Manager) ApplyRules(ctx context.Context, ids []int64) (int, error)

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

func (m *Manager) Copy(ctx context.Context, ids []int64, to int64) error

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

func (m *Manager) Delete(ctx context.Context, ids []int64) error

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

func (m *Manager) DeleteMailbox(ctx context.Context, id int64) error

DeleteMailbox deletes a mailbox, those inside it, and their messages (on Gmail the labels; the messages stay in All Mail).

func (*Manager) DraftsChanged

func (m *Manager) DraftsChanged(accountID int64)

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

func (m *Manager) EraseMailbox(ctx context.Context, id int64) (int, error)

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) FetchBody

func (m *Manager) FetchBody(ctx context.Context, messageID int64) (string, error)

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

func (m *Manager) FetchDisplay(ctx context.Context, messageID int64) (string, error)

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

func (m *Manager) FireReminders(ctx context.Context, now time.Time) error

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) Kick

func (m *Manager) Kick(accountID int64)

Kick tells an account's IMAP actor that offline ops are queued.

func (*Manager) Move

func (m *Manager) Move(ctx context.Context, ids []int64, from, to int64) error

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

func (m *Manager) OutboxChanged(accountID int64)

OutboxChanged wakes an account's sender: a message was queued or retried.

func (*Manager) Reload

func (m *Manager) Reload(ctx context.Context) error

Reload starts actors for accounts without one and stops actors whose account is gone.

func (*Manager) RenameMailbox

func (m *Manager) RenameMailbox(ctx context.Context, id int64, name string) (store.Mailbox, error)

RenameMailbox gives a mailbox a new name in the same place.

func (*Manager) Restart

func (m *Manager) Restart(ctx context.Context, accountID int64) error

Restart stops an account's actor and starts a fresh one with the current settings and password.

func (*Manager) SetFlags

func (m *Manager) SetFlags(ctx context.Context, ids []int64, c store.FlagChange) error

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) Start

func (m *Manager) Start(ctx context.Context) error

Start runs an actor for every account until ctx ends.

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.

func (*Manager) SyncNow

func (m *Manager) SyncNow(accountID int64) error

SyncNow asks an account's actor for a full pass.

func (*Manager) Verify

func (m *Manager) Verify(ctx context.Context, accountID int64) ([]Check, error)

Verify compares an account's synced folders with its server.

func (*Manager) Wait

func (m *Manager) Wait()

Wait blocks until every actor has stopped after the Start context ended.

type TokenSource

type TokenSource interface {
	AccessToken(ctx context.Context, accountID int64) (string, error)
	// Invalidate drops a token the server refused.
	Invalidate(accountID int64)
}

TokenSource gives OAuth access tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL