mailstore

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 10 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 store owns the SQLite database. Nothing else issues SQL.

Index

Constants

View Source
const SentWeight = 5

SentWeight is the NoteContact weight of an address the user wrote to.

View Source
const SpamSQL = `` /* 129-byte string literal not displayed */

SpamSQL is true for a message (as m) that is spam in the inbox's sense: the local junk filter flagged it, or the AI labelled its conversation spam.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a row does not exist.

Functions

func CleanupIdentity added in v0.135.0

func CleanupIdentity(account, folder int64, validity, uid uint32) string

CleanupIdentity identifies one message across local row reuse and mailbox resets.

Types

type Account

type Account struct {
	ID        int64  `json:"id"`
	Name      string `json:"name"`
	Email     string `json:"email"`
	Username  string `json:"username"`
	IMAPHost  string `json:"imap_host"`
	IMAPPort  int    `json:"imap_port"`
	IMAPTLS   string `json:"imap_tls"`
	SMTPHost  string `json:"smtp_host"`
	SMTPPort  int    `json:"smtp_port"`
	SMTPTLS   string `json:"smtp_tls"`
	Color     string `json:"color"`
	Signature string `json:"signature"`
	CreatedAt int64  `json:"created_at"`
	LastSync  int64  `json:"last_sync"`
	LastError string `json:"last_error"`
	// Auth is how the account signs in: "password" (the default; the secret
	// store holds the password) or "oauth2:google" / "oauth2:microsoft" (it
	// holds the refresh token).
	Auth string `json:"auth"`
}

Account is a configured mailbox. The password lives in the secret store.

type Address

type Address struct {
	Name string `json:"name"`
	Addr string `json:"addr"`
}

Address is a display name and an address.

type CleanupQuery added in v0.135.0

type CleanupQuery struct {
	View      string `json:"view"`
	AccountID int64  `json:"account"`
	Before    int64  `json:"before"`
	MinScore  int    `json:"min_score"`
}

CleanupQuery describes the synced messages a user can review for cleanup. Before is a message-date cutoff in Unix milliseconds, not time spent in Trash.

func (CleanupQuery) Validate added in v0.135.0

func (q CleanupQuery) Validate() error

type Contact

type Contact struct {
	Addr string `json:"addr"`
	Name string `json:"name"`
}

Contact is an address seen in mail.

type Counts

type Counts struct {
	Inbox   int `json:"inbox"`
	Unread  int `json:"unread"`
	Flagged int `json:"flagged"`
	// Junk is the inbox conversations that are spam, by the junk filter or
	// the AI (not the Junk folder).
	Junk int `json:"junk"`
}

Counts are the sidebar badges.

type Folder

type Folder struct {
	ID          int64  `json:"id"`
	AccountID   int64  `json:"account_id"`
	Name        string `json:"name"`
	Delim       string `json:"delim"`
	Role        string `json:"role"`
	UIDValidity uint32 `json:"-"`
	LastUID     uint32 `json:"-"`
	Total       int    `json:"total"`
	Unread      int    `json:"unread"`
}

Folder is a server mailbox.

type JunkInput added in v0.125.0

type JunkInput struct {
	ID        int64
	AccountID int64
	FolderID  int64
	UID       uint32
	Role      string // its folder's role
	MessageID string
	ThreadKey string
	Subject   string
	FromName  string
	FromAddr  string
	ReplyTo   string
	Seen      bool
	Flagged   bool
	Answered  bool
	BodyText  string
	BodyHTML  string
	BodyState int
	Hdr       string // JSON spam.Header, ” when not read yet
	Junk      bool
	JunkScore int
}

JunkInput is a message as the junk filter reads it.

type Message

