summaryrefslogtreecommitdiff
path: root/src/cmd.rs
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2023-08-15 15:13:16 +0200
committerJannis M. Hoffmann <jannis@fehcom.de>2023-08-15 15:13:16 +0200
commit161b052713f5cee08f20ce6ade0881e274c47fdd (patch)
tree4f541f79bb4c4a4089373926ca66783a4af26119 /src/cmd.rs
initial commit
Diffstat (limited to 'src/cmd.rs')
-rw-r--r--src/cmd.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/cmd.rs b/src/cmd.rs
new file mode 100644
index 0000000..364fcfb
--- /dev/null
+++ b/src/cmd.rs
@@ -0,0 +1,90 @@
+use std::io::ErrorKind as IOErrKind;
+use std::path::PathBuf;
+
+use maildir::Maildir;
+use serde::Serialize as _;
+use serde::Serializer as _;
+
+mod count;
+mod folders;
+mod list;
+mod raw;
+
+use crate::error::Result;
+use crate::rfc822::{MIMEHeader, Mail, TopMailHeader};
+
+pub use count::{count, CountInfo};
+pub use folders::folders;
+pub use list::list;
+pub use raw::raw;
+
+pub enum Return {
+ Read(Mail),
+ Raw(MIMEHeader, Vec<u8>),
+ List(Vec<TopMailHeader>),
+ Folders(Vec<String>),
+ Count(CountInfo),
+ Search(Vec<Mail>),
+ Move,
+}
+
+pub fn serialize_to<W>(res: Result<Return>, mut write: W) -> std::io::Result<()>
+where
+ W: std::io::Write + Copy,
+{
+ let ser = &mut serde_json::Serializer::new(write);
+
+ match res {
+ Err(e) => {
+ e.serialize(ser)?;
+ std::process::exit(3)
+ }
+ Ok(r) => {
+ match r {
+ Return::Folders(fs) => fs.serialize(ser),
+ Return::Count(ci) => ci.serialize(ser),
+ Return::List(tmhs) => tmhs.serialize(ser),
+ Return::Search(ms) => ms.serialize(ser),
+ Return::Read(m) => m.serialize(ser),
+ Return::Raw(mh, b) => {
+ let r = match mh.serialize(ser) {
+ Ok(x) => x,
+ Err(e) => return Err(e.into()),
+ };
+ write.write_all(b"\n")?;
+ write.write_all(&b)?;
+ Ok(r)
+ }
+ Return::Move => ser.serialize_unit(),
+ }?;
+ Ok(())
+ }
+ }
+}
+
+pub fn open_submaildir(mut path: PathBuf, sub: &str) -> Maildir {
+ if sub != "" {
+ path.push(String::from(".") + sub);
+ }
+ Maildir::from(path)
+}
+
+pub fn read(md: &Maildir, mid: &str) -> Result<Mail> {
+ md.add_flags(mid, "S")?;
+
+ let mut mail = md.find(mid).ok_or_else(|| {
+ std::io::Error::new(IOErrKind::NotFound, format!("mail {} not found", mid))
+ })?;
+
+ Ok(mail.parsed()?.try_into()?)
+}
+
+pub fn move_mail(p: PathBuf, mid: &str, from_f: &str, to_f: &str) -> Result<()> {
+ let from = open_submaildir(p.clone(), from_f);
+ let to = open_submaildir(p, to_f);
+ from.move_to(mid, &to).map_err(|e| e.into())
+}
+
+pub fn search(_md: &Maildir, _pattern: &str) -> Result<Vec<Mail>> {
+ todo!()
+}