mirror of https://github.com/terhechte/postsack
Removed warnings, started implementing apple mail
parent
2f34e13355
commit
6bdc412833
@ -0,0 +1,46 @@
|
||||
//! We use a stubbornly stupid algorithm where we just
|
||||
//! recursively drill down into the appropriate folder
|
||||
//! until we find `emlx` files and return those.
|
||||
|
||||
use eyre::Result;
|
||||
use rayon::prelude::*;
|
||||
use tracing::trace;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use super::super::shared::filesystem::folders_in;
|
||||
use super::super::{Message, MessageSender};
|
||||
use super::raw_email::RawEmailEntry;
|
||||
use crate::types::Config;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
fn test_walkdir() {
|
||||
for entry in WalkDir::new("foo").int_par_iter().filter_map(|e| e.ok()) {
|
||||
println!("{}", entry.path().display());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_emails(config: &Config, sender: MessageSender) -> Result<Vec<RawEmailEntry>> {
|
||||
Ok(folders_in(&config.emails_folder_path, sender, read_folder)?)
|
||||
}
|
||||
|
||||
fn read_folder(path: &Path, sender: MessageSender) -> Result<Vec<RawEmailEntry>> {
|
||||
let result = Ok(std::fs::read_dir(path)?
|
||||
.into_iter()
|
||||
.par_bridge()
|
||||
.filter_map(|entry| {
|
||||
let path = entry
|
||||
.map_err(|e| tracing::error!("{} {:?}", &path.display(), &e))
|
||||
.ok()?
|
||||
.path();
|
||||
if path.is_dir() {
|
||||
return None;
|
||||
}
|
||||
trace!("Reading {}", &path.display());
|
||||
RawEmailEntry::new(path)
|
||||
})
|
||||
.collect());
|
||||
// We're done reading the folder
|
||||
sender.send(Message::ReadOne).unwrap();
|
||||
result
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
use emlx::parse_emlx;
|
||||
use eyre::Result;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::super::shared::email::EmailMeta;
|
||||
use super::super::shared::parse::ParseableEmail;
|
||||
|
||||
pub struct Mail {
|
||||
path: PathBuf,
|
||||
// This is parsed out of the `emlx` as it is parsed
|
||||
is_seen: bool,
|
||||
// This is parsed out of the `path`
|
||||
label: Option<String>,
|
||||
// Maildata
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Mail {
|
||||
pub fn new(path: &Path) -> Result<Self> {
|
||||
// find the folder ending with `.mbox` in the path
|
||||
let ext = ".mbox";
|
||||
let label = path
|
||||
.iter()
|
||||
.map(|e| e.to_str())
|
||||
.flatten()
|
||||
.find(|s| s.ends_with(ext))
|
||||
.map(|s| s.replace(ext, "").to_string());
|
||||
Ok(Self {
|
||||
path: path.to_path_buf(),
|
||||
is_seen: false,
|
||||
label,
|
||||
data: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseableEmail for Mail {
|
||||
fn prepare(&mut self) -> Result<()> {
|
||||
let data = std::fs::read(self.path.as_path())?;
|
||||
let parsed = parse_emlx(&data)?;
|
||||
self.is_seen = !parsed.flags.is_read;
|
||||
self.data = parsed.message.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
fn message<'a>(&'a self) -> Result<Cow<'a, [u8]>> {
|
||||
Ok(Cow::Borrowed(self.data.as_slice()))
|
||||
}
|
||||
fn path(&self) -> &Path {
|
||||
self.path.as_path()
|
||||
}
|
||||
fn meta(&self) -> Result<Option<EmailMeta>> {
|
||||
let tags = match self.label {
|
||||
Some(ref n) => vec![n.clone()],
|
||||
None => vec![],
|
||||
};
|
||||
let meta = EmailMeta {
|
||||
tags,
|
||||
is_seen: self.is_seen,
|
||||
};
|
||||
Ok(Some(meta))
|
||||
}
|
||||
}
|
@ -1,2 +1,16 @@
|
||||
mod mail;
|
||||
|
||||
/// FIXME: Not sure if the number changes with each macOS release?
|
||||
const DEFAULT_FOLDER: &str = "~/Library/Mail/V8/";
|
||||
|
||||
use super::{Config, ImporterFormat, MessageSender, Result};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AppleMail {}
|
||||
|
||||
impl ImporterFormat for AppleMail {
|
||||
type Item = mail::Mail;
|
||||
fn emails(&self, config: &Config, sender: MessageSender) -> Result<Vec<Self::Item>> {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue