You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
75 lines
2.3 KiB
use std::fs::OpenOptions;
|
|
use std::io::{Read, Write};
|
|
use std::time::SystemTime;
|
|
use std::io::{prelude::*, BufReader};
|
|
use std::net::TcpStream;
|
|
use std::str;
|
|
use std::thread;
|
|
use regex::Regex;
|
|
|
|
|
|
pub fn send_cmd(mut stream: &TcpStream, cmd: &str, msg: String) -> Result<usize, std::io::Error> {
|
|
let mut cmd = cmd.to_string();
|
|
cmd.push_str(" ");
|
|
cmd.push_str(&msg);
|
|
println!("sending: {}", cmd.trim());
|
|
stream.write(cmd.as_bytes())
|
|
}
|
|
|
|
|
|
pub fn write_log(log: &str, first_line: bool) -> Result<(), std::io::Error> {
|
|
if std::fs::File::open(crate::IRC_LOG).is_err() {
|
|
let _ = std::fs::write(crate::IRC_LOG, "");
|
|
}
|
|
let mut f = OpenOptions::new()
|
|
.append(true)
|
|
.open(crate::IRC_LOG)
|
|
.unwrap();
|
|
if first_line {
|
|
let _ = writeln!(f, "-------------writing to file-------------");
|
|
}
|
|
if let Err(e) = writeln!(f, "{}", log.to_string()) {
|
|
eprintln!("[!] Failed to write to file: {}", e);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn gen_name() -> String {
|
|
let secs = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
|
return format!("wowboombox{}", secs);
|
|
}
|
|
|
|
pub fn read_irc_log(active_channel: String) -> String {
|
|
let mut s: String = String::new();
|
|
let f = std::fs::File::open(crate::IRC_LOG); // ("unable to open irc log");
|
|
if f.is_err() { return "".to_owned() }
|
|
if active_channel.len() > 0 {
|
|
let re = Regex::new(format!(r".*PRIVMSG {} :.*", active_channel).as_str()).unwrap();
|
|
let reader = BufReader::new(f.unwrap());
|
|
for line in reader.lines() {
|
|
if line.is_err() { continue }
|
|
let msg = line.unwrap();
|
|
if re.is_match(msg.as_str()) {
|
|
s = s + "\n" + msg.as_str();
|
|
}
|
|
}
|
|
} else {
|
|
let _ = f.unwrap().read_to_string(&mut s);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
|
|
pub fn send_tune(&self) -> std::io::Result<()> {
|
|
let stream = connect()?;
|
|
thread::spawn(move || send_cmd(&stream, "PRIVMSG", "#wownero-music :!tune\r\n".to_owned()));
|
|
Ok(())
|
|
}
|
|
|
|
pub fn run(&self) -> Result<TcpStream, std::io::Error> {
|
|
let send_stream = connect()?;
|
|
let recv_stream = send_stream.try_clone()?;
|
|
// https://doc.rust-lang.org/nightly/std/thread/
|
|
thread::spawn(move || receive(&recv_stream).expect("error setting up recv_stream"));
|
|
Ok(send_stream)
|
|
} |