mx-sanebot: add command that can show torrent status
This commit is contained in:
parent
afc916c9f8
commit
1c5a7b72ea
|
@ -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<Request, ()> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user