# bulwarkmail/webmail architecture

> Architecture diagram of the GitHub repository bulwarkmail/webmail, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bulwarkmail/webmail
- Repository: https://github.com/bulwarkmail/webmail

## Overview

#### Purpose and Runtime
Bulwark Webmail is a self-hosted Next.js 16/TypeScript webmail suite for Stalwart Mail Server, using JMAP as its primary backend protocol. It delivers mail, calendar, contacts, files, settings, PWA support, plugins, and administration. It runs as a Node/Next server, commonly in Docker: `Dockerfile`, `docker-compose.yml`, `next.config.ts`.

#### Application Entry and Routing
The App Router is the UI and server entry surface. The main shell, locale-scoped user workspace, setup flow, admin console, and sandboxed plugin pages are separated by route groups:
- `app/(main)/layout.tsx`, `app/(main)/[locale]/layout.tsx`
- `app/(main)/setup/page.tsx`, `app/(main)/admin/layout.tsx`
- `app/(sandbox)/plugin-sandbox/page.tsx`

Locale detection/routing and translation assembly form a cross-cutting boundary around user routes: `i18n/routing.ts`, `i18n/request.ts`, `locales/en/common.json`.

#### Client State and JMAP Data Plane
Zustand stores own client-side application state for accounts, auth, mail, calendars, contacts, files, settings, and plugins. UI components consume these stores; stores use the custom JMAP client and account-state coordination to operate across one or multiple accounts.
- `stores/account-store.ts`, `stores/email-store.ts`, `stores/calendar-store.ts`
- `lib/jmap/client.ts`, `lib/account-state-manager.ts`, `lib/unified-mailbox.ts`

The JMAP boundary isolates RFC 8620 calls, capability types, search, and Stalwart-specific passthrough. Stalwart integration also exposes JMAP through a server route where needed: `lib/jmap/client-interface.ts`, `lib/stalwart/jmap-passthrough.ts`, `app/api/account/stalwart/jmap/route.ts`.

#### User Workspace Modules
The locale workspace hosts the primary mail experience plus dedicated calendar, contacts, and files pages. Feature components render data managed by the corresponding stores and JMAP capabilities.
- `app/(main)/[locale]/page.tsx`, `components/email/email-composer.tsx`, `components/email/email-viewer.tsx`
- `app/(main)/[locale]/calendar/page.tsx`, `components/calendar/calendar-month-view.tsx`
- `app/(main)/[locale]/contacts/page.tsx`, `app/(main)/[locale]/files/page.tsx`, `components/files/file-browser.tsx`

Cross-feature protocols include CalDAV discovery/subscriptions, WebDAV/file operations, iCalendar fetching, and browser `mailto:`/`webcal:` handling: `app/api/caldav/discover/route.ts`, `app/api/webdav/route.ts`, `app/(main)/protocol/mailto/page.tsx`.

#### Authentication and Security Boundary
Authentication supports JMAP credentials, OAuth/OIDC discovery and PKCE/token exchange, SSO reauthentication, pairing, session management, impersonation, and TOTP-related exchange. Session credentials are encrypted and cookie-backed; external identity-provider interaction remains server-side API logic.
- `app/api/auth/session/route.ts`, `app/api/auth/sso/start/route.ts`, `app/api/auth/token/route.ts`
- `lib/auth/crypto.ts`, `lib/auth/session-cookie.ts`, `lib/oauth/pkce-server.ts`

#### Setup and Administrative Control Plane
First-launch setup persists administrator-managed configuration, branding, security, and JMAP server configuration; environment values override this managed state. The separate admin UI/API controls policy, audit, marketplace, plugins, themes, telemetry, and version checks.
- `app/api/setup/step/route.ts`, `lib/setup/state.ts`, `lib/admin/config-manager.ts`
- `app/(main)/admin/page.tsx`, `app/api/admin/config/route.ts`, `app/api/admin/plugins/route.ts`

Persistent deployment state is filesystem-backed under configured admin/state/settings directories, not a database. External services may include JMAP/Stalwart, OAuth/OIDC issuers, an extension directory, and web-push endpoints.

