From 501899bc0283d5728c8c2567b5fbb6e7ebad2d56 Mon Sep 17 00:00:00 2001 From: Listum Date: Wed, 8 Nov 2023 02:08:09 +0300 Subject: [PATCH] Added authorization (shitcode) --- src/authorization.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/telegram.rs | 25 +++++++++++++--- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/authorization.rs diff --git a/src/authorization.rs b/src/authorization.rs new file mode 100644 index 0000000..b453f80 --- /dev/null +++ b/src/authorization.rs @@ -0,0 +1,69 @@ +use std::fs::File; +use std::io::{Read, Write}; + +fn write(data: Vec) -> Result<(), Box>{ + match File::create("auth") { + Ok(mut file) => { + for num in data { + file.write_all(format!("{}\n", num).as_bytes())?; } + } + Err(_) => {} + } + Ok(()) +} + +fn read() -> Result, Box>{ + let mut file = File::open("auth")?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let ids: Vec = content + .lines() + .filter_map(|line| line.parse().ok()) + .collect(); + Ok(ids) +} + +pub fn add(id: i64) -> Result<(), Box>{ + if check(id) {} else { + match read() { + Ok(ids) => { + let mut array: Vec = ids; + array.push(id); + write(array)?; + } + Err(e) => { + println!("Error reading from file: {}, recreating auth", e); + init(id)?; + } + }} + Ok(()) +} + +pub fn check(id: i64) -> bool { + let mut valid: Option = None; + match read() { + Ok(ids) => { + let array: &[i64] = ids.as_slice(); + if array.contains(&id) { + valid = Some(true); + } else { + valid = Some(false); + } + } + Err(e) => { + println!("Error reading from file: {}, recreating auth", e); + init(id).expect("init"); + } + } + return valid.unwrap(); +} + + +fn init(id: i64) -> Result<(), Box> { + let mut file = File::create("auth")?; + let vec: Option> = Some(Vec::from([id])); + for num in vec.unwrap() { + file.write_all(format!("{}\n", num).as_bytes())?; + } + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6b2bba8..6e35da1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod telegram; mod build; mod search; +mod authorization; #[tokio::main] async fn main() { diff --git a/src/telegram.rs b/src/telegram.rs index a89e598..dfc952b 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -2,6 +2,7 @@ use teloxide::{prelude::*, utils::command::BotCommands}; use crate::build::{clone, copy, build, delete, repo_add}; use crate::search; use std::env; +use crate::authorization::{add, check}; pub async fn main(bot: Bot){ Commands::repl(bot, answer).await; @@ -13,11 +14,12 @@ enum Commands{ #[command(description = "Build package.")] Upload(String), #[command(description = "Search packages", parse_with = "split")] - Search{ pkg: String, num: u8 } + Search{ pkg: String, num: u8 }, + Auth(String) } async fn answer(bot: Bot, msg: Message, cmd: Commands) -> ResponseResult<()> { match cmd { - Commands::Upload(pkg) => { + Commands::Upload(pkg) => { if check(msg.chat.id.0) { let default_dir = env::current_dir()?; let pkg_dir = format!("pkgs/{}", pkg); let repo_dir = format!("repo/"); @@ -43,7 +45,7 @@ async fn answer(bot: Bot, msg: Message, cmd: Commands) -> ResponseResult<()> { bot.send_message(msg.chat.id, copy).await?; let delete = match delete(pkg_dir) { - Ok(..) => { format!("Sources deleted")}, + Ok(..) => { format!("Sources deleted") }, Err(e) => { format!("Sources deletion error: {}", e) } }; bot.send_message(msg.chat.id, delete).await?; @@ -55,11 +57,26 @@ async fn answer(bot: Bot, msg: Message, cmd: Commands) -> ResponseResult<()> { }; bot.send_message(msg.chat.id, repo_add).await?; env::set_current_dir(default_dir)?; - + } else { bot.send_message(msg.chat.id, "This bot is private, motherfucker.").await?; } } Commands::Search{pkg, num} => { bot.send_message(msg.chat.id, search::search(pkg, num).await).await?; } + Commands::Auth(pass) => { + match env::var("PASS") { + Ok(value) => { + if pass == value { + bot.send_message(msg.chat.id, format!("Authorized!")).await?; + add(msg.chat.id.0).expect("add"); + } else { + bot.send_message(msg.chat.id, format!("This bot is private, motherfucker.")).await?; + } + }, + Err(_) => { + bot.send_message(msg.chat.id, format!("The password variable is not set.")).await?; + } + } + } } Ok(()) }