backend

package
v1.33.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 5 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 backend is the seam between Godwit and a mail protocol (ADR 0037). A protocol backend (GLOSSARY: 协议后端) speaks its protocol and hands over protocol-neutral change sets; storing them, claiming, orphans, merging and the order of a sync round stay local. This package and its backends depend on neither core nor store.

An account has one Receiver and one Sender. A backend owns its connections: it dials, reconnects and keeps sessions itself, takes credentials from the CredentialsFunc it is given, and reports refused credentials as ErrAuthFailed.

Index

Constants

View Source
const Cancelled = "cancelled"

Cancelled is the Reason of a Purge its Confirm called off.

Variables

AllFeatures lists every Feature, in order.

View Source
var ErrAuthFailed = errors.New("authentication failed")

ErrAuthFailed means the server refused the credentials. Core then refreshes a token and retries, or marks the account as needing reauthorization. Wrap it; server text is never matched.

View Source
var ErrFolderReset = fmt.Errorf("%w: the folder was reset on the server", ErrNotFound)

ErrFolderReset is the ErrNotFound of a location whose folder the server reset since the sync that stored it (IMAP's UIDVALIDITY changed): the next sync reports the FolderReset.

View Source
var ErrNotFound = errors.New("backend: not found on the server")

ErrNotFound means a server reference no longer names anything on the server.

View Source
var ErrNotSynced = errors.New("folder not synced yet")

ErrNotSynced is Sync with SyncRequest.Older on a folder whose first sync at the server's current state has not completed: a full sync comes first.

View Source
var ErrUncertain = errors.New("backend: the server may have accepted the message")

ErrUncertain means the whole message reached the server but its verdict did not come back: it may have been accepted. Retrying could deliver it twice (ADR 0006).

View Source
var ErrUnsupported = errors.New("backend: not supported")

ErrUnsupported is Watch's answer when the server cannot push: core polls at Description.PollInterval instead.

Functions

This section is empty.

Types

type AddToFolder

type AddToFolder struct {
	At Location
	To FolderRef
}

AddToFolder gives the message at At one more location, in folder To: the same message, not a copy (Gmail labels, JMAP mailboxIds).

type Append

type Append struct {
	To        FolderRef
	Raw       []byte
	Flags     Flags
	Date      time.Time // when the server received it; zero for now
	MessageID string    // without angle brackets
}

Append stores Raw into folder To, as with a sent copy. A backend does not store it again when To already holds a message with MessageID and Raw's size.

type Change

type Change interface {
	// contains filtered or unexported methods
}

Change is one entry of a change set: FolderUpdated, FolderGone, FolderReset, MessageAppeared, MessageGone, FlagsChanged or FolderSnapshot.

type CheckResult

type CheckResult struct {
	Host    string // the host the backend actually talks to, such as JMAP's session host
	Folders int
	Description
}

CheckResult is the add-account check's finding.

type Content

type Content struct {
	Size   int64
	Date   time.Time // when the server received it
	Header []byte    // the SyncRequest.HeaderFields the message has, raw
	// Structure is the MIME structure; nil when the backend cannot give one,
	// and then Raw holds the whole message.
	Structure *Part
	Text      map[PartRef][]byte // the first SyncRequest.TextBytes of each SyncRequest.TextParts part
	Raw       []byte
}

Content is what a sync stores of a message.

type ContentRequest

type ContentRequest struct {
	Location
	Part         PartRef  // "" for the whole message
	HeaderFields []string // non-nil: only these header fields of the message
}

ContentRequest names content to fetch: the whole message, one part, or some header fields.

type Copy

type Copy struct {
	At Location
	To FolderRef
}

Copy puts an independent copy of the message at At into folder To.

type Credentials

type Credentials struct {
	Username, Password, Token string
}

Credentials sign in to an account: Username with either Password (IMAP LOGIN, SMTP AUTH, HTTP Basic) or Token (XOAUTH2, HTTP Bearer).

type CredentialsFunc

type CredentialsFunc func(ctx context.Context) (Credentials, error)

CredentialsFunc gives the account's current credentials. A backend calls it for each sign-in, so a token core refreshed after ErrAuthFailed is used.

type Cursor

type Cursor string

Cursor is an opaque sync position, at the account or at one folder. The backend decides which it uses; local storage keeps it as it is given.

type Delete

type Delete struct{ At Location }

Delete deletes the message at At the server's own way: to its trash where it has one.

type Description

type Description struct {
	Features     map[Feature]Support // one entry for each of AllFeatures
	Identity     Identity
	PollInterval time.Duration // how often to sync when not woken by Watch
}

Description is what a backend declares of an account. The protocol fixes part of Features; the rest depends on the server, so core records it after connecting (ADR 0034's moments: the add-account check, a server change, a sign-in).

type Feature

type Feature string

Feature is something a backend may or may not do on an account (GLOSSARY: 协议能力). A difference that changes the user's data is a different Feature; one of precision or matching is a note on the same Feature.

const (
	FeatureMove             Feature = "move"
	FeatureCopy             Feature = "copy"          // an independent copy
	FeatureAddToFolder      Feature = "add_to_folder" // the same message in one more folder
	FeatureRemoveFromFolder Feature = "remove_from_folder"
	FeaturePurge            Feature = "purge"
	FeatureAppend           Feature = "append"
	FeatureSeen             Feature = "seen"
	FeatureFlagged          Feature = "flagged"
	FeatureAnswered         Feature = "answered"
	FeatureDraft            Feature = "draft"
	FeatureKeywords         Feature = "keywords"
	FeatureFolderCreate     Feature = "folder_create"
	FeatureFolderRename     Feature = "folder_rename"
	FeatureFolderDelete     Feature = "folder_delete" // the folder and the mail in it
	FeatureLabelDelete      Feature = "label_delete"  // the folder only; its mail stays
	FeatureSavesSent        Feature = "saves_sent"    // the server stores sent copies itself
	FeaturePush             Feature = "push"
	// FeatureAllMail: every message stays in the account's all-mail store
	// whatever folders it is in (Gmail's All Mail): archiving takes it out
	// of the inbox only, and one left in no folder synced still exists.
	// Unsupported, archiving moves to the archive folder and a message in
	// no folder is gone.
	FeatureAllMail Feature = "all_mail"
)

type Flags

type Flags struct {
	Seen, Flagged, Answered, Draft bool
	Keywords                       []string // custom keywords, as the server spells them
}

Flags are a message's flags, written protocol-neutrally. Local storage converts them to its own \Seen spelling at the boundary.

type FlagsChanged

type FlagsChanged struct {
	Location
	Flags Flags
}

FlagsChanged reports a message's flags as they now are.

type Folder

type Folder struct {
	Ref    FolderRef
	Name   string    // the full name the user sees, segments joined by Delim
	Delim  string    // the hierarchy delimiter in Name, "" when flat
	Parent FolderRef // "" at the top level
	Role   Role
}

Folder is a folder on the server.

type FolderError

type FolderError struct {
	Folder FolderRef
	Err    error
}

FolderError is the server's refusal of one folder in a sync round. The round went on with the other folders; Sync returns the refusals joined.

func (*FolderError) Error

func (e *FolderError) Error() string

func (*FolderError) Unwrap

func (e *FolderError) Unwrap() error

type FolderGone

type FolderGone struct{ Ref FolderRef }

FolderGone reports a folder deleted on the server.

type FolderRef

type FolderRef string

FolderRef is an opaque reference to a folder on the server, stable across a rename: IMAP uses the name, other protocols the server's folder id.

type FolderReset

type FolderReset struct{ Ref FolderRef }

FolderReset voids every server location in the folder (IMAP's UIDVALIDITY changed, JMAP cannot calculate changes): local storage forgets them and claims the messages again as they reappear. The folder's cursor is cleared.

type FolderSnapshot

type FolderSnapshot struct {
	Folder   FolderRef
	Messages map[MessageRef]Flags
}

FolderSnapshot is the whole folder as the server now has it: every location and its flags. Local locations missing from it are gone; local flags that differ changed. Locations local storage does not hold are ignored.

type FolderState

type FolderState struct {
	Cursor Cursor
	Synced bool      // a first sync at this cursor completed
	Filled time.Time // a range backfill completed down to this date; zero for none
}

FolderState is what local storage keeps of a folder's sync: the backend's cursor and the local progress the backend reports back. A backend whose cursor is the account's (JMAP) reports each folder once with an empty Cursor and Synced: the folder then has no generations, so its locations stay current until a FolderReset or a change removes them.

type FolderUpdated

type FolderUpdated struct{ Folder }

FolderUpdated reports a folder that appeared or changed (a rename keeps Ref).

type Identity

type Identity string

Identity is what identifies a message on the account.

const (
	// IdentityStable: Location.ServerID is stable within the account (Gmail
	// IMAP, JMAP); one message may have several locations.
	IdentityStable Identity = "stable"
	// IdentityLocation: only the location identifies it (plain IMAP, EWS).
	IdentityLocation Identity = "location"
	// IdentityInboxOnly: only an inbox (POP3); mail gone from the server
	// stays local.
	IdentityInboxOnly Identity = "inbox_only"
)

type Level

type Level string

Level is how far a Feature is supported.

const (
	LevelSupported   Level = "supported"
	LevelNoted       Level = "noted" // supported, with a note
	LevelUnsupported Level = "unsupported"
)

type Local

type Local interface {
	// KnownIDs reports which of ids (Location.ServerID) are stored.
	KnownIDs(ctx context.Context, ids []string) (map[string]bool, error)
	// HeldRefs reports which of refs in folder are stored.
	HeldRefs(ctx context.Context, folder FolderRef, refs []MessageRef) (map[MessageRef]bool, error)
	// Held is every location stored in folder, with its ServerID ("" when
	// none is stored).
	Held(ctx context.Context, folder FolderRef) (map[MessageRef]string, error)
	// Lost is the ServerIDs of stored messages left at no server location,
	// which local storage is about to delete as gone. A backend that still
	// finds one reports it as a MessageAppeared without content; local
	// storage takes its flags from the folder's next snapshot, not from it.
	Lost(ctx context.Context) ([]string, error)
}

Local answers what local storage already holds, so a backend fetches no more than it must. A nil Local holds nothing.

type Location

type Location struct {
	Folder FolderRef
	Ref    MessageRef
	// ServerID is the message's identity when the account's is IdentityStable,
	// else "". A backend may use it to find the message's other locations.
	ServerID string
}

Location is one server location of a message.

type MessageAppeared

type MessageAppeared struct {
	Location
	Flags Flags
	// Backfill is true for older mail filled in, false for a new arrival.
	// Notifications, rules and message.received follow it.
	Backfill bool
	// Content is nil when SyncRequest.Local reported ServerID as known: only
	// a new location of a stored message. It is nil too for a location Local
	// holds without a ServerID (Local.Held): the change gives it its ServerID.
	Content *Content
}

MessageAppeared reports a message at a location local storage may not know.

type MessageGone

type MessageGone struct{ Location }

MessageGone reports a location gone from the server.

type MessageRef

type MessageRef string

MessageRef is an opaque server location of a message within one folder (GLOSSARY: 服务器位置): IMAP encodes UIDVALIDITY and UID, JMAP uses the Email id. Only the backend that gave it reads it.

type Move

type Move struct {
	At Location
	To FolderRef
}

Move moves the message from At to folder To.

type Op

type Op interface {
	// contains filtered or unexported methods
}

Op is one write-back of a local change: SetFlags, Move, Copy, AddToFolder, RemoveFromFolder, Delete, Purge or Append.

type Outcome

type Outcome string

Outcome is how a write-back ended.

const (
	// Done: Result.Ref is the message's new location in the target folder
	// (Move, Copy, AddToFolder, Append), "" for the others.
	Done Outcome = "done"
	// Unlocated: done, but the server did not say where the message is now.
	// Local storage claims it by Message-ID and size (ADR 0005); a round with
	// one pushes a second time.
	Unlocated Outcome = "unlocated"
	// Failed: the server refused it for good; retrying will not help.
	Failed Outcome = "failed"
	// Unsupported: this backend or server cannot do it; Result.Reason says why.
	Unsupported Outcome = "unsupported"
)

type Page

type Page struct {
	Changes []Change
	Account Cursor                    // the account's new cursor; "" keeps it
	Folders map[FolderRef]FolderState // the new state of the folders this page advanced
}

Page is one page of a change set. Local storage writes its changes and its cursors in one transaction.

type Part

type Part struct {
	Ref               PartRef // for Fetch; set on leaves
	ContentType       string  // lower case "type/subtype"
	Params            map[string]string
	Disposition       string // lower case, such as "attachment"; "" if none
	DispositionParams map[string]string
	ContentID         string
	Encoding          string // Content-Transfer-Encoding, lower case
	Size              int64  // encoded size in bytes
	Children          []Part // the parts of a multipart, in order
}

Part is a node of a message's MIME structure.

type PartRef

type PartRef string

PartRef is an opaque reference to one MIME part of a message, from Part.Ref.

type Purge

type Purge struct {
	At Location
	// Confirm, if set, is asked again before each step that brings the
	// deletion closer, the last one just before it cannot be undone. When it
	// answers false the backend undoes what this or an earlier Apply of the
	// purge left behind and returns Failed with Reason Cancelled.
	Confirm func() bool
}

Purge deletes the message at At for good (永久删除).

type Receiver

type Receiver interface {
	// Sync runs one sync round, handing the change set to commit page by
	// page. more reports that a backfill used its Pages with work left.
	// An error from commit stops the round and is returned.
	Sync(ctx context.Context, req SyncRequest, commit func(Page) error) (more bool, err error)
	// Fetch reads content by server reference; ErrNotFound when it is gone.
	Fetch(ctx context.Context, req ContentRequest) (io.ReadCloser, error)
	// Headers reads the header fields of the messages at refs in folder, in
	// as few requests as the protocol allows; a ref gone from the server is
	// missing from the result. A *FolderError is the server refusing folder.
	Headers(ctx context.Context, folder FolderRef, refs []MessageRef, fields []string) (map[MessageRef][]byte, error)
	// Find lists the messages in folder whose Message-ID (without angle
	// brackets) is messageID and, when size > 0, of that size.
	Find(ctx context.Context, folder FolderRef, messageID string, size int64) ([]MessageRef, error)
	// Apply writes one Op back. ErrNotFound: the op's location no longer
	// names the message (its folder was reset, or with IdentityStable the
	// message left that folder), and nothing was done. A *FolderError: the
	// server refused a folder the op had to read first, and nothing was done.
	// Any other error means the outcome is not known (a lost connection): the
	// op is tried again later.
	Apply(ctx context.Context, op Op) (Result, error)
	// Watch calls wake when the server has changes, with the folders that
	// changed (nil: anywhere), until ctx ends or the connection fails. It
	// wakes once as soon as it is watching, for what changed before: a sync
	// after that wake misses nothing. ErrUnsupported: the server cannot push.
	Watch(ctx context.Context, wake func(folders []FolderRef)) error
	// Check is the add-account check.
	Check(ctx context.Context) (CheckResult, error)
	// Describe declares the account's features, identity and poll interval.
	Describe(ctx context.Context) (Description, error)
	Close() error
}

Receiver is an account's receiving backend.

type RejectedError

type RejectedError struct {
	Stage     string // the refused step, such as SMTP's "MAIL", "RCPT" or "DATA"
	Recipient string // the refused address, "" when the refusal is not about one
	Code      int    // the reply code, such as 550: 4xx refuses for now, 5xx for good
	Enhanced  string // the enhanced status code, such as "5.1.1"; "" if none
	Message   string // the server's text as it sent it
}

RejectedError is the server's refusal of a message it was given to deliver. The server has not accepted the message.

func (*RejectedError) Error

func (e *RejectedError) Error() string

type RemoveFromFolder

type RemoveFromFolder struct{ At Location }

RemoveFromFolder takes the location At away; the message stays in its others.

type Result

type Result struct {
	Outcome Outcome
	Ref     MessageRef
	Reason  string // for Failed and Unsupported: a Unsupported reason is the feature's
}

Result is a write-back's outcome.

type Role

type Role string

Role is what a folder is for, as the backend recognizes it; "" for none.

const (
	RoleInbox   Role = "inbox"
	RoleSent    Role = "sent"
	RoleDrafts  Role = "drafts"
	RoleTrash   Role = "trash"
	RoleJunk    Role = "junk"
	RoleArchive Role = "archive"
	RoleAll     Role = "all"     // every message, such as Gmail's All Mail
	RoleFlagged Role = "flagged" // a server view of flagged mail, such as Gmail's Starred
)

type Sender

type Sender interface {
	// Send delivers raw to rcpts. nil: accepted. ErrUncertain: it may have
	// been. *RejectedError: refused. Any other error came before the server
	// could accept it, so retrying is safe.
	Send(ctx context.Context, from string, rcpts []string, raw []byte) error
	// SavesSent reports that the server stores a sent copy itself; else the
	// Receiver Appends one to the sent folder.
	SavesSent() bool
}

Sender is an account's sending backend. The outbox, approvals and sending at most once stay in core (ADR 0003, 0006).

type SetFlags

type SetFlags struct {
	At          Location
	Add, Remove Flags
}

SetFlags adds and removes flags of the message at At.

type Support

type Support struct {
	Level  Level
	Reason string
}

Support is one Feature's level. Reason is a stable key the UI and Wit translate, such as "imap.no_uidplus": the note for LevelNoted, why not for LevelUnsupported, "" for LevelSupported.

type SyncRequest

type SyncRequest struct {
	Account Cursor                    // the account's cursor, "" at first
	Folders map[FolderRef]FolderState // the folders local storage holds
	Only    []FolderRef               // sync only these folders (a Watch wake); nil for all
	Window  Window
	// Older, with Only, fetches only up to this many messages of each folder
	// that the window left out, newest first (the user's 加载更早); more then
	// reports that the server holds still more. ErrNotSynced: a full sync of
	// the folder comes first.
	Older int
	// Pages bounds how many pages a backfill takes this round; 0 for no bound.
	Pages int
	// PageSize is how many messages one page fetches at most; 0 for the
	// backend's own.
	PageSize     int
	HeaderFields []string
	TextBytes    int64
	// TextParts picks the parts of a structure whose text a sync stores.
	TextParts func(*Part) []PartRef
	Local     Local
}

SyncRequest is one sync round's input.

type Window

type Window struct {
	Newest int
	Since  time.Time
}

Window is how much mail the account keeps per folder: the newest Newest messages if Newest > 0, else the mail since Since if set, else all.

Directories

Path Synopsis
Package backendtest is the protocol backend contract (ADR 0037): one suite every backend.Receiver runs, against a fixture that starts the backend and puts mail on, or injects faults into, its server.
Package backendtest is the protocol backend contract (ADR 0037): one suite every backend.Receiver runs, against a fixture that starts the backend and puts mail on, or injects faults into, its server.
Package conn dials and authenticates IMAP and SMTP connections for the protocol backends.
Package conn dials and authenticates IMAP and SMTP connections for the protocol backends.
Package graph is the Microsoft Graph sending backend (ADR 0037), for a mailbox whose SMTP AUTH is disabled.
Package graph is the Microsoft Graph sending backend (ADR 0037), for a mailbox whose SMTP AUTH is disabled.
Package imapbackend is the IMAP protocol backend (ADR 0037): it speaks IMAP, with Gmail's extensions, and hands over change sets.
Package imapbackend is the IMAP protocol backend (ADR 0037): it speaks IMAP, with Gmail's extensions, and hands over change sets.
Package jmap is the JMAP protocol backend (RFC 8620, 8621; ADR 0037): a backend.Receiver over an account's JMAP session.
Package jmap is the JMAP protocol backend (RFC 8620, 8621; ADR 0037): a backend.Receiver over an account's JMAP session.
Package smtpbackend is the SMTP sending backend (ADR 0037).
Package smtpbackend is the SMTP sending backend (ADR 0037).

Jump to

Keyboard shortcuts

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