config

package
v0.27.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 8 Imported by: 0

README

config

The config package handles all persistent application state: user configuration, email/contacts/drafts caching, folder caching, and email signatures. All data is stored as JSON files under ~/.config/matcha/.

Architecture

This package acts as the data layer for Matcha. It manages:

  • Account configuration with multi-account support (Gmail, iCloud, custom IMAP/SMTP)
  • Secure credential storage via the OS keyring (with automatic migration from plain-text passwords)
  • Local caches for emails, contacts, drafts, and folder listings to enable fast startup and offline browsing
  • Email signatures stored as plain text

All cache files use JSON serialization with restrictive file permissions (0600/0700).

Files

File Description
config.go Core configuration types (Account, Config, MailingList) and functions for loading, saving, and managing accounts. Handles IMAP/SMTP server resolution per provider, OS keyring integration, and legacy config migration.
cache.go Email, contacts, and drafts caching. Provides CRUD operations for EmailCache, ContactsCache (with search and frequency-based ranking), and DraftsCache (with save/delete/get operations).
folder_cache.go Caches IMAP folder listings per account and per-folder email metadata. Stores folder names to avoid repeated IMAP LIST commands, and caches email headers per folder for fast navigation.
signature.go Loads and saves the user's email signature from ~/.config/matcha/signature.txt.
config_test.go Unit tests for configuration logic.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddContact added in v0.8.0

func AddContact(name, email string) error

AddContact adds or updates a contact in the cache.

func ClearEmailCache added in v0.8.0

func ClearEmailCache() error

ClearEmailCache removes the cache file.

func DeleteDraft added in v0.8.0

func DeleteDraft(id string) error

DeleteDraft removes a draft by ID.

func GetCachedFolders added in v0.22.0

func GetCachedFolders(accountID string) []string

GetCachedFolders returns cached folder names for a specific account.

func GetConfigDir added in v0.21.0

func GetConfigDir() (string, error)

GetConfigDir returns the path to the configuration directory (exported).

func HasDrafts added in v0.8.0

func HasDrafts() bool

HasDrafts checks if there are any saved drafts.

func HasEmailCache added in v0.8.0

func HasEmailCache() bool

HasEmailCache checks if a cache file exists.

func HasSignature added in v0.15.0

func HasSignature() bool

HasSignature checks if a signature file exists and is non-empty.

func LoadSignature added in v0.15.0

func LoadSignature() (string, error)

LoadSignature loads the signature from the signature file.

func SaveAccountFolders added in v0.22.0

func SaveAccountFolders(accountID string, folders []string) error

SaveAccountFolders saves folder names for a specific account, merging into the existing cache.

func SaveConfig

func SaveConfig(config *Config) error

SaveConfig saves the given configuration to the config file and passwords to the keyring.

func SaveContactsCache added in v0.8.0

func SaveContactsCache(cache *ContactsCache) error

SaveContactsCache saves contacts to the cache file.

func SaveDraft added in v0.8.0

func SaveDraft(draft Draft) error

SaveDraft saves or updates a draft.

func SaveDraftsCache added in v0.8.0

func SaveDraftsCache(cache *DraftsCache) error

SaveDraftsCache saves drafts to the cache file.

func SaveEmailCache added in v0.8.0

func SaveEmailCache(cache *EmailCache) error

SaveEmailCache saves emails to the cache file.

func SaveFolderCache added in v0.22.0

func SaveFolderCache(cache *FolderCache) error

SaveFolderCache saves the folder cache to disk.

func SaveFolderEmailCache added in v0.22.0

func SaveFolderEmailCache(folderName string, emails []CachedEmail) error

SaveFolderEmailCache saves emails for a folder to disk.

func SaveSignature added in v0.15.0

func SaveSignature(signature string) error

SaveSignature saves the signature to the signature file.

Types

type Account added in v0.8.0

type Account struct {
	ID              string `json:"id"`
	Name            string `json:"name"`
	Email           string `json:"email"`
	Password        string `json:"-"`                // "-" prevents the password from being saved to config.json
	ServiceProvider string `json:"service_provider"` // "gmail", "icloud", or "custom"
	// FetchEmail is the single email address for which messages should be fetched.
	// If empty, it will default to `Email` when accounts are added.
	FetchEmail string `json:"fetch_email,omitempty"`

	// Custom server settings (used when ServiceProvider is "custom")
	IMAPServer string `json:"imap_server,omitempty"`
	IMAPPort   int    `json:"imap_port,omitempty"`
	SMTPServer string `json:"smtp_server,omitempty"`
	SMTPPort   int    `json:"smtp_port,omitempty"`
	Insecure   bool   `json:"insecure,omitempty"`

	// S/MIME settings
	SMIMECert          string `json:"smime_cert,omitempty"`            // Path to the public certificate PEM
	SMIMEKey           string `json:"smime_key,omitempty"`             // Path to the private key PEM
	SMIMESignByDefault bool   `json:"smime_sign_by_default,omitempty"` // Whether to enable S/MIME signing by default
}

Account stores the configuration for a single email account.

func (*Account) GetIMAPPort added in v0.8.0

func (a *Account) GetIMAPPort() int

GetIMAPPort returns the IMAP port for the account.

func (*Account) GetIMAPServer added in v0.8.0

func (a *Account) GetIMAPServer() string

GetIMAPServer returns the IMAP server address for the account.

func (*Account) GetSMTPPort added in v0.8.0

func (a *Account) GetSMTPPort() int

GetSMTPPort returns the SMTP port for the account.

func (*Account) GetSMTPServer added in v0.8.0

func (a *Account) GetSMTPServer() string

GetSMTPServer returns the SMTP server address for the account.

