Tweaks and cleanup

This commit is contained in:
Travis Burtrum 2017-08-12 23:59:43 -04:00
parent e72fbf5f75
commit 5112be68a9
3 changed files with 28 additions and 24 deletions

2
Cargo.lock generated
View File

@ -1,5 +1,5 @@
[root] [root]
name = "sendxmpp-rs" name = "sendxmpp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"gumdrop 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gumdrop 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,7 +1,10 @@
[package] [package]
name = "sendxmpp-rs" name = "sendxmpp"
version = "0.1.0" version = "0.1.0"
authors = ["moparisthebest <admin@moparisthebest.com>"] authors = ["moparisthebest <admin@moparisthebest.com>"]
description = "A fun game where you guess what number the computer has chosen."
license = "GPL-3.0+"
readme = "README.md"
[dependencies] [dependencies]
xmpp = { git = "https://gitlab.com/lumi/xmpp-rs.git" } xmpp = { git = "https://gitlab.com/lumi/xmpp-rs.git" }
@ -10,4 +13,4 @@ toml = "0.4.4"
serde_derive = "1.0.11" serde_derive = "1.0.11"
serde = "1.0.11" serde = "1.0.11"
gumdrop = "0.3.0" gumdrop = "0.3.0"
gumdrop_derive = "0.3.0" gumdrop_derive = "0.3.0"

View File

@ -16,6 +16,7 @@ use std::env;
use std::iter::Iterator; use std::iter::Iterator;
use std::io::{Read, stdin}; use std::io::{Read, stdin};
use std::fs::File; use std::fs::File;
use std::path::Path;
use std::env::args; use std::env::args;
use gumdrop::Options; use gumdrop::Options;
@ -26,15 +27,20 @@ struct Config {
password: String, password: String,
} }
fn parse_cfg(path: &str) -> Option<Config> { fn parse_cfg<P: AsRef<Path>>(path: P) -> Option<Config> {
File::open(path).and_then(|mut f| { match File::open(path) {
let mut input = String::new(); Ok(mut f) => {
f.read_to_string(&mut input)?; let mut input = String::new();
match toml::from_str(&input) { match f.read_to_string(&mut input) {
Ok(toml) => Ok(toml), Ok(_) => match toml::from_str(&input) {
Err(error) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, error)) Ok(toml) => Some(toml),
Err(_) => None
},
Err(_) => None
}
} }
}).ok() Err(_) => None
}
} }
#[derive(Default, Options)] #[derive(Default, Options)]
@ -77,20 +83,15 @@ fn main() {
return; return;
} }
let recipients: Vec<Jid> = opts.recipients.iter().map(|s| s.parse::<Jid>().unwrap()).collect(); let recipients: Vec<Jid> = opts.recipients.iter().map(|s| s.parse::<Jid>().expect("invalid recipient jid")).collect();
let cfg = match opts.config { let cfg = match opts.config {
Some(config) => parse_cfg(&config).unwrap(), Some(config) => parse_cfg(&config).expect("provided config cannot be found/parsed"),
None => { None => parse_cfg(env::home_dir().expect("cannot find home directory").join(".config/sendxmpp.toml"))
let mut home_cfg = String::new(); .or_else(|| parse_cfg("/etc/sendxmpp/sendxmpp.toml")).expect("valid config file not found")
home_cfg += env::home_dir().unwrap().to_str().unwrap();
home_cfg += "/.config/sendxmpp.toml";
parse_cfg(&home_cfg).unwrap_or_else(|| parse_cfg("/etc/sendxmpp/sendxmpp.toml").unwrap())
}
}; };
let jid: Jid = cfg.jid.parse().unwrap(); let jid: Jid = cfg.jid.parse().expect("invalid jid in config file");
let mut data = String::new(); let mut data = String::new();
stdin().read_to_string(&mut data).expect("error reading from stdin"); stdin().read_to_string(&mut data).expect("error reading from stdin");
@ -98,17 +99,17 @@ fn main() {
let mut client = ClientBuilder::new(jid) let mut client = ClientBuilder::new(jid)
.password(cfg.password) .password(cfg.password)
.connect() .connect()
.unwrap(); .expect("client cannot connect");
client.register_plugin(MessagingPlugin::new()); client.register_plugin(MessagingPlugin::new());
for recipient in recipients { for recipient in recipients {
client.plugin::<MessagingPlugin>().send_message(&recipient, &data).unwrap(); client.plugin::<MessagingPlugin>().send_message(&recipient, &data).expect("error sending message");
} }
thread::spawn(|| { thread::spawn(|| {
thread::sleep(Duration::from_millis(4000)); thread::sleep(Duration::from_millis(4000));
std::process::exit(0); std::process::exit(0);
}); });
client.main().unwrap() client.main().expect("error during client main")
} }