mailsync

package
v1.40.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 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 stores what an account's protocol backend reports (ADR 0037): folders, server locations, claims, orphans, merges and threads.

Index

Constants

View Source
const InboxOnly = "EXISTS (SELECT 1 FROM accounts WHERE id = ? AND CASE WHEN json_valid(features) THEN json_extract(features, '$.Identity') END = '" + string(backend.IdentityInboxOnly) + "')"

InboxOnly is the condition that the account bound to ? has only an inbox on its server (backend.IdentityInboxOnly, POP3), as its check recorded. Such an account keeps its mail locally (spec #627): a location gone from the server only loses its server_ref, moves are local, and a purge DELEs the message at its inbox location wherever it shows.

View Source
const MaxAttempts = 5

MaxAttempts is how many server refusals an op takes before it is marked failed.

View Source
const PurgeWaits = "EXISTS (SELECT 1 FROM ops o WHERE o.message_id = m.id AND o.kind = 'purge' AND o.state = 'pending')"

PurgeWaits is the condition on messages alias m that a permanent delete of it waits to be pushed: until then CancelPurges can take it back.

View Source
const Purged = "EXISTS (SELECT 1 FROM ops o WHERE o.message_id = m.id AND o.kind = 'purge' AND o.state = 'done')"

Purged is the condition on messages alias m that it was deleted for good; the fetch drops its row.

Variables

View Source
var BackfillPage = 1000

BackfillPage is how many messages one page of a sync asks for, which bounds its memory. Tests lower it.

View Source
var BackfillPages = 2

BackfillPages is how many pages a range backfill takes per sync round before it yields, so new mail, ops and SyncNow wait at most that long; the next round goes on. Tests lower it.

View Source
var BeforeFlagsWrite func()

BeforeFlagsWrite, when set, runs between applySnapshot reading the local flags and writing the server's. Tests only.

View Source
var ErrNotSynced = backend.ErrNotSynced

ErrNotSynced is FetchOlder on a folder whose first sync (at the server's current state) has not completed: the next sync, not a page, fills it.

View Source
var OlderPage = 200

OlderPage is how many messages one FetchOlder imports. Tests lower it.

View Source
var PartFetchBytes int64 = 3 * mail.MaxBodyBytes

PartFetchBytes is how much of one text part a sync downloads: enough encoded bytes for mail.MaxBodyBytes of text even in quoted-printable. Tests lower it.

View Source
var PurgeRoles = []string{"junk", "trash"}

PurgeRoles are the roles a purge can empty.

View Source
var PurgeStep func(step string)

PurgeStep, when set (tests only), runs at each Confirm of a purge's push: "selected", then "stored" (on IMAP: \Deleted set, UID EXPUNGE not sent yet).

View Source
var Roles = []string{"drafts", "sent", "junk", "trash", "archive"}

Roles are the roles a user can give a folder (folders.role_override).

Functions

func AltRole added in v1.5.0

func AltRole(role string) string

AltRole is the detected role that stands in for role when no folder is detected with it: Gmail's All Mail for archive. Else role itself.

func CancelPurges added in v1.37.0

func CancelPurges(msgs ...int64) sqlite.Statement

CancelPurges cancels the purges of the messages that wait to be pushed (a rescue, an undo); its Changes count them. One being pushed still completes.

func ConflictAll added in v1.37.0

func ConflictAll(accountID int64, reason string) sqlite.Statement

ConflictAll ends every unfinished op of the account as a conflict, with reason: the server they were queued for is gone.

func DeleteLocal added in v1.31.0

func DeleteLocal(ctx context.Context, db *store.DB, accountID int64, ids []int64) (hashes []string, tids []int64, err error)

DeleteLocal deletes the local archive's messages ids for good (ADR 0036) and returns the content hashes of their originals, for the caller to remove, and the threads they were in.

func Fetch

func Fetch(ctx context.Context, db *store.DB, recv backend.Receiver, accountID int64, bus *events.Bus, only []backend.FolderRef, core ...Core) (more bool, err error)

Fetch syncs the folders of the account in only (nil: every folder) through recv, then, after a round of every folder, refreshes the Authentication-Results of cached messages. more reports that a backfill has work left: the caller should sync again soon. core, at most one, is core's part in it.

func FetchOlder

func FetchOlder(ctx context.Context, db *store.DB, recv backend.Receiver, accountID, folderID int64, bus *events.Bus, core ...Core) (more bool, err error)

FetchOlder imports one page of the folder's mail that the sync range left out, through recv. It keeps what it imports; the range's backfill only ever adds what is missing, so the next sync neither refetches nor deletes it. more reports whether the server holds still more the folder lacks. core is Fetch's.

func MarkSent added in v1.37.0

func MarkSent(ctx context.Context, db *store.DB, id int64, before *backend.MoveBaseline) error

MarkSent leaves op id as a push cut off after sending does: an attempt counted that may have reached the server, with the destination's baseline before it when before is set. Tests reproduce a lost reply with it.

func NeedsFullFetch added in v1.37.0

func NeedsFullFetch(ctx context.Context, db *store.DB, accountID int64) (bool, error)

NeedsFullFetch reports whether the account's next fetch must cover every folder, also after a restart: an op is unfinished (a move awaits its claim, a change may need the server's state), or a purge is done and its message's row is left for the fetch to drop.

func NextOpDue added in v1.30.0

func NextOpDue(ctx context.Context, db *store.DB, accountID int64, t time.Time) (time.Time, error)

NextOpDue is the account's first undo deadline after t; zero for none.

func PushNewOps

func PushNewOps(ctx context.Context, db *store.DB, recv backend.Receiver, accountID int64, now time.Time) error

PushNewOps is PushOps limited to ops never attempted, for a second round after a fetch: ops that waited for a claim (an Unlocated move), for another server location (a deferred archive) or that were queued meanwhile run, ones that failed are not retried. Without such an op it sends nothing to the server.

func PushOps

func PushOps(ctx context.Context, db *store.DB, recv backend.Receiver, accountID int64, now time.Time) error

PushOps sends the account's pending ops to the server in id order. Ops whose message has no server location yet, or whose dependency is still pending or awaiting its claim, wait for a later round, as do purges whose undo window is still open at now; once the dependency failed or conflicted the op is re-based (depends_on cleared) and runs from the message's current server location. The backend is asked only when an op is ready. Problems with single ops come back as *OpsError; any other error means the connection is unusable and the push stopped.

func QueueFlags added in v1.37.0

func QueueFlags(accountID, msg int64, add, remove []string) sqlite.Statement

QueueFlags queues adding then removing flags (canonical names) on message msg.

func QueueMove added in v1.37.0

func QueueMove(accountID, msg, src, dst int64, due time.Time) sqlite.Statement

QueueMove queues moving message msg out of folder src into dst; dst 0 only takes it out of src (Gmail's archive with All Mail hidden). A non-zero due opens an undo window: the push leaves the move until then. Nothing is queued on an inbox-only account, where a move is local (InboxOnly).

func QueuePurge

func QueuePurge(ctx context.Context, db *store.DB, recv backend.Receiver, accountID int64) error

QueuePurge queues the account's due junk and trash purges as purge ops, which PushOps expunges after checking again. A folder is purged at most once a day, and only on a server whose backend declares the purge feature (IMAP's needs UIDPLUS: a plain EXPUNGE would also remove mail other clients marked \Deleted). When the role's folder is not the one the policy's clock ran for, the clock restarts there and nothing is purged.

func QueuePurgeNow added in v1.37.0

func QueuePurgeNow(accountID, msg, folder int64, role string, due time.Time) sqlite.Statement

QueuePurgeNow queues deleting message msg for good from folder, the account's folder of role (trash, junk), at due, whatever the policy: until then CancelPurges takes it back.

func QueueRestore added in v1.37.0

func QueueRestore(ctx context.Context, db *store.DB, accountID, msg, folder int64, takeBack bool) ([]sqlite.Statement, error)

QueueRestore queues adding message msg to folder again (Gmail: its label back), for one transaction. With takeBack, its archive out of folder that nothing has sent yet is cancelled instead.

func RoleFolder

func RoleFolder(ctx context.Context, db *store.DB, accountID int64, role string) (id int64, name string, err error)

RoleFolder is the account's folder of role, the one place roles resolve: the folder the user gave the role, else the lowest id detected with it, unless the user gave that folder another role or none. Without an archive detected, Gmail's All Mail is it: archiving there takes mail out of the inbox. 0 when there is none.

func SentFolder

func SentFolder(ctx context.Context, db *store.DB, accountID int64) (ref backend.FolderRef, known bool, err error)

SentFolder returns the account's Sent folder (RoleFolder) on the server, or "" when it has none there. known is false before a sync stored any folder: whether it has one is not known yet.

func ShowIn added in v1.30.0

func ShowIn(msg, folder int64, out []int64) []sqlite.Statement

ShowIn returns the statements that show message msg in folder from now (entered_at) and no longer in the folders out; with out nil, nowhere else. With folder 0 it only hides out (Gmail archive with All Mail hidden).

func Sync added in v1.33.0

func Sync(ctx context.Context, db *store.DB, recv backend.Receiver, accountID int64, bus *events.Bus) (more bool, err error)

Sync runs one sync round of the account through recv and stores the change set it reports. Rows committed before an error are still announced.

Types

type Core added in v1.40.0

type Core interface {
	Originals
	// MergedReplyDate is, of threads about to merge into one conversation,
	// the one whose thread_deadlines row the merged conversation keeps (0:
	// none has one). Which are in effect is 回复状态's to say, and only core
	// computes that.
	MergedReplyDate(ctx context.Context, threads []int64) (int64, error)
}

Core is core's part in a sync of one account: it keeps an inbox-only account's originals, and picks the 期限 a thread merge keeps.

type Local added in v1.31.0

type Local struct {
	ID, ThreadID int64
	Merged       []int64 // threads merged into ThreadID
	Moved        []int64 // their messages
}

Local is a message InsertLocal stored, and the threads it merged.

func InsertLocal added in v1.31.0

func InsertLocal(ctx context.Context, db *store.DB, accountID, folderID int64, p *mail.Parsed, size int64, hash string, core Core) (Local, error)

InsertLocal stores p, a local archive's message (or an inbox-only account's sent copy) whose original file hashes to hash, in folderID with no server location (ADR 0036): its thread, attachment list, full-text index and security verdict, as importMessage does. It is old mail: read, with no receive event, and already labeled and scanned, so neither the AI labeler nor the security scanner takes it. core is Fetch's.

type Op added in v1.37.0

type Op struct {
	ID          int64
	Kind, State string // Kind: flags, move or purge. State: pending, awaiting_claim, conflict or failed
	Attempts    int
	LastError   string
}

Op is a change the push has not completed, as the user sees it.

func ListOps added in v1.37.0

func ListOps(ctx context.Context, db *store.DB, accountID int64) ([]Op, error)

ListOps lists the account's ops that are not done (or cancelled), oldest first.

type OpsError

type OpsError struct {
	Problems []string
	Changed  []int64
}

OpsError lists ops that PushOps marked conflict or failed; the push went on. Changed holds messages shown in a different folder because a move ended so.

func (*OpsError) Error

func (e *OpsError) Error() string

type Originals added in v1.34.0

type Originals interface {
	Keep(hash string, raw []byte) error
	// Drop removes the originals of messages deleted for good, best effort.
	Drop(hashes []string)
}

Originals keeps the originals of an inbox-only account's mail, which its server may delete: core stores them as a local archive does, by content_hash, and reads body, attachments and raw from them.

Jump to

Keyboard shortcuts

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