Cleanup & Fixes
This commit is contained in:
1 parent
340b8db1b9
commit
68de25838c
6 files changed
+510
-765
No files matched your search
@@ -17,6 +17,7 @@ clap.workspace = true
|
||||
url.workspace = true
|
||||
lalrpop-util = "0.22.1"
|
||||
error-stack.workspace = true
|
||||
flume.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
lalrpop = "0.22.1"
|
||||
+452
-437
@@ -1,21 +1,18 @@
|
||||
use std::future::pending;
|
||||
use banklib::{AuthenticatedMessage, BankClient, BankError, BankResult, Response};
|
||||
use banklib::config::{load_config, save_config};
|
||||
use banklib::extended::{Client, Credentials, State};
|
||||
use banklib::{AuthenticatedMessage, BankClient, BankError, BankResult, ClientAction, Response, ResponseMessage};
|
||||
use clap::Parser;
|
||||
use std::ops::{Add, Deref};
|
||||
use std::time::Duration;
|
||||
use std::io;
|
||||
use std::io::BufRead;
|
||||
use std::thread::yield_now;
|
||||
use error_stack::{report, Report};
|
||||
use tokio::io::BufReader;
|
||||
use flume::Receiver;
|
||||
use std::io::stdin;
|
||||
use std::ops::Add;
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use tokio::select;
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::metadata::LevelFilter;
|
||||
use tracing::{error, info, warn};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use url::Url;
|
||||
use banklib::config::{load_config, save_config};
|
||||
use banklib::extended::{Client, Credentials, State};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Args {
|
||||
@@ -62,7 +59,8 @@ async fn main() {
|
||||
url = arg;
|
||||
} else {
|
||||
if !config.server.last_server.is_empty() && config.general.use_last_server {
|
||||
url = Url::parse(&config.server.last_server).expect("Invalid last server url in config");
|
||||
url =
|
||||
Url::parse(&config.server.last_server).expect("Invalid last server url in config");
|
||||
} else {
|
||||
error!("Cannot use last server url - You need to provide one using --url");
|
||||
return;
|
||||
@@ -70,19 +68,31 @@ async fn main() {
|
||||
}
|
||||
let client = BankClient::connect(url.clone()).await;
|
||||
|
||||
let mut client = Client { client, config, state: State::default() };
|
||||
let mut client = Client {
|
||||
client,
|
||||
config,
|
||||
state: State::default(),
|
||||
};
|
||||
client.config.server.last_server = url.to_string();
|
||||
save_config(&client.config);
|
||||
|
||||
if client.config.general.use_default_account {
|
||||
let password = client.config.accounts.accounts.get(&client.config.accounts.default_account);
|
||||
let password = client
|
||||
.config
|
||||
.accounts
|
||||
.accounts
|
||||
.get(&client.config.accounts.default_account);
|
||||
if let Some(password) = password {
|
||||
let username = client.config.accounts.default_account.clone();
|
||||
let password = password.clone();
|
||||
try_login(&mut client, Credentials { password, username }).await;
|
||||
}
|
||||
} else if client.config.general.use_last_account {
|
||||
let password = client.config.accounts.accounts.get(&client.config.accounts.last_account);
|
||||
let password = client
|
||||
.config
|
||||
.accounts
|
||||
.accounts
|
||||
.get(&client.config.accounts.last_account);
|
||||
if let Some(password) = password {
|
||||
let username = client.config.accounts.last_account.clone();
|
||||
let password = password.clone();
|
||||
@@ -90,382 +100,27 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
let lines = read_lines();
|
||||
let messages = client.client.receiver_msg().clone();
|
||||
let actions = client.client.receiver_act().clone();
|
||||
for _ in actions.try_iter() {}
|
||||
loop {
|
||||
let line = read_line(Some(&mut client)).await;
|
||||
let mut split = line.split(' ');
|
||||
let command: &str = split.next().unwrap();
|
||||
|
||||
match command {
|
||||
"account" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument == "add" {
|
||||
let credentials = expect_credentials(split, &mut client, false).await;
|
||||
if let Some(credentials) = credentials {
|
||||
client.config.accounts.accounts.insert(credentials.username.clone(), credentials.password.clone());
|
||||
if client.config.accounts.default_account.is_empty() {
|
||||
client.config.accounts.default_account = credentials.username.clone();
|
||||
}
|
||||
save_config(&client.config);
|
||||
if ask_confirmation("Do you want to test the account now?").await {
|
||||
let old_credentials = client.state.last_credentials.clone();
|
||||
let success = try_login(&mut client, credentials.clone()).await;
|
||||
if success {
|
||||
setup_new_account(&mut client, credentials.clone(), old_credentials).await;
|
||||
} else {
|
||||
if ask_confirmation("The login was not successful - Do you want to register this account instead?").await {
|
||||
let result = client.register(credentials.clone()).await;
|
||||
if result.is_ok() {
|
||||
let result = client.login(credentials.clone()).await;
|
||||
if result.is_ok() {
|
||||
setup_new_account(&mut client, credentials, old_credentials).await;
|
||||
} else {
|
||||
handle_error(&result);
|
||||
warn!("The login was not successful but the account was most likely still registered - Please try to login again")
|
||||
}
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("You need to enter a username and password");
|
||||
}
|
||||
} else if argument == "use" {
|
||||
let username: &str = split.next().or(Some("")).unwrap();
|
||||
if client.config.accounts.accounts.contains_key(username) {
|
||||
let password = client.config.accounts.accounts.get(username).unwrap().clone();
|
||||
try_login(&mut client, Credentials { username: username.into(), password }).await;
|
||||
} else {
|
||||
println!("You need to enter a valid username")
|
||||
}
|
||||
} else if argument == "list" {
|
||||
println!("--- Accounts ---");
|
||||
let i: u8 = 0;
|
||||
for (name, _) in &client.config.accounts.accounts {
|
||||
if client.config.accounts.default_account.eq(name) {
|
||||
println!("{}. {} (Default)", i + 1, name);
|
||||
} else {
|
||||
println!("{}. {}", i + 1, name);
|
||||
}
|
||||
}
|
||||
println!("--- Accounts ---");
|
||||
} else if argument == "remove" {
|
||||
let username: &str = split.next().or(Some("")).unwrap();
|
||||
if client.config.accounts.accounts.contains_key(username) {
|
||||
client.config.accounts.accounts.remove(username);
|
||||
println!("Account removed");
|
||||
} else {
|
||||
println!("You need to enter a valid username")
|
||||
}
|
||||
} else if argument == "help" {
|
||||
println!("--- Available Commands ---");
|
||||
println!("account add <username> <password> - Add a new account");
|
||||
println!("account use [username] - Login with the default or the specified account");
|
||||
println!("account remove <username> - Remove the account from the list");
|
||||
println!("account default <username> - Make the account the default account");
|
||||
println!("account list - List available accounts");
|
||||
} else {
|
||||
println!("Invalid subcommand! Use account help for a list of valid subcommands");
|
||||
select! {
|
||||
Ok(line) = lines.recv_async() => {
|
||||
if handle_line(&mut client, &lines, line).await {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"settings" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument == "set" {
|
||||
let setting = split.next();
|
||||
let value = split.next();
|
||||
if setting.is_some() && value.is_some() {
|
||||
{
|
||||
match setting.unwrap() {
|
||||
"show_motd_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_motd_after_login = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"show_inbox_count_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_inbox_count_after_login = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"show_pm_inbox_count_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_pm_inbox_count_after_login = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"show_balance_after_payment" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_balance_after_payment = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"use_default_account" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_default_account = value;
|
||||
if value {
|
||||
client.config.general.use_last_account = false;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"use_last_account" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_last_account = value;
|
||||
if value {
|
||||
client.config.general.use_default_account = false;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
"use_last_server" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_last_server = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
error!("Invalid setting")
|
||||
}
|
||||
}
|
||||
info!("Setting changed");
|
||||
save_config(&client.config);
|
||||
}
|
||||
} else {
|
||||
println!("You need to enter a setting and value");
|
||||
}
|
||||
} else if argument == "list" {
|
||||
println!("--- Settings ---");
|
||||
println!(
|
||||
"show_motd_after_login: {}",
|
||||
client.config.general.show_motd_after_login
|
||||
);
|
||||
println!(
|
||||
"show_inbox_count_after_login: {}",
|
||||
client.config.general.show_inbox_count_after_login
|
||||
);
|
||||
println!(
|
||||
"show_pm_inbox_count_after_login: {}",
|
||||
client.config.general.show_pm_inbox_count_after_login
|
||||
);
|
||||
println!(
|
||||
"show_balance_after_payment: {}",
|
||||
client.config.general.show_balance_after_payment
|
||||
);
|
||||
println!(
|
||||
"use_default_account: {}",
|
||||
client.config.general.use_default_account
|
||||
);
|
||||
println!(
|
||||
"use_last_account: {}",
|
||||
client.config.general.use_last_account
|
||||
);
|
||||
println!(
|
||||
"use_last_server: {}",
|
||||
client.config.general.use_last_server
|
||||
);
|
||||
} else if argument == "help" {
|
||||
println!("--- Available Commands ---");
|
||||
println!(
|
||||
"settings set <setting> <value> - Set a setting to the specified value"
|
||||
);
|
||||
println!("settings list - List all settings");
|
||||
} else {
|
||||
println!("Invalid subcommand! Use settings help for a list of valid subcommands");
|
||||
}
|
||||
}
|
||||
"register" => {
|
||||
let credentials = expect_credentials(split, &mut client, true).await;
|
||||
if let Some(credentials) = credentials {
|
||||
let result = client.register(credentials.clone()).await;
|
||||
if let Some(last_credentials) = &client.state.last_credentials {
|
||||
handle_error(&client.client.set_credentials(last_credentials.username.clone(), last_credentials.password.clone()));
|
||||
}
|
||||
if let Err(error) = result {
|
||||
match error.current_context() {
|
||||
BankError::AuthenticationError => {
|
||||
error!("Username is already taken");
|
||||
if ask_confirmation("Do you want to login instead?").await {
|
||||
try_login(&mut client, credentials).await;
|
||||
}
|
||||
}
|
||||
_ => print_error(&error)
|
||||
}
|
||||
} else {
|
||||
if ask_confirmation("Do you want to login with your new account?").await {
|
||||
try_login(&mut client, credentials).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"login" => {
|
||||
let credentials = expect_credentials(split, &mut client, true).await;
|
||||
if let Some(credentials) = credentials {
|
||||
try_login(&mut client, credentials).await;
|
||||
}
|
||||
}
|
||||
"balance" => {
|
||||
let balance = client.get_balance().await;
|
||||
if let Ok(balance) = balance {
|
||||
info!("You currently have a balance of {}₿", balance);
|
||||
} else {
|
||||
handle_error(&balance);
|
||||
}
|
||||
}
|
||||
"pay" => {
|
||||
let destination = split.next();
|
||||
let amount = split.next();
|
||||
if destination.is_none() || amount.is_none() || amount.unwrap().parse::<u32>().is_err() || amount.unwrap().parse::<u32>().unwrap() <= 0 {
|
||||
error!("You need to enter a destination username and amount greater than 0");
|
||||
} else {
|
||||
let result = client.pay(destination.unwrap().into(), amount.unwrap().parse::<u32>().unwrap()).await;
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
"inbox" => {
|
||||
let size = client.get_inbox_size().await;
|
||||
if let Ok(size) = size {
|
||||
info!("--- {} Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
for i in 0..size {
|
||||
let msg = client.get_inbox_msg((i + 1) as i8).await;
|
||||
if let Ok((id, msg)) = msg {
|
||||
info!("{id}. {msg}");
|
||||
} else {
|
||||
handle_error(&msg);
|
||||
}
|
||||
}
|
||||
info!("--- {} Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
} else {
|
||||
handle_error(&size);
|
||||
}
|
||||
}
|
||||
"pm" => {
|
||||
let username = split.next();
|
||||
let message = split.next();
|
||||
if username.is_some() && message.is_some() {
|
||||
let mut message: String = message.unwrap().into();
|
||||
let mut segment = split.next();
|
||||
while segment.is_some() {
|
||||
message = message.add(" ");
|
||||
message = message.add(segment.unwrap().into());
|
||||
segment = split.next();
|
||||
}
|
||||
|
||||
let result = client.send_pm(username.unwrap().into(), message).await;
|
||||
handle_error(&result);
|
||||
} else if username.is_none() && username.is_none() {
|
||||
let size = client.get_pm_size().await;
|
||||
if let Ok(size) = size {
|
||||
info!("--- {} Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
for i in 0..size {
|
||||
let msg = client.get_pm_msg((i + 1) as i8).await;
|
||||
if let Ok((id, msg)) = msg {
|
||||
info!("{}. {}", id, msg);
|
||||
} else {
|
||||
handle_error(&msg);
|
||||
}
|
||||
}
|
||||
info!("--- {} Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
} else {
|
||||
handle_error(&size);
|
||||
}
|
||||
} else {
|
||||
error!("You need to enter a username and message or nothing to view your PMs")
|
||||
}
|
||||
}
|
||||
"delete" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
let id = split.next().or(Some("")).unwrap().parse();
|
||||
if argument == "inbox" && id.is_ok() {
|
||||
let result = client.delete_inbox_msg(id.unwrap()).await;
|
||||
handle_error(&result);
|
||||
} else if argument == "pm" && id.is_ok() {
|
||||
let result = client.delete_pm_msg(id.unwrap()).await;
|
||||
handle_error(&result);
|
||||
} else {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
}
|
||||
}
|
||||
"motd" => {
|
||||
let motd = client.get_motd().await;
|
||||
if let Ok(motd) = motd {
|
||||
println!("--- {} ---", motd)
|
||||
} else {
|
||||
handle_error(&motd);
|
||||
}
|
||||
}
|
||||
"logout" => {
|
||||
if !client.state.logged_in {
|
||||
warn!("You are already logged out")
|
||||
} else {
|
||||
let result = client.logout().await;
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
"exit" => {
|
||||
if client.state.logged_in {
|
||||
let result = client.logout().await;
|
||||
handle_error(&result);
|
||||
}
|
||||
client.client.close("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nBefator verabschiedet sich!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n".into());
|
||||
save_config(&client.config);
|
||||
break;
|
||||
}
|
||||
"spam" => {
|
||||
for _ in 0..100 {
|
||||
for i in 0..5 {
|
||||
let action: AuthenticatedMessage = match i {
|
||||
0 => AuthenticatedMessage::Authenticate,
|
||||
1 => AuthenticatedMessage::Motd,
|
||||
2 => AuthenticatedMessage::GetBalance,
|
||||
3 => AuthenticatedMessage::Register,
|
||||
4 => AuthenticatedMessage::Pay {
|
||||
destination: "thc".into(),
|
||||
amount: 0,
|
||||
},
|
||||
_ => AuthenticatedMessage::Logout,
|
||||
};
|
||||
handle_error(&client.client.send_authenticated(action, |_| true).await);
|
||||
}
|
||||
}
|
||||
}
|
||||
"help" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument.eq_ignore_ascii_case("extended") {
|
||||
println!("--- Available extended Commands ---");
|
||||
println!("register <username> <password> - Register a new account and log into that account");
|
||||
println!("login [username] [password] - Log into the specified account or the account you were last logged into");
|
||||
println!("spam - Sends various requests to the Server");
|
||||
} else {
|
||||
println!("--- Available Commands ---");
|
||||
println!("account <help|add|use|list|remove> - Use account help for a description of all subcommands");
|
||||
println!("balance - View your current balance");
|
||||
println!("pay <username> <amount> - Pay someone Steam Coins");
|
||||
println!("inbox - View your inbox");
|
||||
println!("pm [destination] [message]- View your private messages or send one");
|
||||
println!("delete <inbox|pm> <id> - Delete a message from your inbox or PMs");
|
||||
println!("motd - View the current server motd");
|
||||
println!("logout - Logout of your current account");
|
||||
println!("exit - Logout from the server and exit");
|
||||
println!("settings <list|set> - Manage settings");
|
||||
println!("help [extended] - See a (extended) list of commands");
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown command - Use help for a list of commands")
|
||||
}
|
||||
},
|
||||
Ok(message) = messages.recv_async() => {
|
||||
handle_message(&mut client, message).await;
|
||||
},
|
||||
Ok(action) = actions.recv_async() => {
|
||||
handle_action(&mut client, action).await;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
|
||||
async fn try_login(client: &mut Client, credentials: Credentials) -> bool {
|
||||
@@ -480,7 +135,7 @@ async fn try_login(client: &mut Client, credentials: Credentials) -> bool {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => handle_error(&result)
|
||||
_ => handle_error(&result),
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
@@ -488,13 +143,13 @@ async fn try_login(client: &mut Client, credentials: Credentials) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
async fn setup_new_account(client: &mut Client, credentials: Credentials, old_credentials: Option<Credentials>) {
|
||||
async fn setup_new_account(client: &mut Client, rx: &Receiver<String>, credentials: Credentials, old_credentials: Option<Credentials>) {
|
||||
if !client.config.accounts.default_account.eq(&credentials.username) {
|
||||
if ask_confirmation("Do you want to make this account your default account?").await {
|
||||
if ask_confirmation(rx, "Do you want to make this account your default account?").await {
|
||||
client.config.accounts.default_account = credentials.username.clone();
|
||||
}
|
||||
}
|
||||
if !ask_confirmation("Do you want to use this account now?").await {
|
||||
if !ask_confirmation(rx, "Do you want to use this account now?").await {
|
||||
if let Some(old_credentials) = old_credentials {
|
||||
if !old_credentials.eq(&credentials) {
|
||||
let result = client.login(old_credentials).await;
|
||||
@@ -510,51 +165,29 @@ async fn setup_new_account(client: &mut Client, credentials: Credentials, old_cr
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async fn read_line(client: Option<&Client>) -> String {
|
||||
let mut thread: Option<JoinHandle<_>> = None;
|
||||
if let Some(client) = client {
|
||||
thread = Some(tokio::spawn(async move {
|
||||
/*loop {
|
||||
if let Ok(message) = client.client.receiver().recv() {
|
||||
match message.message {
|
||||
Response::DisplayMessage { message, id, .. } => {
|
||||
match id.as_str() {
|
||||
"pay.received" => {
|
||||
println!("{}", message);
|
||||
if client.config.general.show_balance_after_payment {
|
||||
let balance = client.get_balance().await;
|
||||
if let Ok(balance) = balance {
|
||||
info!("You now have a balance of {}₿", balance);
|
||||
} else {
|
||||
handle_error(&balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
"pm_inbox.received" => {
|
||||
println!("{}", message);
|
||||
}
|
||||
_ => { print_error(&report!(BankError::UnexpectedMessage)); }
|
||||
}
|
||||
}
|
||||
_ => { print_error(&report!(BankError::UnexpectedMessage)); }
|
||||
}
|
||||
fn read_lines() -> Receiver<String> {
|
||||
let (tx, rx) = flume::bounded::<String>(16);
|
||||
thread::spawn(move || {
|
||||
let mut input = stdin().lines();
|
||||
while let Some(line) = input.next() {
|
||||
match line {
|
||||
Ok(line) => {
|
||||
tx.send(line).unwrap();
|
||||
}
|
||||
}*/
|
||||
}));
|
||||
}
|
||||
let mut line = String::new();
|
||||
io::stdin().read_line(&mut line).unwrap();
|
||||
let result = line.replace("\n", "").replace("\r", "");
|
||||
if let Some(thread) = thread {
|
||||
thread.abort();
|
||||
}
|
||||
result
|
||||
Err(err) => {
|
||||
error!("\n{:#?}", Report::new(err));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
rx
|
||||
}
|
||||
|
||||
async fn ask_confirmation(prompt: &str) -> bool {
|
||||
async fn ask_confirmation(rx: &Receiver<String>, prompt: &str) -> bool {
|
||||
println!("{} (y/n): ", prompt);
|
||||
let answer = read_line(None).await;
|
||||
let answer = rx.recv_async().await.unwrap();
|
||||
answer.as_str() == "y"
|
||||
}
|
||||
|
||||
@@ -566,11 +199,7 @@ fn try_parse_bool(value: Option<&str>) -> Result<bool, ()> {
|
||||
parsed.map_err(|_| ())
|
||||
}
|
||||
|
||||
async fn expect_credentials(
|
||||
mut split: std::str::Split<'_, char>,
|
||||
client: &mut Client,
|
||||
try_last_login: bool,
|
||||
) -> Option<Credentials> {
|
||||
async fn expect_credentials(mut split: std::str::Split<'_, char>, client: &mut Client, try_last_login: bool) -> Option<Credentials> {
|
||||
let username = split.next();
|
||||
let password = split.next();
|
||||
if username.is_none() || password.is_none() {
|
||||
@@ -599,5 +228,391 @@ fn handle_error<T>(result: &BankResult<T, BankError>) {
|
||||
}
|
||||
|
||||
fn print_error(error: &Report<BankError>) {
|
||||
error!("\n{:?}", error);
|
||||
//error!("\n{:?}", error);
|
||||
error!("{}", error.as_error().to_string());
|
||||
}
|
||||
|
||||
async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -> bool {
|
||||
let mut split = line.split(' ');
|
||||
let command: &str = split.next().unwrap();
|
||||
|
||||
match command {
|
||||
"account" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument == "add" {
|
||||
let credentials = expect_credentials(split, client, false).await;
|
||||
if let Some(credentials) = credentials {
|
||||
client.config.accounts.accounts.insert(credentials.username.clone(), credentials.password.clone());
|
||||
if client.config.accounts.default_account.is_empty() {
|
||||
client.config.accounts.default_account = credentials.username.clone();
|
||||
}
|
||||
save_config(&client.config);
|
||||
if ask_confirmation(rx, "Do you want to test the account now?").await {
|
||||
let old_credentials = client.state.last_credentials.clone();
|
||||
let success = try_login(client, credentials.clone()).await;
|
||||
if success {
|
||||
setup_new_account(client, rx, credentials.clone(), old_credentials).await;
|
||||
} else {
|
||||
if ask_confirmation(rx, "The login was not successful - Do you want to register this account instead?").await {
|
||||
let result = client.register(credentials.clone()).await;
|
||||
if let Some(last_credentials) = &client.state.last_credentials {
|
||||
handle_error(&client.client.set_credentials(last_credentials.username.clone(), last_credentials.password.clone()));
|
||||
}
|
||||
if result.is_ok() {
|
||||
let result = client.login(credentials.clone()).await;
|
||||
if result.is_ok() {
|
||||
setup_new_account(client, rx, credentials, old_credentials).await;
|
||||
} else {
|
||||
handle_error(&result);
|
||||
warn!("The login was not successful but the account was most likely still registered - Please try to login again")
|
||||
}
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("You need to enter a username and password");
|
||||
}
|
||||
} else if argument == "use" {
|
||||
let username: &str = split.next().or(Some("")).unwrap();
|
||||
if client.config.accounts.accounts.contains_key(username) {
|
||||
let password = client.config.accounts.accounts.get(username).unwrap().clone();
|
||||
try_login(client, Credentials { username: username.into(), password }).await;
|
||||
} else {
|
||||
println!("You need to enter a valid username")
|
||||
}
|
||||
} else if argument == "list" {
|
||||
println!("--- Accounts ---");
|
||||
let i: u8 = 0;
|
||||
for (name, _) in &client.config.accounts.accounts {
|
||||
if client.config.accounts.default_account.eq(name) {
|
||||
println!("{}. {} (Default)", i + 1, name);
|
||||
} else {
|
||||
println!("{}. {}", i + 1, name);
|
||||
}
|
||||
}
|
||||
println!("--- Accounts ---");
|
||||
} else if argument == "remove" {
|
||||
let username: &str = split.next().or(Some("")).unwrap();
|
||||
if client.config.accounts.accounts.contains_key(username) {
|
||||
client.config.accounts.accounts.remove(username);
|
||||
println!("Account removed");
|
||||
} else {
|
||||
println!("You need to enter a valid username")
|
||||
}
|
||||
} else if argument == "help" {
|
||||
println!("--- Available Commands ---");
|
||||
println!("account add <username> <password> - Add a new account");
|
||||
println!("account use [username] - Login with the default or the specified account");
|
||||
println!("account remove <username> - Remove the account from the list");
|
||||
println!("account default <username> - Make the account the default account");
|
||||
println!("account list - List available accounts");
|
||||
} else {
|
||||
println!("Invalid subcommand! Use account help for a list of valid subcommands");
|
||||
}
|
||||
}
|
||||
"settings" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument == "set" {
|
||||
let setting = split.next();
|
||||
let value = split.next();
|
||||
if setting.is_some() && value.is_some() {
|
||||
match setting.unwrap() {
|
||||
"show_motd_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_motd_after_login = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"show_inbox_count_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_inbox_count_after_login = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"show_pm_inbox_count_after_login" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_pm_inbox_count_after_login = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"show_balance_after_payment" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.show_balance_after_payment = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"use_default_account" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_default_account = value;
|
||||
if value {
|
||||
client.config.general.use_last_account = false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"use_last_account" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_last_account = value;
|
||||
if value {
|
||||
client.config.general.use_default_account = false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
"use_last_server" => {
|
||||
if let Ok(value) = try_parse_bool(value) {
|
||||
client.config.general.use_last_server = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_ => { error!("Invalid setting") }
|
||||
}
|
||||
info!("Setting changed");
|
||||
save_config(&client.config);
|
||||
} else {
|
||||
println!("You need to enter a setting and value");
|
||||
}
|
||||
} else if argument == "list" {
|
||||
println!("--- Settings ---");
|
||||
println!("show_motd_after_login: {}", client.config.general.show_motd_after_login);
|
||||
println!("show_inbox_count_after_login: {}", client.config.general.show_inbox_count_after_login);
|
||||
println!("show_pm_inbox_count_after_login: {}", client.config.general.show_pm_inbox_count_after_login);
|
||||
println!("show_balance_after_payment: {}", client.config.general.show_balance_after_payment);
|
||||
println!("use_default_account: {}", client.config.general.use_default_account);
|
||||
println!("use_last_account: {}", client.config.general.use_last_account);
|
||||
println!("use_last_server: {}", client.config.general.use_last_server);
|
||||
} else if argument == "help" {
|
||||
println!("--- Available Commands ---");
|
||||
println!("settings set <setting> <value> - Set a setting to the specified value");
|
||||
println!("settings list - List all settings");
|
||||
} else {
|
||||
println!("Invalid subcommand! Use settings help for a list of valid subcommands");
|
||||
}
|
||||
}
|
||||
"register" => {
|
||||
let credentials = expect_credentials(split, client, true).await;
|
||||
if let Some(credentials) = credentials {
|
||||
let result = client.register(credentials.clone()).await;
|
||||
if let Some(last_credentials) = &client.state.last_credentials {
|
||||
handle_error(&client.client.set_credentials(last_credentials.username.clone(), last_credentials.password.clone()));
|
||||
}
|
||||
if let Err(error) = result {
|
||||
match error.current_context() {
|
||||
BankError::AuthenticationError => {
|
||||
error!("Username is already taken");
|
||||
if ask_confirmation(rx, "Do you want to login instead?").await {
|
||||
try_login(client, credentials).await;
|
||||
}
|
||||
}
|
||||
_ => print_error(&error),
|
||||
}
|
||||
} else {
|
||||
if ask_confirmation(rx, "Do you want to login with your new account?").await {
|
||||
try_login(client, credentials).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"login" => {
|
||||
let credentials = expect_credentials(split, client, true).await;
|
||||
if let Some(credentials) = credentials {
|
||||
try_login(client, credentials).await;
|
||||
}
|
||||
}
|
||||
"balance" => {
|
||||
let balance = client.get_balance().await;
|
||||
if let Ok(balance) = balance {
|
||||
info!("You currently have a balance of {}₿", balance);
|
||||
} else {
|
||||
handle_error(&balance);
|
||||
}
|
||||
}
|
||||
"pay" => {
|
||||
let destination = split.next();
|
||||
let amount = split.next();
|
||||
if destination.is_none() || amount.is_none() || amount.unwrap().parse::<u32>().is_err() || amount.unwrap().parse::<u32>().unwrap() <= 0 {
|
||||
error!("You need to enter a destination username and amount greater than 0");
|
||||
} else {
|
||||
let result = client.pay(destination.unwrap().into(), amount.unwrap().parse::<u32>().unwrap()).await;
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
"inbox" => {
|
||||
let size = client.get_inbox_size().await;
|
||||
if let Ok(size) = size {
|
||||
info!("--- {} Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
for i in 0..size {
|
||||
let msg = client.get_inbox_msg((i + 1) as i8).await;
|
||||
if let Ok((id, msg)) = msg {
|
||||
info!("{id}. {msg}");
|
||||
} else {
|
||||
handle_error(&msg);
|
||||
}
|
||||
}
|
||||
info!("--- {} Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
} else {
|
||||
handle_error(&size);
|
||||
}
|
||||
}
|
||||
"pm" => {
|
||||
let username = split.next();
|
||||
let message = split.next();
|
||||
if username.is_some() && message.is_some() {
|
||||
let mut message: String = message.unwrap().into();
|
||||
let mut segment = split.next();
|
||||
while segment.is_some() {
|
||||
message = message.add(" ");
|
||||
message = message.add(segment.unwrap().into());
|
||||
segment = split.next();
|
||||
}
|
||||
|
||||
let result = client.send_pm(username.unwrap().into(), message).await;
|
||||
handle_error(&result);
|
||||
} else if username.is_none() && username.is_none() {
|
||||
let size = client.get_pm_size().await;
|
||||
if let Ok(size) = size {
|
||||
info!("--- {} Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
for i in 0..size {
|
||||
let msg = client.get_pm_msg((i + 1) as i8).await;
|
||||
if let Ok((id, msg)) = msg {
|
||||
info!("{}. {}", id, msg);
|
||||
} else {
|
||||
handle_error(&msg);
|
||||
}
|
||||
}
|
||||
info!("--- {} Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
} else {
|
||||
handle_error(&size);
|
||||
}
|
||||
} else {
|
||||
error!("You need to enter a username and message or nothing to view your PMs")
|
||||
}
|
||||
}
|
||||
"delete" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
let id = split.next().or(Some("")).unwrap().parse();
|
||||
if argument == "inbox" && id.is_ok() {
|
||||
let result = client.delete_inbox_msg(id.unwrap()).await;
|
||||
handle_error(&result);
|
||||
} else if argument == "pm" && id.is_ok() {
|
||||
let result = client.delete_pm_msg(id.unwrap()).await;
|
||||
handle_error(&result);
|
||||
} else {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
}
|
||||
}
|
||||
"motd" => {
|
||||
let motd = client.get_motd().await;
|
||||
if let Ok(motd) = motd {
|
||||
println!("--- {} ---", motd)
|
||||
} else {
|
||||
handle_error(&motd);
|
||||
}
|
||||
}
|
||||
"logout" => {
|
||||
if !client.state.logged_in {
|
||||
warn!("You are already logged out")
|
||||
} else {
|
||||
let result = client.logout().await;
|
||||
handle_error(&result);
|
||||
}
|
||||
}
|
||||
"exit" => {
|
||||
if client.state.logged_in {
|
||||
let result = client.logout().await;
|
||||
handle_error(&result);
|
||||
}
|
||||
client.client.close("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nBefator verabschiedet sich!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n".into());
|
||||
save_config(&client.config);
|
||||
return true;
|
||||
}
|
||||
"spam" => {
|
||||
for _ in 0..100 {
|
||||
for i in 0..5 {
|
||||
let action: AuthenticatedMessage = match i {
|
||||
0 => AuthenticatedMessage::Authenticate,
|
||||
1 => AuthenticatedMessage::Motd,
|
||||
2 => AuthenticatedMessage::GetBalance,
|
||||
3 => AuthenticatedMessage::Register,
|
||||
4 => AuthenticatedMessage::Pay {
|
||||
destination: "thc".into(),
|
||||
amount: 0,
|
||||
},
|
||||
_ => AuthenticatedMessage::Logout,
|
||||
};
|
||||
handle_error(&client.client.send_authenticated(action, |_| true).await);
|
||||
}
|
||||
}
|
||||
}
|
||||
"help" => {
|
||||
let argument = split.next().or(Some("")).unwrap();
|
||||
if argument.eq_ignore_ascii_case("extended") {
|
||||
println!("--- Available extended Commands ---");
|
||||
println!("register <username> <password> - Register a new account and log into that account");
|
||||
println!("login [username] [password] - Log into the specified account or the account you were last logged into");
|
||||
println!("spam - Sends various requests to the Server");
|
||||
} else {
|
||||
println!("--- Available Commands ---");
|
||||
println!("account <help|add|use|list|remove> - Use account help for a description of all subcommands");
|
||||
println!("balance - View your current balance");
|
||||
println!("pay <username> <amount> - Pay someone Steam Coins");
|
||||
println!("inbox - View your inbox");
|
||||
println!("pm [destination] [message]- View your private messages or send one");
|
||||
println!("delete <inbox|pm> <id> - Delete a message from your inbox or PMs");
|
||||
println!("motd - View the current server motd");
|
||||
println!("logout - Logout of your current account");
|
||||
println!("exit - Logout from the server and exit");
|
||||
println!("settings <list|set> - Manage settings");
|
||||
println!("help [extended] - See a (extended) list of commands");
|
||||
}
|
||||
}
|
||||
_ => { println!("Unknown command - Use help for a list of commands") }
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
async fn handle_message(client: &mut Client, response: ResponseMessage) {
|
||||
match response.clone().message {
|
||||
Response::DisplayMessage { message, id, .. } => match id.as_str() {
|
||||
"pay.received" => {
|
||||
println!("{}", message);
|
||||
if client.config.general.show_balance_after_payment {
|
||||
let balance = client.get_balance().await;
|
||||
if let Ok(balance) = balance {
|
||||
info!("You now have a balance of {}₿", balance);
|
||||
} else {
|
||||
handle_error(&balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
"pm_inbox.received" => {
|
||||
println!("{}", message);
|
||||
}
|
||||
_ => { print_error(&report!(BankError::UnexpectedMessage(response))); }
|
||||
},
|
||||
_ => { print_error(&report!(BankError::UnexpectedMessage(response))); }
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_action(client: &mut Client, action: ClientAction) {
|
||||
match action {
|
||||
ClientAction::Connected => {
|
||||
if client.state.logged_in {
|
||||
client.state.logged_in = false;
|
||||
handle_error(&client.login(client.state.last_credentials.clone().unwrap()).await);
|
||||
}
|
||||
}
|
||||
ClientAction::Disconnected => {}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user