diff options
-rw-r--r-- | Cargo.lock | 38 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 57 | ||||
-rw-r--r-- | src/message.rs | 9 |
4 files changed, 77 insertions, 28 deletions
@@ -2,6 +2,7 @@ name = "rust-pushbullet" version = "0.1.0" dependencies = [ + "notify-rust 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", "websocket 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -17,6 +18,11 @@ dependencies = [ ] [[package]] +name = "ansi_term" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "bitflags" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -27,6 +33,16 @@ version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "clap" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "cookie" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -38,6 +54,14 @@ dependencies = [ ] [[package]] +name = "dbus" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "gcc" version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -134,6 +158,15 @@ dependencies = [ ] [[package]] +name = "notify-rust" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 1.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "dbus 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "num_cpus" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -201,6 +234,11 @@ dependencies = [ ] [[package]] +name = "strsim" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "tempdir" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -7,3 +7,4 @@ authors = ["David Li <li.davidm96@gmail.com>"] websocket = "~0.12.2" rustc-serialize = "0.3" toml = "0.1" +notify-rust = "3.0" diff --git a/src/main.rs b/src/main.rs index ddf4feb..d904f6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ -use std::process::Command; use std::fs::File; use std::io::prelude::*; +extern crate notify_rust; extern crate toml; extern crate websocket; extern crate rustc_serialize; -use rustc_serialize::{Decodable}; -use rustc_serialize::json::{Json, Decoder}; +use notify_rust::Notification; mod message; use message::{Message, Push}; @@ -16,11 +15,18 @@ fn main() { use websocket::client::request::Url; use websocket::Client; - let mut cfg_file = File::open("config.toml").unwrap(); + let mut cfg_file = File::open("config.toml").expect("Could not find config.toml."); let mut s = String::new(); - cfg_file.read_to_string(&mut s); + cfg_file.read_to_string(&mut s).expect("Could not read config.toml"); let cfg = toml::Parser::new(s.as_ref()).parse().unwrap(); - let token = cfg.get("pushbullet").unwrap().as_table().unwrap().get("token").expect("Could not find 'token' in config.toml").as_str().unwrap(); + let cfg_pb = cfg.get("pushbullet") + .expect("Could not find [pushbullet] section in config.") + .as_table() + .expect("Config should contain a [pushbullet] section."); + let token = cfg_pb.get("token") + .expect("[pushbullet] section should contain 'token'") + .as_str() + .expect("'token' should be a string"); let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token); let url = Url::parse(wss_url.as_ref()).unwrap(); @@ -44,28 +50,23 @@ fn main() { return; } websocket::Message::Text(message) => { - let json = Json::from_str(message.as_ref()).unwrap(); - let mut decoder = Decoder::new(json); - let msg = Decodable::decode(&mut decoder).unwrap(); - println!("Got {:?}", msg); - match msg { - Message::Nop => {} - Message::Push(Push::Mirror { - notification_id, - title, - body, - application_name, - .. - }) => { - let _ = Command::new("notify-send") - .arg(title) - .arg(body) - .arg("-t") - .arg("8000") - .status().unwrap(); - } - Message::Push(Push::Dismissal { notification_id }) => { - + let msg = Message::parse(message.as_ref()); + if let Some(msg) = msg { + match msg { + Message::Push(Push::Mirror { + title, + body, + application_name, + .. + }) => { + let title = format!("{}: {}", application_name, title); + Notification::new() + .body(body.as_ref()) + .summary(title.as_ref()) + .show() + .unwrap(); + } + _ => {} } } } diff --git a/src/message.rs b/src/message.rs index c0a766c..0617c4d 100644 --- a/src/message.rs +++ b/src/message.rs @@ -23,6 +23,15 @@ pub enum Message { Push(Push) } +impl Message { + pub fn parse(message: &str) -> Option<Message> { + let json = json::Json::from_str(message).unwrap(); + let mut decoder = json::Decoder::new(json); + let result : Result<Message, _> = Decodable::decode(&mut decoder); + result.ok() + } +} + impl Decodable for Push { fn decode<D: Decoder>(d: &mut D) -> Result<Push, D::Error> { d.read_struct("root", 0, |d| { |