yggdrasil/src/dbtool/search.rs

112 lines
3.8 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::str::FromStr;
use anyhow::{bail, Result};
use log::{info, warn};
use yggdrasil::*;
use yggdrasil::structs::account::Account;
use crate::dbtool::Args;
use crate::util::structs::profile::Profile;
pub struct Search {}
impl Search {
// Account id
async fn search_accountid(db: &Database, query: Vec<String>) -> Result<()> {
Ok(for q in query {
let id = match i64::from_str(&q) {
Ok(id) => id,
Err(_) => bail!("id({q}) isn't a valid i64")
};
match Account::from_id(db, id).await {
None => warn!("Account(id = {id}) doesn't exist"),
Some(a) => info!("{a:#?}")
}
})
}
// Profile id
async fn search_profileid(db: &Database, query: Vec<String>) -> Result<()> {
Ok(for q in query {
let id = match i64::from_str(&q) {
Ok(id) => id,
Err(_) => bail!("id({q}) isn't a valid i64")
};
match Profile::from_id(db, id).await {
None => warn!("Profile(id = {id}) doesn't exist"),
Some(p) => info!("{:#?}", p.to_simple())
}
})
}
// Account name
async fn search_email(db: &Database, query: Vec<String>) -> Result<()> {
Ok(for q in query {
match Account::from_email(db, q.to_string()).await {
None => warn!("Account(email = \"{q}\") doesn't exist"),
Some(a) => info!("{a:#?}")
}
})
}
// Profile name
async fn search_name(db: &Database, query: Vec<String>) -> Result<()> {
Ok(for q in query {
match Profile::from_name(db, q.to_string()).await {
None => warn!("Profile(name = \"{q}\") doesn't exist"),
Some(p) => info!("{:#?}", p.to_simple())
}
})
}
// Profile uuid
async fn search_uuid(db: &Database, query: Vec<String>) -> Result<()> {
Ok(for q in query {
match Profile::from_uuid(db, q.to_string()).await {
None => warn!("Profile(uuid = \"{q}\") doesn't exist"),
Some(p) => info!("{:#?}", p.to_simple())
}
})
}
pub async fn exec(args: Args, db: &Database) -> Result<()> {
if args.arguments.len() < 2 { bail!("Not enough arguments. search <type> <query> [query..]\ntype: account-id | profile-id | email | name | uuid") }
let query_type = args.arguments.get(0).unwrap().to_lowercase();
let queries = args.arguments[1..args.arguments.len()].to_vec();
match query_type.as_str() {
"accountid" |
"account-id" => Self::search_accountid(db, queries).await?,
"profileid" |
"profile-id" => Self::search_profileid(db, queries).await?,
"email" => Self::search_email(db, queries).await?,
"name" => Self::search_name(db, queries).await?,
"uuid" => Self::search_uuid(db, queries).await?,
_ => bail!("Invalid type \"{query_type}\"")
}
Ok(())
}
}