Documentation
¶
Overview ¶
Package gmail implements the provider interfaces on the Gmail REST API (Google Workspace accounts), with OAuth auth code + PKCE login.
Index ¶
- Constants
- Variables
- func SaveClient(store *auth.Store, alias string, c *Client) error
- func Scopes(m policy.Mode) []string
- type Client
- type Config
- type LoginParams
- type LoginResult
- type Provider
- func (p *Provider) CreateDraft(ctx context.Context, in provider.DraftInput) (*provider.Message, error)
- func (p *Provider) CreateForwardDraft(ctx context.Context, in provider.ForwardInput) (*provider.Message, error)
- func (p *Provider) CreateReplyDraft(ctx context.Context, in provider.ReplyInput) (*provider.Message, error)
- func (p *Provider) DeleteDraft(ctx context.Context, id string) error
- func (p *Provider) Get(ctx context.Context, id string, o provider.GetOptions) (*provider.Message, error)
- func (p *Provider) GetAttachment(ctx context.Context, id, partID string) (*provider.Attachment, error)
- func (p *Provider) List(ctx context.Context, o provider.ListOptions) (*provider.Page, error)
- func (p *Provider) ListAttachments(ctx context.Context, id string) ([]provider.AttachmentMeta, error)
- func (p *Provider) ListDrafts(ctx context.Context, o provider.ListOptions) (*provider.Page, error)
- func (p *Provider) Mark(ctx context.Context, id string, o provider.MarkOptions) (*provider.Message, error)
- func (p *Provider) Move(ctx context.Context, id, dest string) (*provider.Message, error)
- func (p *Provider) Name() string
- func (p *Provider) Raw(ctx context.Context, r provider.RawRequest) (*provider.RawResponse, error)
- func (p *Provider) Relabel(ctx context.Context, id string, add, remove []string) (*provider.Message, error)
- func (p *Provider) Search(ctx context.Context, o provider.SearchOptions) (*provider.Page, error)
- func (p *Provider) Send(ctx context.Context, id string) (*provider.SendResult, error)
- func (p *Provider) Sync(ctx context.Context, o provider.SyncOptions) (*provider.SyncResult, error)
- func (p *Provider) Thread(ctx context.Context, threadID string, limit int) ([]provider.Message, error)
- func (p *Provider) UpdateDraft(ctx context.Context, id string, u provider.DraftUpdate) (*provider.Message, error)
- type TokenSource
- type TokenSourceParams
Constants ¶
const DefaultBaseURL = "https://gmail.googleapis.com"
const MaxMessageSize = 35 << 20
MaxMessageSize is Gmail's limit for a message including attachments (after base64 encoding) through the upload endpoint.
const (
Name = "gmail"
)
Variables ¶
var Endpoint = oauth2.Endpoint{ AuthURL: "https://accounts.google.com/o/oauth2/v2/auth", TokenURL: "https://oauth2.googleapis.com/token", AuthStyle: oauth2.AuthStyleInParams, }
Google OAuth endpoints. Declared here instead of importing x/oauth2/google, which pulls in the GCE metadata client.
Functions ¶
func SaveClient ¶
SaveClient stores the OAuth client for the account (0600).
Types ¶
type Client ¶
Client is the OAuth client (GCP "Desktop app" type). Google calls the secret of an installed-app client non-confidential, but it is still kept out of account.json and out of Git.
func LoadClient ¶
LoadClient returns the stored OAuth client, or nil if there is none.
func LoadClientFile ¶
LoadClientFile reads the JSON that the GCP console downloads for a Desktop OAuth client ({"installed": {"client_id": …, "client_secret": …}}).
type LoginParams ¶
type LoginParams struct {
Store *auth.Store
Account *auth.Account // Alias, Mode set; Username set on re-login (lock)
Client *Client
HTTP *http.Client
Endpoint oauth2.Endpoint // default Endpoint
BaseURL string // Gmail base, for the profile call
UserAgent string
Manual bool
Stdin io.Reader
OnPrompt func(oauthflow.Prompt) error
OpenBrowser func(string) error
}
type LoginResult ¶
func Login ¶
func Login(ctx context.Context, p LoginParams) (*LoginResult, error)
Login runs the browser flow (loopback or --manual), redeems the code with the PKCE verifier, checks the granted scopes, locks the account to the Gmail profile address and writes google-token.json.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
func (*Provider) CreateDraft ¶
func (*Provider) CreateForwardDraft ¶
func (p *Provider) CreateForwardDraft(ctx context.Context, in provider.ForwardInput) (*provider.Message, error)
CreateForwardDraft quotes the original as a forwarded block and carries its (non-inline) attachments, in the original thread.
func (*Provider) CreateReplyDraft ¶
func (p *Provider) CreateReplyDraft(ctx context.Context, in provider.ReplyInput) (*provider.Message, error)
CreateReplyDraft builds the reply locally (Gmail has no server-side createReply): In-Reply-To/References from the original, the same threadId, and the original quoted below the agent's text.
func (*Provider) Get ¶
func (p *Provider) Get(ctx context.Context, id string, o provider.GetOptions) (*provider.Message, error)
Get fetches one message. A draft id ("r…") fetches the draft. HTML is returned as HTML; the service derives text from it locally so hidden text is stripped.
func (*Provider) GetAttachment ¶
func (p *Provider) GetAttachment(ctx context.Context, id, partID string) (*provider.Attachment, error)
GetAttachment fetches an attachment by MIME part id.
func (*Provider) ListAttachments ¶
func (*Provider) ListDrafts ¶
ListDrafts lists drafts newest first; ids are draft ids.
func (*Provider) Move ¶
Move changes labels the way Gmail's "Move to" does: archive removes INBOX; spam and user labels replace INBOX; trash uses messages.trash.
func (*Provider) Raw ¶
func (p *Provider) Raw(ctx context.Context, r provider.RawRequest) (*provider.RawResponse, error)
Raw allows only the mailbox's own Gmail paths; the policy layer has already checked the method (and send-like paths) against the mode.
func (*Provider) Relabel ¶
func (p *Provider) Relabel(ctx context.Context, id string, add, remove []string) (*provider.Message, error)
Relabel adds and removes labels. User labels are created on first use.
func (*Provider) Search ¶
Search passes the query to Gmail's q= unchanged (same syntax as the Gmail search box). Spam and trash are excluded unless the query asks for them with in:spam / in:trash.
func (*Provider) Send ¶
Send sends an existing draft with drafts.send; Gmail deletes the draft and returns the sent message.
func (*Provider) Sync ¶
func (p *Provider) Sync(ctx context.Context, o provider.SyncOptions) (*provider.SyncResult, error)
Sync uses history.list from the stored historyId. The first run records the mailbox's current historyId and returns nothing. When Gmail no longer has the history (404: the id is too old), a new baseline is recorded.
func (*Provider) Thread ¶
func (p *Provider) Thread(ctx context.Context, threadID string, limit int) ([]provider.Message, error)
Thread returns a thread oldest first.
func (*Provider) UpdateDraft ¶
func (p *Provider) UpdateDraft(ctx context.Context, id string, u provider.DraftUpdate) (*provider.Message, error)
UpdateDraft rewrites the draft (Gmail replaces drafts whole). Fields not given are carried over from the current draft: recipients, subject, threading headers, the plain-text body and non-inline attachments. A draft written in Gmail with formatting keeps its text but loses the formatting and inline images.
type TokenSource ¶
TokenSource returns a valid access token for the Gmail API.
func NewTokenSource ¶
func NewTokenSource(p TokenSourceParams) (TokenSource, error)
NewTokenSource returns the TokenSource for a logged-in gmail account. Refreshed tokens are written back to google-token.json; after each refresh the mailbox is checked against the account lock.