imapx

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 23 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 imapx is frostmail's only door to IMAP. It wraps the patched go-imap v2 client in third_party/go-imap (docs/adr/0004-go-imap-fork.md), so sync code never imports go-imap directly and the fork stays replaceable.

Index

Constants

View Source
const PreviewBytes = 2048

PreviewBytes is how much of a message's text part a header fetch reads for its preview.

View Source
const PreviewRunes = 200

PreviewRunes is the length of a stored preview.

Variables

View Source
var ErrAuth = errors.New("imapx: login rejected")

ErrAuth means the server rejected the credentials. Retrying with the same password will not help.

View Source
var ErrNoDisplay = errors.New("imapx: no display copy")

ErrNoDisplay means a message's structure cannot be made into a display copy (a multipart without a boundary, a part without a header): fetch the whole message instead.

View Source
var ErrNoMessage = errors.New("imapx: message not on the server")

ErrNoMessage means the server no longer has the requested message.

View Source
var ErrNotGmail = errors.New("imapx: the server has no Gmail extensions")

ErrNotGmail means a Gmail command was sent to a server without X-GM-EXT-1.

View Source
var ErrReadOnly = errors.New("imapx: the session is read-only")

ErrReadOnly means a read-only session was asked to change the server.

View Source
var ErrUnavailable = errors.New("imapx: mailbox unavailable")

ErrUnavailable means the server accepted the credentials but cannot open the mailbox now, so a later login can succeed: the response code UNAVAILABLE (RFC 5530), or Outlook's "User is authenticated but not connected.", which it answers while the mailbox's backend is away.

Functions

func AssignRoles

func AssignRoles(list []store.ServerMailbox)

AssignRoles gives each role to at most one mailbox. Roles the server states (INBOX and special-use attributes) are given first, in list order; roles guessed from names only go to roles still free, so iCloud's "Sent Messages" (\Sent) wins over a "Sent Items" another client made.

func Dial

func Dial(ctx context.Context, opts DialOptions) (*imapclient.Client, error)

Dial connects, secures the connection as opts.TLS says, and logs in.

func RoleFor

func RoleFor(path, delim string, attrs []string) api.MailboxRole

RoleFor resolves a mailbox's role from its SPECIAL-USE attributes, then from common names (top-level only).

Types

type Capabilities

type Capabilities struct {
	CondStore    bool
	ESearch      bool
	Binary       bool
	Move         bool
	UIDPlus      bool
	Idle         bool
	SpecialUse   bool
	ListExtended bool
	// Gmail is X-GM-EXT-1: X-GM-MSGID, X-GM-THRID, X-GM-LABELS (ADR-0012).
	Gmail bool
}

Capabilities are the server extensions sync uses, probed once after login.

type DialOptions

type DialOptions struct {
	Host     string
	Port     int
	TLS      api.TLSMode
	Username string
	Password string
	// OAuth makes Password an OAuth access token, sent with AUTHENTICATE
	// XOAUTH2 instead of LOGIN.
	OAuth bool
	// ReadOnly opens mailboxes with EXAMINE, and the session refuses every
	// command that would change the server with ErrReadOnly.
	ReadOnly bool

	// InsecureSkipVerify accepts any certificate; only for test servers
	// with self-signed certificates.
	InsecureSkipVerify bool
	// Trace, when set, receives the session's lines with their direction
	// and credentials redacted (trace.go); a Closer is closed with the
	// connection. STARTTLS connections are not traced.
	Trace io.Writer
	// Timeout bounds connecting and logging in; zero means 30s.
	Timeout time.Duration
	// OnUpdate, if set, is called from the connection's reader for every
	// unsolicited EXISTS, EXPUNGE or FETCH. It must not block.
	OnUpdate func()
	// Log, if set, receives what the session skips: a message whose
	// BODYSTRUCTURE the parser rejected is logged with the structure's
	// shape, its strings masked.
	Log *slog.Logger
}

DialOptions says how to reach and log in to one IMAP server.

type Part

type Part = store.Part

Part is the part a preview is read from.

type Selected

type Selected struct {
	UIDValidity   uint32
	UIDNext       uint32
	HighestModSeq uint64
	Messages      uint32
}

Selected is what SELECT reports.

type Session

type Session struct {
	Caps Capabilities
	// contains filtered or unexported fields
}

Session is one logged-in IMAP connection. It is not safe for concurrent use: the account actor drives it from one goroutine. Every method takes a context; when the context ends mid-command the connection is closed and the session must be discarded.

func Open

func Open(ctx context.Context, opts DialOptions) (*Session, error)

Open dials, logs in and probes capabilities. opts.OnUpdate, if set, is called for every unsolicited EXISTS, EXPUNGE or FETCH, from the connection's reader goroutine; it must not block.

func (*Session) Append

func (s *Session) Append(ctx context.Context, mailbox string, raw []byte, flags []string) (uint32, error)

Append stores a raw message in mailbox with flags and returns its UID when the server reports it (UIDPLUS), else 0.

func (*Session) Capabilities

func (s *Session) Capabilities() Capabilities

Capabilities returns what the server offers, as probed at login.

func (*Session) Close

func (s *Session) Close() error

Close logs out (best effort) and closes the connection.

func (*Session) Copy

func (s *Session) Copy(ctx context.Context, uids []uint32, dest string) (map[uint32]uint32, error)

Copy copies messages of the selected mailbox to dest by UID and returns each source UID's new UID when the server reports COPYUID (UIDPLUS).

func (*Session) Create

func (s *Session) Create(ctx context.Context, path string) error

Expunge permanently removes uids, which must already be \Deleted, from the selected mailbox. It needs UIDPLUS; a bare EXPUNGE could remove messages another client marked. Create makes a mailbox; one the server already has is not an error.

