Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to export as maildir #600

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 50 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ readme = "README.md"
resolver = "2"

[dependencies]
jmap-client = { version = "0.3", features = ["async"] }
mail-parser = { version = "0.9", features = ["full_encoding", "serde_support", "ludicrous_mode"] }
jmap-client = { version = "0.3", features = ["async"] }
mail-parser = { version = "0.9", features = ["full_encoding", "serde_support", "ludicrous_mode"] }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls-webpki-roots", "http2"]}
tokio = { version = "1.23", features = ["full"] }
num_cpus = "1.13.1"
Expand All @@ -30,3 +30,8 @@ futures = "0.3.28"
pwhash = "1.0.0"
rand = "0.8.5"
mail-auth = { version = "0.4" }
maildir = { version = "0.6.4", default-features = false, optional = true }

[features]
default = ["maildir"]
maildir = ["dep:maildir"]
11 changes: 11 additions & 0 deletions crates/cli/src/modules/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ pub enum ImportCommands {
},
}

#[derive(clap::ValueEnum, Clone)]
pub enum ExportAccountFormat {
Blobs,
#[cfg(feature = "maildir")]
Maildir,
}

#[derive(Subcommand)]
pub enum ExportCommands {
/// Export a JMAP account
Expand All @@ -384,6 +391,10 @@ pub enum ExportCommands {

/// Path to export the account to
path: String,

/// The format to export the emails
#[clap(value_enum, long, default_value = "blobs")]
format: ExportAccountFormat,
},
}

Expand Down
61 changes: 59 additions & 2 deletions crates/cli/src/modules/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use jmap_client::{
use serde::Serialize;
use tokio::io::AsyncWriteExt;

use crate::modules::RETRY_ATTEMPTS;
use crate::modules::{cli::ExportAccountFormat, RETRY_ATTEMPTS};

use super::{
cli::{Client, ExportCommands},
Expand All @@ -35,6 +35,7 @@ impl ExportCommands {
num_concurrent,
account,
path,
format,
} => {
client.set_default_account_id(name_to_id(&client, &account).await);
let max_objects_in_get = client
Expand All @@ -60,7 +61,15 @@ impl ExportCommands {
// Export metadata
let mut blobs = Vec::new();
export_mailboxes(&client, max_objects_in_get, &path).await;
export_emails(&client, max_objects_in_get, &mut blobs, &path).await;
match format {
ExportAccountFormat::Blobs => {
export_emails(&client, max_objects_in_get, &mut blobs, &path).await
}
#[cfg(feature = "maildir")]
ExportAccountFormat::Maildir => {
export_emails_maildir(&client, max_objects_in_get, &path).await
}
}
export_sieve_scripts(&client, max_objects_in_get, &mut blobs, &path).await;
export_identities(&client, &path).await;
export_vacation_responses(&client, &path).await;
Expand Down Expand Up @@ -262,6 +271,54 @@ pub async fn fetch_emails(
results
}

#[cfg(feature = "maildir")]
async fn export_emails_maildir(
client: &jmap_client::client::Client,
max_objects_in_get: usize,
path: &Path,
) {
use maildir::Maildir;

let emails = fetch_emails(client, max_objects_in_get).await;
williamdes marked this conversation as resolved.
Show resolved Hide resolved

let maildir = Maildir::from(path.join("maildir"));
if let Err(err) = maildir.create_dirs() {
eprintln!("Error: dirs count not be created: {err}");
return;
}

for email in &emails {
if let Some(blob_id) = email.blob_id() {
match client.download(&blob_id).await {
Ok(bytes) => {
if let Err(err) = maildir.store_new(bytes.as_slice()) {
eprintln!(
"Error: email {:?} could not be stored: {err}",
email.id().unwrap_or_default()
);
}
}
Err(err) => {
eprintln!(
"Error: email {:?} could not be downloaded: {err}",
email.id().unwrap_or_default()
);
}
};
} else {
eprintln!(
"Warning: email {:?} has no blobId",
email.id().unwrap_or_default()
);
}
}

eprintln!(
"Exported {} emails.",
write_file(path, "emails.json", emails,).await
);
}

async fn export_emails(
client: &jmap_client::client::Client,
max_objects_in_get: usize,
Expand Down