conn

package
v1.38.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 28 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 conn dials and authenticates IMAP and SMTP connections for the protocol backends.

Index

Constants

View Source
const (
	MaxRetries = 3
	MaxBackOff = time.Minute
)

Throttling: a busy answer is waited out and the request sent again, at most MaxRetries times, each wait at most MaxBackOff.

View Source
const TracePrefix = "imap> "

TracePrefix 标记已脱敏的 trace 行,报告移除前缀后保留其原始字节。

Variables

View Source
var (
	HeaderTimeout  = time.Minute     // until the response headers arrive
	RequestTimeout = 5 * time.Minute // a whole request, except on Streaming
	StallTimeout   = time.Minute     // a StallBody with no byte arriving
)

Timeouts of one HTTP backend request, so a server that stops answering cannot hold an account's sync forever; vars so tests can shorten them.

View Source
var ClientVersion = "dev"

ClientVersion is sent in the IMAP ID command; release builds set it.

View Source
var ErrOtherHost = errors.New("refused a redirect to another host")

ErrOtherHost is a redirect, or a link, away from the account's host.

View Source
var ErrSMTPAuthDisabled = errors.New("SMTP AUTH is disabled for the mailbox")

ErrSMTPAuthDisabled is a backend.ErrAuthFailed whose server said SMTP AUTH is turned off for the mailbox (Microsoft's 535 5.7.139), not that the credentials are wrong.

View Source
var ErrStalled = errors.New("the server stopped sending")

ErrStalled is a response body that went too long without a byte arriving.

View Source
var SystemProxy = sysproxy.For

SystemProxy is the proxy for a request to u, nil for none: the user's; tests stand in for it.

View Source
var TLSRoots *x509.CertPool

TLSRoots verifies servers' certificates, nil for the system's; tests stand in for it.

Functions

func Acquire added in v1.37.0

func Acquire(ctx context.Context, account string) (release func(), err error)

Acquire takes account's slot, waiting while another call holds it. account names the server and the user: the API's URL, a NUL, the user name. A wait that ctx ends says so and names the URL's host, so a request's deadline is never the bare context error (#755).

func BackOff added in v1.37.0

func BackOff(err error, try int) (time.Duration, bool)

BackOff is how long to wait before sending again after try (0 for the first) failed with err: a *BusyError's BackOff, or 1s doubling with each try when it named none, at most MaxBackOff. False when err is not a *BusyError, or the retries are spent.

func DialIMAP

func DialIMAP(ctx context.Context, e Endpoint) (*imapclient.Client, error)

DialIMAP connects to e and logs in.

func DialIMAPAbortable

func DialIMAPAbortable(ctx context.Context, e Endpoint, h *imapclient.UnilateralDataHandler) (*imapclient.Client, func(), error)

DialIMAPAbortable is DialIMAP that also returns abort, which closes the network connection at once. Unlike Client.Close it does not wait for a response literal being read, so it can cut off a download. h, if not nil, receives unsolicited server updates (IDLE).

func DialPOP3 added in v1.34.0

func DialPOP3(ctx context.Context, e Endpoint) (c *pop3.Conn, close func(), err error)

DialPOP3 connects to e and signs in with USER/PASS. close cuts the network connection at once, so a ctx that ends can unblock a read: go-pop3 takes no context. The library has no STLS, so it is done here before the connection is handed over.

func DialSMTP

func DialSMTP(ctx context.Context, e Endpoint) (*smtp.Client, error)

DialSMTP connects to e and authenticates with PLAIN, or XOAUTH2 when e has an AccessToken.

func HTTPStatusErr added in v1.37.0

func HTTPStatusErr(resp *http.Response) error

HTTPStatusErr is the error for the statuses every HTTP backend reads alike: 401 is backend.ErrAuthFailed, 429 a *BusyError. Nil for any other, which the backend reads by its protocol.

func HTTPTransport added in v1.35.0

func HTTPTransport() *http.Transport

HTTPTransport is the HTTP backends' transport: it connects as IMAP and SMTP do, through the user's proxy, but to a server on this computer directly, and trusts TLSRoots.

func LogTrace

func LogTrace(c *imapclient.Client, err error)

LogTrace logs the end of c's traffic, masked, when err is a protocol error: a server response go-imap could not parse. The log is what an error report carries, so a report of such an error shows what the server sent.

func NewHTTPClient added in v1.37.0

func NewHTTPClient(t *http.Transport, rt http.RoundTripper) *http.Client

NewHTTPClient is an HTTP backend's client for one account: requests go through rt, nil for t itself, which sends them over t. It waits HeaderTimeout for an answer and RequestTimeout for a whole request, and follows a redirect only as SameHost allows.

func NewXOAuth2Client

func NewXOAuth2Client(username, token string) sasl.Client

NewXOAuth2Client returns a SASL XOAUTH2 client (Google and Microsoft IMAP/SMTP).

func RefusalReply added in v1.34.0

func RefusalReply(err error) string

RefusalReply is the server's reply to a refused login in err (a NO/BAD or an SMTP status, credentials masked), "" when err carries none.

func RetryAfter added in v1.37.0

func RetryAfter(h http.Header) time.Duration

RetryAfter is h's Retry-After, in seconds or as a date; 0 when it names none, or no time in the future.

func SameHost added in v1.37.0

func SameHost(req *http.Request, via []*http.Request) error

SameHost is the HTTP backends' redirect policy: never from HTTPS to plain HTTP, and only to the host the request was for, so the credentials stay there (ErrOtherHost).

func Serial added in v1.37.0

func Serial(ctx context.Context, account string, wait func(context.Context, time.Duration) error, try func() error) error

Serial calls try for account, one call per account at a time, back-off waits included, so an account stays under the server's concurrency limit and stops while it is busy. It sends again after a *BusyError, waiting it out with wait (nil waits in real time), as BackOff says.

func Sleep added in v1.37.0

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits d out, or until ctx is done.

func StallBody added in v1.35.0

func StallBody(body io.ReadCloser, d time.Duration, cancel context.CancelFunc) io.ReadCloser

StallBody is body, failing with ErrStalled once d passes with no byte arriving; cancel, which should cancel the request, is called then and on Close. It bounds a download or event stream that an overall request timeout would cut off.

func Streaming added in v1.37.0

func Streaming(c *http.Client) *http.Client

Streaming is c without the overall timeout, for a download or an event stream that may outlast it: its body is bounded by StallBody, or by the stream's own idle timer, instead.

Types

type BusyError added in v1.37.0

type BusyError struct {
	BackOff time.Duration
	Err     error
}

BusyError is a server's answer to slow down and try again after BackOff, 0 when it named no time. Err, when set, is the answer as the backend read it.

func (*BusyError) Error added in v1.37.0

func (e *BusyError) Error() string

func (*BusyError) Unwrap added in v1.37.0

func (e *BusyError) Unwrap() error

type Endpoint

type Endpoint struct {
	Host               string
	Port               int
	Security           Security
	Username, Password string
	AccessToken        string
}

Endpoint describes one server and the credentials to use on it. A non-empty AccessToken selects XOAUTH2 and Password is ignored.

type FailureKind

type FailureKind string

FailureKind is the broad cause of a failed dial, for diagnostics.

const (
	FailureDNS      FailureKind = "dns"
	FailureConnect  FailureKind = "connect"
	FailureTimeout  FailureKind = "timeout"
	FailureTLS      FailureKind = "tls"
	FailureAuth     FailureKind = "auth"
	FailureProtocol FailureKind = "protocol"

	FailureProxyUnreachable FailureKind = "proxy_unreachable"
	FailureProxyRefused     FailureKind = "proxy_refused"
	FailureProxyAuth        FailureKind = "proxy_auth" // the proxy's credentials, never the account's
)

func Classify

func Classify(err error) FailureKind

Classify maps an error from DialIMAP or DialSMTP to a FailureKind; nil gives "".

type Security

type Security string

Security selects the transport protection of a connection.

const (
	TLS      Security = "tls"
	StartTLS Security = "starttls"
	None     Security = "none"
)

Jump to

Keyboard shortcuts

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