diff --git a/src/flag/cli.rs b/src/flag/cli.rs index 3581ea7..60a4970 100644 --- a/src/flag/cli.rs +++ b/src/flag/cli.rs @@ -50,26 +50,31 @@ pub fn flag_matches(matches: &ArgMatches) -> Result { let config = Config::new_from_file()?; let account = config.find_account_by_name(matches.value_of("account"))?; let mbox = matches.value_of("mailbox").unwrap(); - let mut imap_conn = ImapConnector::new(&account)?; if let Some(matches) = matches.subcommand_matches("set") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let flags = matches.value_of("flags").unwrap(); imap_conn.set_flags(mbox, uid, flags)?; + imap_conn.logout(); return Ok(true); } if let Some(matches) = matches.subcommand_matches("add") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let flags = matches.value_of("flags").unwrap(); imap_conn.add_flags(mbox, uid, flags)?; + imap_conn.logout(); return Ok(true); } if let Some(matches) = matches.subcommand_matches("remove") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let flags = matches.value_of("flags").unwrap(); imap_conn.remove_flags(mbox, uid, flags)?; + imap_conn.logout(); return Ok(true); } diff --git a/src/imap/cli.rs b/src/imap/cli.rs index cd1d0a0..09c5ac8 100644 --- a/src/imap/cli.rs +++ b/src/imap/cli.rs @@ -17,10 +17,10 @@ pub fn imap_subcmds<'a>() -> Vec> { pub fn imap_matches(matches: &ArgMatches) -> Result { let config = Config::new_from_file()?; let account = config.find_account_by_name(matches.value_of("account"))?; - let mut imap_conn = ImapConnector::new(&account)?; let mbox = matches.value_of("mailbox").unwrap(); if let Some(_) = matches.subcommand_matches("idle") { + let mut imap_conn = ImapConnector::new(&account)?; imap_conn.idle(&config, &mbox)?; imap_conn.logout(); return Ok(true); diff --git a/src/mbox/cli.rs b/src/mbox/cli.rs index 4ebb688..eff0067 100644 --- a/src/mbox/cli.rs +++ b/src/mbox/cli.rs @@ -37,9 +37,9 @@ pub fn mbox_matches(matches: &ArgMatches) -> Result { let config = Config::new_from_file()?; let account = config.find_account_by_name(matches.value_of("account"))?; let output_fmt = matches.value_of("output").unwrap(); - let mut imap_conn = ImapConnector::new(&account)?; if let Some(_) = matches.subcommand_matches("mailboxes") { + let mut imap_conn = ImapConnector::new(&account)?; let mboxes = imap_conn.list_mboxes()?; print(&output_fmt, mboxes)?; imap_conn.logout(); diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 44a42a6..1bbea3d 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -163,21 +163,21 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { let account = config.find_account_by_name(matches.value_of("account"))?; let output_fmt = matches.value_of("output").unwrap(); let mbox = matches.value_of("mailbox").unwrap(); - let mut imap_conn = ImapConnector::new(&account)?; loop { if let Some(matches) = matches.subcommand_matches("messages") { + let mut imap_conn = ImapConnector::new(&account)?; let page_size: usize = matches.value_of("size").unwrap().parse().unwrap(); let page: usize = matches.value_of("page").unwrap().parse().unwrap(); - let msgs = imap_conn.list_msgs(&mbox, &page_size, &page)?; let msgs = Msgs::from(&msgs); - print(&output_fmt, msgs)?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("search") { + let mut imap_conn = ImapConnector::new(&account)?; let page_size: usize = matches.value_of("size").unwrap().parse().unwrap(); let page: usize = matches.value_of("page").unwrap().parse().unwrap(); let query = matches @@ -204,26 +204,26 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { }) .1 .join(" "); - let msgs = imap_conn.search_msgs(&mbox, &query, &page_size, &page)?; let msgs = Msgs::from(&msgs); - print(&output_fmt, msgs)?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("read") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let mime = format!("text/{}", matches.value_of("mime-type").unwrap()); - let msg = imap_conn.read_msg(&mbox, &uid)?; let msg = ReadableMsg::from_bytes(&mime, &msg)?; - print(&output_fmt, msg)?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("attachments") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let msg = imap_conn.read_msg(&mbox, &uid)?; @@ -255,11 +255,12 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { } _ => (), } - + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("write") { + let mut imap_conn = ImapConnector::new(&account)?; let attachments = matches .values_of("attachments") .unwrap_or_default() @@ -296,11 +297,12 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { Err(err) => eprintln!("{}", err), } } - + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("template") { + let mut imap_conn = ImapConnector::new(&account)?; if let Some(_) = matches.subcommand_matches("new") { let tpl = Msg::build_new_tpl(&config, &account)?; print(&output_fmt, &tpl)?; @@ -330,11 +332,12 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { print(&output_fmt, &tpl)?; break; } - + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("reply") { + let mut imap_conn = ImapConnector::new(&account)?; let attachments = matches .values_of("attachments") .unwrap_or_default() @@ -379,11 +382,12 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { Err(err) => eprintln!("{}", err), } } - + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("forward") { + let mut imap_conn = ImapConnector::new(&account)?; let attachments = matches .values_of("attachments") .unwrap_or_default() @@ -422,68 +426,72 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { Err(err) => eprintln!("{}", err), } } - + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("copy") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let target = matches.value_of("target").unwrap(); - let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?); let mut flags = msg.flags.deref().to_vec(); flags.push(Flag::Seen); - imap_conn.append_msg(target, &msg.to_vec()?, &flags)?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("move") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); let target = matches.value_of("target").unwrap(); - let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?); let mut flags = msg.flags.deref().to_vec(); flags.push(Flag::Seen); - imap_conn.append_msg(target, &msg.to_vec()?, msg.flags.deref())?; imap_conn.add_flags(mbox, uid, "\\Seen \\Deleted")?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("delete") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); imap_conn.add_flags(mbox, uid, "\\Seen \\Deleted")?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("send") { + let mut imap_conn = ImapConnector::new(&account)?; let msg = matches.value_of("message").unwrap(); let msg = Msg::from(msg.to_string()); let msg = msg.to_sendable_msg()?; - smtp::send(&account, &msg)?; imap_conn.append_msg("Sent", &msg.formatted(), &[Flag::Seen])?; + imap_conn.logout(); break; } if let Some(matches) = matches.subcommand_matches("save") { + let mut imap_conn = ImapConnector::new(&account)?; let msg = matches.value_of("message").unwrap(); let msg = Msg::from(msg.to_string()); - imap_conn.append_msg(mbox, &msg.to_vec()?, &[Flag::Seen])?; + imap_conn.logout(); break; } // Default case: list all messages + let mut imap_conn = ImapConnector::new(&account)?; let msgs = imap_conn.list_msgs(&mbox, &10, &0)?; let msgs = Msgs::from(&msgs); - print(&output_fmt, msgs)?; + imap_conn.logout(); break; } - imap_conn.logout(); Ok(()) }