1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
use std::fs::File;
use std::io::prelude::*;
extern crate notify_rust;
extern crate toml;
extern crate websocket;
extern crate rustc_serialize;
use notify_rust::Notification;
mod message;
use message::{Message, Push};
use websocket::Sender;
use websocket::Receiver;
use websocket::WebSocketStream;
use websocket::DataFrame;
use websocket::Client;
struct PBClient {
client: websocket::client::Client<websocket::dataframe::DataFrame,
websocket::client::sender::Sender<websocket::stream::WebSocketStream>,
websocket::client::receiver::Receiver<websocket::stream::WebSocketStream>>
}
impl PBClient {
fn new(token: &str) -> PBClient {
use websocket::client::request::Url;
use websocket::Client;
let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token);
let url = Url::parse(wss_url.as_ref()).unwrap();
let request = Client::connect(url).unwrap();
let response = request.send().unwrap();
response.validate().unwrap();
PBClient {
client: response.begin()
}
}
}
fn main() {
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).expect("Could not read config.toml");
let cfg = toml::Parser::new(s.as_ref()).parse().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 mut client = PBClient::new(token.as_ref());
let mut receiver = client.client.get_mut_reciever();
for message in receiver.incoming_messages() {
let message = match message {
Ok(m) => m,
Err(e) => {
println!("Error: {:?}", e);
return;
}
};
match message {
websocket::Message::Close(_) => {
return;
}
websocket::Message::Text(message) => {
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();
}
_ => {}
}
}
}
_ => {
println!("Got {:?}", message);
}
}
}
}
|