diff --git a/src/app.rs b/src/ctx.rs similarity index 93% rename from src/app.rs rename to src/ctx.rs index 11259db..6ca41a0 100644 --- a/src/app.rs +++ b/src/ctx.rs @@ -5,7 +5,7 @@ use crate::{ output::model::Output, }; -pub struct App<'a> { +pub struct Ctx<'a> { pub config: &'a Config, pub account: &'a Account, pub output: &'a Output, @@ -13,7 +13,7 @@ pub struct App<'a> { pub arg_matches: &'a clap::ArgMatches<'a>, } -impl<'a> App<'a> { +impl<'a> Ctx<'a> { pub fn new( config: &'a Config, account: &'a Account, diff --git a/src/flag/cli.rs b/src/flag/cli.rs index abddb38..4a94c31 100644 --- a/src/flag/cli.rs +++ b/src/flag/cli.rs @@ -2,7 +2,7 @@ use clap; use error_chain::error_chain; use log::debug; -use crate::{app::App, imap::model::ImapConnector, msg::cli::uid_arg}; +use crate::{ctx::Ctx, imap::model::ImapConnector, msg::cli::uid_arg}; error_chain! { links { @@ -42,8 +42,8 @@ pub fn flag_subcmds<'a>() -> Vec> { )] } -pub fn flag_matches(app: &App) -> Result { - if let Some(matches) = app.arg_matches.subcommand_matches("set") { +pub fn flag_matches(ctx: &Ctx) -> Result { + if let Some(matches) = ctx.arg_matches.subcommand_matches("set") { debug!("set command matched"); let uid = matches.value_of("uid").unwrap(); @@ -52,14 +52,14 @@ pub fn flag_matches(app: &App) -> Result { let flags = matches.value_of("flags").unwrap(); debug!("flags: {}", flags); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.set_flags(app.mbox, uid, flags)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.set_flags(ctx.mbox, uid, flags)?; imap_conn.logout(); return Ok(true); } - if let Some(matches) = app.arg_matches.subcommand_matches("add") { + if let Some(matches) = ctx.arg_matches.subcommand_matches("add") { debug!("add command matched"); let uid = matches.value_of("uid").unwrap(); @@ -68,14 +68,14 @@ pub fn flag_matches(app: &App) -> Result { let flags = matches.value_of("flags").unwrap(); debug!("flags: {}", flags); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.add_flags(app.mbox, uid, flags)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.add_flags(ctx.mbox, uid, flags)?; imap_conn.logout(); return Ok(true); } - if let Some(matches) = app.arg_matches.subcommand_matches("remove") { + if let Some(matches) = ctx.arg_matches.subcommand_matches("remove") { debug!("remove command matched"); let uid = matches.value_of("uid").unwrap(); @@ -84,8 +84,8 @@ pub fn flag_matches(app: &App) -> Result { let flags = matches.value_of("flags").unwrap(); debug!("flags: {}", flags); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.remove_flags(app.mbox, uid, flags)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.remove_flags(ctx.mbox, uid, flags)?; imap_conn.logout(); return Ok(true); diff --git a/src/imap/cli.rs b/src/imap/cli.rs index e85632e..72b4588 100644 --- a/src/imap/cli.rs +++ b/src/imap/cli.rs @@ -2,7 +2,7 @@ use clap; use error_chain::error_chain; use log::debug; -use crate::{app::App, imap::model::ImapConnector}; +use crate::{ctx::Ctx, imap::model::ImapConnector}; error_chain! { links { @@ -37,28 +37,28 @@ pub fn imap_subcmds<'a>() -> Vec> { ] } -pub fn imap_matches(app: &App) -> Result { - if let Some(matches) = app.arg_matches.subcommand_matches("notify") { +pub fn imap_matches(ctx: &Ctx) -> Result { + if let Some(matches) = ctx.arg_matches.subcommand_matches("notify") { debug!("notify command matched"); let keepalive = clap::value_t_or_exit!(matches.value_of("keepalive"), u64); debug!("keepalive: {}", &keepalive); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.notify(&app, keepalive)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.notify(&ctx, keepalive)?; imap_conn.logout(); return Ok(true); } - if let Some(matches) = app.arg_matches.subcommand_matches("watch") { + if let Some(matches) = ctx.arg_matches.subcommand_matches("watch") { debug!("watch command matched"); let keepalive = clap::value_t_or_exit!(matches.value_of("keepalive"), u64); debug!("keepalive: {}", &keepalive); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.watch(&app, keepalive)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.watch(&ctx, keepalive)?; imap_conn.logout(); return Ok(true); diff --git a/src/imap/model.rs b/src/imap/model.rs index 6e1e238..5e50c7a 100644 --- a/src/imap/model.rs +++ b/src/imap/model.rs @@ -4,7 +4,7 @@ use log::{debug, trace}; use native_tls::{self, TlsConnector, TlsStream}; use std::{collections::HashSet, iter::FromIterator, net::TcpStream}; -use crate::{app::App, config::model::Account, flag::model::Flag, msg::model::Msg}; +use crate::{config::model::Account, ctx::Ctx, flag::model::Flag, msg::model::Msg}; error_chain! { links { @@ -102,11 +102,11 @@ impl<'a> ImapConnector<'a> { Ok(uids) } - pub fn notify(&mut self, app: &App, keepalive: u64) -> Result<()> { - debug!("examine mailbox: {}", &app.mbox); + pub fn notify(&mut self, ctx: &Ctx, keepalive: u64) -> Result<()> { + debug!("examine mailbox: {}", &ctx.mbox); self.sess - .examine(&app.mbox) - .chain_err(|| format!("Could not examine mailbox `{}`", &app.mbox))?; + .examine(&ctx.mbox) + .chain_err(|| format!("Could not examine mailbox `{}`", &ctx.mbox))?; debug!("init messages hashset"); let mut msgs_set: HashSet = @@ -151,7 +151,7 @@ impl<'a> ImapConnector<'a> { let uid = fetch.uid.ok_or_else(|| { format!("Could not retrieve message {}'s UID", fetch.message) })?; - app.config.run_notify_cmd(&msg.subject, &msg.sender)?; + ctx.config.run_notify_cmd(&msg.subject, &msg.sender)?; debug!("notify message: {}", uid); trace!("message: {:?}", msg); @@ -165,11 +165,11 @@ impl<'a> ImapConnector<'a> { } } - pub fn watch(&mut self, app: &App, keepalive: u64) -> Result<()> { - debug!("examine mailbox: {}", &app.mbox); + pub fn watch(&mut self, ctx: &Ctx, keepalive: u64) -> Result<()> { + debug!("examine mailbox: {}", &ctx.mbox); self.sess - .examine(&app.mbox) - .chain_err(|| format!("Could not examine mailbox `{}`", &app.mbox))?; + .examine(&ctx.mbox) + .chain_err(|| format!("Could not examine mailbox `{}`", &ctx.mbox))?; loop { debug!("begin loop"); @@ -184,7 +184,7 @@ impl<'a> ImapConnector<'a> { }) }) .chain_err(|| "Could not start the idle mode")?; - app.config.exec_watch_cmds(&app.account)?; + ctx.config.exec_watch_cmds(&ctx.account)?; debug!("end loop"); } } diff --git a/src/lib.rs b/src/lib.rs index e52835d..672508f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ -pub mod app; pub mod comp; pub mod config; +pub mod ctx; pub mod flag; pub mod imap; pub mod input; diff --git a/src/main.rs b/src/main.rs index 6dd1ed2..50a31cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use log::{debug, error, trace}; use std::{env, path::PathBuf, process::exit}; use himalaya::{ - app::App, + ctx::Ctx, comp::cli::{comp_matches, comp_subcmds}, config::{cli::config_args, model::Config}, flag::cli::{flag_matches, flag_subcmds}, @@ -73,7 +73,7 @@ fn run() -> Result<()> { debug!("mailbox: {}", mbox); debug!("begin matching"); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let _matched = mbox_matches(&app)? || flag_matches(&app)? || imap_matches(&app)? || msg_matches(&app)?; diff --git a/src/mbox/cli.rs b/src/mbox/cli.rs index 80571ec..930a19e 100644 --- a/src/mbox/cli.rs +++ b/src/mbox/cli.rs @@ -2,7 +2,7 @@ use clap; use error_chain::error_chain; use log::{debug, trace}; -use crate::{app::App, imap::model::ImapConnector, mbox::model::Mboxes}; +use crate::{ctx::Ctx, imap::model::ImapConnector, mbox::model::Mboxes}; error_chain! { links { @@ -31,16 +31,16 @@ pub fn mbox_subcmds<'a>() -> Vec> { .about("Lists all mailboxes")] } -pub fn mbox_matches(app: &App) -> Result { - if let Some(_) = app.arg_matches.subcommand_matches("mailboxes") { +pub fn mbox_matches(ctx: &Ctx) -> Result { + if let Some(_) = ctx.arg_matches.subcommand_matches("mailboxes") { debug!("mailboxes command matched"); - let mut imap_conn = ImapConnector::new(&app.account)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; let names = imap_conn.list_mboxes()?; let mboxes = Mboxes::from(&names); debug!("found {} mailboxes", mboxes.0.len()); trace!("mailboxes: {:?}", mboxes); - app.output.print(mboxes); + ctx.output.print(mboxes); imap_conn.logout(); return Ok(true); diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 67d7d73..68f0b14 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -9,7 +9,7 @@ use std::{ }; use crate::{ - app::App, + ctx::Ctx, flag::model::Flag, imap::model::ImapConnector, input, @@ -154,41 +154,41 @@ pub fn msg_subcmds<'a>() -> Vec> { ] } -pub fn msg_matches(app: &App) -> Result { - match app.arg_matches.subcommand() { - ("attachments", Some(matches)) => msg_matches_attachments(app, matches), - ("copy", Some(matches)) => msg_matches_copy(app, matches), - ("delete", Some(matches)) => msg_matches_delete(app, matches), - ("forward", Some(matches)) => msg_matches_forward(app, matches), - ("move", Some(matches)) => msg_matches_move(app, matches), - ("read", Some(matches)) => msg_matches_read(app, matches), - ("reply", Some(matches)) => msg_matches_reply(app, matches), - ("save", Some(matches)) => msg_matches_save(app, matches), - ("search", Some(matches)) => msg_matches_search(app, matches), - ("send", Some(matches)) => msg_matches_send(app, matches), - ("write", Some(matches)) => msg_matches_write(app, matches), +pub fn msg_matches(ctx: &Ctx) -> Result { + match ctx.arg_matches.subcommand() { + ("attachments", Some(matches)) => msg_matches_attachments(ctx, matches), + ("copy", Some(matches)) => msg_matches_copy(ctx, matches), + ("delete", Some(matches)) => msg_matches_delete(ctx, matches), + ("forward", Some(matches)) => msg_matches_forward(ctx, matches), + ("move", Some(matches)) => msg_matches_move(ctx, matches), + ("read", Some(matches)) => msg_matches_read(ctx, matches), + ("reply", Some(matches)) => msg_matches_reply(ctx, matches), + ("save", Some(matches)) => msg_matches_save(ctx, matches), + ("search", Some(matches)) => msg_matches_search(ctx, matches), + ("send", Some(matches)) => msg_matches_send(ctx, matches), + ("write", Some(matches)) => msg_matches_write(ctx, matches), - ("template", Some(matches)) => Ok(tpl_matches(app, matches)?), + ("template", Some(matches)) => Ok(tpl_matches(ctx, matches)?), - ("list", opt_matches) => msg_matches_list(app, opt_matches), - (_other, opt_matches) => msg_matches_list(app, opt_matches), + ("list", opt_matches) => msg_matches_list(ctx, opt_matches), + (_other, opt_matches) => msg_matches_list(ctx, opt_matches), } } -fn msg_matches_list(app: &App, opt_matches: Option<&clap::ArgMatches>) -> Result { +fn msg_matches_list(ctx: &Ctx, opt_matches: Option<&clap::ArgMatches>) -> Result { debug!("list command matched"); let page_size: usize = opt_matches .and_then(|matches| matches.value_of("page-size").and_then(|s| s.parse().ok())) - .unwrap_or_else(|| app.config.default_page_size(&app.account)); + .unwrap_or_else(|| ctx.config.default_page_size(&ctx.account)); debug!("page size: {:?}", page_size); let page: usize = opt_matches .and_then(|matches| matches.value_of("page").unwrap().parse().ok()) .unwrap_or_default(); debug!("page: {}", &page); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msgs = imap_conn.list_msgs(&app.mbox, &page_size, &page)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msgs = imap_conn.list_msgs(&ctx.mbox, &page_size, &page)?; let msgs = if let Some(ref fetches) = msgs { Msgs::from(fetches) } else { @@ -196,19 +196,19 @@ fn msg_matches_list(app: &App, opt_matches: Option<&clap::ArgMatches>) -> Result }; trace!("messages: {:?}", msgs); - app.output.print(msgs); + ctx.output.print(msgs); imap_conn.logout(); Ok(true) } -fn msg_matches_search(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_search(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("search command matched"); let page_size: usize = matches .value_of("page-size") .and_then(|s| s.parse().ok()) - .unwrap_or(app.config.default_page_size(&app.account)); + .unwrap_or(ctx.config.default_page_size(&ctx.account)); debug!("page size: {}", &page_size); let page: usize = matches .value_of("page") @@ -243,21 +243,21 @@ fn msg_matches_search(app: &App, matches: &clap::ArgMatches) -> Result { .join(" "); debug!("query: {}", &page); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msgs = imap_conn.search_msgs(&app.mbox, &query, &page_size, &page)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msgs = imap_conn.search_msgs(&ctx.mbox, &query, &page_size, &page)?; let msgs = if let Some(ref fetches) = msgs { Msgs::from(fetches) } else { Msgs::new() }; trace!("messages: {:?}", msgs); - app.output.print(msgs); + ctx.output.print(msgs); imap_conn.logout(); Ok(true) } -fn msg_matches_read(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_read(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("read command matched"); let uid = matches.value_of("uid").unwrap(); @@ -267,30 +267,30 @@ fn msg_matches_read(app: &App, matches: &clap::ArgMatches) -> Result { let raw = matches.is_present("raw"); debug!("raw: {}", raw); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = imap_conn.read_msg(&app.mbox, &uid)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = imap_conn.read_msg(&ctx.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"); - app.output.print(msg); + ctx.output.print(msg); } else { let msg = ReadableMsg::from_bytes(&mime, &msg)?; - app.output.print(msg); + ctx.output.print(msg); } imap_conn.logout(); Ok(true) } -fn msg_matches_attachments(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_attachments(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("attachments command matched"); let uid = matches.value_of("uid").unwrap(); debug!("uid: {}", &uid); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = imap_conn.read_msg(&app.mbox, &uid)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = imap_conn.read_msg(&ctx.mbox, &uid)?; let attachments = Attachments::from_bytes(&msg)?; debug!( "{} attachment(s) found for message {}", @@ -298,9 +298,9 @@ fn msg_matches_attachments(app: &App, matches: &clap::ArgMatches) -> Result Result Result Result { +fn msg_matches_write(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("write command matched"); - let mut imap_conn = ImapConnector::new(&app.account)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; let attachments = matches .values_of("attachments") .unwrap_or_default() .map(String::from) .collect::>(); - let tpl = Tpl::new(&app); + let tpl = Tpl::new(&ctx); let content = input::open_editor_with_tpl(tpl.to_string().as_bytes())?; let mut msg = Msg::from(content); msg.attachments = attachments; @@ -339,10 +339,10 @@ fn msg_matches_write(app: &App, matches: &clap::ArgMatches) -> Result { input::PostEditChoice::Send => { debug!("sending message…"); let msg = msg.to_sendable_msg()?; - smtp::send(&app.account, &msg)?; + smtp::send(&ctx.account, &msg)?; imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?; input::remove_draft()?; - app.output.print("Message successfully sent"); + ctx.output.print("Message successfully sent"); break; } input::PostEditChoice::Edit => { @@ -354,7 +354,7 @@ fn msg_matches_write(app: &App, matches: &clap::ArgMatches) -> Result { debug!("saving to draft…"); imap_conn.append_msg("Drafts", &msg.to_vec()?, vec![Flag::Seen])?; input::remove_draft()?; - app.output.print("Message successfully saved to Drafts"); + ctx.output.print("Message successfully saved to Drafts"); break; } input::PostEditChoice::Discard => { @@ -369,7 +369,7 @@ fn msg_matches_write(app: &App, matches: &clap::ArgMatches) -> Result { Ok(true) } -fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_reply(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("reply command matched"); let uid = matches.value_of("uid").unwrap(); @@ -382,12 +382,12 @@ fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { debug!("found {} attachments", attachments.len()); trace!("attachments: {:?}", attachments); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?); + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = Msg::from(imap_conn.read_msg(&ctx.mbox, &uid)?); let tpl = if matches.is_present("reply-all") { - msg.build_reply_all_tpl(&app.config, &app.account)? + msg.build_reply_all_tpl(&ctx.config, &ctx.account)? } else { - msg.build_reply_tpl(&app.config, &app.account)? + msg.build_reply_tpl(&ctx.config, &ctx.account)? }; let content = input::open_editor_with_tpl(&tpl.to_string().as_bytes())?; @@ -400,11 +400,11 @@ fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { input::PostEditChoice::Send => { debug!("sending message…"); let msg = msg.to_sendable_msg()?; - smtp::send(&app.account, &msg)?; + smtp::send(&ctx.account, &msg)?; imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?; - imap_conn.add_flags(&app.mbox, uid, "\\Answered")?; + imap_conn.add_flags(&ctx.mbox, uid, "\\Answered")?; input::remove_draft()?; - app.output.print("Message successfully sent"); + ctx.output.print("Message successfully sent"); break; } input::PostEditChoice::Edit => { @@ -416,7 +416,7 @@ fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { debug!("saving to draft…"); imap_conn.append_msg("Drafts", &msg.to_vec()?, vec![Flag::Seen])?; input::remove_draft()?; - app.output.print("Message successfully saved to Drafts"); + ctx.output.print("Message successfully saved to Drafts"); break; } input::PostEditChoice::Discard => { @@ -432,7 +432,7 @@ fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { Ok(true) } -fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_forward(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("forward command matched"); let uid = matches.value_of("uid").unwrap(); @@ -445,9 +445,9 @@ fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { debug!("found {} attachments", attachments.len()); trace!("attachments: {:?}", attachments); - 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 mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = Msg::from(imap_conn.read_msg(&ctx.mbox, &uid)?); + let tpl = msg.build_forward_tpl(&ctx.config, &ctx.account)?; let content = input::open_editor_with_tpl(&tpl.to_string().as_bytes())?; let mut msg = Msg::from(content); msg.attachments = attachments; @@ -458,10 +458,10 @@ fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { input::PostEditChoice::Send => { debug!("sending message…"); let msg = msg.to_sendable_msg()?; - smtp::send(&app.account, &msg)?; + smtp::send(&ctx.account, &msg)?; imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?; input::remove_draft()?; - app.output.print("Message successfully sent"); + ctx.output.print("Message successfully sent"); break; } input::PostEditChoice::Edit => { @@ -473,7 +473,7 @@ fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { debug!("saving to draft…"); imap_conn.append_msg("Drafts", &msg.to_vec()?, vec![Flag::Seen])?; input::remove_draft()?; - app.output.print("Message successfully saved to Drafts"); + ctx.output.print("Message successfully saved to Drafts"); break; } input::PostEditChoice::Discard => { @@ -489,7 +489,7 @@ fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { Ok(true) } -fn msg_matches_copy(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_copy(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("copy command matched"); let uid = matches.value_of("uid").unwrap(); @@ -497,13 +497,13 @@ fn msg_matches_copy(app: &App, matches: &clap::ArgMatches) -> Result { let target = matches.value_of("target").unwrap(); debug!("target: {}", &target); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?); + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = Msg::from(imap_conn.read_msg(&ctx.mbox, &uid)?); let mut flags = msg.flags.deref().to_vec(); flags.push(Flag::Seen); imap_conn.append_msg(target, &msg.raw, flags)?; debug!("message {} successfully copied to folder `{}`", uid, target); - app.output.print(format!( + ctx.output.print(format!( "Message {} successfully copied to folder `{}`", uid, target )); @@ -512,7 +512,7 @@ fn msg_matches_copy(app: &App, matches: &clap::ArgMatches) -> Result { Ok(true) } -fn msg_matches_move(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_move(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("move command matched"); let uid = matches.value_of("uid").unwrap(); @@ -520,44 +520,44 @@ fn msg_matches_move(app: &App, matches: &clap::ArgMatches) -> Result { let target = matches.value_of("target").unwrap(); debug!("target: {}", &target); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = Msg::from(imap_conn.read_msg(&app.mbox, &uid)?); + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = Msg::from(imap_conn.read_msg(&ctx.mbox, &uid)?); let mut flags = msg.flags.to_vec(); flags.push(Flag::Seen); imap_conn.append_msg(target, &msg.raw, flags)?; - imap_conn.add_flags(&app.mbox, uid, "\\Seen \\Deleted")?; + imap_conn.add_flags(&ctx.mbox, uid, "\\Seen \\Deleted")?; debug!("message {} successfully moved to folder `{}`", uid, target); - app.output.print(format!( + ctx.output.print(format!( "Message {} successfully moved to folder `{}`", uid, target )); - imap_conn.expunge(&app.mbox)?; + imap_conn.expunge(&ctx.mbox)?; imap_conn.logout(); Ok(true) } -fn msg_matches_delete(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_delete(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("delete command matched"); let uid = matches.value_of("uid").unwrap(); debug!("uid: {}", &uid); - let mut imap_conn = ImapConnector::new(&app.account)?; - imap_conn.add_flags(&app.mbox, uid, "\\Seen \\Deleted")?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + imap_conn.add_flags(&ctx.mbox, uid, "\\Seen \\Deleted")?; debug!("message {} successfully deleted", uid); - app.output + ctx.output .print(format!("Message {} successfully deleted", uid)); - imap_conn.expunge(&app.mbox)?; + imap_conn.expunge(&ctx.mbox)?; imap_conn.logout(); Ok(true) } -fn msg_matches_send(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_send(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("send command matched"); - let mut imap_conn = ImapConnector::new(&app.account)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; let msg = if atty::is(Stream::Stdin) { matches @@ -576,20 +576,20 @@ fn msg_matches_send(app: &App, matches: &clap::ArgMatches) -> Result { }; let msg = Msg::from(msg.to_string()); let msg = msg.to_sendable_msg()?; - smtp::send(&app.account, &msg)?; + smtp::send(&ctx.account, &msg)?; imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?; imap_conn.logout(); Ok(true) } -fn msg_matches_save(app: &App, matches: &clap::ArgMatches) -> Result { +fn msg_matches_save(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("save command matched"); - let mut imap_conn = ImapConnector::new(&app.account)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; let msg = matches.value_of("message").unwrap(); let msg = Msg::from(msg.to_string()); - imap_conn.append_msg(&app.mbox, &msg.to_vec()?, vec![Flag::Seen])?; + imap_conn.append_msg(&ctx.mbox, &msg.to_vec()?, vec![Flag::Seen])?; imap_conn.logout(); Ok(true) diff --git a/src/msg/tpl/cli.rs b/src/msg/tpl/cli.rs index 351a6d6..41ad1f7 100644 --- a/src/msg/tpl/cli.rs +++ b/src/msg/tpl/cli.rs @@ -5,7 +5,7 @@ use log::{debug, trace}; use mailparse; use std::io::{self, BufRead}; -use crate::{app::App, imap::model::ImapConnector, msg::tpl::model::Tpl}; +use crate::{ctx::Ctx, imap::model::ImapConnector, msg::tpl::model::Tpl}; error_chain! { links { @@ -107,11 +107,11 @@ pub fn tpl_args<'a>() -> Vec> { ] } -pub fn tpl_matches(app: &App, matches: &clap::ArgMatches) -> Result { +pub fn tpl_matches(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { match matches.subcommand() { - ("new", Some(matches)) => tpl_matches_new(app, matches), - ("reply", Some(matches)) => tpl_matches_reply(app, matches), - ("forward", Some(matches)) => tpl_matches_forward(app, matches), + ("new", Some(matches)) => tpl_matches_new(ctx, matches), + ("reply", Some(matches)) => tpl_matches_reply(ctx, matches), + ("forward", Some(matches)) => tpl_matches_forward(ctx, matches), // TODO: find a way to show the help message for template subcommand _ => Err("Subcommand not found".into()), @@ -176,51 +176,51 @@ fn override_tpl_with_args(tpl: &mut Tpl, matches: &clap::ArgMatches) { }; } -fn tpl_matches_new(app: &App, matches: &clap::ArgMatches) -> Result { +fn tpl_matches_new(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("new command matched"); - let mut tpl = Tpl::new(&app); + let mut tpl = Tpl::new(&ctx); override_tpl_with_args(&mut tpl, &matches); trace!("tpl: {:?}", tpl); - app.output.print(tpl); + ctx.output.print(tpl); Ok(true) } -fn tpl_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result { +fn tpl_matches_reply(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("reply command matched"); let uid = matches.value_of("uid").unwrap(); debug!("uid: {}", uid); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = &imap_conn.read_msg(&app.mbox, &uid)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = &imap_conn.read_msg(&ctx.mbox, &uid)?; let msg = mailparse::parse_mail(&msg)?; let mut tpl = if matches.is_present("reply-all") { - Tpl::reply(&app, &msg) + Tpl::reply(&ctx, &msg) } else { - Tpl::reply_all(&app, &msg) + Tpl::reply_all(&ctx, &msg) }; override_tpl_with_args(&mut tpl, &matches); trace!("tpl: {:?}", tpl); - app.output.print(tpl); + ctx.output.print(tpl); Ok(true) } -fn tpl_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result { +fn tpl_matches_forward(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("forward command matched"); let uid = matches.value_of("uid").unwrap(); debug!("uid: {}", uid); - let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = &imap_conn.read_msg(&app.mbox, &uid)?; + let mut imap_conn = ImapConnector::new(&ctx.account)?; + let msg = &imap_conn.read_msg(&ctx.mbox, &uid)?; let msg = mailparse::parse_mail(&msg)?; - let mut tpl = Tpl::forward(&app, &msg); + let mut tpl = Tpl::forward(&ctx, &msg); override_tpl_with_args(&mut tpl, &matches); trace!("tpl: {:?}", tpl); - app.output.print(tpl); + ctx.output.print(tpl); Ok(true) } diff --git a/src/msg/tpl/model.rs b/src/msg/tpl/model.rs index 08ae8ce..3111e09 100644 --- a/src/msg/tpl/model.rs +++ b/src/msg/tpl/model.rs @@ -3,7 +3,7 @@ use mailparse::{self, MailHeaderMap}; use serde::Serialize; use std::{collections::HashMap, fmt}; -use crate::{app::App, msg::model::Msg}; +use crate::{ctx::Ctx, msg::model::Msg}; error_chain! {} @@ -17,24 +17,24 @@ pub struct Tpl { } impl Tpl { - pub fn new(app: &App) -> Self { + pub fn new(ctx: &Ctx) -> Self { let mut headers = HashMap::new(); - headers.insert("From".to_string(), app.config.address(app.account)); + headers.insert("From".to_string(), ctx.config.address(ctx.account)); headers.insert("To".to_string(), String::new()); headers.insert("Subject".to_string(), String::new()); Self { headers, body: None, - signature: app.config.signature(app.account), + signature: ctx.config.signature(ctx.account), } } - pub fn reply(app: &App, msg: &mailparse::ParsedMail) -> Self { + pub fn reply(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { let parsed_headers = msg.get_headers(); let mut headers = HashMap::new(); - headers.insert("From".to_string(), app.config.address(app.account)); + headers.insert("From".to_string(), ctx.config.address(ctx.account)); let to = parsed_headers .get_first_value("reply-to") @@ -68,15 +68,15 @@ impl Tpl { Self { headers, body: Some(body), - signature: app.config.signature(&app.account), + signature: ctx.config.signature(&ctx.account), } } - pub fn reply_all(app: &App, msg: &mailparse::ParsedMail) -> Self { + pub fn reply_all(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { let parsed_headers = msg.get_headers(); let mut headers = HashMap::new(); - let from: lettre::message::Mailbox = app.config.address(app.account).parse().unwrap(); + let from: lettre::message::Mailbox = ctx.config.address(ctx.account).parse().unwrap(); headers.insert("From".to_string(), from.to_string()); let to = parsed_headers @@ -143,15 +143,15 @@ impl Tpl { Self { headers, body: Some(body), - signature: app.config.signature(&app.account), + signature: ctx.config.signature(&ctx.account), } } - pub fn forward(app: &App, msg: &mailparse::ParsedMail) -> Self { + pub fn forward(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { let parsed_headers = msg.get_headers(); let mut headers = HashMap::new(); - headers.insert("From".to_string(), app.config.address(app.account)); + headers.insert("From".to_string(), ctx.config.address(ctx.account)); headers.insert("To".to_string(), String::new()); let subject = parsed_headers .get_first_value("subject") @@ -170,7 +170,7 @@ impl Tpl { Self { headers, body: Some(body), - signature: app.config.signature(&app.account), + signature: ctx.config.signature(&ctx.account), } } @@ -223,8 +223,8 @@ impl fmt::Display for Tpl { #[cfg(test)] mod tests { use crate::{ - app::App, config::model::{Account, Config}, + ctx::Ctx, msg::tpl::model::Tpl, output::model::Output, }; @@ -245,7 +245,7 @@ mod tests { let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let tpl = Tpl::new(&app); assert_eq!( @@ -271,7 +271,7 @@ mod tests { let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let tpl = Tpl::new(&app); assert_eq!( @@ -296,7 +296,7 @@ mod tests { let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) @@ -326,7 +326,7 @@ mod tests { let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) @@ -355,7 +355,7 @@ mod tests { let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Message-Id: 1\r Content-Type: text/plain\r @@ -398,7 +398,7 @@ Subject: Re: Test let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) @@ -427,7 +427,7 @@ Subject: Re: Test let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) @@ -457,7 +457,7 @@ Subject: Re: Test let output = Output::new("plain"); let mbox = String::new(); let arg_matches = clap::ArgMatches::new(); - let app = App::new(&config, &account, &output, &mbox, &arg_matches); + let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", )