Skip to content

Commit

Permalink
move watch command from folder to envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Dec 14, 2023
1 parent 7fccdd8 commit d6bf407
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ envelope.list.datetime-local-tz = true
# Override the backend used for listing envelopes.
envelope.list.backend = "imap"

# Override the backend used for sending messages.
message.send.backend = "smtp"
# Send notification on receiving new envelopes
envelope.watch.received.notify.summary = "📬 New message from {sender}"

# Available placeholders: id, subject, sender, sender.name,
# sender.address, recipient, recipient.name, recipient.address.
envelope.watch.received.notify.body = "{subject}"

# Send notification when receiving new messages
message.watch.received.notify.summary = "📬 New message from {sender}"
message.watch.received.notify.body = "{subject}"
# Shell commands can also be executed when envelopes change
# envelope.watch.any.cmd = "mbsync -a"

# Shell commands can also be executed
# message.watch.received.cmd = "mbsync -a"
# Override the backend used for sending messages.
message.send.backend = "smtp"

# IMAP config
imap.host = "localhost"
Expand Down
4 changes: 2 additions & 2 deletions src/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ impl TomlAccountConfig {
}

pub fn get_watch_message_kind(&self) -> Option<&BackendKind> {
self.message
self.envelope
.as_ref()
.and_then(|msg| msg.watch.as_ref())
.and_then(|envelope| envelope.watch.as_ref())
.and_then(|watch| watch.backend.as_ref())
.or_else(|| self.backend.as_ref())
}
Expand Down
19 changes: 10 additions & 9 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use email::imap::{ImapSessionBuilder, ImapSessionSync};
use email::smtp::{SmtpClientBuilder, SmtpClientSync};
use email::{
account::config::AccountConfig,
email::watch::{imap::WatchImapEmails, maildir::WatchMaildirEmails},
envelope::{
get::{imap::GetEnvelopeImap, maildir::GetEnvelopeMaildir},
list::{imap::ListEnvelopesImap, maildir::ListEnvelopesMaildir},
watch::{imap::WatchImapEnvelopes, maildir::WatchMaildirEnvelopes},
Id, SingleId,
},
flag::{
Expand Down Expand Up @@ -358,26 +358,27 @@ impl BackendBuilder {

match toml_account_config.backend {
Some(BackendKind::Maildir) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
ctx.maildir.as_ref().and_then(WatchMaildirEmails::new)
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
ctx.maildir.as_ref().and_then(WatchMaildirEnvelopes::new)
});
}
Some(BackendKind::MaildirForSync) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
ctx.maildir_for_sync
.as_ref()
.and_then(WatchMaildirEmails::new)
.and_then(WatchMaildirEnvelopes::new)
});
}
#[cfg(feature = "imap")]
Some(BackendKind::Imap) => {
backend_builder = backend_builder
.with_watch_emails(|ctx| ctx.imap.as_ref().and_then(WatchImapEmails::new));
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
ctx.imap.as_ref().and_then(WatchImapEnvelopes::new)
});
}
#[cfg(feature = "notmuch")]
Some(BackendKind::Notmuch) => {
backend_builder = backend_builder.with_watch_emails(|ctx| {
ctx.notmuch.as_ref().and_then(WatchNotmuchEmails::new)
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
ctx.notmuch.as_ref().and_then(WatchNotmuchEnvelopes::new)
});
}
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ impl TomlConfig {
}),
envelope: config.envelope.map(|c| EnvelopeConfig {
list: c.list.map(|c| c.remote),
watch: c.watch.map(|c| c.remote),
}),
message: config.message.map(|c| MessageConfig {
read: c.read.map(|c| c.remote),
write: c.write.map(|c| c.remote),
send: c.send.map(|c| c.remote),
watch: c.watch.map(|c| c.remote),
}),
sync: config.sync,
#[cfg(feature = "pgp")]
Expand Down
4 changes: 2 additions & 2 deletions src/email/envelope/command/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
/// This command allows you to list all envelopes included in the
/// given folder.
#[derive(Debug, Parser)]
pub struct EnvelopeListCommand {
pub struct ListEnvelopesCommand {
#[command(flatten)]
pub folder: FolderNameOptionalArg,

Expand All @@ -44,7 +44,7 @@ pub struct EnvelopeListCommand {
pub account: AccountNameFlag,
}

impl EnvelopeListCommand {
impl ListEnvelopesCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing envelope list command");

Expand Down
9 changes: 7 additions & 2 deletions src/email/envelope/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pub mod list;
pub mod watch;

use anyhow::Result;
use clap::Subcommand;

use crate::{config::TomlConfig, printer::Printer};

use self::list::EnvelopeListCommand;
use self::{list::ListEnvelopesCommand, watch::WatchEnvelopesCommand};

/// Manage envelopes.
///
Expand All @@ -16,13 +17,17 @@ use self::list::EnvelopeListCommand;
#[derive(Debug, Subcommand)]
pub enum EnvelopeSubcommand {
#[command(alias = "lst")]
List(EnvelopeListCommand),
List(ListEnvelopesCommand),

#[command()]
Watch(WatchEnvelopesCommand),
}

impl EnvelopeSubcommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
match self {
Self::List(cmd) => cmd.execute(printer, config).await,
Self::Watch(cmd) => cmd.execute(printer, config).await,
}
}
}
22 changes: 12 additions & 10 deletions src/folder/command/watch.rs → src/email/envelope/command/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use log::info;

use crate::{
account::arg::name::AccountNameFlag, backend::Backend, cache::arg::disable::CacheDisableFlag,
config::TomlConfig, folder::arg::name::FolderNameArg, printer::Printer,
config::TomlConfig, folder::arg::name::FolderNameOptionalArg, printer::Printer,
};

/// Watch a folder for changes.
/// Watch envelopes for changes.
///
/// This command allows you to watch a new folder using the given
/// name.
/// This command allows you to watch a folder and execute hooks when
/// changes occur on envelopes.
#[derive(Debug, Parser)]
pub struct FolderWatchCommand {
pub struct WatchEnvelopesCommand {
#[command(flatten)]
pub folder: FolderNameArg,
pub folder: FolderNameOptionalArg,

#[command(flatten)]
pub cache: CacheDisableFlag,
Expand All @@ -23,9 +23,9 @@ pub struct FolderWatchCommand {
pub account: AccountNameFlag,
}

impl FolderWatchCommand {
impl WatchEnvelopesCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing folder watch command");
info!("executing envelopes watch command");

let folder = &self.folder.name;

Expand All @@ -35,8 +35,10 @@ impl FolderWatchCommand {
.into_account_configs(some_account_name, self.cache.disable)?;
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?;

printer.print_log(format!("Start watching folder {folder} for changes…"))?;
printer.print_log(format!(
"Start watching folder {folder} for envelopes changes…"
))?;

backend.watch_emails(&folder).await
backend.watch_envelopes(&folder).await
}
}
37 changes: 31 additions & 6 deletions src/email/envelope/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::backend::BackendKind;

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct EnvelopeConfig {
pub list: Option<EnvelopeListConfig>,
pub get: Option<EnvelopeGetConfig>,
pub list: Option<ListEnvelopesConfig>,
pub watch: Option<WatchEnvelopesConfig>,
pub get: Option<GetEnvelopeConfig>,
}

impl EnvelopeConfig {
Expand All @@ -21,19 +22,43 @@ impl EnvelopeConfig {
kinds.extend(get.get_used_backends());
}

if let Some(watch) = &self.watch {
kinds.extend(watch.get_used_backends());
}

kinds
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct EnvelopeListConfig {
pub struct ListEnvelopesConfig {
pub backend: Option<BackendKind>,

#[serde(flatten)]
pub remote: email::envelope::list::config::EnvelopeListConfig,
}

impl EnvelopeListConfig {
impl ListEnvelopesConfig {
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut kinds = HashSet::default();

if let Some(kind) = &self.backend {
kinds.insert(kind);
}

kinds
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct WatchEnvelopesConfig {
pub backend: Option<BackendKind>,

#[serde(flatten)]
pub remote: email::envelope::watch::config::WatchEnvelopeConfig,
}

impl WatchEnvelopesConfig {
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut kinds = HashSet::default();

Expand All @@ -46,11 +71,11 @@ impl EnvelopeListConfig {
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct EnvelopeGetConfig {
pub struct GetEnvelopeConfig {
pub backend: Option<BackendKind>,
}

impl EnvelopeGetConfig {
impl GetEnvelopeConfig {
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut kinds = HashSet::default();

Expand Down
26 changes: 0 additions & 26 deletions src/email/message/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub struct MessageConfig {
pub copy: Option<MessageCopyConfig>,
#[serde(rename = "move")]
pub move_: Option<MessageMoveConfig>,

pub watch: Option<WatchMessageConfig>,
}

impl MessageConfig {
Expand Down Expand Up @@ -44,10 +42,6 @@ impl MessageConfig {
kinds.extend(move_.get_used_backends());
}

if let Some(watch) = &self.watch {
kinds.extend(watch.get_used_backends());
}

kinds
}
}
Expand Down Expand Up @@ -162,23 +156,3 @@ impl MessageMoveConfig {
kinds
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct WatchMessageConfig {
pub backend: Option<BackendKind>,

#[serde(flatten)]
pub remote: email::message::watch::config::WatchMessageConfig,
}

impl WatchMessageConfig {
pub fn get_used_backends(&self) -> HashSet<&BackendKind> {
let mut kinds = HashSet::default();

if let Some(kind) = &self.backend {
kinds.insert(kind);
}

kinds
}
}
7 changes: 1 addition & 6 deletions src/folder/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod delete;
mod expunge;
mod list;
mod purge;
mod watch;

use anyhow::Result;
use clap::Subcommand;
Expand All @@ -12,7 +11,7 @@ use crate::{config::TomlConfig, printer::Printer};

use self::{
create::FolderCreateCommand, delete::FolderDeleteCommand, expunge::FolderExpungeCommand,
list::FolderListCommand, purge::FolderPurgeCommand, watch::FolderWatchCommand,
list::FolderListCommand, purge::FolderPurgeCommand,
};

/// Manage folders.
Expand All @@ -27,9 +26,6 @@ pub enum FolderSubcommand {
#[command(alias = "lst")]
List(FolderListCommand),

#[command()]
Watch(FolderWatchCommand),

#[command()]
Expunge(FolderExpungeCommand),

Expand All @@ -45,7 +41,6 @@ impl FolderSubcommand {
match self {
Self::Create(cmd) => cmd.execute(printer, config).await,
Self::List(cmd) => cmd.execute(printer, config).await,
Self::Watch(cmd) => cmd.execute(printer, config).await,
Self::Expunge(cmd) => cmd.execute(printer, config).await,
Self::Purge(cmd) => cmd.execute(printer, config).await,
Self::Delete(cmd) => cmd.execute(printer, config).await,
Expand Down

0 comments on commit d6bf407

Please sign in to comment.