mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
improve logging, replace log-level by RUST_LOG
This commit is contained in:
+132
-120
@@ -1,10 +1,10 @@
|
||||
use clap::{self, App, Arg, ArgMatches, SubCommand};
|
||||
use clap;
|
||||
use error_chain::error_chain;
|
||||
use log::{debug, error, info, trace};
|
||||
use log::{debug, error, trace};
|
||||
use std::{fs, ops::Deref};
|
||||
|
||||
use crate::{
|
||||
config::model::{Account, Config},
|
||||
app::App,
|
||||
flag::model::Flag,
|
||||
imap::model::ImapConnector,
|
||||
input,
|
||||
@@ -15,7 +15,6 @@ use crate::{
|
||||
|
||||
error_chain! {
|
||||
links {
|
||||
Config(crate::config::model::Error, crate::config::model::ErrorKind);
|
||||
Imap(crate::imap::model::Error, crate::imap::model::ErrorKind);
|
||||
Input(crate::input::Error, crate::input::ErrorKind);
|
||||
MsgModel(crate::msg::model::Error, crate::msg::model::ErrorKind);
|
||||
@@ -26,30 +25,30 @@ error_chain! {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uid_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("uid")
|
||||
pub fn uid_arg<'a>() -> clap::Arg<'a, 'a> {
|
||||
clap::Arg::with_name("uid")
|
||||
.help("Specifies the targetted message")
|
||||
.value_name("UID")
|
||||
.required(true)
|
||||
}
|
||||
|
||||
fn reply_all_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("reply-all")
|
||||
fn reply_all_arg<'a>() -> clap::Arg<'a, 'a> {
|
||||
clap::Arg::with_name("reply-all")
|
||||
.help("Includes all recipients")
|
||||
.short("a")
|
||||
.long("all")
|
||||
}
|
||||
|
||||
fn page_size_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("page-size")
|
||||
fn page_size_arg<'a>() -> clap::Arg<'a, 'a> {
|
||||
clap::Arg::with_name("page-size")
|
||||
.help("Page size")
|
||||
.short("s")
|
||||
.long("size")
|
||||
.value_name("INT")
|
||||
}
|
||||
|
||||
fn page_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("page")
|
||||
fn page_arg<'a>() -> clap::Arg<'a, 'a> {
|
||||
clap::Arg::with_name("page")
|
||||
.help("Page number")
|
||||
.short("p")
|
||||
.long("page")
|
||||
@@ -57,8 +56,8 @@ fn page_arg<'a>() -> Arg<'a, 'a> {
|
||||
.default_value("0")
|
||||
}
|
||||
|
||||
fn attachment_arg<'a>() -> Arg<'a, 'a> {
|
||||
Arg::with_name("attachments")
|
||||
fn attachment_arg<'a>() -> clap::Arg<'a, 'a> {
|
||||
clap::Arg::with_name("attachments")
|
||||
.help("Adds attachment to the message")
|
||||
.short("a")
|
||||
.long("attachment")
|
||||
@@ -67,41 +66,41 @@ fn attachment_arg<'a>() -> Arg<'a, 'a> {
|
||||
.takes_value(true)
|
||||
}
|
||||
|
||||
pub fn msg_subcmds<'s>() -> Vec<App<'s, 's>> {
|
||||
pub fn msg_subcmds<'a>() -> Vec<clap::App<'a, 'a>> {
|
||||
vec![
|
||||
SubCommand::with_name("list")
|
||||
clap::SubCommand::with_name("list")
|
||||
.aliases(&["lst", "l"])
|
||||
.about("Lists all messages")
|
||||
.arg(page_size_arg())
|
||||
.arg(page_arg()),
|
||||
SubCommand::with_name("search")
|
||||
clap::SubCommand::with_name("search")
|
||||
.aliases(&["query", "q", "s"])
|
||||
.about("Lists messages matching the given IMAP query")
|
||||
.arg(page_size_arg())
|
||||
.arg(page_arg())
|
||||
.arg(
|
||||
Arg::with_name("query")
|
||||
clap::Arg::with_name("query")
|
||||
.help("IMAP query (see https://tools.ietf.org/html/rfc3501#section-6.4.4)")
|
||||
.value_name("QUERY")
|
||||
.multiple(true)
|
||||
.required(true),
|
||||
),
|
||||
SubCommand::with_name("write")
|
||||
clap::SubCommand::with_name("write")
|
||||
.aliases(&["w"])
|
||||
.about("Writes a new message")
|
||||
.arg(attachment_arg()),
|
||||
SubCommand::with_name("send")
|
||||
clap::SubCommand::with_name("send")
|
||||
.about("Sends a raw message")
|
||||
.arg(Arg::with_name("message").raw(true)),
|
||||
SubCommand::with_name("save")
|
||||
.arg(clap::Arg::with_name("message").raw(true)),
|
||||
clap::SubCommand::with_name("save")
|
||||
.about("Saves a raw message")
|
||||
.arg(Arg::with_name("message").raw(true)),
|
||||
SubCommand::with_name("read")
|
||||
.arg(clap::Arg::with_name("message").raw(true)),
|
||||
clap::SubCommand::with_name("read")
|
||||
.aliases(&["r"])
|
||||
.about("Reads text bodies of a message")
|
||||
.arg(uid_arg())
|
||||
.arg(
|
||||
Arg::with_name("mime-type")
|
||||
clap::Arg::with_name("mime-type")
|
||||
.help("MIME type to use")
|
||||
.short("t")
|
||||
.long("mime-type")
|
||||
@@ -110,55 +109,55 @@ pub fn msg_subcmds<'s>() -> Vec<App<'s, 's>> {
|
||||
.default_value("plain"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("raw")
|
||||
clap::Arg::with_name("raw")
|
||||
.help("Reads raw message")
|
||||
.long("raw")
|
||||
.short("r"),
|
||||
),
|
||||
SubCommand::with_name("attachments")
|
||||
clap::SubCommand::with_name("attachments")
|
||||
.aliases(&["attach", "att", "a"])
|
||||
.about("Downloads all message attachments")
|
||||
.arg(uid_arg()),
|
||||
SubCommand::with_name("reply")
|
||||
clap::SubCommand::with_name("reply")
|
||||
.aliases(&["rep", "re"])
|
||||
.about("Answers to a message")
|
||||
.arg(uid_arg())
|
||||
.arg(reply_all_arg()),
|
||||
SubCommand::with_name("forward")
|
||||
clap::SubCommand::with_name("forward")
|
||||
.aliases(&["fwd", "f"])
|
||||
.about("Forwards a message")
|
||||
.arg(uid_arg()),
|
||||
SubCommand::with_name("copy")
|
||||
clap::SubCommand::with_name("copy")
|
||||
.aliases(&["cp", "c"])
|
||||
.about("Copies a message to the targetted mailbox")
|
||||
.arg(uid_arg())
|
||||
.arg(mbox_target_arg()),
|
||||
SubCommand::with_name("move")
|
||||
clap::SubCommand::with_name("move")
|
||||
.aliases(&["mv", "m"])
|
||||
.about("Moves a message to the targetted mailbox")
|
||||
.arg(uid_arg())
|
||||
.arg(mbox_target_arg()),
|
||||
SubCommand::with_name("delete")
|
||||
clap::SubCommand::with_name("delete")
|
||||
.aliases(&["remove", "rm", "del", "d"])
|
||||
.about("Deletes a message")
|
||||
.arg(uid_arg()),
|
||||
SubCommand::with_name("template")
|
||||
clap::SubCommand::with_name("template")
|
||||
.aliases(&["tpl", "t"])
|
||||
.about("Generates a message template")
|
||||
.subcommand(
|
||||
SubCommand::with_name("new")
|
||||
clap::SubCommand::with_name("new")
|
||||
.aliases(&["n"])
|
||||
.about("Generates a new message template"),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("reply")
|
||||
clap::SubCommand::with_name("reply")
|
||||
.aliases(&["rep", "r"])
|
||||
.about("Generates a reply message template")
|
||||
.arg(uid_arg())
|
||||
.arg(reply_all_arg()),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("forward")
|
||||
clap::SubCommand::with_name("forward")
|
||||
.aliases(&["fwd", "fw", "f"])
|
||||
.about("Generates a forward message template")
|
||||
.arg(uid_arg()),
|
||||
@@ -166,19 +165,14 @@ pub fn msg_subcmds<'s>() -> Vec<App<'s, 's>> {
|
||||
]
|
||||
}
|
||||
|
||||
pub fn msg_matches(
|
||||
config: &Config,
|
||||
account: &Account,
|
||||
mbox: &str,
|
||||
matches: &ArgMatches,
|
||||
) -> Result<bool> {
|
||||
if let Some(matches) = matches.subcommand_matches("list") {
|
||||
pub fn msg_matches(app: &App) -> Result<bool> {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("list") {
|
||||
debug!("list command matched");
|
||||
|
||||
let page_size: usize = matches
|
||||
.value_of("page-size")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(config.default_page_size(&account));
|
||||
.unwrap_or(app.config.default_page_size(&app.account));
|
||||
debug!("page size: {}", &page_size);
|
||||
let page: usize = matches
|
||||
.value_of("page")
|
||||
@@ -187,23 +181,23 @@ pub fn msg_matches(
|
||||
.unwrap_or_default();
|
||||
debug!("page: {}", &page);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msgs = imap_conn.list_msgs(&mbox, &page_size, &page)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msgs = imap_conn.list_msgs(&app.mbox, &page_size, &page)?;
|
||||
let msgs = Msgs::from(&msgs);
|
||||
info!("{}", msgs);
|
||||
trace!("messages: {:?}", msgs);
|
||||
app.output.print(msgs);
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("search") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("search") {
|
||||
debug!("search command matched");
|
||||
|
||||
let page_size: usize = matches
|
||||
.value_of("page-size")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(config.default_page_size(&account));
|
||||
.unwrap_or(app.config.default_page_size(&app.account));
|
||||
debug!("page size: {}", &page_size);
|
||||
let page: usize = matches
|
||||
.value_of("page")
|
||||
@@ -238,17 +232,17 @@ pub fn msg_matches(
|
||||
.join(" ");
|
||||
debug!("query: {}", &page);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msgs = imap_conn.search_msgs(&mbox, &query, &page_size, &page)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msgs = imap_conn.search_msgs(&app.mbox, &query, &page_size, &page)?;
|
||||
let msgs = Msgs::from(&msgs);
|
||||
info!("{}", msgs);
|
||||
trace!("messages: {:?}", msgs);
|
||||
app.output.print(msgs);
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("read") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("read") {
|
||||
debug!("read command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
@@ -258,30 +252,30 @@ pub fn msg_matches(
|
||||
let raw = matches.is_present("raw");
|
||||
debug!("raw: {}", raw);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = imap_conn.read_msg(&mbox, &uid)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = imap_conn.read_msg(&app.mbox, &uid)?;
|
||||
if raw {
|
||||
let msg = String::from_utf8(msg)
|
||||
.chain_err(|| "Could not decode raw message as utf8 string")?;
|
||||
let msg = msg.trim_end_matches("\n");
|
||||
info!("{}", msg);
|
||||
app.output.print(msg);
|
||||
} else {
|
||||
let msg = ReadableMsg::from_bytes(&mime, &msg)?;
|
||||
info!("{}", msg);
|
||||
app.output.print(msg);
|
||||
}
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("attachments") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("attachments") {
|
||||
debug!("attachments command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
debug!("uid: {}", &uid);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = imap_conn.read_msg(&mbox, &uid)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = imap_conn.read_msg(&app.mbox, &uid)?;
|
||||
let attachments = Attachments::from_bytes(&msg)?;
|
||||
debug!(
|
||||
"{} attachment(s) found for message {}",
|
||||
@@ -289,30 +283,37 @@ pub fn msg_matches(
|
||||
&uid
|
||||
);
|
||||
for attachment in attachments.0.iter() {
|
||||
let filepath = config.downloads_filepath(&account, &attachment.filename);
|
||||
let filepath = app
|
||||
.config
|
||||
.downloads_filepath(&app.account, &attachment.filename);
|
||||
debug!("downloading {}…", &attachment.filename);
|
||||
fs::write(&filepath, &attachment.raw)
|
||||
.chain_err(|| format!("Could not save attachment {:?}", filepath))?;
|
||||
}
|
||||
info!(
|
||||
|
||||
debug!(
|
||||
"{} attachment(s) successfully downloaded",
|
||||
&attachments.0.len()
|
||||
);
|
||||
app.output.print(format!(
|
||||
"{} attachment(s) successfully downloaded",
|
||||
&attachments.0.len()
|
||||
));
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("write") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("write") {
|
||||
debug!("write command matched");
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let attachments = matches
|
||||
.values_of("attachments")
|
||||
.unwrap_or_default()
|
||||
.map(String::from)
|
||||
.collect::<Vec<_>>();
|
||||
let tpl = Msg::build_new_tpl(&config, &account)?;
|
||||
let tpl = Msg::build_new_tpl(&app.config, &app.account)?;
|
||||
let content = input::open_editor_with_tpl(tpl.to_string().as_bytes())?;
|
||||
let mut msg = Msg::from(content);
|
||||
msg.attachments = attachments;
|
||||
@@ -323,10 +324,10 @@ pub fn msg_matches(
|
||||
input::PostEditChoice::Send => {
|
||||
debug!("sending message…");
|
||||
let msg = msg.to_sendable_msg()?;
|
||||
smtp::send(&account, &msg)?;
|
||||
smtp::send(&app.account, &msg)?;
|
||||
imap_conn.append_msg("Sent", &msg.formatted(), &[Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully sent");
|
||||
app.output.print("Message successfully sent");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Edit => {
|
||||
@@ -338,7 +339,7 @@ pub fn msg_matches(
|
||||
debug!("saving to draft…");
|
||||
imap_conn.append_msg("Drafts", &msg.to_vec()?, &[Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully saved to Drafts");
|
||||
app.output.print("Message successfully saved to Drafts");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Discard => {
|
||||
@@ -353,7 +354,7 @@ pub fn msg_matches(
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("reply") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("reply") {
|
||||
debug!("reply command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
@@ -366,12 +367,12 @@ pub fn msg_matches(
|
||||
debug!("found {} attachments", attachments.len());
|
||||
trace!("attachments: {:?}", attachments);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let tpl = if matches.is_present("reply-all") {
|
||||
msg.build_reply_all_tpl(&config, &account)?
|
||||
msg.build_reply_all_tpl(&app.config, &app.account)?
|
||||
} else {
|
||||
msg.build_reply_tpl(&config, &account)?
|
||||
msg.build_reply_tpl(&app.config, &app.account)?
|
||||
};
|
||||
|
||||
let content = input::open_editor_with_tpl(&tpl.to_string().as_bytes())?;
|
||||
@@ -384,11 +385,11 @@ pub fn msg_matches(
|
||||
input::PostEditChoice::Send => {
|
||||
debug!("sending message…");
|
||||
let msg = msg.to_sendable_msg()?;
|
||||
smtp::send(&account, &msg)?;
|
||||
smtp::send(&app.account, &msg)?;
|
||||
imap_conn.append_msg("Sent", &msg.formatted(), &[Flag::Seen])?;
|
||||
imap_conn.add_flags(&mbox, uid, "\\Answered")?;
|
||||
imap_conn.add_flags(&app.mbox, uid, "\\Answered")?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully sent");
|
||||
app.output.print("Message successfully sent");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Edit => {
|
||||
@@ -400,7 +401,7 @@ pub fn msg_matches(
|
||||
debug!("saving to draft…");
|
||||
imap_conn.append_msg("Drafts", &msg.to_vec()?, &[Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully saved to Drafts");
|
||||
app.output.print("Message successfully saved to Drafts");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Discard => {
|
||||
@@ -416,7 +417,7 @@ pub fn msg_matches(
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("forward") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("forward") {
|
||||
debug!("forward command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
@@ -429,9 +430,9 @@ pub fn msg_matches(
|
||||
debug!("found {} attachments", attachments.len());
|
||||
trace!("attachments: {:?}", attachments);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let tpl = msg.build_forward_tpl(&config, &account)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let tpl = msg.build_forward_tpl(&app.config, &app.account)?;
|
||||
let content = input::open_editor_with_tpl(&tpl.to_string().as_bytes())?;
|
||||
let mut msg = Msg::from(content);
|
||||
msg.attachments = attachments;
|
||||
@@ -442,10 +443,10 @@ pub fn msg_matches(
|
||||
input::PostEditChoice::Send => {
|
||||
debug!("sending message…");
|
||||
let msg = msg.to_sendable_msg()?;
|
||||
smtp::send(&account, &msg)?;
|
||||
smtp::send(&app.account, &msg)?;
|
||||
imap_conn.append_msg("Sent", &msg.formatted(), &[Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully sent");
|
||||
app.output.print("Message successfully sent");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Edit => {
|
||||
@@ -457,7 +458,7 @@ pub fn msg_matches(
|
||||
debug!("saving to draft…");
|
||||
imap_conn.append_msg("Drafts", &msg.to_vec()?, &[Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
info!("Message successfully saved to Drafts");
|
||||
app.output.print("Message successfully saved to Drafts");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Discard => {
|
||||
@@ -473,14 +474,14 @@ pub fn msg_matches(
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("template") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("template") {
|
||||
debug!("template command matched");
|
||||
|
||||
if let Some(_) = matches.subcommand_matches("new") {
|
||||
debug!("new command matched");
|
||||
let tpl = Msg::build_new_tpl(&config, &account)?;
|
||||
info!("{}", tpl);
|
||||
let tpl = Msg::build_new_tpl(&app.config, &app.account)?;
|
||||
trace!("tpl: {:?}", tpl);
|
||||
app.output.print(tpl);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("reply") {
|
||||
@@ -489,15 +490,15 @@ pub fn msg_matches(
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
debug!("uid: {}", uid);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let tpl = if matches.is_present("reply-all") {
|
||||
msg.build_reply_all_tpl(&config, &account)?
|
||||
msg.build_reply_all_tpl(&app.config, &app.account)?
|
||||
} else {
|
||||
msg.build_reply_tpl(&config, &account)?
|
||||
msg.build_reply_tpl(&app.config, &app.account)?
|
||||
};
|
||||
info!("{}", tpl);
|
||||
trace!("tpl: {:?}", tpl);
|
||||
app.output.print(tpl);
|
||||
|
||||
imap_conn.logout();
|
||||
}
|
||||
@@ -508,11 +509,11 @@ pub fn msg_matches(
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
debug!("uid: {}", uid);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let tpl = msg.build_forward_tpl(&config, &account)?;
|
||||
info!("{}", tpl);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let tpl = msg.build_forward_tpl(&app.config, &app.account)?;
|
||||
trace!("tpl: {:?}", tpl);
|
||||
app.output.print(tpl);
|
||||
|
||||
imap_conn.logout();
|
||||
}
|
||||
@@ -520,7 +521,7 @@ pub fn msg_matches(
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("copy") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("copy") {
|
||||
debug!("copy command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
@@ -528,18 +529,22 @@ pub fn msg_matches(
|
||||
let target = matches.value_of("target").unwrap();
|
||||
debug!("target: {}", &target);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let mut flags = msg.flags.deref().to_vec();
|
||||
flags.push(Flag::Seen);
|
||||
imap_conn.append_msg(target, &msg.raw, &flags)?;
|
||||
info!("Message {} successfully copied to folder `{}`", uid, target);
|
||||
debug!("message {} successfully copied to folder `{}`", uid, target);
|
||||
app.output.print(format!(
|
||||
"Message {} successfully copied to folder `{}`",
|
||||
uid, target
|
||||
));
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("move") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("move") {
|
||||
debug!("move command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
@@ -547,55 +552,61 @@ pub fn msg_matches(
|
||||
let target = matches.value_of("target").unwrap();
|
||||
debug!("target: {}", &target);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?);
|
||||
let mut flags = msg.flags.deref().to_vec();
|
||||
flags.push(Flag::Seen);
|
||||
imap_conn.append_msg(target, &msg.raw, msg.flags.deref())?;
|
||||
imap_conn.add_flags(&mbox, uid, "\\Seen \\Deleted")?;
|
||||
info!("Message {} successfully moved to folder `{}`", uid, target);
|
||||
imap_conn.add_flags(&app.mbox, uid, "\\Seen \\Deleted")?;
|
||||
debug!("message {} successfully moved to folder `{}`", uid, target);
|
||||
app.output.print(format!(
|
||||
"Message {} successfully moved to folder `{}`",
|
||||
uid, target
|
||||
));
|
||||
|
||||
imap_conn.expunge(&mbox)?;
|
||||
imap_conn.expunge(&app.mbox)?;
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("delete") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("delete") {
|
||||
debug!("delete command matched");
|
||||
|
||||
let uid = matches.value_of("uid").unwrap();
|
||||
debug!("uid: {}", &uid);
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
imap_conn.add_flags(&mbox, uid, "\\Seen \\Deleted")?;
|
||||
info!("Message {} successfully deleted", uid);
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
imap_conn.add_flags(&app.mbox, uid, "\\Seen \\Deleted")?;
|
||||
debug!("message {} successfully deleted", uid);
|
||||
app.output
|
||||
.print(format!("Message {} successfully deleted", uid));
|
||||
|
||||
imap_conn.expunge(&mbox)?;
|
||||
imap_conn.expunge(&app.mbox)?;
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("send") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("send") {
|
||||
debug!("send command matched");
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.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)?;
|
||||
smtp::send(&app.account, &msg)?;
|
||||
imap_conn.append_msg("Sent", &msg.formatted(), &[Flag::Seen])?;
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("save") {
|
||||
if let Some(matches) = app.arg_matches.subcommand_matches("save") {
|
||||
debug!("save command matched");
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.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.append_msg(&app.mbox, &msg.to_vec()?, &[Flag::Seen])?;
|
||||
|
||||
imap_conn.logout();
|
||||
return Ok(true);
|
||||
@@ -604,10 +615,11 @@ pub fn msg_matches(
|
||||
{
|
||||
debug!("default list command matched");
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&account)?;
|
||||
let msgs = imap_conn.list_msgs(&mbox, &config.default_page_size(&account), &0)?;
|
||||
let mut imap_conn = ImapConnector::new(&app.account)?;
|
||||
let msgs =
|
||||
imap_conn.list_msgs(&app.mbox, &app.config.default_page_size(&app.account), &0)?;
|
||||
let msgs = Msgs::from(&msgs);
|
||||
info!("{}", msgs);
|
||||
app.output.print(msgs);
|
||||
|
||||
imap_conn.logout();
|
||||
Ok(true)
|
||||
|
||||
+3
-34
@@ -15,7 +15,6 @@ use uuid::Uuid;
|
||||
use crate::{
|
||||
config::model::{Account, Config},
|
||||
flag::model::{Flag, Flags},
|
||||
output::fmt::{get_output_fmt, OutputFmt, Response},
|
||||
table::{Cell, Row, Table},
|
||||
};
|
||||
|
||||
@@ -33,17 +32,7 @@ pub struct Tpl(String);
|
||||
|
||||
impl fmt::Display for Tpl {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
unsafe {
|
||||
match get_output_fmt() {
|
||||
&OutputFmt::Plain => {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
&OutputFmt::Json => {
|
||||
let res = serde_json::to_string(&Response::new(self)).unwrap();
|
||||
write!(f, "{}", res)
|
||||
}
|
||||
}
|
||||
}
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,17 +121,7 @@ impl Serialize for ReadableMsg {
|
||||
|
||||
impl fmt::Display for ReadableMsg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
unsafe {
|
||||
match get_output_fmt() {
|
||||
&OutputFmt::Plain => {
|
||||
writeln!(f, "{}", self.content)
|
||||
}
|
||||
&OutputFmt::Json => {
|
||||
let res = serde_json::to_string(&Response::new(self)).unwrap();
|
||||
write!(f, "{}", res)
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln!(f, "{}", self.content)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -677,16 +656,6 @@ impl<'m> From<&'m imap::types::ZeroCopy<Vec<imap::types::Fetch>>> for Msgs<'m> {
|
||||
|
||||
impl<'m> fmt::Display for Msgs<'m> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
unsafe {
|
||||
match get_output_fmt() {
|
||||
&OutputFmt::Plain => {
|
||||
writeln!(f, "\n{}", Table::render(&self.0))
|
||||
}
|
||||
&OutputFmt::Json => {
|
||||
let res = serde_json::to_string(&Response::new(self)).unwrap();
|
||||
write!(f, "{}", res)
|
||||
}
|
||||
}
|
||||
}
|
||||
writeln!(f, "\n{}", Table::render(&self.0))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user