impl invaders

This commit is contained in:
legitnull 2023-02-17 23:47:35 -07:00
parent 2767b192f9
commit 130c63d33f
5 changed files with 101 additions and 13 deletions

View File

@ -13,3 +13,4 @@ rand = "0.8.4"
regex = "1.7.1"
toml = "0.7.2"
serde = "1.0.152"
scoped_threadpool = "0.1.9"

View File

@ -3,6 +3,7 @@
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# TODO
- multi server
- add bots
- proxy support
- ident rand

View File

@ -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();
}
}
}

View File

@ -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<String>,
openai: String,
model: String,
accents: String,
@ -61,7 +60,7 @@ async fn ai(user_message: &str, username: &str, channel: &str) -> Vec<String> {
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"#,

86
src/modules/invade.rs Normal file
View File

@ -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<String>,
server: String,
port: u16,
}
pub struct InvadeCommand;
impl Command for InvadeCommand {
fn handle(&self, message: &str) -> Vec<String> {
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::<u32>().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::<Value>().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
}
}