From 74e856315a85d7c93369b97efc4ffa209648285a Mon Sep 17 00:00:00 2001 From: legitnull Date: Fri, 17 Feb 2023 03:50:15 -0700 Subject: [PATCH] cargo.toml & password authentication --- Cargo.toml | 3 ++- src/main.rs | 52 ++++++++++++++++++++++++++++++++--------------- src/modules/ai.rs | 8 ++++---- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6d86bd..b98132b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ async-openai = "0.6.1" tokio = { version = "1.23.0", features = ["full"] } rand = "0.8.4" regex = "1.7.1" -config = "0.13.3" \ No newline at end of file +toml = "0.7.2" +serde = "1.0.152" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ed0bd74..201194d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,13 @@ use std::net::TcpStream; use std::time::{Instant, Duration}; use std::fs; use std::thread; -use toml::Value; 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 serde::Deserialize; mod modules { pub trait Command { fn handle(&self, message: &str) -> Vec; @@ -18,33 +18,53 @@ mod modules { pub mod kill; pub mod ai; } -use modules::ai::Ai; + +use modules::ai::Ai; // FIX THIS BS use modules::ping::PingCommand; -use modules::kill::KillCommand; +use modules::kill::KillCommand; // ... use crate::modules::Command; + +#[derive(Deserialize)] +struct Config { + server: String, + port: u16, + nick: String, + password: String, + channels: Vec, +} + fn main() { let rt = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap(); +// PUT CONFIG IN A SEPRATE FILE IE: CONFIG.TOML + // read the contents of the config file into a string + let config_str = std::fs::read_to_string("config.toml").unwrap(); + + // parse the string into a toml::Value + let config_value = config_str.parse::().unwrap(); + + // deserialize the value into a Config struct + let config: Config = config_value.try_into().unwrap(); + + let stream = TcpStream::connect(format!("{}:{}", config.server, config.port)).unwrap();; // DONT DO DRUGS YOU WILL END UP LIKE ME let connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); -// PUT CONFIG IN A SEPRATE FILE IE: YAML, JSON, CONFIG, TOML12 - let stream = TcpStream::connect("ircd.chat:6697").unwrap(); // setup tor & custom masking - let mut ssl_stream = connector.connect("ircd.chat", stream).unwrap(); - - let nick_command = "NICK g1r\r\n"; // set SASL Passwords User Nicks - let user_command = "USER g1r 0 * :g1r\r\n"; - - - let channels = vec!["#tcpdirect", "#macros"]; // CHANNELS - let join_command = format!("JOIN {}\r\n", channels.join(",")); + let mut ssl_stream = connector.connect(&config.server, stream).unwrap(); + let nick_command = format!("NICK {}_\r\n", config.nick); //setup passwords + let user_command = format!("USER {} 0 * :{}\r\n", config.nick, config.nick); + ssl_stream.write_all(nick_command.as_bytes()).unwrap(); + ssl_stream.write_all(user_command.as_bytes()).unwrap(); + let identify_command = format!("PRIVMSG NickServ :IDENTIFY {} {}\r\n", config.nick, config.password); + ssl_stream.write(identify_command.as_bytes()).unwrap(); + let channels = config.channels.join(","); + let join_command = format!("JOIN {}\r\n", channels); + let admin_users = vec!["s4d", "s4d[m]"]; // ADMINS let ignored_users = vec!["maple", "aibird", "proffesserOak"]; // IGNORED // ... - ssl_stream.write_all(nick_command.as_bytes()).unwrap(); - ssl_stream.write_all(user_command.as_bytes()).unwrap(); ssl_stream.write_all(join_command.as_bytes()).unwrap(); let mut buf = [0; 512]; diff --git a/src/modules/ai.rs b/src/modules/ai.rs index a75c499..d06b091 100644 --- a/src/modules/ai.rs +++ b/src/modules/ai.rs @@ -9,7 +9,7 @@ impl Command for Ai { fn handle(&self, message: &str) -> Vec { let mut responses = Vec::new(); if message.starts_with(":") && message.contains("PRIVMSG ") && message.contains("g1r") { - let channel = message.split("PRIVMSG ").nth(1).and_then(|s| s.splitn(2, ' ').next()).unwrap(); + let channel = message.split("PRIVMSG ").nth(1).and_then(|s| s.splitn(2, ' ').next()).unwrap(); // set the response to varible let user_message = "The following is a chat log:\n".to_owned() + message.split(&format!("PRIVMSG {} :", channel.to_string())).nth(1).unwrap() + "\nRespond funny, completely insane, and hyperactive as you are chatting as GIR from Invader Zim: \n\n"; let parts: Vec<&str> = message.splitn(2, ' ').collect(); let username = parts[0].trim_start_matches(':').split("!").next().unwrap(); @@ -23,7 +23,7 @@ impl Command for Ai { } } async fn ai(user_message: &str, username: &str, channel: &str) -> Vec { - let api_key = "sk-*"; // set this from config and add rotatation + let api_key = "sk-*"; // set this from config let client = Client::new().with_api_key(api_key); println!("[?] PROMPT: {}: {}", username, user_message); @@ -39,9 +39,9 @@ async fn ai(user_message: &str, username: &str, channel: &str) -> Vec { .await .unwrap(); 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(); let response_text = regex.replace_all(response_text, "").trim().to_string(); let response_lines = response_text.split("\n").filter(|line| !line.trim().is_empty()); let mut responses = Vec::new();