From 20d247bae690eed12e4edbfbe19c611375314da1 Mon Sep 17 00:00:00 2001 From: "Jannis M. Hoffmann" Date: Sun, 17 Nov 2024 13:49:11 +0100 Subject: add "add_folder" command --- Cargo.lock | 2 +- Cargo.toml | 2 +- jwebmail.proto | 9 +++++++++ src/arguments.rs | 1 + src/cmd.rs | 2 ++ src/cmd/add_folder.rs | 37 +++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- 7 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/cmd/add_folder.rs diff --git a/Cargo.lock b/Cargo.lock index a1f201c..4592685 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,7 +335,7 @@ dependencies = [ [[package]] name = "jwebmail-extract" -version = "0.7.0" +version = "0.8.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index cc2b1a1..91de65b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jwebmail-extract" -version = "0.7.0" +version = "0.8.0" authors = ["Jannis M. Hoffmann "] edition = "2021" rust-version = "1.80" diff --git a/jwebmail.proto b/jwebmail.proto index bf1454a..e4cba3b 100644 --- a/jwebmail.proto +++ b/jwebmail.proto @@ -144,6 +144,14 @@ message RemoveReq { message RemoveResp { } +message AddFolderReq { + string name = 1; +} + +message AddFolderResp { + int32 status = 1; +} + service MailService { rpc List(ListReq) returns (ListResp); rpc Stats(StatsReq) returns (StatsResp); @@ -153,4 +161,5 @@ service MailService { rpc Folders(FoldersReq) returns (FoldersResp); rpc Move(MoveReq) returns (MoveResp); rpc Remove(RemoveReq) returns (RemoveResp); + rpc AddFolder(AddFolderReq) returns (AddFolderResp); } diff --git a/src/arguments.rs b/src/arguments.rs index c017744..a944abe 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -12,6 +12,7 @@ pub enum Mode { Folders, Move, Remove, + AddFolder, } #[derive(Parser)] diff --git a/src/cmd.rs b/src/cmd.rs index 308cc5f..201f498 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; use maildir::Maildir; +mod add_folder; mod count; mod folders; mod list; @@ -10,6 +11,7 @@ mod raw; mod read; mod remove; +pub use add_folder::add_folder; pub use count::count; pub use folders::folders; pub use list::list; diff --git a/src/cmd/add_folder.rs b/src/cmd/add_folder.rs new file mode 100644 index 0000000..1a77c63 --- /dev/null +++ b/src/cmd/add_folder.rs @@ -0,0 +1,37 @@ +use std::fs::create_dir; +use std::path::PathBuf; + +use protobuf::Message as _; + +use crate::error::Result; +use crate::pb3::jwebmail::{AddFolderReq, AddFolderResp}; + +pub fn add_folder(mut p: PathBuf, req: &[u8]) -> Result> { + let r = AddFolderReq::parse_from_bytes(req)?; + let mut resp = AddFolderResp::new(); + + let mut folder = ".".to_owned(); + folder.push_str(&r.name); + p.push(folder); + + if p.is_dir() { + resp.status = 1; + return resp.write_to_bytes().map_err(|e| e.into()); + } + + create_dir(p.clone())?; + + p.push("tmp"); + create_dir(p.clone())?; + + p.pop(); + p.push("new"); + create_dir(p.clone())?; + + p.pop(); + p.push("cur"); + create_dir(p)?; + + resp.status = 0; + resp.write_to_bytes().map_err(|e| e.into()) +} diff --git a/src/main.rs b/src/main.rs index f9ba899..5598b30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,8 +69,9 @@ fn main() -> Result<()> { //Mode::Search => cmd::search(&path, &req), Mode::Move => cmd::move_mail(path, &req), Mode::Remove => cmd::remove(path, &req), + Mode::AddFolder => cmd::add_folder(path, &req), _ => todo!(), }?; - stdout().write(&res).map(|_| ()).map_err(|e| e.into()) + stdout().write_all(&res).map_err(|e| e.into()) } -- cgit v1.2.3