engine

package
v0.153.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 33 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 engine talks to mail servers: it keeps the local copy in step with IMAP, applies the user's actions there, and sends over SMTP.

Index

Constants

View Source
const (
	ActRead    = "read"
	ActUnread  = "unread"
	ActFlag    = "flag"
	ActUnflag  = "unflag"
	ActArchive = "archive"
	ActTrash   = "trash"
	ActSpam    = "spam"
	ActNotSpam = "inbox" // move back to the inbox
	ActMove    = "move"
	// ActNotJunk is the user's "Not spam": a message in a Junk folder goes
	// back to the inbox, one the local junk filter hid shows there again;
	// either way the filter learns it and never flags the sender again.
	ActNotJunk = "notjunk"
)

Action names accepted by Apply.

Variables

View Source
var (
	FullSyncEvery = 5 * time.Minute
	PollEvery     = 2 * time.Minute // when the server has no IDLE
	IdleRestart   = 20 * time.Minute
)

Intervals; tests shorten them.

View Source
var (
	// AutoLearnCap is how many messages of each kind are learned without the
	// user (the Junk folder as spam; the Sent folder and mail the user
	// replied to, starred, or got from a correspondent as ham). The user's own
	// Spam and Not spam are always learned.
	AutoLearnCap = 2000
	// JunkHeaderStep is how many stored inbox messages get their header read
	// again per sync pass, newest first, until every one has.
	JunkHeaderStep = 300
	// RejudgeCap is how many of the newest inbox messages are judged again
	// (locally, from stored facts) when the classifier first becomes ready.
	RejudgeCap = 2000
	// MaxJunkTokens bounds the classifier's table.
	MaxJunkTokens = 60000
)

Limits; tests shorten them.

View Source
var (
	InitialWindow = 500   // newest messages fetched on the first sync of a folder
	BackfillStep  = 500   // older messages added per full sync
	MaxPerFolder  = 20000 // stop backfilling past this
	BodiesPerPass = 300   // bodies fetched per folder per pass
	MaxTextPart   = int64(2 << 20)
)

How much history is kept locally; older mail is fetched in steps.

View Source
var ErrBadAction = errors.New("unknown action")

ErrBadAction is returned for an unknown action.

View Source
var ErrNoPart = errors.New("no such part")

ErrNoPart is returned for an unknown part path.

View Source
var ErrNoRecipients = errors.New("no recipients")

ErrNoRecipients is returned for a draft with nobody to send to.

View Source
var TLSConfig *tls.Config

TLSConfig is used for every TLS connection; tests replace it to trust a local server.

Functions

func CleanHTML

func CleanHTML(h string) string

CleanHTML removes what can act: scripts, frames, refreshes, event handlers. It is defence in depth; the reader also renders mail in a sandboxed iframe under a CSP that blocks scripts and remote loads.

func Credential added in v0.116.0

func Credential(ctx context.Context, sec secrets.Store, a store.Account) (cred, refresh string, err error)

Credential is what signs an account in: its password, or for an OAuth account an access token refreshed from the stored refresh token (cached until a minute before it expires). refresh is the refresh token in use, "" for a password account. A refresh token the provider rotated is saved back.

func TestAccount

func TestAccount(a store.Account, password string) (store.Account, error)

TestAccount checks that IMAP and SMTP accept the credentials, and returns the account with the IMAP sign-in name that worked. For an OAuth account password is the access token.

Types

type Attachment

type Attachment struct {
	Filename string `json:"filename"`
	Type     string `json:"type"`
	Data     []byte `json:"data"` // base64 in JSON
}

Attachment is a file to send.

type Draft

type Draft struct {
	AccountID   int64           `json:"account_id"`
	To          []store.Address `json:"to"`
	Cc          []store.Address `json:"cc"`
	Bcc         []store.Address `json:"bcc"`
	Subject     string          `json:"subject"`
	Text        string          `json:"text"`
	HTML        string          `json:"html"`
	InReplyTo   int64           `json:"in_reply_to"` // local message id being answered
	Attachments []Attachment    `json:"attachments"`
}

Draft is a message to send.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine runs one worker per account.

func New

func New(st *store.Store, sec secrets.Store, notify func(kind string, accountID int64)) *Engine

New creates an engine. notify is called after anything changes locally.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, ids []int64, action string, targetFolder int64) error

Apply performs an action on messages, which may span accounts and folders. targetFolder is used by ActMove.

func (*Engine) Build

func (e *Engine) Build(ctx context.Context, d Draft) ([]byte, []string, store.Account, error)

Build renders a draft as an RFC 5322 message. Returns the bytes and envelope recipients.

func (*Engine) Cleanup added in v0.135.0

func (e *Engine) Cleanup(ctx context.Context, ids []int64, view string, identities map[int64]string) error

Cleanup is scoped: Spam only moves to Trash; Trash only deletes from Trash. It never falls back to expunging unrelated messages on older IMAP servers.

func (*Engine) Credential added in v0.116.0

func (e *Engine) Credential(ctx context.Context, a store.Account) (string, error)

Credential is the engine's view of the package function.

func (*Engine) EnsureBody

func (e *Engine) EnsureBody(ctx context.Context, id int64) (store.Message, error)

EnsureBody fetches a message's body now if the background pass has not yet.

func (*Engine) FetchPart

func (e *Engine) FetchPart(ctx context.Context, id int64, path string) (store.Part, []byte, error)

FetchPart downloads one decoded part (an attachment or inline image).

func (*Engine) Send

func (e *Engine) Send(ctx context.Context, d Draft) error

Send delivers a draft over SMTP, files it in Sent and marks the original answered.

func (*Engine) Start

func (e *Engine) Start() error

Start launches a worker for every stored account.

func (*Engine) StartAccount

func (e *Engine) StartAccount(id int64)

StartAccount launches (or restarts) the worker of an account.

func (*Engine) Stop

func (e *Engine) Stop()

Stop ends every worker and waits for them.

func (*Engine) StopAccount

func (e *Engine) StopAccount(id int64)

StopAccount ends an account's worker.

func (*Engine) SyncNow

func (e *Engine) SyncNow(accountID int64)

SyncNow asks every worker (or one) to sync at once.

Jump to

Keyboard shortcuts

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