type Message struct {
	ID         int64     `json:"id"`
	AccountID  int64     `json:"account_id"`
	FolderID   int64     `json:"folder_id"`
	UID        uint32    `json:"uid"`
	MessageID  string    `json:"message_id"`
	InReplyTo  string    `json:"in_reply_to"`
	Refs       string    `json:"references"`
	ThreadKey  string    `json:"thread_key"`
	Subject    string    `json:"subject"`
	FromName   string    `json:"from_name"`
	FromAddr   string    `json:"from_addr"`
	To         []Address `json:"to"`
	Cc         []Address `json:"cc"`
	ReplyTo    string    `json:"reply_to"`
	Date       int64     `json:"date"`
	Size       int64     `json:"size"`
	Seen       bool      `json:"seen"`
	Flagged    bool      `json:"flagged"`
	Answered   bool      `json:"answered"`
	HasAttach  bool      `json:"has_attach"`
	Snippet    string    `json:"snippet"`
	BodyText   string    `json:"body_text,omitempty"`
	BodyHTML   string    `json:"body_html,omitempty"`
	Structure  Structure `json:"structure"`
	BodyState  int       `json:"body_state"`
	ThreadSize int       `json:"thread_size,omitempty"`
	// The local junk filter's verdict (internal/mail/spam), inbox mail only.
	Junk      bool   `json:"junk"`
	JunkScore int    `json:"junk_score"`
	JunkWhy   string `json:"junk_why,omitempty"`
	// JunkHdr is the filter's facts from the header (JSON spam.Header),
	// written once when the header is stored.
	JunkHdr string `json:"-"`
	// AI is the assistant's label for the conversation (List only).
	AI *Triage `json:"ai,omitempty"`
}

Message is a stored message.

type Part

type Part struct {
	Path       string `json:"path"` // "1.2"
	Type       string `json:"type"` // text/plain
	Charset    string `json:"charset,omitempty"`
	Encoding   string `json:"encoding,omitempty"`
	Filename   string `json:"filename,omitempty"`
	Size       int64  `json:"size"`
	ContentID  string `json:"cid,omitempty"`
	Inline     bool   `json:"inline,omitempty"`
	IsAttach   bool   `json:"attachment"`
	IsTextBody bool   `json:"-"`
	IsHTMLBody bool   `json:"-"`
}

Part is a body part addressed by its IMAP part path.

type Query

type Query struct {
	View      string // inbox unread flagged sent drafts trash junk archive folder all
	AccountID int64
	FolderID  int64
	Search    string
	// Junk decides what happens to mail the junk filter flagged: "" leaves
	// it out of inbox, unread and all (it shows everywhere else),
	// "include" keeps it, "only" lists nothing else.
	Junk string
	// Category narrows the list to conversations the assistant labelled so:
	// important updates newsletter spam, or "nospam" for everything not
	// labelled spam. "" is no filter.
	Category string
	Before   int64 // date cursor, exclusive
	BeforeID int64
	Limit    int
}

Query selects a message list.

type Store

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

Store wraps the database.

func Open

func Open(path string) (*Store, error)

Open opens (and creates) the database at path.

func (*Store) Account

func (s *Store) Account(ctx context.Context, id int64) (Account, error)

Account loads one account.

func (*Store) Accounts

func (s *Store) Accounts(ctx context.Context) ([]Account, error)

Accounts lists all accounts in creation order.

func (*Store) AddAccount

func (s *Store) AddAccount(ctx context.Context, a Account) (int64, error)

AddAccount inserts an account and returns its id.

func (*Store) AllowSender added in v0.125.0

func (s *Store) AllowSender(ctx context.Context, addr string) error

AllowSender remembers that the user said "Not spam" for an address, and lifts the verdict from every inbox message it sent.

func (*Store) CleanupIDs added in v0.135.0

func (s *Store) CleanupIDs(ctx context.Context, q CleanupQuery) ([]int64, error)

CleanupIDs includes all matching synced messages, independent of list paging. Spam is the server's Junk folder plus the filter/triage catch in the Inbox.

func (*Store) CleanupSnapshot added in v0.135.0

func (s *Store) CleanupSnapshot(ctx context.Context, q CleanupQuery) ([]int64, map[int64]string, error)

CleanupSnapshot binds local IDs to IMAP identities: SQLite may reuse a deleted ID.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) DeleteAccount