type CachedEmail added in v0.8.0

type CachedEmail struct {
	UID       uint32    `json:"uid"`
	From      string    `json:"from"`
	To        []string  `json:"to"`
	Subject   string    `json:"subject"`
	Date      time.Time `json:"date"`
	MessageID string    `json:"message_id"`
	AccountID string    `json:"account_id"`
	IsRead    bool      `json:"is_read"`
}

CachedEmail stores essential email data for caching.

func LoadFolderEmailCache added in v0.22.0

func LoadFolderEmailCache(folderName string) ([]CachedEmail, error)

LoadFolderEmailCache loads cached emails for a folder from disk.

type CachedFolders added in v0.22.0

type CachedFolders struct {
	AccountID string    `json:"account_id"`
	Folders   []string  `json:"folders"`
	UpdatedAt time.Time `json:"updated_at"`
}

CachedFolders stores folder names for a single account.

type Config

type Config struct {
	Accounts      []Account     `json:"accounts"`
	DisableImages bool          `json:"disable_images,omitempty"`
	HideTips      bool          `json:"hide_tips,omitempty"`
	Theme         string        `json:"theme,omitempty"`
	MailingLists  []MailingList `json:"mailing_lists,omitempty"`
}

Config stores the user's email configuration with multiple accounts.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads the configuration from the config file and passwords from the keyring. It automatically migrates plain-text passwords to the OS keyring if they exist.

func (*Config) AddAccount added in v0.8.0

func (c *Config) AddAccount(account Account)

AddAccount adds a new account to the configuration.

func (*Config) GetAccountByEmail added in v0.8.0

func (c *Config) GetAccountByEmail(email string) *Account

GetAccountByEmail returns an account by its email address.

func (*Config) GetAccountByID added in v0.8.0

func (c *Config) GetAccountByID(id string) *Account

GetAccountByID returns an account by its ID.

func (*Config) GetFirstAccount added in v0.8.0

func (c *Config) GetFirstAccount() *Account

GetFirstAccount returns the first account or nil if none exist.

func (*Config) HasAccounts added in v0.8.0

func (c *Config) HasAccounts() bool

HasAccounts returns true if there are any configured accounts.

func (*Config) RemoveAccount added in v0.8.0

func (c *Config) RemoveAccount(id string) bool

RemoveAccount removes an account by its ID and deletes its password from the keyring.

type Contact added in v0.8.0

type Contact struct {
	Name     string    `json:"name"`
	Email    string    `json:"email"`
	LastUsed time.Time `json:"last_used"`
	UseCount int       `json:"use_count"`
}

Contact stores a contact's name and email address.

func SearchContacts added in v0.8.0

func SearchContacts(query string) []Contact

SearchContacts searches for contacts matching the query.

type ContactsCache added in v0.8.0

type ContactsCache struct {
	Contacts  []Contact `json:"contacts"`
	UpdatedAt time.Time `json:"updated_at"`
}

ContactsCache stores all known contacts.

func LoadContactsCache added in v0.8.0

func LoadContactsCache() (*ContactsCache, error)

LoadContactsCache loads contacts from the cache file.

type Draft added in v0.8.0

type Draft struct {
	ID              string    `json:"id"`
	To              string    `json:"to"`
	Cc              string    `json:"cc,omitempty"`
	Bcc             string    `json:"bcc,omitempty"`
	Subject         string    `json:"subject"`
	Body            string    `json:"body"`
	AttachmentPaths []string  `json:"attachment_paths,omitempty"`
	AccountID       string    `json:"account_id"`
	InReplyTo       string    `json:"in_reply_to,omitempty"`
	References      []string  `json:"references,omitempty"`
	QuotedText      string    `json:"quoted_text,omitempty"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

Draft stores a saved email draft.

func GetAllDrafts added in v0.8.0

func GetAllDrafts() []Draft

GetAllDrafts retrieves all drafts sorted by update time (newest first).

func GetDraft added in v0.8.0

func GetDraft(id string) *Draft

GetDraft retrieves a draft by ID.

type DraftsCache added in v0.8.0

type DraftsCache struct {
	Drafts    []Draft   `json:"drafts"`
	UpdatedAt time.Time `json:"updated_at"`
}

DraftsCache stores all saved drafts.

func LoadDraftsCache added in v0.8.0

func LoadDraftsCache() (*DraftsCache, error)

LoadDraftsCache loads drafts from the cache file.

type EmailCache added in v0.8.0

type EmailCache struct {
	Emails    []CachedEmail `json:"emails"`
	UpdatedAt time.Time     `json:"updated_at"`
}

EmailCache stores cached emails for all accounts.

func LoadEmailCache added in v0.8.0

func LoadEmailCache() (*EmailCache, error)

LoadEmailCache loads emails from the cache file.

type FolderCache added in v0.22.0

type FolderCache struct {
	Accounts  []CachedFolders `json:"accounts"`
	UpdatedAt time.Time       `json:"updated_at"`
}

FolderCache stores cached folders for all accounts.

func LoadFolderCache added in v0.22.0

func LoadFolderCache() (*FolderCache, error)

LoadFolderCache loads the folder cache from disk.

type FolderEmailCache added in v0.22.0

type FolderEmailCache struct {
	FolderName string        `json:"folder_name"`
	Emails     []CachedEmail `json:"emails"`
	UpdatedAt  time.Time     `json:"updated_at"`
}

FolderEmailCache stores cached emails for a specific folder.

type MailingList added in v0.20.0

type MailingList struct {
	Name      string   `json:"name"`
	Addresses []string `json:"addresses"`
}

MailingList represents a named group of email addresses.

Jump to

Keyboard shortcuts

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