From ef1133f04579625dae76b386a50edb68b9d8cff4 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Sun, 22 Nov 2026 11:18:40 -0600 Subject: [PATCH] Add optional desktop notifications to owly receive Add `--notify` to `owly receive`, connecting to `org.freedesktop.Notifications` over session D-Bus with `zbus` (no new crate dependencies). Incoming `ASK-2` requests present Accept/Decline notification actions alongside the terminal prompt, cleanly close notifications answered in the terminal, handle closed-stdin background invocation without stalling the 15 s prompt, sanitize untrusted metadata per SEC-4, and notify when files, URLs, or errors finish. --- crates/owly-cli/src/airdrop.rs | 130 +++++++++++- crates/owly-cli/src/main.rs | 1 + crates/owly-cli/src/notify.rs | 357 +++++++++++++++++++++++++++++++++ 3 files changed, 480 insertions(+), 8 deletions(-) create mode 100644 crates/owly-cli/src/notify.rs diff --git a/crates/owly-cli/src/airdrop.rs b/crates/owly-cli/src/airdrop.rs index e36ee90..c042c5a 100644 --- a/crates/owly-cli/src/airdrop.rs +++ b/crates/owly-cli/src/airdrop.rs @@ -30,6 +30,7 @@ use owlyd::api::{self, LinkInfo, RadioProxy}; use tokio::io::AsyncBufReadExt; use crate::bluez::{BluezAdvertiser, Registration}; +use crate::notify::{Notifier, sanitize_text}; /// Apple's receivers listen here (MDNS-1), and an AWDL peer with no mDNS /// is tried here (DISC-5). @@ -64,6 +65,9 @@ pub struct ReceiveArgs { /// Accept every request without asking (ASK-3). #[arg(long)] accept: bool, + /// Send desktop notifications (with Accept/Decline actions) over D-Bus. + #[arg(long)] + notify: bool, /// Don't announce over mDNS, for when another responder does. #[arg(long)] no_mdns: bool, @@ -647,10 +651,22 @@ async fn run_receiver( dir, port, accept, + notify, no_mdns, flags, .. } = args; + let notifier = if notify { + match Notifier::connect().await { + Ok(n) => Some(n), + Err(e) => { + eprintln!("not sending desktop notifications: {e}"); + None + } + } + } else { + None + }; let state = std::env::var_os("XDG_STATE_HOME") .map(PathBuf::from) .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/state"))) @@ -724,28 +740,126 @@ async fn run_receiver( request.accept(); continue; } + let mut prompt = match ¬ifier { + Some(n) => match n + .prompt_request( + &request.ask.sender_computer_name, + &request.ask.sender_model_name, + &request.ask.offer, + Duration::from_secs(15), + ) + .await + { + Ok(p) => Some(p), + Err(e) => { + eprintln!("notification prompt failed: {e}"); + None + } + }, + None => None, + }; eprint!("accept? [y/N] "); // ASK-2: the receiver declines by itself after 15 s. - let answer = tokio::time::timeout(Duration::from_secs(15), stdin.next_line()).await; - match answer { - Ok(Ok(Some(line))) if line.trim().eq_ignore_ascii_case("y") => request.accept(), - Ok(_) => request.decline(), - Err(_) => eprintln!("\ntimed out"), + let deadline = tokio::time::Instant::now() + Duration::from_secs(15); + let mut stdin_open = true; + enum PromptOutcome { + Stdin(bool), + Notify(bool), + TimedOut, + } + let outcome = loop { + tokio::select! { + () = tokio::time::sleep_until(deadline) => break PromptOutcome::TimedOut, + line = stdin.next_line(), if stdin_open => { + match line { + Ok(Some(line)) => { + break PromptOutcome::Stdin(line.trim().eq_ignore_ascii_case("y")); + } + Ok(None) if prompt.is_some() => stdin_open = false, + Ok(None) | Err(_) => break PromptOutcome::Stdin(false), + } + } + decision = async { + match prompt.as_mut() { + Some(p) => p.wait().await, + None => std::future::pending().await, + } + }, if prompt.is_some() => { + match decision { + Some(yes) => break PromptOutcome::Notify(yes), + None => prompt = None, + } + } + } + }; + match outcome { + PromptOutcome::Stdin(true) => { + request.accept(); + if let Some(p) = prompt { + p.close().await; + } + } + PromptOutcome::Stdin(false) => { + request.decline(); + if let Some(p) = prompt { + p.close().await; + } + } + PromptOutcome::Notify(true) => { + eprintln!("accepted (notification)"); + request.accept(); + if let Some(p) = prompt { + p.close().await; + } + } + PromptOutcome::Notify(false) => { + eprintln!("declined (notification)"); + request.decline(); + if let Some(p) = prompt { + p.close().await; + } + } + PromptOutcome::TimedOut => { + eprintln!("\ntimed out"); + if let Some(p) = prompt { + p.close().await; + } + } } } ReceiverEvent::Withdrawn { .. } => eprintln!("the request was withdrawn"), // SEC-4: shown, never opened. ReceiverEvent::Urls { urls, .. } => { - for u in urls { + for u in &urls { println!("{u:?}"); } + if let Some(n) = ¬ifier { + let first = urls.first().map_or("", String::as_str); + n.notify_info("AirDrop link received", first, Duration::from_secs(5)) + .await; + } } ReceiverEvent::Received { paths, .. } => { - for p in paths { + for p in &paths { println!("{}", p.display()); } + if let Some(n) = ¬ifier { + let body = crate::notify::format_received_summary(&paths); + n.notify_info("AirDrop received", &body, Duration::from_secs(5)) + .await; + } + } + ReceiverEvent::Failed { reason, .. } => { + eprintln!("transfer failed: {reason}"); + if let Some(n) = ¬ifier { + n.notify_info( + "AirDrop failed", + &sanitize_text(&reason, 200), + Duration::from_secs(5), + ) + .await; + } } - ReceiverEvent::Failed { reason, .. } => eprintln!("transfer failed: {reason}"), } } } diff --git a/crates/owly-cli/src/main.rs b/crates/owly-cli/src/main.rs index d2bcdd3..492b831 100644 --- a/crates/owly-cli/src/main.rs +++ b/crates/owly-cli/src/main.rs @@ -19,6 +19,7 @@ mod airdrop; mod bluez; mod capture; mod doctor; +mod notify; mod radio; mod test_card; mod wifi; diff --git a/crates/owly-cli/src/notify.rs b/crates/owly-cli/src/notify.rs new file mode 100644 index 0000000..c077993 --- /dev/null +++ b/crates/owly-cli/src/notify.rs @@ -0,0 +1,357 @@ +//! Desktop notifications (`org.freedesktop.Notifications`) for `owly receive`. + +use std::collections::HashMap; +use std::path::PathBuf; +use std::time::Duration; + +use futures_util::StreamExt; +use owly_airdrop::proto::messages::Offer; +use zbus::zvariant::Value; + +#[zbus::proxy( + interface = "org.freedesktop.Notifications", + default_service = "org.freedesktop.Notifications", + default_path = "/org/freedesktop/Notifications" +)] +trait Notifications { + #[allow(clippy::too_many_arguments)] + fn notify( + &self, + app_name: &str, + replaces_id: u32, + app_icon: &str, + summary: &str, + body: &str, + actions: &[&str], + hints: HashMap<&str, Value<'_>>, + expire_timeout: i32, + ) -> zbus::Result; + + fn close_notification(&self, id: u32) -> zbus::Result<()>; + + #[zbus(signal)] + fn action_invoked(&self, id: u32, action_key: String) -> zbus::Result<()>; + + #[zbus(signal)] + fn notification_closed(&self, id: u32, reason: u32) -> zbus::Result<()>; +} + +/// A connection to the session notification daemon (`org.freedesktop.Notifications`). +pub struct Notifier { + proxy: NotificationsProxy<'static>, +} + +/// An active Accept/Decline prompt notification awaiting user interaction. +pub struct PendingPrompt { + proxy: NotificationsProxy<'static>, + id: u32, + actions: ActionInvokedStream, + actions_done: bool, + closed: NotificationClosedStream, + closed_done: bool, +} + +impl Notifier { + pub async fn connect() -> Result { + let conn = zbus::Connection::session() + .await + .map_err(|e| format!("session D-Bus: {e}"))?; + let proxy = NotificationsProxy::new(&conn) + .await + .map_err(|e| format!("org.freedesktop.Notifications: {e}"))?; + Ok(Self { proxy }) + } + + pub async fn notify_info(&self, summary: &str, body: &str, timeout: Duration) { + let summary = escape_markup(&sanitize_text(summary, 120)); + let body = escape_markup(&sanitize_text(body, 300)); + let timeout_ms = i32::try_from(timeout.as_millis()).unwrap_or(i32::MAX); + let _ = self + .proxy + .notify( + "owly", + 0, + "network-wireless", + &summary, + &body, + &[], + HashMap::new(), + timeout_ms, + ) + .await; + } + + pub async fn prompt_request( + &self, + sender: &str, + model: &str, + offer: &Offer, + timeout: Duration, + ) -> Result { + let actions = self + .proxy + .receive_action_invoked() + .await + .map_err(|e| format!("ActionInvoked stream: {e}"))?; + let closed = self + .proxy + .receive_notification_closed() + .await + .map_err(|e| format!("NotificationClosed stream: {e}"))?; + let summary = escape_markup(&format!("AirDrop from {}", sanitize_text(sender, 64))); + let body = escape_markup(&format_offer_summary(sender, model, offer)); + let timeout_ms = i32::try_from(timeout.as_millis()).unwrap_or(i32::MAX); + let id = self + .proxy + .notify( + "owly", + 0, + "network-wireless", + &summary, + &body, + &["accept", "Accept", "decline", "Decline"], + HashMap::new(), + timeout_ms, + ) + .await + .map_err(|e| format!("Notify: {e}"))?; + Ok(PendingPrompt { + proxy: self.proxy.clone(), + id, + actions, + actions_done: false, + closed, + closed_done: false, + }) + } +} + +impl PendingPrompt { + pub async fn wait(&mut self) -> Option { + while !self.actions_done || !self.closed_done { + tokio::select! { + ev = self.actions.next(), if !self.actions_done => { + match ev { + Some(sig) => { + if let Ok(args) = sig.args() + && args.id == self.id + && let Some(decision) = parse_action_key(&args.action_key) + { + return Some(decision); + } + } + None => self.actions_done = true, + } + } + ev = self.closed.next(), if !self.closed_done => { + match ev { + Some(sig) => { + if let Ok(args) = sig.args() && args.id == self.id { + return Some(false); + } + } + None => self.closed_done = true, + } + } + } + } + None + } + + pub async fn close(self) { + let _ = self.proxy.close_notification(self.id).await; + } +} + +/// SEC-4: strips control characters and caps length before handing untrusted +/// sender metadata to the desktop notification daemon. +pub fn sanitize_text(input: &str, max_chars: usize) -> String { + let mut cleaned = String::with_capacity(input.len().min(max_chars)); + let mut prev_space = true; + for ch in input.chars() { + let normalized = if ch.is_control() { ' ' } else { ch }; + if normalized.is_whitespace() { + if !prev_space { + cleaned.push(' '); + prev_space = true; + } + } else { + cleaned.push(normalized); + prev_space = false; + } + } + let trimmed = cleaned.trim(); + let count = trimmed.chars().count(); + if count <= max_chars { + return trimmed.to_string(); + } + if max_chars == 0 { + return String::new(); + } + let mut truncated: String = trimmed.chars().take(max_chars.saturating_sub(1)).collect(); + truncated.push('…'); + truncated +} + +/// Notification bodies support XML-like markup. Escape all remotely supplied +/// text so a sender cannot inject links, images, or misleading formatting. +pub fn escape_markup(input: &str) -> String { + let mut escaped = String::with_capacity(input.len()); + for ch in input.chars() { + match ch { + '&' => escaped.push_str("&"), + '<' => escaped.push_str("<"), + '>' => escaped.push_str(">"), + '\'' => escaped.push_str("'"), + '"' => escaped.push_str("""), + _ => escaped.push(ch), + } + } + escaped +} + +pub fn format_offer_summary(sender: &str, model: &str, offer: &Offer) -> String { + let model = sanitize_text(model, 48); + let from = if model.is_empty() { + sanitize_text(sender, 48) + } else { + format!("{} ({model})", sanitize_text(sender, 48)) + }; + let what = match offer { + Offer::Files(files) => match files.as_slice() { + [] => "no files".to_string(), + [only] => match only.size { + Some(bytes) => format!("{} ({bytes} bytes)", sanitize_text(&only.name, 80)), + None => sanitize_text(&only.name, 80), + }, + many => format!( + "{} files (first: {})", + many.len(), + sanitize_text(&many[0].name, 60) + ), + }, + Offer::Urls(urls) => match urls.as_slice() { + [] => "link".to_string(), + [only] => format!("link: {}", sanitize_text(only, 120)), + many => format!( + "{} links (first: {})", + many.len(), + sanitize_text(&many[0], 80) + ), + }, + }; + sanitize_text(&format!("{from}: {what}"), 240) +} + +pub fn format_received_summary(paths: &[PathBuf]) -> String { + match paths { + [] => "AirDrop received".to_string(), + [only] => { + let name = only + .file_name() + .and_then(|n| n.to_str()) + .map_or_else(|| only.display().to_string(), str::to_string); + let name = sanitize_text(&name, 80); + match std::fs::metadata(only) { + Ok(m) if m.is_file() => format!("Saved {name} ({} bytes)", m.len()), + _ => format!("Saved {name}"), + } + } + many => format!("Saved {} files", many.len()), + } +} + +pub fn parse_action_key(key: &str) -> Option { + match key { + "accept" => Some(true), + "decline" => Some(false), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use owly_airdrop::proto::messages::AskFile; + + use super::*; + + #[test] + fn sanitize_text_strips_controls_and_truncates() { + assert_eq!(sanitize_text(" hi\x1b[31m\nthere\t ", 40), "hi [31m there"); + assert_eq!(sanitize_text("abcdef", 4), "abc…"); + assert_eq!(sanitize_text("abcd", 4), "abcd"); + } + + #[test] + fn escape_markup_renders_sender_input_as_text() { + assert_eq!( + escape_markup(r#"photo & link"#), + "<a href="https://evil.example">photo & link</a>" + ); + } + + #[test] + fn format_offer_summary_handles_files_and_urls() { + let single = Offer::Files(vec![AskFile { + name: "photo.jpg".into(), + file_type: "public.jpeg".into(), + size: Some(1234), + bom_path: "./photo.jpg".into(), + is_directory: false, + }]); + assert_eq!( + format_offer_summary("iPhone", "iPhone16,2", &single), + "iPhone (iPhone16,2): photo.jpg (1234 bytes)" + ); + + let multi = Offer::Files(vec![ + AskFile { + name: "one\x07.png".into(), + file_type: "public.png".into(), + size: None, + bom_path: "./one.png".into(), + is_directory: false, + }, + AskFile { + name: "two.png".into(), + file_type: "public.png".into(), + size: None, + bom_path: "./two.png".into(), + is_directory: false, + }, + ]); + assert_eq!( + format_offer_summary("iPad", "", &multi), + "iPad: 2 files (first: one .png)" + ); + + let urls = Offer::Urls(vec!["https://example.com/\x1b".into()]); + assert_eq!( + format_offer_summary("Mac", "Mac15,9", &urls), + "Mac (Mac15,9): link: https://example.com/" + ); + } + + #[test] + fn format_received_summary_reports_single_and_multiple_paths() { + let dir = tempfile::tempdir().unwrap(); + let file = dir.path().join("IMG_0001.jpg"); + std::fs::write(&file, b"hello world").unwrap(); + assert_eq!( + format_received_summary(std::slice::from_ref(&file)), + "Saved IMG_0001.jpg (11 bytes)" + ); + assert_eq!( + format_received_summary(&[file, dir.path().join("two.jpg")]), + "Saved 2 files" + ); + assert_eq!(format_received_summary(&[]), "AirDrop received"); + } + + #[test] + fn parse_action_key_matches_accept_and_decline() { + assert_eq!(parse_action_key("accept"), Some(true)); + assert_eq!(parse_action_key("decline"), Some(false)); + assert_eq!(parse_action_key("default"), None); + } +}