diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 867decc..90183ed 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -339,8 +339,24 @@ révèle la longueur des conversations. est durable : l'invariant **persister avant d'envoyer ou d'acquitter** repose sur cette garantie. * Un verrou exclusif `.lock` (`File::try_lock`) empêche qu'un deuxième processus fasse reculer l'état d'un ratchet. -* En attendant le lot 4.5, la CLI utilise encore le fichier provisoire `*.mwid2` de - `meow-client` (un profil postcard chiffré d'un seul bloc). + +### 6.8 Utilisation par le client + +* `MeowClient` garde en mémoire l'état de chaque espace déverrouillé et ne sauvegarde que les + espaces modifiés : un espace caché ouvert mais inutilisé n'est jamais réécrit. +* Les acks en attente sont rangés dans l'espace qui possède la mailbox. +* `unlock(mot de passe)` rend les contacts d'un espace caché utilisables (listés, relevés, + joignables) jusqu'à la fermeture du client. Tant qu'un espace caché n'est pas déverrouillé, ses + mailbox ne sont pas relevées et ses messages attendent sur le serveur (voir §3.6). +* `hide_contact(nom, mot de passe)` déplace un contact, son historique, ses fichiers et ses acks + en attente vers l'espace caché ouvert par ce mot de passe, en le créant au besoin. + `reveal_contact` fait l'inverse. La destination est écrite avant la source : un plantage entre + les deux laisse une copie, jamais une perte. +* Les nouveaux contacts naissent dans l'espace principal. Une fois cachés, leurs anciens blocs + sont effacés puis recyclés. +* CLI : `-u/--unlock ` (répétable), `contact hide --space-password …`, + `contact reveal`, `identity password --new-password …`. Un mot de passe qui n'ouvre rien est + ignoré sans message. --- diff --git a/Cargo.lock b/Cargo.lock index 089c9c1..343c03c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1139,9 +1139,8 @@ dependencies = [ "meow-crypto", "meow-proto", "meow-server", - "postcard", + "meow-store", "reqwest", - "serde", "thiserror", "tokio", "tracing", diff --git a/README.md b/README.md index 1901818..a339623 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,11 @@ meow server-invitation create carol # → URL à transmettre à carol meow send alice "bonjour" meow # synchronise (= meow sync) meow longpoll # écoute en continu + +# contacts cachés (voir ARCHITECTURE.md §6) : invisibles sans leur mot de passe +meow contact hide alice --space-password 'autre secret' +meow -u 'autre secret' contact list # alice réapparaît (marquée [hidden]) +meow -u 'autre secret' # synchronise aussi les contacts cachés ``` ## Tests diff --git a/ROADMAP.md b/ROADMAP.md index a42be04..700e7d9 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -15,7 +15,7 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les ### Ordre conseillé pour la suite 1. ~~**3.8** livraison au moins une fois~~ ✅ -2. **4.5** brancher `meow-store` dans le client et la CLI (4.1 → 4.3 faits). +2. ~~**4.5** brancher `meow-store` dans le client et la CLI~~ ✅ 3. **5.5, 5.6, 5.9** file d'envoi, accusés, flux d'événements (prérequis de l'UI). 4. **7.1 → 7.3** FFI et intégration dans bastet. 5. Le reste (phases 8–10) selon les priorités. @@ -24,6 +24,7 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les | Date | Lots | Notes | |---|---|---| +| 2026-11-20 | 4.5 | `meow-client` passe sur `meow-store` (un état par espace, sauvegarde des seuls espaces modifiés, acks par espace) ; API `unlock`, `hide_contact`, `reveal_contact`, `change_password`, `history_page`, `file`. CLI : `-u`, `contact hide/reveal`, `identity password`. Store derrière un `Mutex` pour garder le client `Send + Sync` (requis pour la FFI). Le fichier `.mwid2` provisoire disparaît. 38 tests, e2e avec sections « contact caché » et « changement de mot de passe ». | | 2026-11-20 | 4.1–4.3 | `meow-store` repensé après relecture du stockage v1 : objectifs anti-saisie (rien ne relie les blocs sans mot de passe, espaces cachés à un ou plusieurs contacts). Sac de blocs uniformes, espaces, segments. Un premier brouillon avec une colonne `contact` en clair a été abandonné. 6 tests de store. Pas encore branché dans le client. | | 2026-11-20 | 3.8 | Pull non destructif + acks signés dans `MailboxAuth`, `PendingAck` enregistrés avec l'état, redélivrances dédupliquées, pagination (`more`), mise en retraite des mailbox disparues. Redis : liste → stream. Profil client au format 2 (les `.mwid2` de format 1 sont refusés). 31 tests, e2e mémoire et Redis verts, et plus aucun message résiduel dans Redis à la fin de l'e2e. | | 2026-11-20 | 0.x, 1.1–1.4, 2.1–2.4, 3.1–3.4, 4.0, 5.1–5.4, 6.1–6.5 | Squelette complet et fonctionnel. 29 tests Rust + `tests/e2e.sh` (≈ 60 appels CLI, 2 serveurs, mémoire et Redis, bascule quand un serveur tombe). Décision : protocole v2 sans compatibilité v1. | @@ -77,12 +78,12 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les | Lot | Contenu | Fin | État | |---|---|---|---| -| 4.0 | *Provisoire* : fichier d'identité chiffré unique (`*.mwid2`) dans `meow-client`, avec écriture atomique et verrou exclusif | La CLI fonctionne | ✅ | +| 4.0 | *Provisoire, remplacé par 4.5* : fichier d'identité chiffré unique dans `meow-client` | La CLI fonctionne | ✅ (retiré) | | 4.1 | **Sac de blocs** : un fichier SQLite `meta` + `blocks(id aléatoire, 4096 octets)`, objets découpés en blocs, réécriture vers d'autres blocs avec effacement des anciens, recyclage des blocs libres, aléa de remplissage non enregistré, migrations | Tests : uniformité des blocs, rien en clair, blocs non permutables | ✅ | | 4.2 | **Espaces** : racine à `HMAC(Argon2id(mot de passe, sel))`, espace principal et espaces cachés (un ou plusieurs contacts), `unlock` à coût constant, changement de mot de passe | Tests : un espace caché est invisible avec le mot de passe principal | ✅ | | 4.3 | **Segments** de conversation (≈ 16 Kio), historique page par page, fichiers reçus enregistrés avec le message (donc avant l'ack) | Tests | ✅ | | 4.4 | Recherche dans l'historique, aperçus de fichiers, compactage du fichier (reconstruction avec l'aléa) | Tests | ⬜ | -| 4.5 | Bascule de `meow-client` vers `meow-store`, avec les espaces dans l'API (`unlock`, `create_space`, déplacement d'un contact vers un espace caché) et dans la CLI. Les espaces cachés déverrouillés sont relevés comme les autres | e2e vert | ⬜ **prochain** | +| 4.5 | Bascule de `meow-client` vers `meow-store`, avec les espaces dans l'API (`unlock`, `create_space`, déplacement d'un contact vers un espace caché) et dans la CLI. Les espaces cachés déverrouillés sont relevés comme les autres | e2e vert | ✅ | | 4.6 | Test de type « analyse d'appareil » avec plusieurs copies du fichier : mesurer ce qu'un attaquant déduit de la différence entre deux copies, et ajuster la réécriture aléatoire des blocs libres et l'ajout d'aléa (voir ARCHITECTURE §6.6) | Rapport | ⬜ | ## Phase 5 — `meow-client` (façade asynchrone) diff --git a/crates/meow-cli/src/main.rs b/crates/meow-cli/src/main.rs index ac55a0c..bda0bee 100644 --- a/crates/meow-cli/src/main.rs +++ b/crates/meow-cli/src/main.rs @@ -20,6 +20,10 @@ struct Cli { /// Password protecting the identity file. #[arg(short, long, env = "MEOW_PASSWORD", hide_env_values = true, global = true)] password: Option, + /// Hidden-space password(s) to try after opening (repeatable). A password that opens + /// nothing is silently ignored. + #[arg(short = 'u', long = "unlock", global = true)] + unlock: Vec, /// Use very weak key derivation (tests only!). #[arg(long, env = "MEOW_INSECURE_FAST_KDF", hide = true, global = true)] insecure_fast_kdf: bool, @@ -87,6 +91,11 @@ enum IdentityCmd { }, /// Show the identity summary. Show, + /// Change the main password. + Password { + #[arg(long)] + new_password: String, + }, } #[derive(Subcommand)] @@ -104,8 +113,23 @@ enum ServerCmd { #[derive(Subcommand)] enum ContactCmd { List, - Show { name: String }, - Remove { name: String }, + Show { + name: String, + }, + Remove { + name: String, + }, + /// Move a contact (with its history) into the hidden space opened by a password + /// (created if needed). Several contacts may share one. + Hide { + name: String, + #[arg(long)] + space_password: String, + }, + /// Move a hidden contact (unlock its space with -u) back to the main space. + Reveal { + name: String, + }, } #[derive(Args)] @@ -186,15 +210,20 @@ fn default_identity() -> PathBuf { struct Ctx { identity: PathBuf, password: String, + unlock: Vec, kdf: KdfParams, downloads: PathBuf, } impl Ctx { async fn open(&self) -> Result { - MeowClient::open(&self.identity, &self.password, self.kdf) + let mut c = MeowClient::open(&self.identity, &self.password) .await - .with_context(|| format!("cannot open identity {}", self.identity.display())) + .with_context(|| format!("cannot open identity {}", self.identity.display()))?; + for pw in &self.unlock { + c.unlock(pw).await?; + } + Ok(c) } } @@ -223,6 +252,7 @@ async fn run() -> Result<()> { let ctx = Ctx { identity: cli.identity.unwrap_or_else(default_identity), password, + unlock: cli.unlock, kdf: if cli.insecure_fast_kdf { KdfParams::INSECURE_TEST } else { @@ -245,6 +275,10 @@ async fn run() -> Result<()> { println!("servers: {}", c.servers().len()); println!("contacts: {}", c.contacts().len()); } + Cmd::Identity(IdentityCmd::Password { new_password }) => { + ctx.open().await?.change_password(&new_password).await?; + println!("password changed"); + } Cmd::Server(ServerCmd::Add { url }) => { let mut c = ctx.open().await?; let s = c.add_server(&url).await?; @@ -263,7 +297,8 @@ async fn run() -> Result<()> { Cmd::Contact(ContactCmd::List) => { let c = ctx.open().await?; for k in c.contacts() { - println!("{}\t{:?}", k.name, k.state); + let hidden = if c.is_hidden(&k.name)? { "\t[hidden]" } else { "" }; + println!("{}\t{:?}{hidden}", k.name, k.state); } } Cmd::Contact(ContactCmd::Show { name }) => { @@ -300,6 +335,14 @@ async fn run() -> Result<()> { ctx.open().await?.remove_contact(&name).await?; println!("removed {name}"); } + Cmd::Contact(ContactCmd::Hide { name, space_password }) => { + ctx.open().await?.hide_contact(&name, &space_password).await?; + println!("{name} is now hidden"); + } + Cmd::Contact(ContactCmd::Reveal { name }) => { + ctx.open().await?.reveal_contact(&name).await?; + println!("{name} is now visible"); + } Cmd::Invitation(cmd) => invitation(&ctx, cmd).await?, Cmd::ServerInvitation(ServerInvitationCmd::Create { contact, @@ -455,7 +498,9 @@ fn print_events(ctx: &Ctx, events: &[Event]) -> Result<()> { for e in events { match e { Event::Message { contact, text, .. } => println!("[{contact}] {text}"), - Event::File { contact, name, data } => { + Event::File { + contact, name, data, .. + } => { std::fs::create_dir_all(&ctx.downloads)?; let path = ctx.downloads.join(safe_name(name)); std::fs::write(&path, data)?; diff --git a/crates/meow-client/Cargo.toml b/crates/meow-client/Cargo.toml index 5dd5c50..536739b 100644 --- a/crates/meow-client/Cargo.toml +++ b/crates/meow-client/Cargo.toml @@ -9,11 +9,10 @@ license.workspace = true meow-core = { path = "../meow-core" } meow-proto = { path = "../meow-proto" } meow-crypto = { path = "../meow-crypto" } +meow-store = { path = "../meow-store" } reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] } tokio = { version = "1", features = ["fs", "time", "macros"] } futures-util = "0.3" -serde = { version = "1", features = ["derive"] } -postcard = { version = "1", default-features = false, features = ["alloc"] } thiserror = "2" tracing = "0.1" diff --git a/crates/meow-client/src/lib.rs b/crates/meow-client/src/lib.rs index f1d0e17..3ebb88f 100644 --- a/crates/meow-client/src/lib.rs +++ b/crates/meow-client/src/lib.rs @@ -1,20 +1,26 @@ //! High-level async client API for Meow v2. //! -//! [`MeowClient`] owns one identity (profile) and exposes servers, contacts, -//! invitations, messaging and synchronisation. It is the API mirrored by the CLI -//! and, later, by the Flutter FFI layer (`meow-ffi`). +//! [`MeowClient`] owns one identity store ([`meow_store`]) and exposes servers, +//! contacts, invitations, messaging and synchronisation. It is the API mirrored by the +//! CLI and, later, by the Flutter FFI layer (`meow-ffi`). +//! +//! **Spaces.** The main password opens the main space (nickname, servers, visible +//! contacts). [`MeowClient::unlock`] opens hidden spaces, each holding one or several +//! contacts; their contacts then behave like the others (listed, polled, usable) until +//! the client is dropped. Only spaces that changed are written back, so an unused +//! hidden space is never touched on disk. //! //! Invariant: any ratchet/contact state change is **persisted before** the -//! resulting bytes leave the device, so a crash can never make us reuse a key. +//! resulting bytes leave the device (or the server is told to delete a message), so +//! a crash can never make us reuse a key or lose a message. #![forbid(unsafe_code)] #![cfg_attr(not(test), warn(clippy::unwrap_used, clippy::expect_used))] -pub mod profile; pub mod transport; use std::collections::BTreeMap; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use futures_util::future::join_all; @@ -26,10 +32,10 @@ use meow_crypto::password::KdfParams; use meow_crypto::{SignPublic, SigningKeyPair}; use meow_proto::v2::{self, request::Body as Req, response::Body as Resp}; use meow_proto::Message; +use meow_store::{Batch, RetiredMailbox, SpaceId, State as SpaceState, Store}; pub use meow_core::contact::ContactState as State; -use profile::RetiredMailbox; -pub use profile::{Content, HistoryEntry, PendingAck}; +pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck}; /// Maximum file size sent in a single message (chunking: roadmap 5.7). pub const MAX_FILE_BYTES: usize = 4 * 1024 * 1024; @@ -38,10 +44,12 @@ pub const DEFAULT_INVITATION_TTL: u64 = 7 * 86_400; #[derive(Debug, thiserror::Error)] pub enum Error { - #[error("wrong password or corrupted identity file")] + #[error("wrong password")] BadPassword, - #[error("corrupted identity file: {0}")] - Corrupted(String), + #[error("identity {0} is in use by another process")] + Locked(String), + #[error("storage: {0}")] + Store(meow_store::Error), #[error("i/o: {0}")] Io(#[from] std::io::Error), #[error("network: {0}")] @@ -60,8 +68,18 @@ pub enum Error { NameTaken(String), #[error("{0}")] Invalid(String), - #[error("identity {0} is in use by another process")] - Locked(String), +} + +impl From for Error { + fn from(e: meow_store::Error) -> Self { + match e { + meow_store::Error::BadPassword => Error::BadPassword, + meow_store::Error::Locked(p) => Error::Locked(p), + meow_store::Error::Exists(p) => Error::Invalid(format!("{p} already exists (use --force)")), + meow_store::Error::Io(e) => Error::Io(e), + other => Error::Store(other), + } + } } pub type Result = std::result::Result; @@ -94,8 +112,10 @@ pub enum Event { text: String, sent_at_ms: u64, }, + /// A file was received (its content is also kept: [`MeowClient::file`]). File { contact: String, + id: Vec, name: String, data: Vec, }, @@ -109,85 +129,195 @@ pub struct SendReport { pub delivered_to: usize, } -/// One identity, with its servers, contacts and history. /// Acknowledged ids sent per mailbox and pull (server limit: 1024). const MAX_ACKS_PER_MAILBOX: usize = 512; #[derive(Default)] struct PullRound { - /// Messages to process. - new: Vec, + /// Messages to process, with the index of the space owning their mailbox. + new: Vec<(usize, v2::StoredMessage)>, /// A server held messages back. more: bool, } -pub struct MeowClient { - path: PathBuf, - password: String, - kdf: KdfParams, - profile: profile::Profile, - transport: transport::Transport, - /// Exclusive OS lock on `.lock`, held for the client's lifetime: - /// two processes saving the same ratchet state would roll sessions back. - _lock: std::fs::File, +/// One unlocked space, in memory. +struct Space { + id: SpaceId, + state: SpaceState, + /// History and files waiting for the next save of this space. + batch: Batch, + /// Changed since the last save. + dirty: bool, } -fn lock_identity(path: &Path) -> Result { - let mut lock_path = path.as_os_str().to_owned(); - lock_path.push(".lock"); - let f = std::fs::OpenOptions::new() - .create(true) - .truncate(false) - .write(true) - .open(PathBuf::from(lock_path))?; - match f.try_lock() { - Ok(()) => Ok(f), - Err(std::fs::TryLockError::WouldBlock) => Err(Error::Locked(path.display().to_string())), - Err(std::fs::TryLockError::Error(e)) => Err(Error::Io(e)), - } +/// One identity: its store and unlocked spaces. Index 0 is always the main space. +pub struct MeowClient { + /// Behind a mutex only to make the client `Sync` (SQLite connections are not); + /// never held across an `.await`. + store: std::sync::Mutex, + spaces: Vec, + transport: transport::Transport, +} + +type Mailboxes = Vec<(SigningKeyPair, Vec)>; + +fn lock(store: &std::sync::Mutex) -> Result> { + store.lock().map_err(|_| Error::Invalid("store lock poisoned".into())) } impl MeowClient { // ───────────────────────── lifecycle ───────────────────────── - /// Create a new identity file. Fails if it exists, unless `overwrite`. + /// Create a new identity. Fails if `path` exists, unless `overwrite`. pub async fn create(path: &Path, nickname: &str, password: &str, kdf: KdfParams, overwrite: bool) -> Result { - if !overwrite && path.exists() { - return Err(Error::Invalid(format!( - "{} already exists (use --force)", - path.display() - ))); - } - let c = Self { - path: path.to_path_buf(), - password: password.to_string(), - kdf, - profile: profile::Profile::new(nickname), - transport: transport::Transport::new()?, - _lock: lock_identity(path)?, - }; - c.save().await?; - Ok(c) + let store = Store::create(path, nickname, password, kdf, overwrite)?; + Self::with_store(store) } - /// Open an existing identity file. `kdf` is used for subsequent saves. - pub async fn open(path: &Path, password: &str, kdf: KdfParams) -> Result { + /// Open an existing identity with its main password. + pub async fn open(path: &Path, password: &str) -> Result { + Self::with_store(Store::open(path, password)?) + } + + fn with_store(mut store: Store) -> Result { + let state = store.load(SpaceId::MAIN)?; Ok(Self { - path: path.to_path_buf(), - password: password.to_string(), - kdf, - _lock: lock_identity(path)?, - profile: profile::Profile::load(path, password).await?, + store: std::sync::Mutex::new(store), + spaces: vec![Space { + id: SpaceId::MAIN, + state, + batch: Batch::default(), + dirty: false, + }], transport: transport::Transport::new()?, }) } - pub async fn save(&self) -> Result<()> { - self.profile.save(&self.path, &self.password, self.kdf).await + /// Write every changed space. + pub async fn save(&mut self) -> Result<()> { + for i in 0..self.spaces.len() { + if self.spaces[i].dirty { + self.save_space(i)?; + } + } + Ok(()) + } + + fn save_space(&mut self, i: usize) -> Result<()> { + let sp = &mut self.spaces[i]; + let batch = std::mem::take(&mut sp.batch); + lock(&self.store)?.save(sp.id, &sp.state, batch)?; + sp.dirty = false; + Ok(()) + } + + /// Change the main password (instantaneous: only the space root moves). + pub async fn change_password(&mut self, new_password: &str) -> Result<()> { + Ok(lock(&self.store)?.change_password(SpaceId::MAIN, new_password)?) } pub fn nickname(&self) -> &str { - &self.profile.nickname + &self.main().nickname + } + + fn main(&self) -> &SpaceState { + &self.spaces[0].state + } + + // ───────────────────────── hidden spaces ───────────────────────── + + /// Try a hidden-space password; `true` if it opened one (its contacts are now + /// available). Same cost whether or not such a space exists. + pub async fn unlock(&mut self, password: &str) -> Result { + let found = lock(&self.store)?.unlock(password)?; + match found { + Some(id) => { + self.space_index(id)?; + Ok(true) + } + None => Ok(false), + } + } + + /// Index of an unlocked space in `self.spaces`, loading it if needed. + fn space_index(&mut self, id: SpaceId) -> Result { + if let Some(i) = self.spaces.iter().position(|s| s.id == id) { + return Ok(i); + } + let state = lock(&self.store)?.load(id)?; + self.spaces.push(Space { + id, + state, + batch: Batch::default(), + dirty: false, + }); + Ok(self.spaces.len() - 1) + } + + /// Move a contact, with its history and files, into the hidden space opened by + /// `password` (created if none). Several contacts may share a space. + pub async fn hide_contact(&mut self, name: &str, password: &str) -> Result<()> { + let found = lock(&self.store)?.unlock(password)?; + let id = match found { + Some(id) => id, + None => lock(&self.store)?.create_space(password)?, + }; + if id == SpaceId::MAIN { + return Err(Error::Invalid("this is the main password".into())); + } + let target = self.space_index(id)?; + self.move_contact(name, target) + } + + /// Move a hidden contact back to the main space. + pub async fn reveal_contact(&mut self, name: &str) -> Result<()> { + self.move_contact(name, 0) + } + + /// Whether a contact lives in a hidden space. + pub fn is_hidden(&self, name: &str) -> Result { + Ok(self.locate(name)?.0 != 0) + } + + fn move_contact(&mut self, name: &str, target: usize) -> Result<()> { + let (si, ci) = self.locate(name)?; + if si == target { + return Ok(()); + } + let contact = self.spaces[si].state.contacts[ci].clone(); + let src = self.spaces[si].id; + let history = lock(&self.store)?.full_history(src, &contact.id)?; + let mut files = Vec::new(); + for h in history.iter().filter(|h| !h.outbound) { + if matches!(h.content, Content::File { .. }) { + if let Some(data) = lock(&self.store)?.file(src, &contact.id, &h.id)? { + files.push((contact.id.clone(), h.id.clone(), data)); + } + } + } + let mailboxes: Vec> = contact + .mailboxes() + .iter() + .map(|(kp, _)| kp.public().as_bytes().to_vec()) + .collect(); + let acks: Vec = self.spaces[si] + .state + .pending_acks + .iter() + .filter(|a| mailboxes.contains(&a.mailbox)) + .cloned() + .collect(); + // Write the destination first: a crash in between leaves a copy, never a loss. + let t = &mut self.spaces[target]; + t.state.contacts.push(contact); + t.state.pending_acks.extend(acks); + t.batch.history.extend(history); + t.batch.files.extend(files); + self.save_space(target)?; + let s = &mut self.spaces[si]; + s.state.contacts.remove(ci); + s.state.pending_acks.retain(|a| !mailboxes.contains(&a.mailbox)); + self.save_space(si) } // ───────────────────────── servers ───────────────────────── @@ -199,7 +329,7 @@ impl MeowClient { tracing::warn!("server announces {} (contacted as {url})", info.url); } if let Some(known) = self - .profile + .main() .servers .iter() .find(|s| s.url == info.url || s.sign_key == info.sign_key) @@ -209,38 +339,39 @@ impl MeowClient { } return Ok(known.clone()); } - self.profile.servers.push(info.clone()); - self.save().await?; + self.spaces[0].state.servers.push(info.clone()); + self.save_space(0)?; Ok(info) } pub fn servers(&self) -> &[ServerInfo] { - &self.profile.servers + &self.main().servers } pub async fn remove_server(&mut self, url_or_name: &str) -> Result<()> { let u = canonical_url(url_or_name); - let before = self.profile.servers.len(); - self.profile.servers.retain(|s| s.url != u && s.name != url_or_name); - if self.profile.servers.len() == before { + let servers = &mut self.spaces[0].state.servers; + let before = servers.len(); + servers.retain(|s| s.url != u && s.name != url_or_name); + if servers.len() == before { return Err(Error::UnknownServer(url_or_name.to_string())); } - self.save().await + self.save_space(0) } /// Resolve URLs to known servers; an empty list means "all my servers". fn resolve_servers(&self, urls: &[String]) -> Result> { + let servers = &self.main().servers; if urls.is_empty() { - if self.profile.servers.is_empty() { + if servers.is_empty() { return Err(Error::Invalid("no server configured: add one first".into())); } - return Ok(self.profile.servers.clone()); + return Ok(servers.clone()); } urls.iter() .map(|u| { let cu = canonical_url(u); - self.profile - .servers + servers .iter() .find(|s| s.url == cu || s.name == *u) .cloned() @@ -251,48 +382,68 @@ impl MeowClient { // ───────────────────────── contacts ───────────────────────── - pub fn contacts(&self) -> &[Contact] { - &self.profile.contacts + /// Contacts of every unlocked space (main space first). + pub fn contacts(&self) -> Vec<&Contact> { + self.spaces.iter().flat_map(|s| s.state.contacts.iter()).collect() } pub fn contact(&self, name: &str) -> Result<&Contact> { - self.profile - .contacts - .iter() - .find(|c| c.name == name) + let (si, ci) = self.locate(name)?; + Ok(&self.spaces[si].state.contacts[ci]) + } + + /// (space index, contact index) of a contact. + fn locate(&self, name: &str) -> Result<(usize, usize)> { + self.find(|c| c.name == name) .ok_or_else(|| Error::UnknownContact(name.to_string())) } - fn contact_index(&self, name: &str) -> Result { - self.profile - .contacts + fn find(&self, pred: impl Fn(&Contact) -> bool) -> Option<(usize, usize)> { + self.spaces .iter() - .position(|c| c.name == name) - .ok_or_else(|| Error::UnknownContact(name.to_string())) + .enumerate() + .find_map(|(si, s)| s.state.contacts.iter().position(&pred).map(|ci| (si, ci))) } fn ensure_free_name(&self, name: &str) -> Result<()> { - if self.profile.contacts.iter().any(|c| c.name == name) { + if self.find(|c| c.name == name).is_some() { return Err(Error::NameTaken(name.to_string())); } Ok(()) } + /// Delete a contact with its history and files. pub async fn remove_contact(&mut self, name: &str) -> Result<()> { - let i = self.contact_index(name)?; - let before = self.contact_mailboxes(); - let c = self.profile.contacts.remove(i); - self.profile.history.retain(|h| h.contact != c.id); - self.retire_mailboxes(before); - self.save().await + let (si, ci) = self.locate(name)?; + let before = self.space_mailboxes(si); + self.spaces[si].state.contacts.remove(ci); + self.retire_mailboxes(si, before); + self.save_space(si) } - pub fn history(&self, name: &str) -> Result> { - let id = &self.contact(name)?.id; - Ok(self.profile.history.iter().filter(|h| &h.contact == id).collect()) + /// Whole conversation with a contact, oldest first. + pub fn history(&self, name: &str) -> Result> { + let (si, ci) = self.locate(name)?; + let sp = &self.spaces[si]; + Ok(lock(&self.store)?.full_history(sp.id, &sp.state.contacts[ci].id)?) + } + + /// One segment of a conversation (`None`: the latest), oldest first. + pub fn history_page(&self, name: &str, page: Option) -> Result { + let (si, ci) = self.locate(name)?; + let sp = &self.spaces[si]; + Ok(lock(&self.store)?.history(sp.id, &sp.state.contacts[ci].id, page)?) + } + + /// Content of a received file. + pub fn file(&self, name: &str, message_id: &[u8]) -> Result>> { + let (si, ci) = self.locate(name)?; + let sp = &self.spaces[si]; + Ok(lock(&self.store)?.file(sp.id, &sp.state.contacts[ci].id, message_id)?) } // ───────────────────────── invitations ───────────────────────── + // New contacts are created in the main space; hide them afterwards if needed. /// Step 1: create an invitation for `contact_name`. Returns the encoded offer. pub async fn create_invitation( @@ -304,9 +455,9 @@ impl MeowClient { ) -> Result> { self.ensure_free_name(contact_name)?; let servers = self.resolve_servers(server_urls)?; - let offer = invitation::create_offer(contact_name, &self.profile.nickname, servers, message, now(), ttl)?; - self.profile.contacts.push(offer.contact); - self.save().await?; + let offer = invitation::create_offer(contact_name, self.nickname(), servers, message, now(), ttl)?; + self.spaces[0].state.contacts.push(offer.contact); + self.save_space(0)?; Ok(offer.offer_bytes) } @@ -324,51 +475,40 @@ impl MeowClient { let info = self.inspect_invitation(offer_bytes)?; let name = contact_name.unwrap_or(&info.initiator_name).to_string(); self.ensure_free_name(&name)?; - if self - .profile - .contacts - .iter() - .any(|c| c.invitation_id == info.invitation_id) - { + if self.find(|c| c.invitation_id == info.invitation_id).is_some() { return Err(Error::Invalid("this invitation was already answered".into())); } let servers = self.resolve_servers(server_urls)?; - let (contact, answer) = invitation::answer(offer_bytes, &name, &self.profile.nickname, servers, now())?; - self.profile.contacts.push(contact); - self.save().await?; + let (contact, answer) = invitation::answer(offer_bytes, &name, self.nickname(), servers, now())?; + self.spaces[0].state.contacts.push(contact); + self.save_space(0)?; Ok(answer.encode_to_vec()) } - fn by_invitation(&mut self, id: &[u8]) -> Result<&mut Contact> { - self.profile - .contacts - .iter_mut() - .find(|c| c.invitation_id.as_slice() == id) + fn by_invitation(&self, id: &[u8]) -> Result<(usize, usize)> { + self.find(|c| c.invitation_id.as_slice() == id) .ok_or_else(|| Error::Invalid("no pending invitation matches".into())) } /// Step 3 (file flow): process an answer, returns the encoded confirmation. pub async fn accept_answer(&mut self, answer_bytes: &[u8]) -> Result> { let msg = v2::InvitationMessage::decode(answer_bytes).map_err(meow_core::Error::from)?; - let contact = self.by_invitation(&msg.invitation_id)?; - let confirm = invitation::accept(contact, &msg, now())?; - self.save().await?; + let (si, ci) = self.by_invitation(&msg.invitation_id)?; + let confirm = invitation::accept(&mut self.spaces[si].state.contacts[ci], &msg, now())?; + self.save_space(si)?; Ok(confirm.encode_to_vec()) } /// Step 4 (file flow): process a confirmation and send the ack through the network. pub async fn finalize_invitation(&mut self, confirm_bytes: &[u8]) -> Result { let msg = v2::InvitationMessage::decode(confirm_bytes).map_err(meow_core::Error::from)?; - let contact = self.by_invitation(&msg.invitation_id)?; + let (si, ci) = self.by_invitation(&msg.invitation_id)?; + let contact = &mut self.spaces[si].state.contacts[ci]; let ack = invitation::finalize(contact, &msg)?; let deposit = messaging::deposit_for(contact, &ack)?; - let servers = contact - .peer - .as_ref() - .map(|p| p.pull_servers.clone()) - .unwrap_or_default(); + let servers = peer_servers(contact); let name = contact.name.clone(); - self.save().await?; + self.save_space(si)?; self.deliver(&servers, vec![deposit]).await?; Ok(name) } @@ -473,16 +613,13 @@ impl MeowClient { } async fn send(&mut self, contact_name: &str, msg: v2::UserMessage, content: Content) -> Result { - let i = self.contact_index(contact_name)?; - let contact = &mut self.profile.contacts[i]; + let (si, ci) = self.locate(contact_name)?; + let sp = &mut self.spaces[si]; + let contact = &mut sp.state.contacts[ci]; let env = messaging::encrypt(contact, &msg)?; let deposit = messaging::deposit_for(contact, &env)?; - let servers = contact - .peer - .as_ref() - .map(|p| p.pull_servers.clone()) - .unwrap_or_default(); - self.profile.history.push(HistoryEntry { + let servers = peer_servers(contact); + sp.batch.history.push(HistoryEntry { id: msg.id.clone(), contact: contact.id.clone(), outbound: true, @@ -490,7 +627,7 @@ impl MeowClient { received_at: now(), content, }); - self.save().await?; + self.save_space(si)?; let delivered_to = self.deliver(&servers, vec![deposit]).await?; Ok(SendReport { message_id: msg.id, @@ -532,9 +669,10 @@ impl MeowClient { // ───────────────────────── sync ───────────────────────── - /// Pull every mailbox on every server (one request per server), process what - /// arrived and return the resulting events. With `wait > 0` the servers hold the - /// request until something arrives or the delay expires (long-poll). + /// Pull every mailbox of every unlocked space on every server (one request per + /// server), process what arrived and return the resulting events. With `wait > 0` + /// the servers hold the request until something arrives or the delay expires + /// (long-poll). /// /// Delivery is at-least-once: servers keep messages until they are acknowledged, /// and a message is acknowledged (in the next pull) only once its effects are saved. @@ -569,43 +707,49 @@ impl MeowClient { } /// One pull per server, carrying the pending acks. New (not yet processed) messages - /// are returned and recorded as pending acks; they are saved by [`Self::process`]. + /// are returned and recorded as pending acks of the space owning their mailbox; + /// they are saved by [`Self::process`]. async fn pull_round(&mut self, wait: Duration) -> Result { - // server (by signing key) → (server, mailbox keys) - let pending = &self.profile.pending_acks; - self.profile.retired_mailboxes.retain(|r| { - pending + // server (by signing key) → (server, [(space index, mailbox key)]) + let mut jobs: BTreeMap)> = BTreeMap::new(); + for si in 0..self.spaces.len() { + let st = &mut self.spaces[si].state; + let pending = &st.pending_acks; + st.retired_mailboxes.retain(|r| { + pending + .iter() + .any(|a| a.mailbox.as_slice() == r.key.public().as_bytes().as_slice()) + }); + let retired: Mailboxes = self.spaces[si] + .state + .retired_mailboxes .iter() - .any(|a| a.mailbox.as_slice() == r.key.public().as_bytes().as_slice()) - }); - let mut jobs: BTreeMap)> = BTreeMap::new(); - let retired = self - .profile - .retired_mailboxes - .iter() - .map(|r| (r.key.clone(), r.servers.clone())); - for (kp, servers) in self.contact_mailboxes().into_iter().chain(retired) { - for s in servers { - jobs.entry(s.sign_key) - .or_insert_with(|| (s.clone(), Vec::new())) - .1 - .push(kp.clone()); + .map(|r| (r.key.clone(), r.servers.clone())) + .collect(); + for (kp, servers) in self.space_mailboxes(si).into_iter().chain(retired) { + for s in servers { + jobs.entry(s.sign_key) + .or_insert_with(|| (s.clone(), Vec::new())) + .1 + .push((si, kp.clone())); + } } } // Drop acks nobody can send any more (contact or server removed). - self.profile.pending_acks.retain(|a| { - jobs.get(&a.server).is_some_and(|(_, kps)| { - kps.iter() - .any(|kp| kp.public().as_bytes().as_slice() == a.mailbox.as_slice()) - }) - }); + for (si, sp) in self.spaces.iter_mut().enumerate() { + sp.state.pending_acks.retain(|a| { + jobs.get(&a.server).is_some_and(|(_, kps)| { + kps.iter() + .any(|(s, kp)| *s == si && kp.public().as_bytes().as_slice() == a.mailbox.as_slice()) + }) + }); + } let mut round = PullRound::default(); if jobs.is_empty() { return Ok(round); } let wait_s = wait.as_secs().min(u32::MAX as u64) as u32; - let pending = &self.profile.pending_acks; - let mut sent_acks: Vec> = Vec::with_capacity(jobs.len()); + let mut sent_acks: Vec> = Vec::with_capacity(jobs.len()); let mut requests = Vec::with_capacity(jobs.len()); for (server, kps) in jobs.values() { let rid = meow_crypto::random_bytes::<16>().to_vec(); @@ -613,13 +757,15 @@ impl MeowClient { let mut sent = Vec::new(); let auths = kps .iter() - .map(|kp| { + .map(|(si, kp)| { let mb = kp.public(); - let acks: Vec> = pending + let acks: Vec> = self.spaces[*si] + .state + .pending_acks .iter() .filter(|a| a.server == server.sign_key && a.mailbox.as_slice() == mb.as_bytes().as_slice()) .take(MAX_ACKS_PER_MAILBOX) - .inspect(|a| sent.push((*a).clone())) + .inspect(|a| sent.push((*si, (*a).clone()))) .map(|a| a.id.clone()) .collect(); rpc::mailbox_auth(kp, server, &rid, acks, ts) @@ -643,23 +789,34 @@ impl MeowClient { let mut failures = 0; let mut last_err = None; - for (((server, _), r), sent) in jobs.values().zip(results).zip(sent_acks) { + for (((server, kps), r), sent) in jobs.values().zip(results).zip(sent_acks) { match r { Ok(Resp::PullResult(p)) => { // The server applied our acks before answering. Forgetting them // needs no save: at worst they are sent again (acks are idempotent). - self.profile.pending_acks.retain(|a| !sent.contains(a)); + for (si, a) in &sent { + self.spaces[*si].state.pending_acks.retain(|x| x != a); + } round.more |= p.more; for m in p.messages { + let Some((si, _)) = kps + .iter() + .find(|(_, kp)| kp.public().as_bytes().as_slice() == m.mailbox.as_slice()) + else { + tracing::debug!("server sent a message for a mailbox we did not pull"); + continue; + }; let ack = PendingAck { server: server.sign_key, mailbox: m.mailbox.clone(), id: m.id.clone(), }; + let sp = &mut self.spaces[*si]; // Already processed (its ack was lost): only re-acknowledge. - if !self.profile.pending_acks.contains(&ack) { - self.profile.pending_acks.push(ack); - round.new.push(m); + if !sp.state.pending_acks.contains(&ack) { + sp.state.pending_acks.push(ack); + sp.dirty = true; + round.new.push((*si, m)); } } } @@ -677,9 +834,10 @@ impl MeowClient { Ok(round) } - /// Every mailbox currently used by a contact, with its pull servers. - fn contact_mailboxes(&self) -> Vec<(SigningKeyPair, Vec)> { - self.profile + /// Every mailbox currently used by a contact of a space, with its pull servers. + fn space_mailboxes(&self, si: usize) -> Mailboxes { + self.spaces[si] + .state .contacts .iter() .flat_map(|c| c.mailboxes()) @@ -687,62 +845,58 @@ impl MeowClient { .collect() } - /// Keep the keys of mailboxes that disappeared (`before` → now) while they still - /// have pending acks, so the next pull can acknowledge their last messages. - fn retire_mailboxes(&mut self, before: Vec<(SigningKeyPair, Vec)>) { - let current: Vec = self.contact_mailboxes().iter().map(|(kp, _)| kp.public()).collect(); + /// Keep the keys of mailboxes that disappeared from a space (`before` → now) while + /// they still have pending acks, so the next pull can acknowledge their last messages. + fn retire_mailboxes(&mut self, si: usize, before: Mailboxes) { + let current: Vec = self.space_mailboxes(si).iter().map(|(kp, _)| kp.public()).collect(); + let st = &mut self.spaces[si].state; for (key, servers) in before { let pk = key.public(); - let has_acks = self - .profile + let has_acks = st .pending_acks .iter() .any(|a| a.mailbox.as_slice() == pk.as_bytes().as_slice()); - let known = self.profile.retired_mailboxes.iter().any(|r| r.key.public() == pk); + let known = st.retired_mailboxes.iter().any(|r| r.key.public() == pk); if has_acks && !known && !current.contains(&pk) { - self.profile.retired_mailboxes.push(RetiredMailbox { key, servers }); + st.retired_mailboxes.push(RetiredMailbox { key, servers }); } } } - async fn process(&mut self, stored: Vec) -> Result> { - let mailboxes_before = self.contact_mailboxes(); + async fn process(&mut self, stored: Vec<(usize, v2::StoredMessage)>) -> Result> { + let mailboxes_before: Vec = (0..self.spaces.len()).map(|si| self.space_mailboxes(si)).collect(); // Handshake messages first (step 2 before step 3), then ratchet messages by arrival. - let mut envs: Vec<(u32, u64, Vec, v2::PeerEnvelope)> = stored + let mut envs: Vec<(u32, u64, usize, Vec, v2::PeerEnvelope)> = stored .into_iter() - .filter_map(|m| { + .filter_map(|(si, m)| { let env = v2::PeerEnvelope::decode(m.payload.as_slice()).ok()?; let rank = match &env.body { Some(v2::peer_envelope::Body::Invitation(i)) => i.step, _ => 10, }; - Some((rank, m.received_at, m.mailbox, env)) + Some((rank, m.received_at, si, m.mailbox, env)) }) .collect(); - envs.sort_by_key(|(rank, at, _, _)| (*rank, *at)); + envs.sort_by_key(|(rank, at, _, _, _)| (*rank, *at)); let mut events = Vec::new(); let mut outgoing: Vec<(Vec, v2::Deposit)> = Vec::new(); - for (_, received_at, mailbox, env) in envs { - let Some(i) = self.profile.contacts.iter().position(|c| { + for (_, received_at, si, mailbox, env) in envs { + let sp = &mut self.spaces[si]; + let Some(ci) = sp.state.contacts.iter().position(|c| { c.mailboxes() .iter() .any(|(kp, _)| kp.public().as_bytes().as_slice() == mailbox.as_slice()) }) else { - tracing::debug!("message for an unknown mailbox"); + tracing::debug!("message for a retired mailbox"); continue; }; - let contact = &mut self.profile.contacts[i]; + let contact = &mut sp.state.contacts[ci]; match env.body { Some(v2::peer_envelope::Body::Invitation(inv)) if inv.step == 2 => { match invitation::accept(contact, &inv, now()) { Ok(confirm) => { - let servers = contact - .peer - .as_ref() - .map(|p| p.pull_servers.clone()) - .unwrap_or_default(); - outgoing.push((servers, invitation::confirm_deposit(contact, &confirm)?)); + outgoing.push((peer_servers(contact), invitation::confirm_deposit(contact, &confirm)?)); events.push(Event::InvitationAccepted { contact: contact.name.clone(), }); @@ -753,12 +907,7 @@ impl MeowClient { Some(v2::peer_envelope::Body::Invitation(inv)) if inv.step == 3 => { match invitation::finalize(contact, &inv) { Ok(ack) => { - let servers = contact - .peer - .as_ref() - .map(|p| p.pull_servers.clone()) - .unwrap_or_default(); - outgoing.push((servers, messaging::deposit_for(contact, &ack)?)); + outgoing.push((peer_servers(contact), messaging::deposit_for(contact, &ack)?)); events.push(Event::ContactActivated { contact: contact.name.clone(), }); @@ -781,19 +930,17 @@ impl MeowClient { if was != ContactState::Active && contact.state == ContactState::Active { events.push(Event::ContactActivated { contact: name.clone() }); } - let mut record = |content| { - self.profile.history.push(HistoryEntry { - id: msg.id.clone(), - contact: cid.clone(), - outbound: false, - sent_at_ms: msg.sent_at_ms, - received_at, - content, - }) + let entry = |content| HistoryEntry { + id: msg.id.clone(), + contact: cid.clone(), + outbound: false, + sent_at_ms: msg.sent_at_ms, + received_at, + content, }; match &msg.body { Some(v2::user_message::Body::Text(t)) => { - record(Content::Text(t.text.clone())); + sp.batch.history.push(entry(Content::Text(t.text.clone()))); events.push(Event::Message { contact: name, id: msg.id.clone(), @@ -802,12 +949,14 @@ impl MeowClient { }); } Some(v2::user_message::Body::File(f)) => { - record(Content::File { + sp.batch.history.push(entry(Content::File { name: f.filename.clone(), size: f.size, - }); + })); + sp.batch.files.push((cid.clone(), msg.id.clone(), f.data.clone())); events.push(Event::File { contact: name, + id: msg.id.clone(), name: f.filename.clone(), data: f.data.clone(), }); @@ -817,8 +966,13 @@ impl MeowClient { } _ => tracing::debug!("ignoring unsupported envelope"), } + sp.dirty = true; + } + for (si, before) in mailboxes_before.into_iter().enumerate() { + if self.spaces[si].dirty { + self.retire_mailboxes(si, before); + } } - self.retire_mailboxes(mailboxes_before); // Persist before anything leaves the device. self.save().await?; for (servers, deposit) in outgoing { @@ -829,3 +983,11 @@ impl MeowClient { Ok(events) } } + +fn peer_servers(contact: &Contact) -> Vec { + contact + .peer + .as_ref() + .map(|p| p.pull_servers.clone()) + .unwrap_or_default() +} diff --git a/crates/meow-client/src/profile.rs b/crates/meow-client/src/profile.rs deleted file mode 100644 index 70880f0..0000000 --- a/crates/meow-client/src/profile.rs +++ /dev/null @@ -1,98 +0,0 @@ -//! Provisional persistence: one encrypted file per identity (`*.mwid2`). -//! -//! Replaced by the SQLite store of `meow-store` in roadmap phase 4. The whole -//! profile is serialised with postcard and encrypted with Argon2id + -//! XChaCha20-Poly1305 ([`meow_crypto::password`]). - -use std::path::Path; - -use meow_core::contact::{Contact, ContactId}; -use meow_core::server::ServerInfo; -use meow_crypto::password::{self, KdfParams}; -use meow_crypto::{SignPublic, SigningKeyPair}; -use serde::{Deserialize, Serialize}; - -use crate::{Error, Result}; - -const FORMAT_VERSION: u32 = 2; - -/// One entry of a conversation history. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub struct HistoryEntry { - pub id: Vec, - pub contact: ContactId, - pub outbound: bool, - pub sent_at_ms: u64, - pub received_at: u64, - pub content: Content, -} - -/// Displayable content of a message. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub enum Content { - Text(String), - File { name: String, size: u64 }, -} - -/// A message received from a server and processed (its effects are in this profile), -/// whose deletion the server has not confirmed yet. Sent with the next pull to that -/// server; also used to ignore a redelivery of the same message. -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub struct PendingAck { - pub server: SignPublic, - pub mailbox: Vec, - pub id: Vec, -} - -/// A mailbox no longer in use (temporary invitation mailbox, removed contact) that -/// still has pending acks: its key is kept just long enough to send them. -#[derive(Clone, Serialize, Deserialize)] -pub struct RetiredMailbox { - pub key: SigningKeyPair, - pub servers: Vec, -} - -#[derive(Clone, Serialize, Deserialize)] -pub struct Profile { - pub format: u32, - pub nickname: String, - pub servers: Vec, - pub contacts: Vec, - pub history: Vec, - pub pending_acks: Vec, - pub retired_mailboxes: Vec, -} - -impl Profile { - pub fn new(nickname: &str) -> Self { - Self { - format: FORMAT_VERSION, - nickname: nickname.to_string(), - servers: vec![], - contacts: vec![], - history: vec![], - pending_acks: vec![], - retired_mailboxes: vec![], - } - } - - pub async fn load(path: &Path, password: &str) -> Result { - let blob = tokio::fs::read(path).await?; - let plain = password::decrypt(password.as_bytes(), &blob).map_err(|_| Error::BadPassword)?; - let p: Profile = postcard::from_bytes(&plain).map_err(|e| Error::Corrupted(e.to_string()))?; - if p.format != FORMAT_VERSION { - return Err(Error::Corrupted(format!("unsupported profile format {}", p.format))); - } - Ok(p) - } - - /// Atomic save (write to a temporary file then rename). - pub async fn save(&self, path: &Path, password: &str, kdf: KdfParams) -> Result<()> { - let plain = postcard::to_allocvec(self).map_err(|e| Error::Corrupted(e.to_string()))?; - let blob = password::encrypt(password.as_bytes(), &plain, kdf)?; - let tmp = path.with_extension("tmp"); - tokio::fs::write(&tmp, blob).await?; - tokio::fs::rename(&tmp, path).await?; - Ok(()) - } -} diff --git a/crates/meow-client/tests/flow.rs b/crates/meow-client/tests/flow.rs index 07e18b7..3a67e23 100644 --- a/crates/meow-client/tests/flow.rs +++ b/crates/meow-client/tests/flow.rs @@ -107,11 +107,23 @@ async fn file_and_server_invitations_then_messaging() { std::fs::write(&f, b"file content").unwrap(); bob.send_file("alice", &f).await.unwrap(); let ev = alice.sync(Duration::ZERO).await.unwrap(); - assert!(ev.contains(&Event::File { - contact: "bob".into(), - name: "note.txt".into(), - data: b"file content".to_vec() - })); + let file_id = ev + .iter() + .find_map(|e| match e { + Event::File { + contact, + id, + name, + data, + } if contact == "bob" && name == "note.txt" => { + assert_eq!(data, b"file content"); + Some(id.clone()) + } + _ => None, + }) + .expect("file event"); + // The content is kept in the store. + assert_eq!(alice.file("bob", &file_id).unwrap().unwrap(), b"file content"); // ── server invitation ── let url = bob @@ -148,7 +160,7 @@ async fn file_and_server_invitations_then_messaging() { let path = dir.join("alice.mwid2"); drop(alice); let waiter = tokio::spawn(async move { - let mut alice = MeowClient::open(&path, "pw", KdfParams::INSECURE_TEST).await.unwrap(); + let mut alice = MeowClient::open(&path, "pw").await.unwrap(); let start = std::time::Instant::now(); let ev = alice.sync(Duration::from_secs(20)).await.unwrap(); (texts(&ev), start.elapsed()) @@ -161,20 +173,14 @@ async fn file_and_server_invitations_then_messaging() { // ── one process at a time per identity ── assert!(matches!( - MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST).await, + MeowClient::open(&dir.join("bob.mwid2"), "pw").await, Err(meow_client::Error::Locked(_)) )); drop(bob); // ── persistence & wrong password ── - assert!( - MeowClient::open(&dir.join("bob.mwid2"), "nope", KdfParams::INSECURE_TEST) - .await - .is_err() - ); - let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST) - .await - .unwrap(); + assert!(MeowClient::open(&dir.join("bob.mwid2"), "nope").await.is_err()); + let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw").await.unwrap(); assert_eq!(bob2.contacts().len(), 2); assert_eq!(bob2.history("alice").unwrap().len(), 6); // 5 sent + "hi bob" @@ -306,7 +312,7 @@ async fn no_message_lost_when_the_client_dies() { assert_eq!(raw_pull(&server, &mailbox).await.len(), 1); let path = dir.join("alice.mwid2"); drop(alice); - let mut alice = MeowClient::open(&path, "pw", KdfParams::INSECURE_TEST).await.unwrap(); + let mut alice = MeowClient::open(&path, "pw").await.unwrap(); assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty()); store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst); assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty()); @@ -337,3 +343,75 @@ async fn no_message_lost_when_the_client_dies() { let _ = std::fs::remove_dir_all(&dir); } + +// ───────────── hidden spaces (roadmap lot 4.5) ───────────── + +/// File invitation from `a` to `b`, completed. +async fn connect(a: &mut MeowClient, b: &mut MeowClient, b_name: &str) { + let offer = a.create_invitation(b_name, &[], "", 3600).await.unwrap(); + let answer = b.answer_invitation(&offer, None, &[]).await.unwrap(); + let confirm = a.accept_answer(&answer).await.unwrap(); + b.finalize_invitation(&confirm).await.unwrap(); + a.sync(Duration::ZERO).await.unwrap(); +} + +fn names(c: &MeowClient) -> Vec { + c.contacts().iter().map(|k| k.name.clone()).collect() +} + +#[tokio::test(flavor = "multi_thread")] +async fn hidden_contacts() { + let servers = vec![spawn_server().await]; + let dir = tmpdir(); + let path = dir.join("alice.mwid2"); + let mut alice = client(&dir, "alice", &servers).await; + let mut bob = client(&dir, "bob", &servers).await; + let mut carol = client(&dir, "carol", &servers).await; + connect(&mut alice, &mut bob, "bob").await; + connect(&mut alice, &mut carol, "carol").await; + bob.send_text("alice", "before hiding").await.unwrap(); + assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["before hiding"]); + + // Hide bob, with his history. + alice.hide_contact("bob", "bob-pw").await.unwrap(); + assert!(alice.is_hidden("bob").unwrap()); + assert!(!alice.is_hidden("carol").unwrap()); + assert_eq!(alice.history("bob").unwrap().len(), 1); + drop(alice); + + // Without the hidden password, bob does not exist and is not even polled. + let mut alice = MeowClient::open(&path, "pw").await.unwrap(); + assert_eq!(names(&alice), vec!["carol"]); + assert!(alice.history("bob").is_err()); + bob.send_text("alice", "while locked").await.unwrap(); + carol.send_text("alice", "hi").await.unwrap(); + assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["hi"]); + + // With it, bob is back and his messages were waiting on the server. + assert!(!alice.unlock("wrong").await.unwrap()); + assert!(alice.unlock("bob-pw").await.unwrap()); + assert_eq!(names(&alice), vec!["carol", "bob"]); + assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["while locked"]); + alice.send_text("bob", "from the hidden side").await.unwrap(); + assert_eq!( + texts(&bob.sync(Duration::ZERO).await.unwrap()), + vec!["from the hidden side"] + ); + assert_eq!(alice.history("bob").unwrap().len(), 3); + + // Several contacts can share a hidden space. + alice.hide_contact("carol", "bob-pw").await.unwrap(); + drop(alice); + let mut alice = MeowClient::open(&path, "pw").await.unwrap(); + assert!(names(&alice).is_empty()); + assert!(alice.unlock("bob-pw").await.unwrap()); + assert_eq!(names(&alice), vec!["bob", "carol"]); + + // Back to the main space. + alice.reveal_contact("bob").await.unwrap(); + drop(alice); + let alice = MeowClient::open(&path, "pw").await.unwrap(); + assert_eq!(names(&alice), vec!["bob"]); + assert_eq!(alice.history("bob").unwrap().len(), 3); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/tests/e2e.sh b/tests/e2e.sh index 0719611..b70ba7a 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -160,6 +160,21 @@ meow alice send bob "ab2?" >/dev/null expect "alice" "[bob] ba2?" meow alice longpoll --wait 5 --exit expect "bob" "[alice] ab2?" meow bob longpoll --wait 5 --exit +# ── hidden contact ── +step "HIDDEN CONTACT" +expect "hide" "remote is now hidden" meow bob contact hide remote --space-password hide-me +OUT="$(meow bob contact list)"; printf '%s\n' "$OUT" | sed 's/^/ /' +if grep -q '^remote' <<<"$OUT"; then red "hidden contact listed without its password"; exit 1; fi +expect "listed with its password" "[hidden]" meow bob -u hide-me contact list +expect "a wrong space password is silent" "alice" meow bob -u not-it contact list +meow remote send bob "to the hidden side" >/dev/null +OUT="$(meow bob)" +if grep -qF "to the hidden side" <<<"$OUT"; then red "hidden contact polled without its password"; exit 1; fi +expect "received once unlocked" "[remote] to the hidden side" meow bob -u hide-me +expect "reply from the hidden side" "sent to remote" meow bob -u hide-me send remote "hidden reply" +expect "remote gets it" "[bob] hidden reply" meow remote +expect "history kept" "[remote] hello from remote" meow bob -u hide-me history remote + # ── resilience: one server down ── step "ONE SERVER DOWN" kill "${PIDS[1]}"; wait "${PIDS[1]}" 2>/dev/null || true; PIDS[1]="" @@ -169,4 +184,10 @@ expect "receive with one server down" "[bob] still here" meow alice 2>/dev/null step "HISTORY" expect "history" "[alice] hi bob" meow bob history alice +step "PASSWORD CHANGE" +expect "change" "password changed" meow alice identity password --new-password new-pass +expect "old password refused" "cannot open identity" \ + bash -c "'$MEOW_BIN' -i '$WORK/alice.mwid2' identity show 2>&1 || true" +expect "new password accepted" "nickname: alice" meow alice -p new-pass identity show + green "ALL E2E TESTS PASSED"