summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-03-29 19:37:02 +0200
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-06-10 19:40:41 +0300
commitd19bda89776346b72c8e5dd36880a94f58299cec (patch)
treec9410971f4b29bbcc9031ca5b539635d81fa3838
parent1aa4eaa3142661ceb5df871fd0361abf289682a0 (diff)
melib: properly save drafts in maildir backend
-rw-r--r--melib/src/error.rs7
-rw-r--r--melib/src/mailbox/backends.rs2
-rw-r--r--melib/src/mailbox/backends/maildir/backend.rs24
-rw-r--r--melib/src/mailbox/email/compose.rs1
-rw-r--r--ui/src/components/mail/compose.rs14
-rw-r--r--ui/src/conf/accounts.rs3
6 files changed, 35 insertions, 16 deletions
diff --git a/melib/src/error.rs b/melib/src/error.rs
index c8962ac0..12216b24 100644
--- a/melib/src/error.rs
+++ b/melib/src/error.rs
@@ -57,6 +57,13 @@ impl fmt::Display for MeliError {
}
}
+impl Into<String> for MeliError {
+ fn into(self) -> String {
+ format!("{}", self)
+ }
+}
+
+
impl Error for MeliError {
fn description(&self) -> &str {
&self.details
diff --git a/melib/src/mailbox/backends.rs b/melib/src/mailbox/backends.rs
index 799926e1..a650eda4 100644
--- a/melib/src/mailbox/backends.rs
+++ b/melib/src/mailbox/backends.rs
@@ -151,7 +151,7 @@ pub trait MailBackend: ::std::fmt::Debug {
fn folders(&self) -> Vec<Folder>;
fn operation(&self, hash: EnvelopeHash, folder_hash: FolderHash) -> Box<BackendOp>;
- fn save(&self, message: String, folder: &str) -> Result<()>;
+ fn save(&self, bytes: &[u8], folder: &str) -> Result<()>;
//login function
}
diff --git a/melib/src/mailbox/backends/maildir/backend.rs b/melib/src/mailbox/backends/maildir/backend.rs
index 89c3edc3..c70c0b94 100644
--- a/melib/src/mailbox/backends/maildir/backend.rs
+++ b/melib/src/mailbox/backends/maildir/backend.rs
@@ -48,8 +48,7 @@ use std::collections::hash_map::DefaultHasher;
use std::ffi::OsStr;
use std::fs;
use std::hash::{Hash, Hasher};
-use std::io;
-use std::io::Write;
+use std::io::{self, Read, Write};
use std::ops::{Deref, DerefMut};
use std::path::{Component, Path, PathBuf};
use std::result;
@@ -343,25 +342,34 @@ eprintln!("DEBUG: hash {}, path: {} couldn't be parsed in `add_path_to_index`",
Box::new(MaildirOp::new(hash, self.hash_indexes.clone(), folder_hash))
}
- fn save(&self, message: String, folder: &str) -> Result<()> {
+ fn save(&self, bytes: &[u8], folder: &str) -> Result<()> {
for f in &self.folders {
if f.name == folder {
let mut path = f.path.clone();
path.push("cur");
- path.push("draft:2,");
+ {
+ let mut rand_buf = [0u8; 16];
+ let mut f = fs::File::open("/dev/urandom").expect("Could not open /dev/urandom for reading");
+ f.read_exact(&mut rand_buf).expect("Could not read from /dev/urandom/");
+ let mut hostn_buf = String::with_capacity(256);
+ let mut f = fs::File::open("/etc/hostname").expect("Could not open /etc/hostname for reading");
+ f.read_to_string(&mut hostn_buf).expect("Could not read from /etc/hostname");
+ let timestamp = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap().as_millis();
+ path.push(&format!("{}.{:x}_{}.{}:2,", timestamp, u128::from_be_bytes(rand_buf), std::process::id(), hostn_buf.trim()));
+ }
if cfg!(feature = "debug_log") {
eprintln!("saving at {}", path.display());
}
- let file = fs::File::create(path)?;
+ let file = fs::File::create(path).unwrap();
let mut writer = io::BufWriter::new(file);
- writer.write_all(&message.into_bytes())?;
+ writer.write_all(bytes).unwrap();
return Ok(());
}
}
Err(MeliError::new(format!(
- "'{}' is not a valid folder.",
- folder
+ "'{}' is not a valid folder.",
+ folder
)))
}
}
diff --git a/melib/src/mailbox/email/compose.rs b/melib/src/mailbox/email/compose.rs
index 80f0f323..6161b0bb 100644
--- a/melib/src/mailbox/email/compose.rs
+++ b/melib/src/mailbox/email/compose.rs
@@ -184,6 +184,7 @@ impl Draft {
Ok(ret)
}
+
pub fn finalise(self) -> Result<String> {
let mut ret = String::new();
diff --git a/ui/src/components/mail/compose.rs b/ui/src/components/mail/compose.rs
index 532fa282..5ec25443 100644
--- a/ui/src/components/mail/compose.rs
+++ b/ui/src/components/mail/compose.rs
@@ -487,13 +487,15 @@ impl Component for Composer {
('y', ViewMode::Discard(u)) => {
let account = &context.accounts[self.account_cursor];
let draft = std::mem::replace(&mut self.draft, Draft::default());
- if cfg!(feature = "debug_log") {
- eprintln!("{:?}", account.save_draft(draft));
+ if let Err(e) = account.save_draft(draft) {
+ if cfg!(feature = "debug_log") {
+ eprintln!("{:?} could not save draft", e);
+ }
+ context.replies.push_back(UIEvent {
+ id: 0,
+ event_type: UIEventType::Notification(Some("Could not save draft.".into()), e.into())
+ });
}
-
- //if cfg!(feature = "debug_log") {
- //eprintln!("{:?}", self.draft.to_string());
- //}
context.replies.push_back(UIEvent {
id: 0,
event_type: UIEventType::Action(Tab(Kill(*u))),
diff --git a/ui/src/conf/accounts.rs b/ui/src/conf/accounts.rs
index 7cc9fbf7..e32d2492 100644
--- a/ui/src/conf/accounts.rs
+++ b/ui/src/conf/accounts.rs
@@ -302,8 +302,9 @@ impl Account {
}
pub fn save_draft(&self, draft: Draft) -> Result<()> {
+ let finalize = draft.finalise()?;
self.backend
- .save(draft.to_string()?, &self.settings.conf.draft_folder)
+ .save(&finalize.as_bytes(), &self.settings.conf.draft_folder)
}
}