mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
* feat: add craft-proto (IMAP4rev1 + SMTP clients) Port from mailcraft: IMAP4rev1 and SMTP protocol clients over any Read + Write stream, no sockets or browser APIs. ## IMAP (RFC 3501, 4315, 4959, 6154, 6851) - Mailbox operations: SELECT, EXAMINE, LIST, LSUB - Message operations: FETCH, STORE, COPY, MOVE, EXPUNGE - IDLE (RFC 2177): server-initiated change notifications - AUTH: PLAIN, LOGIN, XOAUTH2 (RFC 4954) - STATUS (RFC 3501 §6.3.10): mailbox stats without opening - Modified UTF-7 encoding for mailbox names ## SMTP (RFC 5321, 3207, 4954) - EHLO/HELO, MAIL FROM, RCPT TO, DATA - STARTTLS (RFC 3207) - AUTH: PLAIN, LOGIN, XOAUTH2 ## Design - No I/O of its own: works over any Read + Write stream - Hostile-input bounded reading: lines, literals, counts capped - Passwords and tokens never reach logs or errors (Secrets wiped on drop) - Follows craft-libs safety pattern: no unwrap/expect/panic * fix(proto): restore standalone protocol wiring and safe authentication helpers * fix(proto): register dependencies and export message macro * fix(proto): retain upstream private trace filtering and regression coverage * fix(proto): redact uppercase private reply text and preserve error API * fix(proto): satisfy hosted workspace lint gates * fix(proto): bound IDLE polling and preserve mailbox updates safely * test(proto): exercise BODY sections in FETCH context * test(proto): distinguish fixed UID ranges from wildcard bounds
85 lines
2.4 KiB
Rust
85 lines
2.4 KiB
Rust
// SPDX-FileCopyrightText: MailCraft contributors
|
||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
//! Test streams: a scripted server, and a stream that only fails.
|
||
|
||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||
use std::io::{Error, ErrorKind, Read, Write};
|
||
|
||
/// A canned server: reads hand out what the server "says" (a few bytes at a time, so every
|
||
/// boundary gets split somewhere), writes are kept for the test to look at.
|
||
#[derive(Debug)]
|
||
pub(crate) struct Script {
|
||
input: Vec<u8>,
|
||
pos: usize,
|
||
step: usize,
|
||
trickle: bool,
|
||
pub(crate) written: Vec<u8>,
|
||
}
|
||
|
||
impl Script {
|
||
/// Hands the bytes out 1–7 at a time.
|
||
pub(crate) fn new(server: impl AsRef<[u8]>) -> Script {
|
||
Script { input: server.as_ref().to_vec(), pos: 0, step: 0, trickle: true, written: Vec::new() }
|
||
}
|
||
|
||
/// Hands out as much as each read asks for.
|
||
pub(crate) fn whole(server: impl AsRef<[u8]>) -> Script {
|
||
Script { trickle: false, ..Script::new(server) }
|
||
}
|
||
|
||
/// What the client wrote, as text.
|
||
pub(crate) fn sent(&self) -> String {
|
||
String::from_utf8_lossy(&self.written).into_owned()
|
||
}
|
||
|
||
/// Whether the client read everything the script had.
|
||
pub(crate) fn finished(&self) -> bool {
|
||
self.pos >= self.input.len()
|
||
}
|
||
}
|
||
|
||
impl Read for Script {
|
||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||
let left = &self.input[self.pos.min(self.input.len())..];
|
||
let mut n = left.len().min(buf.len());
|
||
if self.trickle {
|
||
n = n.min([3, 1, 7, 2, 5, 4, 6][self.step % 7]);
|
||
self.step += 1;
|
||
}
|
||
buf[..n].copy_from_slice(&left[..n]);
|
||
self.pos += n;
|
||
Ok(n)
|
||
}
|
||
}
|
||
|
||
impl Write for Script {
|
||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||
self.written.extend_from_slice(buf);
|
||
Ok(buf.len())
|
||
}
|
||
|
||
fn flush(&mut self) -> std::io::Result<()> {
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
/// A stream whose every read and write fails.
|
||
#[derive(Debug)]
|
||
pub(crate) struct Broken(pub(crate) ErrorKind);
|
||
|
||
impl Read for Broken {
|
||
fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
|
||
Err(Error::from(self.0))
|
||
}
|
||
}
|
||
|
||
impl Write for Broken {
|
||
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
|
||
Err(Error::from(self.0))
|
||
}
|
||
|
||
fn flush(&mut self) -> std::io::Result<()> {
|
||
Err(Error::from(self.0))
|
||
}
|
||
}
|