Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Plugins must stay self-contained: if a plugin is not loaded, no trace of it shou

Per connection hooks for plugins that change the wire format: `connection.parserOptions` and `connection.compilerOptions` are passed to the `imap-handler` parser and compiler, `connection.importMailboxName(name)` converts every mailbox name argument (the `mailboxArguments` positions) to the modified UTF-7 storage name and throws a BAD error for an invalid name, and `connection.exportMailboxName(path)` converts a storage name for output. Every response that carries a mailbox name must convert it: either call `exportMailboxName` (LIST, LSUB, NAMESPACE), or put `{ type: 'MAILBOX', value: storageName }` in the attributes (also inside lists, like the MAILBOX correlator of ESEARCH), which `send` converts for the receiving session and sends as an atom or a string (STATUS, QUOTAROOT, METADATA, needed for notifications that go to several sessions). `matchFolders` takes the export function as its third argument. A command whose mailbox argument has no fixed position calls `importMailboxName` itself (GETMETADATA, ESEARCH). `connection.searchCharset` fixes the SEARCH charset of a session: a CHARSET argument is then BAD, and SORT/THREAD accept only that charset; `connection.defaultSearchCharset` only changes the charset assumed without CHARSET (IMAP4rev2: UTF-8). UTF8=ACCEPT and IMAP4rev2 set these through `src/utf8-session.ts`, which computes the UTF-8 state of a session from `connection.enabled`, so both plugins work in any load order. `connection.disabledSearchKeys` and `connection.disabledStatusItems` (Sets, or null) remove SEARCH keys and STATUS items from a session (BAD), `connection.addedStatusItems` allows STATUS items for one session only, `connection.number64` makes the `number64` SEARCH argument type (LARGER, SMALLER) take 63-bit values, and `connection.parserOptions.number64` (imap-handler) literal sizes and partial ranges (IMAP4rev2 sets both), `connection.messageGlobal` makes message/global parts encapsulate a message in BODYSTRUCTURE and section numbers (`resolveNode(tree, path, global)`, `embeddedMessage()` in `mimeparser.ts`), and `connection.getFlags(message)` can be replaced per connection (IMAP4rev2 leaves out `\Recent`). A plugin that needs another one lists its name in `plugin.requires` (UTF8=ACCEPT requires ENABLE), `src/load-plugins.ts` loads it first.

Other modules: `mimeparser.ts`, `bodystructure.ts`, `envelope.ts`, `addressparser.ts` produce BODYSTRUCTURE/ENVELOPE data from raw messages; `encoded-words.ts` decodes RFC 2047 encoded words for SEARCH header keys, SORT and THREAD; `sorting.ts` (collation, base subject, sent date, address and Message-ID values, and the search step) and `threading.ts` (THREAD command, ORDEREDSUBJECT and REFERENCES) back the SORT, SORT=DISPLAY and THREAD=* plugins; `smtp-listener.ts` is the optional SMTP listener (built on the optional `smtp-server` peer dependency) that appends incoming mail to INBOX; `cert/` holds the self-signed localhost cert used for STARTTLS and `secureConnection` (embedded through the generated `src/cert.ts`).
Other modules: `mimeparser.ts`, `bodystructure.ts`, `envelope.ts`, `addressparser.ts` produce BODYSTRUCTURE/ENVELOPE data from raw messages; `encoded-words.ts` decodes RFC 2047 encoded words for SEARCH header keys, SORT and THREAD; `sorting.ts` (collation, base subject, sent date, address and Message-ID values, and the search step) and `threading.ts` (THREAD command, ORDEREDSUBJECT and REFERENCES) back the SORT, SORT=DISPLAY and THREAD=* plugins; `maildir.ts` (`loadMaildir(dir)`, not in the package root or the browser bundle because it reads files) builds a storage object from a Maildir++ directory or a directory of `.eml` files for `imapkit --maildir`; `smtp-listener.ts` is the optional SMTP listener (built on the optional `smtp-server` peer dependency) that appends incoming mail to INBOX; `cert/` holds the self-signed localhost cert used for STARTTLS and `secureConnection` (embedded through the generated `src/cert.ts`).

## Tests

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Point your IMAP client to `localhost:1143` and log in with user name `testuser`