func (*Session) Delete

func (s *Session) Delete(ctx context.Context, path string) error

Delete deletes a mailbox; one the server does not have is not an error.

func (*Session) Expunge

func (s *Session) Expunge(ctx context.Context, uids []uint32) error

func (*Session) FetchDisplay

func (s *Session) FetchDisplay(ctx context.Context, uid uint32) ([]byte, error)

FetchDisplay fetches the display copy of message uid in the selected mailbox: its BODYSTRUCTURE and header, then the MIME headers of its parts and the content of those a reader shows (shown).

func (*Session) FetchFlags

func (s *Session) FetchFlags(ctx context.Context, uids []uint32, changedSince uint64) ([]store.FlagUpdate, error)

FetchFlags fetches flags for uids in the selected mailbox; with changedSince > 0 and CONDSTORE, only those changed since that modseq.

func (*Session) FetchGmailChanges

func (s *Session) FetchGmailChanges(ctx context.Context, uids []uint32, changedSince uint64) ([]store.FlagUpdate, error)

FetchGmailChanges is FetchFlags plus each message's X-GM-LABELS.

func (*Session) FetchGmailHeaders

func (s *Session) FetchGmailHeaders(ctx context.Context, uids []uint32) ([]store.MessageHeader, error)

FetchGmailHeaders is FetchHeaders plus X-GM-MSGID, X-GM-THRID and X-GM-LABELS (Caps.Gmail).

func (*Session) FetchHeaders

func (s *Session) FetchHeaders(ctx context.Context, uids []uint32) ([]store.MessageHeader, error)

FetchHeaders fetches headers, structure, flags and a preview for uids in the selected mailbox. UIDs the server no longer has are missing from the result.

func (*Session) FetchRaw

func (s *Session) FetchRaw(ctx context.Context, uid uint32) ([]byte, error)

FetchRaw fetches the whole message (BODY.PEEK[]) of uid in the selected mailbox.

func (*Session) Idle

func (s *Session) Idle(ctx context.Context, wake <-chan struct{}, max time.Duration, started func()) error

Idle runs IDLE on the selected mailbox until ctx ends, wake fires, or max elapses. It returns nil in all three cases unless the connection failed. started, if not nil, is called once the server has accepted IDLE.

func (*Session) List

func (s *Session) List(ctx context.Context) ([]store.ServerMailbox, error)

List returns every mailbox with its role resolved (RoleFor).

func (*Session) Mailbox

func (s *Session) Mailbox() string

Mailbox is the selected mailbox's path; "" when none is.

func (*Session) Move

func (s *Session) Move(ctx context.Context, uids []uint32, dest string) (map[uint32]uint32, error)

Move moves uids from the selected mailbox to dest and returns the new UIDs by old UID when the server reports them (UIDPLUS). Without MOVE, go-imap copies, flags \Deleted and expunges by UID; without UIDPLUS either, the move fails rather than risk a bare EXPUNGE.

func (*Session) Rename

func (s *Session) Rename(ctx context.Context, from, to string) error

Rename renames a mailbox and the mailboxes inside it.

func (*Session) SearchGmailRaw

func (s *Session) SearchGmailRaw(ctx context.Context, raw string, since time.Time, minUID uint32) ([]uint32, error)

SearchGmailRaw returns the UIDs in the selected mailbox that a Gmail search matches (X-GM-RAW, in the web's search language, such as "category:promotions"), limited to UIDs at or above minUID unless it is 0 and to mail that arrived on since's day or later unless it is zero; ascending.

func (*Session) SearchHeader

func (s *Session) SearchHeader(ctx context.Context, field, value string) ([]uint32, error)

SearchHeader returns the UIDs in the selected mailbox whose header field contains value, ascending.

func (*Session) SearchMessageID

func (s *Session) SearchMessageID(ctx context.Context, msgid string) ([]uint32, error)

SearchMessageID returns the UIDs in the selected mailbox whose Message-ID header contains msgid (given without angle brackets), ascending.

func (*Session) Select

func (s *Session) Select(ctx context.Context, path string) (Selected, error)

Select opens a mailbox: read-write, or with EXAMINE on a read-only session.

func (*Session) Status

func (s *Session) Status(ctx context.Context, path string) (Selected, error)

Status reports a mailbox's state without selecting it.

func (*Session) StoreFlags

func (s *Session) StoreFlags(ctx context.Context, uids []uint32, add, remove []string) error

StoreFlags adds and removes flags on uids in the selected mailbox.

func (*Session) StoreLabels

func (s *Session) StoreLabels(ctx context.Context, uids []uint32, add, remove []string) error

StoreLabels adds and removes Gmail labels (STORE ±X-GM-LABELS) on uids in the selected mailbox.

func (*Session) UIDs

func (s *Session) UIDs(ctx context.Context) ([]uint32, error)

UIDs returns every UID in the selected mailbox, ascending.

func (*Session) UIDsSince

func (s *Session) UIDsSince(ctx context.Context, since time.Time) ([]uint32, error)

UIDsSince returns the UIDs of the messages in the selected mailbox that arrived on since's day or later (SEARCH SINCE, by INTERNALDATE), or every UID when since is zero; ascending.

Directories

Path Synopsis
Package imapxtest runs go-imap's in-memory IMAP server for unit tests.
Package imapxtest runs go-imap's in-memory IMAP server for unit tests.
Package replay serves a recorded IMAP session back to a client (docs/design/testing.md, transcript replay).
Package replay serves a recorded IMAP session back to a client (docs/design/testing.md, transcript replay).

Jump to

Keyboard shortcuts

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