From 130c63d33fff4051b7538cdcd6a700ecee301c1a Mon Sep 17 00:00:00 2001 From: legitnull Date: Fri, 17 Feb 2023 23:47:35 -0700 Subject: [PATCH] impl invaders --- Cargo.toml | 3 +- README.md | 1 + src/main.rs | 19 +++++----- src/modules/ai.rs | 5 +-- src/modules/invade.rs | 86 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/modules/invade.rs diff --git a/Cargo.toml b/Cargo.toml index b98132b..5ad5040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ tokio = { version = "1.23.0", features = ["full"] } rand = "0.8.4" regex = "1.7.1" toml = "0.7.2" -serde = "1.0.152" \ No newline at end of file +serde = "1.0.152" +scoped_threadpool = "0.1.9" \ No newline at end of file diff --git a/README.md b/README.md index 957c140..98c991b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA # TODO +- multi server - add bots - proxy support - ident rand diff --git a/src/main.rs b/src/main.rs index 718f587..58ef021 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,8 @@ use std::io::prelude::*; use std::net::TcpStream; -use std::time::{Instant, Duration}; -use std::fs; -use std::thread; -use std::io::{self, Write}; -use regex::Regex; -use rand::{thread_rng, Rng}; -use openssl::ssl::{SslMethod, SslConnector, SslStream}; -use async_openai::{Client, types::{CreateCompletionRequestArgs, ResponseFormat}}; -use toml::{from_str, Value}; +use std::io::Write; +use openssl::ssl::{SslMethod, SslConnector}; +use toml::Value; use serde::Deserialize; mod modules { pub trait Command { @@ -17,10 +11,12 @@ mod modules { pub mod ping; pub mod kill; pub mod ai; + pub mod invade; } use modules::ai::Ai; // FIX THIS BS use modules::ping::PingCommand; +use modules::invade::InvadeCommand; use modules::kill::KillCommand; // ... use crate::modules::Command; @@ -86,6 +82,7 @@ fn main() { // MODULES let ping_command = PingCommand; let kill_command = KillCommand; + let invade_command = InvadeCommand; let ai = Ai; // ADMIN MODULES @@ -104,6 +101,10 @@ fn main() { for response in kill_command.handle(message) { ssl_stream.write_all(response.as_bytes()).unwrap(); } + } else if message.contains(":%invade") { + for response in invade_command.handle(message) { + ssl_stream.write_all(response.as_bytes()).unwrap(); + } } } diff --git a/src/modules/ai.rs b/src/modules/ai.rs index 5312410..22ef96a 100644 --- a/src/modules/ai.rs +++ b/src/modules/ai.rs @@ -2,12 +2,11 @@ use async_openai::{Client, types::{CreateCompletionRequestArgs}}; use regex::Regex; use crate::modules::Command; -use toml::{from_str, Value}; +use toml::Value; use serde::Deserialize; #[derive(Deserialize)] struct Config { nick: String, - channels: Vec, openai: String, model: String, accents: String, @@ -61,7 +60,7 @@ async fn ai(user_message: &str, username: &str, channel: &str) -> Vec { println!("[+] RESPONSE: {}", chat_response.choices.first().unwrap().text); //modify regex for varible username ie G1R g1r GIR gir but as handle nick for bots let response_text = &chat_response.choices.first().unwrap().text; - let regex = Regex::new(r#""|[gG][1iI][rR]:\s*|[mM][eE]:?\s"#).unwrap(); + let regex = Regex::new(r#""|[gG][1iI][rR]:\s*|[mM][eE]:?\s"#).unwrap(); // THIS IS FUCKING UP EVERYTHING //let nick = &config.nick; //let regex_str = format!( // r#""|[{}{}{}]|\b[gG][1iI][rR]:\s*|\b[mM][eE]:?\s"#, diff --git a/src/modules/invade.rs b/src/modules/invade.rs new file mode 100644 index 0000000..fc1296e --- /dev/null +++ b/src/modules/invade.rs @@ -0,0 +1,86 @@ +use crate::modules::Command; + +use std::cell::RefCell; +use std::io::{BufRead, BufReader, Write}; +use std::net::TcpStream; +use std::rc::Rc; + +use openssl::ssl::{SslConnector, SslMethod}; +use serde::Deserialize; +use toml::Value; + + + +#[derive(Clone, Deserialize)] +struct Config { + invaders: Vec, + server: String, + port: u16, +} + +pub struct InvadeCommand; + +impl Command for InvadeCommand { + fn handle(&self, message: &str) -> Vec { + let mut response = vec![]; + + if message.contains("PRIVMSG") && message.contains(":%invade") { + let parts: Vec<&str> = message.split_whitespace().collect(); + let num_bots = parts[4].parse::().unwrap_or(1) as usize; + let channel = parts[2]; + let invadechannel = parts[5]; + let scream = parts[6]; + let config_str = std::fs::read_to_string("config.toml").unwrap(); + let config_value = config_str.parse::().unwrap(); + let config: Config = config_value.try_into().unwrap(); + + for invader in &config.invaders { + let thread_channel = invadechannel.to_string(); + let thread_invader = invader.to_string(); + let config_clone = config.clone(); + let screaming = scream.to_string(); + + + std::thread::spawn(move || { + let stream = TcpStream::connect(format!("{}:{}", config_clone.server, config_clone.port)).unwrap(); + let connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); + let ssl_stream = connector.connect(&config_clone.server, stream).unwrap(); + let ssl_stream = Rc::new(RefCell::new(ssl_stream)); + let nick_command = format!("NICK {}\r\n", thread_invader); + let user_command = format!("USER {} 0 * :{}\r\n", thread_invader, thread_invader); + ssl_stream.borrow_mut().write_all(nick_command.as_bytes()).unwrap(); + ssl_stream.borrow_mut().write_all(user_command.as_bytes()).unwrap(); + let join_command = format!("JOIN {} \r\n", thread_channel); + ssl_stream.borrow_mut().write_all(join_command.as_bytes()).unwrap(); + let msg = format!("PRIVMSG {} :{}\r\n", thread_channel, screaming); + ssl_stream.borrow_mut().write_all(msg.as_bytes()).unwrap(); + + + loop { + let mut ssl_stream_ref = ssl_stream.borrow_mut(); + let mut reader = BufReader::new(&mut *ssl_stream_ref); + + let mut message = String::new(); + match reader.read_line(&mut message) { + Ok(0) => break, + Ok(_) => { + if message.starts_with("PING") { + let response = message.replace("PING", "PONG"); + ssl_stream.borrow_mut().write_all(response.as_bytes()).unwrap(); + } + } + Err(e) => { + eprintln!("Error reading from server: {}", e); + break; + } + } + } + }); + } + + response.push(format!("PRIVMSG {} :INVADING with {} bots..\r\n", channel, num_bots)); + } + + response + } +}