mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
set up cli arg parser #15
This commit is contained in:
+10
-7
@@ -96,17 +96,14 @@ fn date_from_fetch(fetch: &imap::types::Fetch) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_new_emails(imap_sess: &mut ImapSession, mbox: &str) -> imap::Result<()> {
|
||||
pub fn read_emails(imap_sess: &mut ImapSession, mbox: &str, query: &str) -> imap::Result<()> {
|
||||
imap_sess.select(mbox)?;
|
||||
// let mboxes = imap_sess.list(Some(""), Some("*"))?;
|
||||
// println!("Mboxes {:?}", mboxes);
|
||||
|
||||
let seqs = imap_sess
|
||||
.search("NOT SEEN")?
|
||||
.search(query)?
|
||||
.iter()
|
||||
.map(|n| n.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let table_head = vec![
|
||||
table::Cell::new(
|
||||
@@ -128,7 +125,10 @@ pub fn read_new_emails(imap_sess: &mut ImapSession, mbox: &str) -> imap::Result<
|
||||
];
|
||||
|
||||
let mut table_rows = imap_sess
|
||||
.fetch(seqs, "(INTERNALDATE ENVELOPE)")?
|
||||
.fetch(
|
||||
seqs[..20.min(seqs.len())].join(","),
|
||||
"(INTERNALDATE ENVELOPE)",
|
||||
)?
|
||||
.iter()
|
||||
.map(|fetch| {
|
||||
vec![
|
||||
@@ -146,3 +146,6 @@ pub fn read_new_emails(imap_sess: &mut ImapSession, mbox: &str) -> imap::Result<
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// List mailboxes
|
||||
// let mboxes = imap_sess.list(Some(""), Some("*"))?;
|
||||
|
||||
+80
-6
@@ -2,16 +2,90 @@ mod config;
|
||||
mod imap;
|
||||
mod table;
|
||||
|
||||
use std::env;
|
||||
use clap::{App, Arg, SubCommand};
|
||||
|
||||
fn mailbox_arg() -> Arg<'static, 'static> {
|
||||
Arg::with_name("mailbox")
|
||||
.help("Name of the targeted mailbox")
|
||||
.value_name("MAILBOX")
|
||||
.required(true)
|
||||
}
|
||||
|
||||
fn uid_arg() -> Arg<'static, 'static> {
|
||||
Arg::with_name("uid")
|
||||
.help("UID of the targeted email")
|
||||
.value_name("UID")
|
||||
.required(true)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mbox = env::args().nth(1).unwrap_or(String::from("inbox"));
|
||||
let args = env::args().skip(2).collect::<Vec<_>>().join(" ").to_owned();
|
||||
let config = config::read_file();
|
||||
let mut imap_sess = imap::login(&config);
|
||||
|
||||
match args.as_str() {
|
||||
"read new" => imap::read_new_emails(&mut imap_sess, &mbox).unwrap(),
|
||||
_ => println!("Himalaya: command not found e"),
|
||||
let matches = App::new("Himalaya")
|
||||
.version("0.1.0")
|
||||
.about("📫 Minimalist CLI mail client")
|
||||
.author("soywod <clement.douin@posteo.net>")
|
||||
.subcommand(
|
||||
SubCommand::with_name("read")
|
||||
.about("Reads an email by its UID")
|
||||
.arg(mailbox_arg())
|
||||
.arg(uid_arg()),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("query")
|
||||
.about("Prints emails filtered by the given IMAP query")
|
||||
.arg(mailbox_arg())
|
||||
.arg(
|
||||
Arg::with_name("query")
|
||||
.help("IMAP query (see https://tools.ietf.org/html/rfc3501#section-6.4.4)")
|
||||
.value_name("COMMANDS")
|
||||
.multiple(true)
|
||||
.required(true),
|
||||
),
|
||||
)
|
||||
.subcommand(SubCommand::with_name("write").about("Writes a new email"))
|
||||
.subcommand(
|
||||
SubCommand::with_name("forward")
|
||||
.about("Forwards an email by its UID")
|
||||
.arg(mailbox_arg())
|
||||
.arg(uid_arg()),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("reply")
|
||||
.about("Replies to an email by its UID")
|
||||
.arg(mailbox_arg())
|
||||
.arg(uid_arg())
|
||||
.arg(
|
||||
Arg::with_name("reply all")
|
||||
.help("Replies to all recipients")
|
||||
.short("a")
|
||||
.long("all"),
|
||||
),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("query") {
|
||||
let mbox = matches.value_of("mailbox").unwrap_or("inbox");
|
||||
|
||||
if let Some(matches) = matches.values_of("query") {
|
||||
let query = matches
|
||||
.fold((false, vec![]), |(escape, mut cmds), cmd| {
|
||||
if ["subject", "body", "text"].contains(&cmd.to_lowercase().as_str()) {
|
||||
cmds.push(cmd.to_string());
|
||||
(true, cmds)
|
||||
} else if escape {
|
||||
cmds.push(format!("\"{}\"", cmd));
|
||||
(false, cmds)
|
||||
} else {
|
||||
cmds.push(cmd.to_string());
|
||||
(false, cmds)
|
||||
}
|
||||
})
|
||||
.1
|
||||
.join(" ");
|
||||
|
||||
imap::read_emails(&mut imap_sess, &mbox, &query).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user