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
- func AttachmentsFolder(source string) string
- func LockKey(source string) int32
- func MessageFolder(eventID string) string
- type Attachment
- type AttachmentLibrary
- type Config
- type EventReader
- type External
- type File
- type Files
- type FolderMove
- type Lock
- type Mailbox
- type Mark
- type MarkStore
- type Marks
- type Metrics
- type Objects
- type Payload
- type Poller
- type RawWindows
- type Recorder
- type Rejections
- type RemovedFolder
- type RepairRecord
- type RepairReport
- type RepairStore
- type Repairer
- type Repairs
- type Sources
- type StoredFile
Constants ¶
const EventType = "mail"
EventType is the event_type of every message an imap source files.
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.
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.
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).
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 ¶
AttachmentsFolder is the library folder every attachment a source saved sits under, removed with the source.
func MessageFolder ¶ added in v1.169.3
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 File ¶
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
FolderMove is one folder moved.
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 ¶
NewMailbox returns the admin side of the imap input.
func (*Mailbox) Check ¶
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.
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.
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 (*Poller) SetStored ¶ added in v1.152.0
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.
type RawWindows ¶
type RawWindows = whwrite.RawWindows
RawWindows registers a window's raw partition so its events are queryable before compaction.
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
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"`
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"`
}
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
Owed reports whether src has not been repaired by this version yet.
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
NewRepairs returns the store, nil without a database.
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 is replaced, since a second pass reads exactly the folders the first kept.