diff --git a/pkgs/mx-sanebot/src/msg_handler.rs b/pkgs/mx-sanebot/src/msg_handler.rs index 31715509..013c4503 100644 --- a/pkgs/mx-sanebot/src/msg_handler.rs +++ b/pkgs/mx-sanebot/src/msg_handler.rs @@ -1,4 +1,7 @@ +use std::borrow::ToOwned; use std::fmt; +use std::process; +use std::str; pub struct MessageHandler; @@ -14,10 +17,10 @@ impl MessageHandler { fn parse_msg(&self, msg: &str) -> Result { let msg = msg.trim(); - if msg == "!help" { - Ok(Request::Help) - } else { - Err(()) + match msg { + "!help" => Ok(Request::Help), + "!bt" => Ok(Request::Bt), + _ => Err(()) } } } @@ -25,24 +28,41 @@ impl MessageHandler { enum Request { Help, + Bt, } impl Request { fn evaluate(self) -> Response { match self { - Request::Help => Response::About, + Request::Help => Response::Help, + Request::Bt => Response::Bt( + process::Command::new("sane-bt-show") + .output() + .ok() + .and_then(|output| + str::from_utf8(&output.stdout).ok().map(ToOwned::to_owned) + ).unwrap_or_else(|| + "failed to retrieve torrent status".to_owned()) + ), } } } enum Response { - About, + Help, + Bt(String), } impl fmt::Display for Response { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "commands:\n")?; - write!(f, " !help => show this message\n")?; + match self { + Response::Help => { + write!(f, "commands:\n")?; + write!(f, " !help => show this message\n")?; + write!(f, " !bt => show torrent statuses\n")?; + }, + Response::Bt(stdout) => write!(f, "{}", stdout)? + } Ok(()) } }