#### Plugin and Theme Extension Boundary
Plugins are installed and governed by admin APIs, validated/signed, then projected into UI slots. Third-party execution is isolated through sandbox pages, iframe bridging, consent, integrity/signature checks, tiering, and restricted host APIs.
- `lib/plugin-loader.ts`, `lib/plugin-validator.ts`, `lib/plugin-sandbox/runtime.tsx`
- `components/plugins/plugin-slot-renderer.tsx`, `app/(sandbox)/plugin-sandbox-privileged/page.tsx`
- `lib/theme-compiler.ts`, `lib/theme-loader.ts`

#### Operational Interfaces
Health, runtime configuration, update status, PWA assets, push-preview, telemetry, and error instrumentation are server-facing operational surfaces. Static service-worker and manifest support installation/offline browser integration.
- `app/api/health/route.ts`, `app/api/config/route.ts`, `app/api/system/update-status/route.ts`
- `app/manifest.ts`, `public/sw.js`, `instrumentation.node.ts`

Main entry points: [`app/(main)/[locale]/layout.tsx`](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/layout.tsx) (App shell & locale workspace), [`stores/account-store.ts`](https://github.com/bulwarkmail/webmail/blob/main/stores/account-store.ts) (Application stores), [`app/api/admin/config/route.ts`](https://github.com/bulwarkmail/webmail/blob/main/app/api/admin/config/route.ts) (Admin APIs), [`lib/plugin-loader.ts`](https://github.com/bulwarkmail/webmail/blob/main/lib/plugin-loader.ts) (Plugin & theme lifecycle)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_routes["App routes"]
  node_app_shell["App shell &amp; locale workspace<br/>Next.js routes<br/>[layout.tsx]"]
  node_mail_workspace["Mail workspace<br/>feature UI<br/>[page.tsx]"]
  node_calendar_workspace["Calendar workspace<br/>feature UI<br/>[page.tsx]"]
  node_contacts_files["Contacts &amp; files<br/>feature UI<br/>[page.tsx]"]
  node_protocol_handlers["Protocol handlers<br/>routes &amp; APIs<br/>[page.tsx]"]
  node_admin_ui["Admin console<br/>Next.js route<br/>[page.tsx]"]
  node_plugin_sandbox["Plugin sandbox<br/>isolated route<br/>[page.tsx]"]
end

subgraph group_client["Client data plane"]
  node_i18n["Locale routing &amp; translations<br/>i18n boundary<br/>[routing.ts]"]
  node_zustand_stores["Application stores<br/>Zustand state<br/>[account-store.ts]"]
  node_account_coordination["Account coordination<br/>state manager"]
  node_jmap_client["JMAP client<br/>protocol client<br/>[client.ts]"]
end

subgraph group_server["Server control plane"]
  node_auth_session["Auth &amp; sessions<br/>server auth APIs<br/>[route.ts]"]
  node_setup_config["Setup &amp; managed config<br/>control-plane state<br/>[state.ts]"]
  node_admin_api["Admin APIs<br/>control-plane APIs<br/>[route.ts]"]
  node_persistent_state[("Filesystem state<br/>deployment storage")]
  node_operations["Operational interfaces<br/>server APIs &amp; PWA<br/>[route.ts]"]
  node_stalwart_jmap_route["Stalwart JMAP route<br/>server proxy<br/>[route.ts]"]
end

subgraph group_extensions["Extensions"]
  node_plugin_theme_system["Plugin &amp; theme lifecycle<br/>extension manager<br/>[plugin-loader.ts]"]
end

subgraph group_external["External systems"]
  node_stalwart{{"Stalwart Mail Server<br/>JMAP backend"}}
  node_identity_providers{{"OAuth/OIDC issuers<br/>identity service"}}
  node_extension_services{{"Extension &amp; push services<br/>external services"}}
end

node_app_shell -->|"localizes"| node_i18n
node_app_shell -->|"hosts"| node_mail_workspace
node_app_shell -->|"hosts"| node_calendar_workspace
node_app_shell -->|"hosts"| node_contacts_files
node_mail_workspace -->|"uses"| node_zustand_stores
node_calendar_workspace -->|"uses"| node_zustand_stores
node_contacts_files -->|"uses"| node_zustand_stores
node_protocol_handlers -->|"uses"| node_jmap_client
node_zustand_stores -->|"coordinates"| node_account_coordination
node_account_coordination -->|"operates"| node_jmap_client
node_jmap_client -->|"JMAP"| node_stalwart
node_stalwart_jmap_route -->|"passthrough"| node_stalwart
node_auth_session -->|"OAuth/OIDC"| node_identity_providers
node_app_shell -->|"authenticates via"| node_auth_session
node_setup_config -->|"persists"| node_persistent_state
node_admin_ui -->|"manages through"| node_admin_api
node_admin_api -->|"updates"| node_setup_config
node_admin_api -->|"persists"| node_persistent_state
node_admin_api -->|"governs"| node_plugin_theme_system
node_plugin_theme_system -->|"loads isolated plugins"| node_plugin_sandbox
node_plugin_sandbox -.->|"restricted host bridge"| node_zustand_stores
node_plugin_theme_system -->|"directory &amp; push"| node_extension_services
node_operations -.->|"reads runtime state"| node_persistent_state

click node_app_shell "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/layout.tsx"
click node_mail_workspace "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/page.tsx"
click node_calendar_workspace "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/calendar/page.tsx"
click node_contacts_files "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/files/page.tsx"
click node_protocol_handlers "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/protocol/mailto/page.tsx"
click node_admin_ui "https://github.com/bulwarkmail/webmail/blob/main/app/(main)/admin/page.tsx"
click node_plugin_sandbox "https://github.com/bulwarkmail/webmail/blob/main/app/(sandbox)/plugin-sandbox/page.tsx"
click node_i18n "https://github.com/bulwarkmail/webmail/blob/main/i18n/routing.ts"
click node_zustand_stores "https://github.com/bulwarkmail/webmail/blob/main/stores/account-store.ts"
click node_account_coordination "https://github.com/bulwarkmail/webmail/blob/main/lib/account-state-manager.ts"
click node_jmap_client "https://github.com/bulwarkmail/webmail/blob/main/lib/jmap/client.ts"
click node_auth_session "https://github.com/bulwarkmail/webmail/blob/main/app/api/auth/session/route.ts"
click node_setup_config "https://github.com/bulwarkmail/webmail/blob/main/lib/setup/state.ts"
click node_admin_api "https://github.com/bulwarkmail/webmail/blob/main/app/api/admin/config/route.ts"
click node_operations "https://github.com/bulwarkmail/webmail/blob/main/app/api/health/route.ts"
click node_plugin_theme_system "https://github.com/bulwarkmail/webmail/blob/main/lib/plugin-loader.ts"
click node_stalwart_jmap_route "https://github.com/bulwarkmail/webmail/blob/main/app/api/account/stalwart/jmap/route.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_shell,node_mail_workspace,node_calendar_workspace,node_contacts_files,node_protocol_handlers,node_admin_ui,node_plugin_sandbox toneBlue
class node_i18n,node_zustand_stores,node_account_coordination,node_jmap_client toneAmber
class node_auth_session,node_setup_config,node_admin_api,node_persistent_state,node_operations,node_stalwart_jmap_route toneMint
class node_plugin_theme_system toneRose
class node_stalwart,node_identity_providers,node_extension_services toneIndigo
```

## Components

### App routes

- [App shell & locale workspace](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/layout.tsx): Next.js routes · Main layout, localized workspace, and setup entry. · `app/(main)/[locale]/layout.tsx`
- [Mail workspace](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/page.tsx): feature UI · Mail views and composer. · `app/(main)/[locale]/page.tsx`
- [Calendar workspace](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/calendar/page.tsx): feature UI · `app/(main)/[locale]/calendar/page.tsx`
- [Contacts & files](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/%5Blocale%5D/files/page.tsx): feature UI · Dedicated contacts and file browser pages. · `app/(main)/[locale]/files/page.tsx`
- [Protocol handlers](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/protocol/mailto/page.tsx): routes & APIs · mailto/webcal, CalDAV discovery, and WebDAV. · `app/(main)/protocol/mailto/page.tsx`
- [Admin console](https://github.com/bulwarkmail/webmail/blob/main/app/(main)/admin/page.tsx): Next.js route · `app/(main)/admin/page.tsx`
- [Plugin sandbox](https://github.com/bulwarkmail/webmail/blob/main/app/(sandbox)/plugin-sandbox/page.tsx): isolated route · Iframe-hosted third-party plugin execution. · `app/(sandbox)/plugin-sandbox/page.tsx`

### Client data plane

- [Locale routing & translations](https://github.com/bulwarkmail/webmail/blob/main/i18n/routing.ts): i18n boundary · `i18n/routing.ts`
- [Application stores](https://github.com/bulwarkmail/webmail/blob/main/stores/account-store.ts): Zustand state · Account, mail, calendar, contacts, files, settings, and plugins. · `stores/account-store.ts`
- [Account coordination](https://github.com/bulwarkmail/webmail/blob/main/lib/account-state-manager.ts): state manager · Coordinates multi-account state and unified mailboxes. · `lib/account-state-manager.ts`
- [JMAP client](https://github.com/bulwarkmail/webmail/blob/main/lib/jmap/client.ts): protocol client · RFC 8620 calls, capabilities, search, and Stalwart passthrough. · `lib/jmap/client.ts`

### Server control plane

- [Auth & sessions](https://github.com/bulwarkmail/webmail/blob/main/app/api/auth/session/route.ts): server auth APIs · Credentials, OAuth/OIDC PKCE, SSO, pairing, and encrypted cookies. · `app/api/auth/session/route.ts`
- [Setup & managed config](https://github.com/bulwarkmail/webmail/blob/main/lib/setup/state.ts): control-plane state · First-launch, branding, security, and JMAP configuration. · `lib/setup/state.ts`
- [Admin APIs](https://github.com/bulwarkmail/webmail/blob/main/app/api/admin/config/route.ts): control-plane APIs · Policy, audit, marketplace, plugins, telemetry, and updates. · `app/api/admin/config/route.ts`
- **Filesystem state**: deployment storage · Configured admin, state, and settings directories.
- [Operational interfaces](https://github.com/bulwarkmail/webmail/blob/main/app/api/health/route.ts): server APIs & PWA · Health, runtime config, updates, push preview, and instrumentation. · `app/api/health/route.ts`
- [Stalwart JMAP route](https://github.com/bulwarkmail/webmail/blob/main/app/api/account/stalwart/jmap/route.ts): server proxy · Server-side JMAP passthrough. · `app/api/account/stalwart/jmap/route.ts`

### Extensions

- [Plugin & theme lifecycle](https://github.com/bulwarkmail/webmail/blob/main/lib/plugin-loader.ts): extension manager · Validation, signing, installation, projection, and theme loading. · `lib/plugin-loader.ts`

### External systems

- **Stalwart Mail Server**: JMAP backend
- **OAuth/OIDC issuers**: identity service
- **Extension & push services**: external services · Extension directory and web-push endpoints.

## Connections

- App shell & locale workspace → Locale routing & translations: localizes
- App shell & locale workspace → Mail workspace: hosts
- App shell & locale workspace → Calendar workspace: hosts
- App shell & locale workspace → Contacts & files: hosts
- Mail workspace → Application stores: uses
- Calendar workspace → Application stores: uses
- Contacts & files → Application stores: uses
- Protocol handlers → JMAP client: uses
- Application stores → Account coordination: coordinates
- Account coordination → JMAP client: operates
- JMAP client → Stalwart Mail Server: JMAP
- Stalwart JMAP route → Stalwart Mail Server: passthrough
- Auth & sessions → OAuth/OIDC issuers: OAuth/OIDC
- App shell & locale workspace → Auth & sessions: authenticates via
- Setup & managed config → Filesystem state: persists
- Admin console → Admin APIs: manages through
- Admin APIs → Setup & managed config: updates
- Admin APIs → Filesystem state: persists
- Admin APIs → Plugin & theme lifecycle: governs
- Plugin & theme lifecycle → Plugin sandbox: loads isolated plugins
- Plugin sandbox → Application stores: restricted host bridge
- Plugin & theme lifecycle → Extension & push services: directory & push
- Operational interfaces → Filesystem state: reads runtime state
