1# plugin
2
3Lua-based plugin system for extending Matcha. Plugins are loaded from `~/.config/matcha/plugins/` and run inside a sandboxed Lua VM (no `os`, `io`, or `debug` libraries).
4
5## How it works
6
7The `Manager` creates a Lua VM at startup, registers the `matcha` module, and loads all plugins from the user's plugins directory. Plugins can be either a single `.lua` file or a directory with an `init.lua` entry point.
8
9Plugins interact with Matcha by registering callbacks on hooks:
10
11```lua
12local matcha = require("matcha")
13
14matcha.on("email_received", function(email)
15 matcha.log("New email from: " .. email.from)
16 matcha.notify("New mail!", 3)
17end)
18```
19
20## Lua API (`matcha` module)
21
22| Function | Description |
23|----------|-------------|
24| `matcha.on(event, callback)` | Register a callback for a hook event |
25| `matcha.log(msg)` | Log a message to stderr |
26| `matcha.notify(msg [, seconds])` | Show a temporary notification in the TUI (default 2s) |
27| `matcha.set_status(area, text)` | Set a persistent status string for a view area (`"inbox"`, `"composer"`, `"email_view"`) |
28| `matcha.set_compose_field(field, value)` | Set a compose field value (`"to"`, `"cc"`, `"bcc"`, `"subject"`, `"body"`) |
29| `matcha.bind_key(key, area, description, callback)` | Register a custom keyboard shortcut for a view area (`"inbox"`, `"email_view"`, `"composer"`) |
30| `matcha.http(options)` | Make an HTTP request (see below) |
31
32## Hook events
33
34| Event | Callback argument | Description |
35|-------|-------------------|-------------|
36| `startup` | — | Matcha has started |
37| `shutdown` | — | Matcha is exiting |
38| `email_received` | Lua table with `uid`, `from`, `to`, `subject`, `date`, `is_read`, `account_id`, `folder` | New email arrived |
39| `email_viewed` | Same as `email_received` | User opened an email |
40| `email_send_before` | Table with `to`, `cc`, `subject`, `account_id` | About to send an email |
41| `email_send_after` | Same as `email_send_before` | Email sent successfully |
42| `folder_changed` | Folder name (string) | User switched folders |
43| `composer_updated` | Table with `body`, `body_len`, `subject`, `to`, `cc`, `bcc` | Composer content changed |
44
45## HTTP requests
46
47`matcha.http(options)` makes an HTTP request and returns `(response, err)`. Options is a table with:
48
49- `url` (string, required) — only `http` and `https` schemes
50- `method` (string, optional, default `"GET"`)
51- `headers` (table, optional)
52- `body` (string, optional)
53
54The response table has `status` (number), `body` (string), and `headers` (table with lowercase keys).
55
56Safety limits: 10s timeout, 1 MB response body cap.
57
58```lua
59local res, err = matcha.http({
60 url = "https://api.example.com/webhook",
61 method = "POST",
62 headers = { ["Content-Type"] = "application/json" },
63 body = '{"text":"hello"}',
64})
65if err then
66 matcha.log("error: " .. err)
67 return
68end
69matcha.log("status: " .. res.status)
70```
71
72## Available plugins
73
74The following example plugins ship in `~/.config/matcha/plugins/`:
75
76- `email_age.lua`
77- `recipient_counter.lua`
78
79## Files
80
81| File | Description |
82|------|-------------|
83| `plugin.go` | Plugin manager — Lua VM setup, plugin discovery and loading, notification/status state |
84| `hooks.go` | Hook definitions, callback registration, and hook invocation helpers |
85| `api.go` | `matcha` Lua module registration (`on`, `log`, `notify`, `set_status`, `set_compose_field`, `bind_key`, `http`) |
86| `http.go` | `matcha.http()` implementation — HTTP client with timeout and body size limits |