Commit Graph
101 Commits
Author SHA1 Message Date
Daniel Eggert dd881edcce Add Command.Tag for command and response tags
`TaggedCommand(tag: "A1 NOOP\r\nA2 DELETE INBOX\r\nA3", command: .noop)`
was written verbatim, injecting a command; likewise `APPEND` start.
`TaggedResponse(tag: "* BYE x\r\nA1", …)` injected a response.

RFC 3501: `tag = 1*<any ASTRING-CHAR except "+">`. New `Command.Tag`
replaces the `String` in `TaggedCommand`, `TaggedResponse`, `Response`,
`CommandStreamPart`, `AppendCommand`, `BadCommand` and NIOIMAP's state
machine errors; `init?(_:)` validates, literals trap. The parser already
enforces the production. IMAPCommands converts its numeric tags.
2026-11-03 14:59:37 +01:00
AnthonyandDaniel Eggert d456d32908 Accept the responses a server may send during IDLE (#855)
Co-authored-by: Daniel Eggert <deggert@apple.com>
2026-10-13 17:08:26 +02:00
Daniel Eggert 088b6eb105 Fix race in ClientBootstrap (#870) 2026-10-12 15:32:49 +02:00
Daniel Eggert 6c8cfaddcc Add a imperative IMAP client and an imap-tool CLI (#841) 2026-09-25 16:16:41 +01:00
Christian 1c7076fab6 Fix framing of zero-length literals (#851) 2026-09-14 14:13:19 -07:00
K Gopal Krishnaandkaygdotorg 26a69caf84 Allow configuring IMAP client line buffer size (#849)
Co-authored-by: kaygdotorg <106914721+kaygdotorg@users.noreply.github.com>
2026-09-09 08:51:12 +02:00
Daniel Eggert 880366a417 Add macOS CI job (#836) 2026-07-07 11:37:57 +02:00
Léon Simmons 79c4237b5c docs: fix typo in AuthenticationStateMachine comment (#832)
authentiction → authentication
2026-07-03 16:33:52 +02:00
Daniel Eggert 0eaa07f877 Fix cr-lf handling in FramingParser (#830) 2026-06-22 08:10:24 -04:00
Daniel Eggert 983e0f1aff InvalidCommandForState error may leak PII (#828) 2026-06-02 16:37:19 +01:00
Daniel Eggert abfc170ff1 Only parse continuation responses while an AUTHENTICATE is in progress (#827) 2026-06-01 14:13:22 -04:00
Arnt Gulbrandsen 0d286ae155 Add support for RFC 9755 (#805) 2025-11-17 22:29:15 +01:00
Daniel Eggert efc0bfe244 Allow receiving untagged responses while appending (#799) 2025-06-11 13:52:48 -04:00
Daniel Eggert e672225d76 Rename → IMAPClientHandler.Message (#793) 2025-05-07 22:39:05 +02:00
Daniel Eggert b838dc2206 Pass response parser options into IMAPClientHandler.init (#789) 2025-05-06 15:30:31 +02:00
Daniel Eggert ae9498d7dd Fix receiving untagged responses during APPEND (#787) 2025-04-14 09:00:33 -07:00
Daniel Eggert 1b27cae1b4 Fix client handler encoding options (#786) 2025-04-11 17:33:25 +02:00
Daniel Eggert 851c3f89b6 Workaround for IMAPClientHandler and APPEND (#785) 2025-04-11 17:01:26 +02:00
Daniel Eggert ffec818c4c Fix IMAPServerHandler not resetting outstanding continuations (#783) 2025-04-08 12:25:11 +09:00
Daniel Eggert 1b3dc9e1cd FramingParser can crash when single CR is inside DQUOTE string (#779) 2025-02-12 14:07:40 +01:00
Daniel Eggert a0441a53d3 FramingParser: Allow un-matched DQUOTE in lines (#776) 2024-12-18 08:45:59 +01:00
Franz Busch 2cd1c5582a [CI] Adopt GitHub Actions (#775)
* [CI] Adopt GitHub Actions

# Motivation

We want to adopt GitHub actions in all of our Swift repositories to replace our current Jenkins CI.

# Modification

This PR adopts the reusable workflows from NIO and the swiftlang organization to ensure soundness and test coverage. Additionally, this PR enables a sematic label check and adds a `release.yml` to make releasing this package easy.

# Result

Modern CI and release flow.
2024-10-30 13:24:35 +00:00
Matt Diephouse 7d0f2cf799 Mark final set of types as Sendable (#771) 2024-09-18 15:06:30 +02:00
Daniel Eggert c041371b50 Fix for receiving untagged response during SASL auth (#743) 2023-06-27 18:03:12 +02:00
Daniel Eggert 4fdfe749c8 Need to allow Response.fetch while in IDLE (#718) 2022-10-07 08:03:02 -07:00
Daniel Eggert d4597194eb FramingParser needs to handle “quoted” strings. (#704) 2022-08-26 17:21:28 +02:00
David Evans 8ac6696b3c Implement encoding logging mode for redaction (#678)
Resolves #662

Command/Response Encoder can be configured using loggingMode. If turned on then user-sensitive strings won't be encoding, but any metadata will.

For example, 1 LOGIN username password becomes 1 LOGIN "" "", thereby hiding the user's information.
2022-01-26 15:50:12 -08:00
David Evans 480785353a Add literal size limit (#671) 2022-01-26 12:07:49 +01:00
David Evans 69f15a10ba Convert line length limit to 8192 (#666)
Resolves #663

Convert the default limits from 1000 to 8192 octets, as per RFC 7162.
2021-12-23 13:36:54 +00:00
Daniel Eggert 737a79e04d FramingParser additions (#667)
Support appending a `UnsafeRawBufferPointer`. This is useful when the input is `Data` or `DispatchData`.

Add `inputBufferByteCount: Int` property -- which is useful for logging and tests.

Add `testTwoCommandsAcrossMultipleBuffers()` test case.
2021-12-23 14:23:34 +01:00
David Evans fe03b16eb1 Fix Hashable implementations across all types (#648)
Resolves #634

The majority of types that were Equatable are now Hashable.

Anywhere that we had a custom == and the type was also Hashable we also now have custom hash functions.
2021-12-10 13:58:41 +00:00
David Evans fd7cef174d Implement a new framing parser case while inside a literal (#646) 2021-12-09 16:45:03 +00:00
David Evans 1615507941 Implement FramingResult (#622)
This introduces FramingResult, which signals either a "complete" frame, "incomplete", or "invalid".

If a frame is incomplete then the minimum number of required bytes to make a complete frame are sent.

if a frame is invalid or complete then you are provided with the bytes in that frame.

Invalid frames are forwarded onto the user via user events.

It doesn't have many bells and whistles yet, but it handles a lot of use cases we'll see in the wild, and you'll probably still find it useful

Given the command A1 LOG it will delivered .incompleteFrame(2) to signal that this frame is incomplete, and that we need at least 2 more bytes (in this case the CR and LF) to complete the frame (this is because it doesn't have the ability to understand the login command fully as it's only used to build frames).

Given the command A1 LOGIN {3}\r\na it will deliver 2 frames:

.complete("A1 LOGIN {3}\r\n
.complete("a") this is a "complete" frame because we're streaming a literal, so need to forward all bytes as soon as possible
Finally a case of invalid, A1 LOGIN {a will deliver 1 frame containing the data, including the byte where we detected the issue: .invalid("A1 LOGIN {a")
2021-12-09 11:09:09 +00:00
David EvansandCory Benfield 275a907a86 Implement the beginnings of a framing parser (#614)
* Boilerplate

* Add extremely simple tests cases

* Basic literal support

* Add literal+, literal-, and binary support

* Fix backtracking

* Start some integration tests

* A few type renames

* Make the framer throwing

* Soundness

* Address some of Cory's comments

* More graceful handling or CRs and LFs

* Apple Cory suggestions part 2

* Swallow LF

* Make it all work

* Refactor

* Define LineFeedStrategy enum

* Refactor integer parsing and add frame status

* Digit constants

* Refactor adjusting frame length

* Defend against infinite size

* Fix literal bugs

* Soundness

* Add some docs

* Update Sources/NIOIMAP/Client/FramingParser.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Prefer () over break

* Use UInt64 where appropriate

* Implement a stepback function

* Fix FrameSatus typo

* Don't allocate 2 buffers

* Rename LiteralSubstate to LiteralHeaderState

* Use Float64

* Remove frame debugging function

* Fix empty frame logic

* Discard empty bytes

* Fix handing optional LF in a literal

* Soundness

* Remove random Darwin import

* Cory nits

* Discard when we've read over half the buffer

* Read at least 1KB before cleaning

* Soundness

* Fix precondition

Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-09-15 16:38:03 +01:00
David Evans 59a57ee70b New client state machine (#610)
The client state machine has now been fully rewritten, and encapsulates the miniature state machines implemented in previous PRs.

I've extracted the state machine from IMAPClientHandler and moved it into ClientStateMachine, hence all the code deletion.

The state machine receives a command, response, or continuation request from the IMAPClientHandler and returns an array of Chunk/EventLoopPromise pairs that the client handler should then write to the network.

Also we didn't previously queue Append commands like we did for others, however it's now become apparent that all commands types should be queued, so we do that now.

A lot of tests have been added to make sure everything works.

Note that a lot of sendCommand tests have been deleted as we now precondition on user errors instead of throwing.
2021-09-10 13:45:11 +01:00
David Evans 79657fd985 Track command tags and restrict potential concurrent commands. (#596)
In most cases in IMAP it's possible to allow multiple commands to run concurrently. However, in some cases this would lead to undefined behaviour.

In particular, when appending, append is the only command that can be sent/active, as for authenticating, and obviously IDLE.

This PR implements that by tracking the outgoing tags, and the incoming tagged responses.
2021-07-30 11:04:15 +01:00
David Evans 9484279fe6 Add Append support to new client state machine (#595)
Identical pattern to that used for IDLE and AUTHENTICATE. The main ClientStateMachine detects the append command being sent, and switches state to contain a sub-machine that is dedicated to validating the lifecycle of an append command.

We support "standard" message appends, as well as catenation, and continuation requests for both.
2021-07-28 17:18:53 +01:00
David Evans d49bbc1968 New state machine authentication flow (#587)
Add support for authentication to the new client state machine, with a typical workflow test, and a test to show that attempting to send another command while authenticating will result in an error.
2021-07-20 13:30:40 +01:00
David Evans d7db35c768 New state machine with boilerplate for IDLE command (#586)
NOTE: The many defaults used when switching over things is just because this is an example. If we're happy with how this looks, I'll remove them and handle properly.

This is a basic demonstrative implementation of the new client state machine that shows how the IDLE command is supported.

For an idle state to be established, the client needs to send the idleStart command, and the server needs to respond with a continuationRequest, at which point the state is idle.
2021-07-19 14:58:44 +01:00
David Evans e02089be0e Make case naming uniform between commands and responses (#589)
Noticed by Matt, we had, for example, Command.tagged, but Response.taggedResponse. In general I've removed the superfluous Response at the end of each case.
2021-07-12 15:45:21 +01:00
David Evans a66eeb228b Use SPI to remove underscored members (#567)
Resolves #556

We've talked quite a bit offline, and decided that for now @_spi is the way to go, as it avoids build system issues. The goal is to remove the number of underscored public types and fields.
2021-06-02 19:21:47 +01:00
David Evans 8565ac900e Rename Command types to better represent what they are (#557)
Resolves #522

Some command names were misleading, they should be better now. Also removed a redundant initialiser.

PartialCommandStream -> SynchronizedCommand

CommandStream -> CommandStreamPart
2021-05-21 16:39:22 +01:00
David Evans 2f538c6bd0 Enable command pipelining (#552)
Resolves #528

Previously the user wasn't able to write a command while another was waiting for continuations. This isn't very friendly, and isn't great In any real-world scenario. Now we queue commands, and as soon as all continuations for one command have been handled, we move on to the next.
2021-04-29 17:09:34 +01:00
David Evans 10bdc22671 Replace KeyValues with OrderedDictionary from swift-collections (#546)
* Add swift-collections

* Delete KeyValues

* Soundness

* Fix comment
2021-04-14 10:05:06 +01:00
David Evans ff1b31f5f3 Remove superfluous underscores. (#548)
Way too many things were underscored that really didn't need to be.
2021-04-13 10:48:34 +01:00
David Evans 51f3058447 Rename authentication method and InitialClientResponse (#545)
* Rename method to mechanism

* Rename InitialClientResponse to ClientResponse

* Rename AuthKind to AuthMechanism
2021-04-12 15:22:10 +01:00
David Evans 0654d550e5 Fix continuation request state tracking to disambiguate idle and authentication (#526)
* Fix state tracking

* Soundness

* Fix tests

* Fix auth encoding

* Make state private

* Add reentrancy test

* Add asserts

* Better error types

* Soundness

* Use "real" example base64

* Soundness

* PR review

* Soundness

* Fix rebase
2021-04-07 13:41:52 +01:00
David EvansandJohannes Weiss 428b7b3488 Enable modifying IMAPClientHandler capabilities (#490)
* Add capabilities setting

* Tests

* Update Sources/NIOIMAP/Coders/IMAPClientHandler.swift

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>

* Fix non-sync literal test

* Use assert methods

* Fix test

* Sanity

* Enable changing capabilities

* Refactor to inout

* Switch to using IDs

* Default closure

* Soundness

* Fix rebase

* Re-add test

* Cleanup

* Soundness

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-04-06 15:04:14 +01:00
David Evans 89f9bbf419 Forward continuation requests as user events (#513)
* Forward user events

* Soundness

* Add test for idle

* IDLE test

* Convert to promises
2021-03-26 17:05:16 +00:00
David Evans a33ea576e3 New API for IDLE (#508)
* Rename to ServerResponse

* Docs

* Rename to IDLE started

* Fix compile

* Fix refactor

* Remove server response

* Soundness

* Remove underscore

* Remove associated data

* Soundness

* Write a continuation

* Soundness
2021-03-26 12:31:46 +00:00