Added authorization (shitcode)

This commit is contained in:
Listum 2023-11-08 02:08:09 +03:00
parent 7f08163710
commit 501899bc02
3 changed files with 91 additions and 4 deletions

69
src/authorization.rs Normal file
View File

@ -0,0 +1,69 @@
use std::fs::File;
use std::io::{Read, Write};
fn write(data: Vec<i64>) -> Result<(), Box<dyn std::error::Error>>{
match File::create("auth") {
Ok(mut file) => {
for num in data {
file.write_all(format!("{}\n", num).as_bytes())?; }
}
Err(_) => {}
}
Ok(())
}
fn read() -> Result<Vec<i64>, Box<dyn std::error::Error>>{
let mut file = File::open("auth")?;
let mut content = String::new();
file.read_to_string(&mut content)?;
let ids: Vec<i64> = content
.lines()
.filter_map(|line| line.parse().ok())
.collect();
Ok(ids)
}
pub fn add(id: i64) -> Result<(), Box<dyn std::error::Error>>{
if check(id) {} else {
match read() {
Ok(ids) => {
let mut array: Vec<i64> = 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<bool> = 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<dyn std::error::Error>> {
let mut file = File::create("auth")?;
let vec: Option<Vec<i64>> = Some(Vec::from([id]));
for num in vec.unwrap() {
file.write_all(format!("{}\n", num).as_bytes())?;
}
Ok(())
}

View File

@ -1,6 +1,7 @@
mod telegram;
mod build;
mod search;
mod authorization;
#[tokio::main]
async fn main() {

View File

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