From 3ec1ecb34989cc942192fd6ea471a1f0bbd9dd71 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 9 Oct 2020 11:56:50 +0300 Subject: [PATCH] Add import mail action --- CHANGELOG.md | 3 +++ docs/meli.1 | 2 ++ src/command.rs | 16 ++++++++++++++++ src/command/actions.rs | 2 ++ src/components/mail/listing.rs | 22 ++++++++++++++++++++++ 5 files changed, 45 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a5771a2..441a39e19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Add import command to import email from files into accounts + ## [alpha-0.6.2] - 2020-09-24 ### Added diff --git a/docs/meli.1 b/docs/meli.1 index 18ab8b6ff..4eefb6956 100644 --- a/docs/meli.1 +++ b/docs/meli.1 @@ -408,6 +408,8 @@ select threads matching query. .It Cm set seen, set unseen Set seen status of message. +.It Cm import Ar FILEPATH Ar MAILBOX_PATH +Import mail from file into given mailbox. .It Cm copyto, moveto Ar MAILBOX_PATH Copy or move to other mailbox. .It Cm copyto, moveto Ar ACCOUNT Ar MAILBOX_PATH diff --git a/src/command.rs b/src/command.rs index 967be7723..370ff8332 100644 --- a/src/command.rs +++ b/src/command.rs @@ -303,6 +303,21 @@ define_commands!([ } ) }, + { tags: ["import "], + desc: "import FILESYSTEM_PATH MAILBOX_PATH", + tokens: &[One(Literal("import")), One(Filepath), One(MailboxPath)], + parser:( + fn import(input: &[u8]) -> IResult<&[u8], Action> { + let (input, _) = tag("import")(input.trim())?; + let (input, _) = is_a(" ")(input)?; + let (input, file) = quoted_argument(input)?; + let (input, _) = is_a(" ")(input)?; + let (input, mailbox_path) = quoted_argument(input)?; + let (input, _) = eof(input)?; + Ok((input, Listing(Import(file.to_string().into(), mailbox_path.to_string())))) + } + ) + }, { tags: ["close"], desc: "close non-sticky tabs", tokens: &[One(Literal("close"))], @@ -769,6 +784,7 @@ fn listing_action(input: &[u8]) -> IResult<&[u8], Action> { seen_flag, delete_message, copymove, + import, search, select, toggle_thread_snooze, diff --git a/src/command/actions.rs b/src/command/actions.rs index fd6b98910..d41445999 100644 --- a/src/command/actions.rs +++ b/src/command/actions.rs @@ -25,6 +25,7 @@ use crate::components::Component; pub use melib::thread::{SortField, SortOrder}; +use std::path::PathBuf; extern crate uuid; use uuid::Uuid; @@ -49,6 +50,7 @@ pub enum ListingAction { CopyToOtherAccount(AccountName, MailboxPath), MoveTo(MailboxPath), MoveToOtherAccount(AccountName, MailboxPath), + Import(PathBuf, MailboxPath), Delete, OpenInNewTab, Tag(TagAction), diff --git a/src/components/mail/listing.rs b/src/components/mail/listing.rs index 7baa2cda3..e347e86b8 100644 --- a/src/components/mail/listing.rs +++ b/src/components/mail/listing.rs @@ -750,6 +750,28 @@ impl Component for Listing { self.component.set_style(IndexStyle::Conversations); return true; } + Action::Listing(ListingAction::Import(file_path, mailbox_path)) => { + let account = &mut context.accounts[self.cursor_pos.0]; + if let Err(err) = account + .mailbox_by_path(&mailbox_path) + .and_then(|mailbox_hash| { + Ok(( + std::fs::read(&file_path).chain_err_summary(|| { + format!("Could not read {}", file_path.display()) + })?, + mailbox_hash, + )) + }) + .and_then(|(bytes, mailbox_hash)| { + account.save(&bytes, mailbox_hash, None) + }) + { + context.replies.push_back(UIEvent::StatusEvent( + StatusEvent::DisplayMessage(err.to_string()), + )); + } + return true; + } Action::Listing(a @ ListingAction::SetSeen) | Action::Listing(a @ ListingAction::SetUnseen) | Action::Listing(a @ ListingAction::Delete)