func (s *Store) DeleteAccount(ctx context.Context, id int64) error

DeleteAccount removes an account and everything stored for it.

func (*Store) DeleteMessages

func (s *Store) DeleteMessages(ctx context.Context, ids []int64) error

DeleteMessages removes rows (expunged or moved away).

func (*Store) DisallowSender added in v0.125.0

func (s *Store) DisallowSender(ctx context.Context, addr string) error

DisallowSender forgets a vouched-for address (the user marked its mail as spam).

func (*Store) Folder

func (s *Store) Folder(ctx context.Context, id int64) (Folder, error)

Folder loads one folder.

func (*Store) FolderByRole

func (s *Store) FolderByRole(ctx context.Context, accountID int64, role string) (Folder, error)

FolderByRole finds an account's folder with a role.

func (*Store) FolderUIDs

func (s *Store) FolderUIDs(ctx context.Context, folderID int64) (map[uint32]UIDState, error)

FolderUIDs returns uid → state for a folder.

func (*Store) Folders

func (s *Store) Folders(ctx context.Context, accountID int64) ([]Folder, error)

Folders lists the folders of one account, or all when accountID is 0.

func (*Store) InboxIDs added in v0.125.0

func (s *Store) InboxIDs(ctx context.Context, limit int) ([]int64, error)

InboxIDs lists the newest messages of every inbox, for judging again.

func (*Store) JunkInputs added in v0.125.0

func (s *Store) JunkInputs(ctx context.Context, ids []int64) ([]JunkInput, error)

JunkInputs loads messages with what the filter needs, bodies included.

func (*Store) JunkTokenCount added in v0.125.0

func (s *Store) JunkTokenCount(ctx context.Context) (int, error)

JunkTokenCount is the classifier's size, for tests and diagnostics.

func (*Store) JunkTotals added in v0.125.0

func (s *Store) JunkTotals(ctx context.Context) (nSpam, nHam int)

JunkTotals is how many spam and ham messages the classifier has learned.

func (*Store) KnownSender added in v0.125.0

func (s *Store) KnownSender(ctx context.Context, addr string) (bool, error)

KnownSender: the user vouched for the address (Not spam), wrote to it, or it is one of the user's own accounts. Mail from a known sender is never junk.

func (*Store) LabelTriage added in v0.125.0

func (s *Store) LabelTriage(ctx context.Context, accountID int64, key, category, sender string) error

LabelTriage is the user's own label for a conversation, kept from then on. A label other than spam also clears the sender, so their later mail is never sorted as spam.

func (*Store) Learn added in v0.125.0

func (s *Store) Learn(ctx context.Context, accountID int64, key string, tokens []string, spam, byUser bool) (bool, error)

Learn teaches the classifier one message. A message already learned the same way is skipped; one learned the other way has its tokens moved across. An automatic lesson (byUser false) never overrides the user's. Reports whether anything changed.

func (*Store) LiftKnownJunk added in v0.125.0

func (s *Store) LiftKnownJunk(ctx context.Context) ([]int64, error)

LiftKnownJunk clears the verdict from inbox mail that turned out to come from a known sender: someone the user has since written to (the Sent folder syncs after the inbox), or a conversation the user has replied in. Returns the folders whose counts changed.

func (*Store) List

func (s *Store) List(ctx context.Context, q Query) ([]Message, error)

List runs a list query, newest first.

func (*Store) Message

func (s *Store) Message(ctx context.Context, id int64) (Message, error)

Message loads one message with its bodies.

func (*Store) Messages

func (s *Store) Messages(ctx context.Context, ids []int64) ([]Message, error)

Messages loads several messages without bodies.

func (*Store) MissingJunkHeader added in v0.125.0

func (s *Store) MissingJunkHeader(ctx context.Context, folderID int64, limit int) ([]UIDRow, error)

MissingJunkHeader lists a folder's messages, newest first, whose header the filter has not read (stored before it existed).

func (*Store) NoteContact

func (s *Store) NoteContact(ctx context.Context, a Address, weight int, when int64) error

