whimap

package
v1.171.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 29 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 whimap is the imap input of a webhook source (#2247): the platform polls a mailbox and files each new message from an allowed, authenticated sender as one event, written exactly as the HTTP receiver writes a request's events. Compaction into Parquet, the source's table, retention and alerts then treat it like any other source. Attachments are written as managed resources the event names.

Nothing here is a tool. The mail reaches a model or a script only through the source's table and the resources, like every webhook's data.

Index

Constants

View Source
const EventType = "mail"

EventType is the event_type of every message an imap source files.

View Source
const LockKeyspace int32 = 4713211

LockKeyspace is the advisory-lock space an imap source's poll is held in; the key within it is a hash of the source's name.

View Source
const OutcomeUnreadable = "unreadable"

OutcomeUnreadable is a message skipped because the mail client could not read the server's answer about it (#2459), as the source's rejections list and webhook_requests_total name it.

View Source
const RepairVersion = 2

RepairVersion is the version of the repair this build runs. 1 was #2836's; 2 adds the collapse of copies no event names (#2842).

View Source
const Tick = 15 * time.Second

Tick is how often the poller looks for a source whose poll is due.

Variables

This section is empty.

Functions

func AttachmentsFolder

func AttachmentsFolder(source string) string

AttachmentsFolder is the library folder every attachment a source saved sits under, removed with the source.

func LockKey

func LockKey(source string) int32

LockKey is the key a source's poll is locked under.

func MessageFolder added in v1.169.3

func MessageFolder(eventID string) string

MessageFolder names the folder a message's attachments are saved in, from the message's event id (#2836): its Message-ID, or where it is for one with none. The server's numbering is not part of it, so a renumbered folder read again finds each attachment at the address it was saved at and records a new version there, instead of filing a second copy of the mailbox. It is a hash, so nothing a sender wrote reaches the path.

Types

type Attachment

type Attachment struct {
	Name        string `json:"name"`
	ContentType string `json:"content_type"`
	Size        int64  `json:"size"`
	URI         string `json:"uri,omitempty"`
	ResourceID  string `json:"resource_id,omitempty"`
	Skipped     string `json:"skipped,omitempty"`
}

Attachment is one file a message carried: the resource it was saved as, or why it was not.

type AttachmentLibrary added in v1.169.3

type AttachmentLibrary interface {
	// ListAttachments lists every file under the source's attachments folder.
	ListAttachments(ctx context.Context, src whsource.Source) ([]StoredFile, error)
	// MoveFolder renames a folder of the source's library, aliasing each
	// file's old address.
	MoveFolder(ctx context.Context, src whsource.Source, from, to string) error
	// RemoveCopy removes the file id and its versions, after handing its
	// addresses to the file keep, so a reference to the copy resolves to the
	// file that stays (#2842).
	RemoveCopy(ctx context.Context, src whsource.Source, id, keep string) error
	// DeleteFolder removes an emptied folder and the folders beneath it.
	DeleteFolder(ctx context.Context, src whsource.Source, folder string) error
}

AttachmentLibrary is the source's attachments folder in its library.

type Config

type Config struct {
	Sources    Sources
	Objects    Objects
	Bucket     string
	Recorder   Recorder
	RawWindows RawWindows
	Marks      MarkStore
	Files      Files
	// Rejections and Metrics record a message skipped as unreadable; either
	// may be nil.
	Rejections Rejections
	Metrics    Metrics
	Lock       Lock
	// Repair is the one-time repair of a source's attachments (#2836), run
	// before a source's first poll after the upgrade; nil runs none.
	Repair  *Repairer
	Replica string
	Logger  *slog.Logger
	// Open opens a session; nil is imapmail.Open.
	Open func(ctx context.Context, a imapmail.Account) (*imapmail.Session, error)
	Now  func() time.Time
}

Config assembles a poller.

type EventReader added in v1.169.3

type EventReader interface {
	Live(ctx context.Context, source string) ([]whstore.Window, error)
	WindowEvents(ctx context.Context, src whsource.Source, w whstore.Window) ([]whevent.Stored, error)
}

EventReader reads the events of a source as compaction keeps them: one per event id, the earliest.

type External

type External struct {
	From    string   `json:"from"`
	To      []string `json:"to"`
	Cc      []string `json:"cc,omitempty"`
	Subject string   `json:"subject"`
	Body    string   `json:"body"`
}

External is what the message's sender wrote, cleaned of invisible and reordering characters, hidden HTML removed from the body.

type FailedFolder added in v1.169.5

type FailedFolder struct {
	Folder string `json:"folder"`
	Error  string `json:"error"`
}

FailedFolder is one folder the repair could not move or remove.

type File

type File struct {
	Folder      string
	Name        string
	ContentType string
	Data        []byte
}

File is one attachment to save: the library folder it goes in, its name, type and bytes.

type Files

type Files interface {
	PutFile(ctx context.Context, src whsource.Source, f File) (uri, id string, err error)
}

Files saves an attachment as a managed resource in the source's library.

type FolderMove added in v1.169.3

type FolderMove struct {
	From string `json:"from"`
	To   string `json:"to"`
}

FolderMove is one folder moved.

type Lock

type Lock func(ctx context.Context, source string) (release func(), ok bool, err error)

Lock takes the one-replica lock on a source's poll, reporting whether it was free.

type Mailbox

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

Mailbox is what the admin service asks of an imap source: whether its mailbox can be read as configured, and how each folder's polls are going.

func NewMailbox

func NewMailbox(marks *Marks, repairs *Repairs) *Mailbox

NewMailbox returns the admin side of the imap input.

func (*Mailbox) Check

func (m *Mailbox) Check(ctx context.Context, src whsource.Source) error

Check logs in and opens each folder read-only, so a source whose login or folders are wrong is refused when it is saved rather than failing every poll.

func (*Mailbox) Marks

func (m *Mailbox) Marks(ctx context.Context, source string) ([]Mark, error)

Marks lists a source's folders as its polls left them.

func (*Mailbox) Repair added in v1.169.3

func (m *Mailbox) Repair(ctx context.Context, source string) (*RepairRecord, error)

Repair is what the source's attachment repair did, nil until it has run (#2836).

type Mark

type Mark struct {
	Source      string `json:"-"`
	Folder      string `json:"folder"`
	UIDValidity uint32 `json:"uid_validity"`
	LastUID     uint32 `json:"last_uid"`
	Stored      int64  `json:"stored"`
	// HiddenNotAllowed and HiddenUnauthenticated count the messages the
	// sender rules withheld. Nothing else of them is kept.
	HiddenNotAllowed      int64      `json:"hidden_not_allowed"`
	HiddenUnauthenticated int64      `json:"hidden_unauthenticated"`
	LastPolledAt          *time.Time `json:"last_polled_at,omitempty"`
	LastError             string     `json:"last_error,omitempty"`
}

Mark is where one folder of a source has been read to, and what its polls found.

type MarkStore

type MarkStore interface {
	Get(ctx context.Context, source, folder string) (Mark, error)
	Put(ctx context.Context, mk Mark) error
}

MarkStore keeps each folder's watermark.

type Marks

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

Marks stores the marks in webhook_imap_marks.

func NewMarks

func NewMarks(db *sql.DB) *Marks

NewMarks returns the store, nil without a database.

func (*Marks) Get

func (m *Marks) Get(ctx context.Context, source, folder string) (Mark, error)

Get returns a folder's mark, zero when it has never been read.

func (*Marks) List

func (m *Marks) List(ctx context.Context, source string) ([]Mark, error)

List returns a source's marks, by folder.

func (*Marks) Put

func (m *Marks) Put(ctx context.Context, mk Mark) error

Put writes a folder's mark.

type Metrics added in v1.153.0

type Metrics interface {
	WebhookRequest(ctx context.Context, source, outcome string)
}

Metrics counts what a poll skipped, under webhook_requests_total.

type Objects

type Objects = whwrite.Objects

Objects writes a segment to the managed-resources bucket.

type Payload

type Payload struct {
	Folder      string `json:"folder"`
	UID         uint32 `json:"uid"`
	UIDValidity uint32 `json:"uid_validity"`
	MessageID   string `json:"message_id,omitempty"`
	InReplyTo   string `json:"in_reply_to,omitempty"`
	// References are the thread's earlier Message-IDs.
	References []string `json:"references,omitempty"`
	Date       string   `json:"date,omitempty"`
	Arrived    string   `json:"arrived"`
	// FromAddress is the sender's address as the From header names it, and
	// SenderAuth what the receiving server recorded of it.
	FromAddress string `json:"from_address"`
	SenderAuth  string `json:"sender_auth"`
	Size        int64  `json:"size"`
	BodyFormat  string `json:"body_format"`
	// BodySkipped says why the body was not read, empty when it was.
	BodySkipped string       `json:"body_skipped,omitempty"`
	Attachments []Attachment `json:"attachments"`
	External    External     `json:"external"`
	Notice      string       `json:"notice"`
}

Payload is one message as its event's payload: what the platform read of it and, under External, everything its sender wrote.

type Poller

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

Poller polls every imap source on its interval.

func New

func New(cfg Config) *Poller

New returns a poller, nil when a dependency is missing.

func (*Poller) Pass

func (p *Poller) Pass(ctx context.Context) error

Pass polls every imap source whose interval has passed.

func (*Poller) Poll

func (p *Poller) Poll(ctx context.Context, src whsource.Source) error

Poll reads every folder of one source once.

func (*Poller) SetStored added in v1.152.0

func (p *Poller) SetStored(fn func(ctx context.Context, source string, events []whevent.Event))

SetStored is what is told each written segment's events from here on: the hook the runs of an automation naming the source are enqueued through (#2332). It is set before the poller starts.

func (*Poller) Start

func (p *Poller) Start(ctx context.Context)

Start runs the poll loop until Stop.

func (*Poller) Stop

func (p *Poller) Stop()

Stop ends the loop and waits for a poll in progress.

type RawWindows

type RawWindows = whwrite.RawWindows

RawWindows registers a window's raw partition so its events are queryable before compaction.

type Recorder

type Recorder = whwrite.Recorder

Recorder records a window a segment was written to.

type Rejections added in v1.153.0

type Rejections interface {
	RecordRejections(ctx context.Context, rejections []whstore.Rejection) error
}

Rejections keeps what a poll skipped, in the source's rejections list beside the receiver's refusals (#2459).

type RemovedFolder added in v1.169.3

type RemovedFolder struct {
	Folder string `json:"folder"`
	Files  int    `json:"files"`
}

RemovedFolder is one later copy deleted.

type RepairRecord added in v1.169.3

type RepairRecord struct {
	RepairedAt   time.Time `json:"repaired_at"`
	Moved        int       `json:"moved"`
	Removed      int       `json:"removed"`
	RemovedFiles int       `json:"removed_files"`
	// Collapsed and CollapsedFiles are the copies of a message no event names
	// deleted into the folder that stays, and the files they held (#2842).
	Collapsed      int `json:"collapsed"`
	CollapsedFiles int `json:"collapsed_files"`
	Kept           int `json:"kept"`
	// Unmoved and Failed are how many folders the last pass left where they
	// were because the message's folder already held files, and because a
	// move or removal failed (#2845).
	Unmoved int          `json:"unmoved"`
	Failed  int          `json:"failed"`
	Report  RepairReport `json:"report"`
}

RepairRecord is a source's repair as its page shows it: the whole counts, and the first repairListLimit folders of each list.

type RepairReport added in v1.169.3

type RepairReport struct {
	// Moved are the folders moved to the new form.
	Moved []FolderMove `json:"moved"`
	// Removed are the later copies deleted, each with how many files it held.
	Removed []RemovedFolder `json:"removed"`
	// Collapsed are the copies of a folder no event names, deleted into the
	// one that stays (#2842), each with how many files it held.
	Collapsed []RemovedFolder `json:"collapsed"`
	// Kept are the folders of the old form no event names that are a copy of
	// nothing.
	Kept []string `json:"kept"`
	// Unmoved are the folders an event names left where they are because the
	// message's folder already held files (#2845).
	Unmoved []string `json:"unmoved"`
	// Failed are the folders a move or removal failed on, each with what the
	// library said, left where they were (#2845).
	Failed []FailedFolder `json:"failed"`
}

RepairReport is what a repair did.

func (RepairReport) CollapsedFiles added in v1.169.4

func (r RepairReport) CollapsedFiles() int

CollapsedFiles is how many files the repair deleted as copies of a folder no event names.

func (RepairReport) RemovedFiles added in v1.169.3

func (r RepairReport) RemovedFiles() int

RemovedFiles is how many files the repair deleted as later copies of a folder an event names.

type RepairStore added in v1.169.3

type RepairStore interface {
	// RepairedVersion is the version of the last repair recorded for source, 0
	// when none has run.
	RepairedVersion(ctx context.Context, source string) (int, error)
	// RecordRepair records a repair of source by version.
	RecordRepair(ctx context.Context, source string, version int, r RepairReport) error
}

RepairStore records each source's repair.

type Repairer added in v1.169.3

type Repairer struct {
	Library AttachmentLibrary
	Events  EventReader
	Store   RepairStore
}

Repairer repairs the attachments of imap sources.

func (*Repairer) Owed added in v1.169.3

func (r *Repairer) Owed(ctx context.Context, src whsource.Source) (bool, error)

Owed reports whether src has not been repaired by this version yet.

func (*Repairer) Repair added in v1.169.3

func (r *Repairer) Repair(ctx context.Context, src whsource.Source) (RepairReport, error)

Repair repairs src's attachments and records what it did. A failure to list the attachments or read the events leaves it owed, so the next poll tries again; a folder that fails is listed in the report, and the pass recorded.

type Repairs added in v1.169.3

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

Repairs stores each source's attachment repair in webhook_imap_repairs.

func NewRepairs added in v1.169.3

func NewRepairs(db *sql.DB) *Repairs

NewRepairs returns the store, nil without a database.

func (*Repairs) Get added in v1.169.3

func (r *Repairs) Get(ctx context.Context, source string) (*RepairRecord, error)

Get returns source's repair, nil when it has not run.

func (*Repairs) RecordRepair added in v1.169.3

func (r *Repairs) RecordRepair(ctx context.Context, source string, version int, rep RepairReport) error

RecordRepair records that source's repair by version ran and what it did. A source repaired before keeps what the earlier pass did (#2842): the moves, removals and collapses are added to it, and kept, unmoved and failed are replaced, since a second pass reads exactly the folders the first left in the old form.

func (*Repairs) RepairedVersion added in v1.169.4

func (r *Repairs) RepairedVersion(ctx context.Context, source string) (int, error)

RepairedVersion is the version of source's last repair, 0 when none ran.

type Sources

type Sources interface {
	List(ctx context.Context) ([]whsource.Source, error)
}

Sources lists the stored sources, secrets decrypted.

type StoredFile added in v1.169.3

type StoredFile struct {
	ID     string
	Folder string
	Name   string
	Size   int64
}

StoredFile is one file under a source's attachments folder.

Jump to

Keyboard shortcuts

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