`imapkit -p 1143 --tls-port=1993` also listens for implicit TLS (IMAPS) connections on port 1993, both ports serve the same mailboxes. `imapkit --smtpPort=1025` also starts an SMTP server that appends every message it receives to INBOX. SMTP needs the optional [smtp-server](https://www.npmjs.com/package/smtp-server) package, which is not installed with ImapKit: `npm install -g smtp-server` (or `npm install smtp-server` next to a local install).

Run `imapkit --help` to see all command line options, the environment variables (`IMAPKIT_PORT`, `IMAPKIT_PLUGINS`, ...) and sample configuration data. For example, `imapkit -p 1143 --plugin=IDLE,MOVE,CONDSTORE --storage=storage.json` loads three plugins and the mailboxes of `storage.json`.
Run `imapkit --help` to see all command line options, the environment variables (`IMAPKIT_PORT`, `IMAPKIT_PLUGINS`, ...) and sample configuration data. For example, `imapkit -p 1143 --plugin=IDLE,MOVE,CONDSTORE --storage=storage.json` loads three plugins and the mailboxes of `storage.json`. `--maildir=<path>` loads the mailboxes from a Maildir++ directory or a directory of `.eml` files instead.

### Include as a Node.js module

Expand Down
18 changes: 12 additions & 6 deletions bin/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Usage: imapkit [OPTS]
-s, --secure=true Starts a secure server, defaults port to 993
-d, --debug=true Writes IMAP traffic to console
--storage=<path> Path to JSON file with the directory tree
--maildir=<path> Loads the mailboxes from a Maildir++ directory
(cur/new, flags from the file names, .Sub
folders) or from a directory of *.eml files
(subdirectories with *.eml files become
mailboxes). The files are read once at start
and never changed
--config=<config> Path to config JSON file
--script=<path> Path to a JSON file with script rules that
make the server misbehave on purpose (see
Expand Down Expand Up @@ -48,12 +54,12 @@ Usage: imapkit [OPTS]
run can be repeated

The options can also be set with environment variables: IMAPKIT_PORT,
IMAPKIT_SECURE, IMAPKIT_DEBUG, IMAPKIT_STORAGE, IMAPKIT_CONFIG,
IMAPKIT_SCRIPT, IMAPKIT_PLUGINS (comma separated), IMAPKIT_TLS_PORT,
IMAPKIT_SMTPPORT, IMAPKIT_REST_PORT, IMAPKIT_REST_HOST, IMAPKIT_REST_TOKEN,
IMAPKIT_OAUTH_PORT, IMAPKIT_OAUTH_HOST, IMAPKIT_OAUTH_CONSENT,
IMAPKIT_OAUTH_SECURE, IMAPKIT_OAUTH_ISSUER, IMAPKIT_QUIRKS and
IMAPKIT_SCRIPT_SEED.
IMAPKIT_SECURE, IMAPKIT_DEBUG, IMAPKIT_STORAGE, IMAPKIT_MAILDIR,
IMAPKIT_CONFIG, IMAPKIT_SCRIPT, IMAPKIT_PLUGINS (comma separated),
IMAPKIT_TLS_PORT, IMAPKIT_SMTPPORT, IMAPKIT_REST_PORT,
IMAPKIT_REST_HOST, IMAPKIT_REST_TOKEN, IMAPKIT_OAUTH_PORT,
IMAPKIT_OAUTH_HOST, IMAPKIT_OAUTH_CONSENT, IMAPKIT_OAUTH_SECURE,
IMAPKIT_OAUTH_ISSUER, IMAPKIT_QUIRKS and IMAPKIT_SCRIPT_SEED.

NB! If port or smtpPort values are below 1024 you most probably need to
use sudo or run the command with administrator rights.
Expand Down
12 changes: 12 additions & 0 deletions bin/imapkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import imapkit from '../dist/esm/index.js';
import { loadMaildir } from '../dist/esm/maildir.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageData = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8'));
Expand All @@ -19,6 +20,7 @@ const { values: argv } = parseArgs({
help: { type: 'boolean', short: 'h' },
config: { type: 'string' },
storage: { type: 'string' },
maildir: { type: 'string' },
script: { type: 'string' },
plugin: { type: 'string', multiple: true },
smtpPort: { type: 'string' },
Expand Down Expand Up @@ -52,6 +54,7 @@ const listOption = value =>

const configLocation = argv.config || process.env.IMAPKIT_CONFIG;
const storageLocation = argv.storage || process.env.IMAPKIT_STORAGE;
const maildirLocation = argv.maildir || process.env.IMAPKIT_MAILDIR;
const scriptLocation = argv.script || process.env.IMAPKIT_SCRIPT;
const smtpPort = argv.smtpPort || argv['smtp-port'] || process.env.IMAPKIT_SMTPPORT;
const tlsPort = argv['tls-port'] || process.env.IMAPKIT_TLS_PORT;
Expand All @@ -75,6 +78,11 @@ if (configLocation) {
config = JSON.parse(fs.readFileSync(configLocation, 'utf-8'));
}

if (storageLocation && maildirLocation) {
console.error('Failed to start ImapKit: --storage and --maildir can not be used together');
process.exit(1);
}

if (storageLocation) {
config.storage = JSON.parse(fs.readFileSync(storageLocation, 'utf-8'));
}
Expand Down Expand Up @@ -151,6 +159,10 @@ if (argv.help) {
}
let server;
try {
if (maildirLocation) {
// read here, not with the other files above, so that --help does not read a large directory
config.storage = loadMaildir(maildirLocation);
}
server = imapkit(config);
} catch (err) {
// an unknown plugin or quirk name, invalid storage ...
Expand Down
20 changes: 19 additions & 1 deletion docs/docs/getting-started/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Install the command with `npm install -g imapkit`, or run it from a project that
| `-s`, `--secure` | `IMAPKIT_SECURE=true` | Implicit TLS on the IMAP port, with the bundled self-signed certificate for `localhost`. |
| `-d`, `--debug` | `IMAPKIT_DEBUG=true` | Writes the IMAP traffic to the console. |
| `--storage=<path>` | `IMAPKIT_STORAGE` | JSON file with the mailbox tree, the `storage` option. See [Storage](../guides/storage.md). |
| `--maildir=<path>` | `IMAPKIT_MAILDIR` | Loads the mailbox tree from a Maildir++ directory or a directory of `.eml` files instead of a storage file, see [Mail files on disk](#mail-files-on-disk). |
| `--config=<path>` | `IMAPKIT_CONFIG` | JSON file with [server options](../reference/server-options.md), see [Configuration file](#configuration-file). |
| `--script=<path>` | `IMAPKIT_SCRIPT` | JSON file with [script rules](../faults/scripted-faults.md), a rule or a list of rules. |
| `--plugin=<names>` | `IMAPKIT_PLUGINS` | Plugins to load. Comma separated (`--plugin=IDLE,MOVE`) or repeated (`--plugin=IDLE --plugin=MOVE`). Names are case-insensitive and capability spellings like `LITERAL+` work too. |
Expand All @@ -46,7 +47,7 @@ Install the command with `npm install -g imapkit`, or run it from a project that
| `--smtpPort=<port>`, `--smtp-port=<port>` | `IMAPKIT_SMTPPORT` | Starts an SMTP server on this port that appends every message it receives to INBOX. Needs the optional `smtp-server` package, see [Installation](installation.md#optional-the-smtp-listener). |
| `-h`, `--help` | | Prints the options, every plugin with a short description, and sample config and storage files. |

A command line option wins over its environment variable, and both win over the config file. `--plugin` replaces the `plugins` list of the config file, `--storage` its `storage`, and `--script` its `script`.
A command line option wins over its environment variable, and both win over the config file. `--plugin` replaces the `plugins` list of the config file, `--storage` and `--maildir` its `storage`, and `--script` its `script`. `--storage` and `--maildir` can not be used together.

Ports below 1024 (143 and 993 included) usually need administrator rights. Pick a port such as 1143 for local use.

Expand Down Expand Up @@ -115,6 +116,23 @@ imapkit -p 1143 --plugin=SPECIAL-USE --storage=storage.json

Without a storage file the server has an empty INBOX. The file is read once at startup and never written. See [Storage](../guides/storage.md) for every key (UIDs, UIDVALIDITY, internal dates, special-use attributes, ACLs and more).

## Mail files on disk

`--maildir` builds the mailbox tree from files instead of a storage file, which is handy for a large test corpus or a copy of a real mailbox:

```bash
imapkit -p 1143 --maildir=./corpus
```

Two layouts are recognized:

- **Maildir++**, a directory with `cur` and `new` (or with `.Name` maildir folders, as Dovecot and Courier keep them). The messages of `new` and `cur` go to INBOX, a `.Work` folder becomes the mailbox `Work` and `.Work.Projects` becomes `Work/Projects`, a missing parent is a `\Noselect` name. Flags come from the `:2,` info of the file names (`D`, `F`, `R`, `S`, `T`, keyword letters are skipped), messages in `new` are `\Recent`, and `tmp` is ignored.
- **A directory of `.eml` files**. They go to INBOX, and every subdirectory that holds `.eml` files somewhere below it becomes a mailbox of the same name. Other files and hidden entries are skipped.

Messages get UIDs in file name order (numbers sorted as numbers), the modification time of a file is the internal date, and LF line breaks are converted to CRLF. Other mailboxes go into the `""` namespace with `/` as separator. Like a storage file, the files are read once at startup and never written: every change a client makes stays in memory. From code, `imapkit/lib/maildir` returns the same storage object, `imapkit({ storage: loadMaildir(dir) })`.

All messages are kept in memory. The parsed MIME structure of the messages a client has fetched adds about a third to their size (a 710 MB corpus takes about 1 GB), so a large corpus may need a bigger heap than the default (`node --max-old-space-size=4096`). A JSON storage file can not hold that much: Node.js reads it into one string, which can not be larger than about 512 MB.

## Script file

`--script` loads [script rules](../faults/scripted-faults.md) that make the server misbehave on purpose. JSON rules use strings for `match` and `send`:
Expand Down
Loading