NoteContact remembers an address; sent-to weighs more than received-from.

func (*Store) PendingBodies

func (s *Store) PendingBodies(ctx context.Context, folderID int64, limit int) ([]Message, error)

PendingBodies lists messages of a folder that still need their body, newest first.

func (*Store) PruneJunkTokens added in v0.125.0

func (s *Store) PruneJunkTokens(ctx context.Context, max int) error

PruneJunkTokens keeps the classifier bounded: past max tokens, the least seen are dropped down to nine tenths of max (a token seen once or twice says almost nothing; Robinson's smoothing already keeps it near 0.5).

func (*Store) RecountFolder

func (s *Store) RecountFolder(ctx context.Context, folderID int64) error

RecountFolder refreshes total and unread from stored rows.

func (*Store) RecountInbox added in v0.125.0

func (s *Store) RecountInbox(ctx context.Context, accountID int64) error

RecountInbox refreshes the counts of an account's inbox folders (a spam label changed: the unread count leaves spam out).

func (*Store) RemoveFoldersExcept

func (s *Store) RemoveFoldersExcept(ctx context.Context, accountID int64, keep []string) error

RemoveFoldersExcept drops folders the server no longer lists.

func (*Store) ResetFolder

func (s *Store) ResetFolder(ctx context.Context, folderID int64, uidValidity uint32) error

ResetFolder drops every stored message (UIDVALIDITY changed) and sets the new validity.

func (*Store) SaveTriage added in v0.125.0

func (s *Store) SaveTriage(ctx context.Context, t Triage, sender string) error

SaveTriage stores the model's label. A row the user labelled is left as it is; a spam verdict for a sender the user cleared takes the user's label.

func (*Store) SetAuth added in v0.116.0

func (s *Store) SetAuth(ctx context.Context, id int64, auth string) error

SetAuth records how an account signs in.

func (*Store) SetBody

func (s *Store) SetBody(ctx context.Context, id int64, text, html, snippet string) error

SetBody stores the decoded text and HTML bodies.

func (*Store) SetFlags

func (s *Store) SetFlags(ctx context.Context, id int64, seen, flagged, answered bool) error

SetFlags updates the local flag state.

func (*Store) SetJunk added in v0.125.0

func (s *Store) SetJunk(ctx context.Context, id int64, junk bool, score int, why string) error

SetJunk records a verdict.

func (*Store) SetJunkHeader added in v0.125.0

func (s *Store) SetJunkHeader(ctx context.Context, id int64, hdr string) error

SetJunkHeader stores the filter's header facts of a message stored before the filter existed.

func (*Store) SetLastUID

func (s *Store) SetLastUID(ctx context.Context, folderID int64, uid uint32) error

SetLastUID records the highest UID stored in a folder.

func (*Store) SetSetting

func (s *Store) SetSetting(ctx context.Context, key, value string) error

SetSetting writes a setting.

func (*Store) SetSyncState

func (s *Store) SetSyncState(ctx context.Context, id int64, syncErr string) error

SetSyncState records the outcome of a sync pass.

func (*Store) Setting

func (s *Store) Setting(ctx context.Context, key, def string) string

Setting reads a setting, or def when unset.

func (*Store) SuggestContacts

func (s *Store) SuggestContacts(ctx context.Context, prefix string, limit int) ([]Contact, error)

SuggestContacts completes a prefix of a name or address.

func (*Store) Thread

func (s *Store) Thread(ctx context.Context, accountID int64, key string) ([]Message, error)

Thread returns every message of an account sharing the thread key, oldest first.

func (*Store) ThreadHasSent added in v0.125.0

func (s *Store) ThreadHasSent(ctx context.Context, accountID int64, threadKey string) (bool, error)

ThreadHasSent: the conversation includes a message the user sent.

func (*Store) ThreadKeyFor

func (s *Store) ThreadKeyFor(ctx context.Context, accountID int64, msgIDs []string) (string, error)

ThreadKeyFor looks up the stored thread key of any message with this Message-ID.

func (*Store) ThreadMail added in v0.125.0

func (s *Store) ThreadMail(ctx context.Context, accountID int64, key string, n int) ([]Message, error)

ThreadMail is the newest n messages of a conversation in an inbox, with whatever body is stored (no server call), newest last.

func (*Store) TokenCounts added in v0.125.0

func (s *Store) TokenCounts(ctx context.Context, tokens []string) (map[string][2]int, error)

TokenCounts returns {spam, ham} for each token the classifier has seen.

func (*Store) TriageCounts added in v0.125.0

func (s *Store) TriageCounts(ctx context.Context) ([]TriageCount, error)

TriageCounts counts labelled conversations that still have mail in an inbox, by category.

func (*Store) TriageOf added in v0.125.0

func (s *Store) TriageOf(ctx context.Context, accountID int64, key string) (Triage, error)

TriageOf loads one conversation's label; ErrNotFound when it has none.

func (*Store) TriagePending added in v0.125.0

func (s *Store) TriagePending(ctx context.Context, window int) ([]TriageTodo, error)

TriagePending lists, newest first, the conversations among the newest window inbox conversations that need sorting.

func (*Store) UnifiedCounts

func (s *Store) UnifiedCounts(ctx context.Context) (Counts, error)

UnifiedCounts counts across accounts. Inbox and unread leave out spam (SpamSQL: the junk filter's flag or the AI's label); Junk counts the inbox conversations that are spam either way.

func (*Store) UnspamTriage added in v0.125.0

func (s *Store) UnspamTriage(ctx context.Context, accountID int64, key, sender string) error

UnspamTriage is the AI's half of the user's "Not spam": a conversation the AI labelled spam takes the user's label "important", and the sender's later mail is never sorted as spam. A conversation the AI sorted otherwise keeps its label.

func (*Store) UpdateAccount

func (s *Store) UpdateAccount(ctx context.Context, a Account) error

UpdateAccount rewrites the editable fields; not auth (SetAuth).

func (*Store) UpsertFolder

func (s *Store) UpsertFolder(ctx context.Context, accountID int64, name, delim, role string) (Folder, error)

UpsertFolder creates the folder or updates its delimiter and role; returns it.

func (*Store) UpsertHeader

func (s *Store) UpsertHeader(ctx context.Context, m *Message) (int64, error)

UpsertHeader stores a message's envelope and flags. Body fields are kept. Returns the row id.

type Structure

type Structure struct {
	Text  *Part  `json:"text,omitempty"`
	HTML  *Part  `json:"html,omitempty"`
	Parts []Part `json:"parts"` // attachments and inline images
}

Structure is what the body structure told us.

type Triage added in v0.125.0

type Triage struct {
	AccountID int64  `json:"-"`
	ThreadKey string `json:"-"`
	MessageID int64  `json:"-"`
	Category  string `json:"category"`
	Reply     bool   `json:"reply,omitempty"`
	Summary   string `json:"summary,omitempty"`
	Model     string `json:"model,omitempty"`
	User      bool   `json:"user,omitempty"` // the user gave this label
	At        int64  `json:"at"`
}

Triage is the assistant's label for one conversation.

type TriageCount added in v0.125.0

type TriageCount struct {
	AccountID int64  `json:"account_id"`
	Category  string `json:"category"`
	N         int    `json:"n"`
}

TriageCount is how many inbox conversations of an account carry a label.

type TriageTodo added in v0.125.0

type TriageTodo struct {
	AccountID int64
	ThreadKey string
	Newest    int64 // the highest message row id of the conversation
}

TriageTodo is an inbox conversation that needs sorting: never sorted, or grown since (and not labelled by the user).

type UIDRow added in v0.125.0

type UIDRow struct {
	ID  int64
	UID uint32
}

UIDRow is a message's row id and UID.

type UIDState

type UIDState struct {
	ID       int64
	Seen     bool
	Flagged  bool
	Answered bool
}

FolderUIDs maps every stored UID of a folder to its row id and flags.

Jump to

Keyboard shortcuts

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