Advanced PMs
This commit is contained in:
1 parent
68de25838c
commit
a8b3f7f871
8 files changed
+607
-137
No files matched your search
@@ -18,6 +18,7 @@ url.workspace = true
|
||||
lalrpop-util = "0.22.1"
|
||||
error-stack.workspace = true
|
||||
flume.workspace = true
|
||||
chrono.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
lalrpop = "0.22.1"
|
||||
+195
-25
@@ -1,6 +1,6 @@
|
||||
use banklib::config::{load_config, save_config};
|
||||
use banklib::extended::{Client, Credentials, State};
|
||||
use banklib::{AuthenticatedMessage, BankClient, BankError, BankResult, ClientAction, Response, ResponseMessage};
|
||||
use banklib::extended::{Client, Credentials, MessageHistoryEntry, State};
|
||||
use banklib::{AuthenticatedMessage, BankClient, BankError, BankResult, ClientAction, MsgType, Response, ResponseMessage};
|
||||
use clap::Parser;
|
||||
use error_stack::{report, Report};
|
||||
use flume::Receiver;
|
||||
@@ -269,6 +269,13 @@ async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
} else {
|
||||
if !ask_confirmation(rx, "Do you still want to add the account?").await {
|
||||
client.config.accounts.accounts.remove(&credentials.username);
|
||||
if client.config.accounts.default_account.eq(&credentials.username) {
|
||||
client.config.accounts.default_account = String::new();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -298,6 +305,9 @@ async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -
|
||||
let username: &str = split.next().or(Some("")).unwrap();
|
||||
if client.config.accounts.accounts.contains_key(username) {
|
||||
client.config.accounts.accounts.remove(username);
|
||||
if client.config.accounts.default_account.eq(username) {
|
||||
client.config.accounts.default_account = String::new();
|
||||
}
|
||||
println!("Account removed");
|
||||
} else {
|
||||
println!("You need to enter a valid username")
|
||||
@@ -479,34 +489,193 @@ async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -
|
||||
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" });
|
||||
let use_fake_inbox = if client.config.acknowledgements.use_fake_pm_inbox {
|
||||
client.config.general.use_fake_pm_inbox
|
||||
} else {
|
||||
handle_error(&size);
|
||||
let use_fake_inbox = ask_confirmation(rx, "To correctly record the first time these messages were seen the messages need to be deleted from the Server. This may prevent you from viewing these private messages using other clients. The messages will still be marked as unread in this client.").await;
|
||||
if ask_confirmation(rx, "Do you want this answer to be saved?").await {
|
||||
client.config.acknowledgements.use_fake_pm_inbox = true;
|
||||
client.config.general.use_fake_pm_inbox = use_fake_inbox;
|
||||
}
|
||||
use_fake_inbox
|
||||
};
|
||||
if use_fake_inbox {
|
||||
let result = client.update_new_pm_history().await;
|
||||
if result.is_err() {
|
||||
handle_error(&result);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
let new = client.get_new_pms().await;
|
||||
if let Ok(new) = new {
|
||||
let history = client.get_new_pm_history().await;
|
||||
if let Ok(history) = history {
|
||||
let size = new.len() + history.len();
|
||||
info!("--- {} New Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
for (id, message) in new {
|
||||
info!("*{}. [?]{}", id, message);
|
||||
}
|
||||
let mut i = 1;
|
||||
for msg in history.clone() {
|
||||
info!("{}. [{}]{}: {}", i, msg.time.format("%M:%H %d.%m.%y"), msg.sender, msg.message);
|
||||
i += 1;
|
||||
}
|
||||
info!("*: Message not saved in the history");
|
||||
info!("--- {} New Private Message{} ---", size, if size == 1 { "" } else { "s" });
|
||||
if ask_confirmation(rx, "Do you want to mark all messages as read?").await {
|
||||
handle_error(&client.move_all_new_pms().await);
|
||||
}
|
||||
} else {
|
||||
handle_error(&history);
|
||||
}
|
||||
} else {
|
||||
handle_error(&new);
|
||||
}
|
||||
} else {
|
||||
error!("You need to enter a username and message or nothing to view your PMs")
|
||||
}
|
||||
}
|
||||
"chat" => {
|
||||
let username = split.next().or(Some("")).unwrap();
|
||||
if !username.is_empty() {
|
||||
let result = client.get_pm_history(username.into()).await;
|
||||
if let Ok(history) = result {
|
||||
if client.config.general.use_fake_pm_inbox {
|
||||
handle_error(&client.update_new_pm_history().await);
|
||||
} else {
|
||||
warn!("You do not have advanced storing of unread private messages enabled. These unread messages will not appear in the context here.")
|
||||
}
|
||||
let result = client.get_new_pm_history().await;
|
||||
if let Ok(new_history) = result {
|
||||
info!("--- Private Messages with {} ---", username);
|
||||
info!("Type \"exit\" to exit");
|
||||
for i in 0..client.config.general.chat_context_window {
|
||||
if history.len() > i as usize {
|
||||
let msg: MessageHistoryEntry = history.get(i as usize).unwrap().clone();
|
||||
info!("[{}]{}: {}", msg.time.format("%H:%M:%S %d.%m.%y"), msg.sender, msg.message)
|
||||
}
|
||||
}
|
||||
for msg in &new_history {
|
||||
if msg.sender.eq_ignore_ascii_case(&username) {
|
||||
info!("*[{}]{}: {}", msg.time.format("%H:%M %d.%m.%y"), msg.sender, msg.message)
|
||||
}
|
||||
}
|
||||
if !new_history.is_empty() && ask_confirmation(rx, "Do you want to mark all unread messages as read now?").await {
|
||||
handle_error(&client.move_all_new_pms().await);
|
||||
}
|
||||
|
||||
let messages = client.client.receiver_msg().clone();
|
||||
let actions = client.client.receiver_act().clone();
|
||||
loop {
|
||||
select! {
|
||||
Ok(line) = rx.recv_async() => {
|
||||
if line.eq_ignore_ascii_case("exit") {
|
||||
info!("Successfully left the chat");
|
||||
break;
|
||||
} else {
|
||||
handle_error(&client.send_pm(username.into(), line).await);
|
||||
}
|
||||
},
|
||||
Ok(message) = messages.recv_async() => {
|
||||
match message.message {
|
||||
Response::DisplayMessage { message: _, msg_type } => match msg_type {
|
||||
MsgType::PmReceived { sender, message: _ } => {
|
||||
if sender.eq_ignore_ascii_case(&username) {
|
||||
let result = client.move_newest_pm_to_history().await;
|
||||
if let Ok(msg) = result {
|
||||
info!("[{}]{}: {}", msg.time.format("%H:%M %d.%m.%y"), msg.sender, msg.message)
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
} else {
|
||||
info!("You got a pm from another user ({})", sender)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
Ok(action) = actions.recv_async() => {
|
||||
match action {
|
||||
ClientAction::Connected => {
|
||||
client.state.logged_in = false;
|
||||
let result = client.login(client.state.last_credentials.clone().unwrap()).await;
|
||||
if result.is_err() {
|
||||
handle_error(&result);
|
||||
break;
|
||||
} else {
|
||||
info!("Reconnected to the chat")
|
||||
}
|
||||
}
|
||||
ClientAction::Disconnected => {}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
} else {
|
||||
handle_error(&result);
|
||||
}
|
||||
} else {
|
||||
error!("You need to enter a username")
|
||||
}
|
||||
}
|
||||
"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);
|
||||
let id = split.next().or(Some("")).unwrap();
|
||||
if argument == "inbox" && !id.is_empty() {
|
||||
let parsed = id.parse();
|
||||
if let Ok(i) = parsed {
|
||||
let result = client.delete_inbox_msg(i).await;
|
||||
handle_error(&result);
|
||||
} else {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
}
|
||||
} else if argument == "pm" && !id.is_empty() {
|
||||
if id.starts_with("*") {
|
||||
let result = id.strip_prefix("*").unwrap().parse();
|
||||
if result.is_err() {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
return false;
|
||||
}
|
||||
let i: u8 = result.unwrap();
|
||||
if client.config.general.use_pm_history {
|
||||
let auto_import = if client.config.acknowledgements.auto_import_on_delete {
|
||||
client.config.general.auto_import_on_delete
|
||||
} else {
|
||||
let auto_import = ask_confirmation(rx, "You have the message history enabled but specified to delete a message that is not in the new message history. Do you want to import all new messages into the non native history that may not be accessible for all clients or delete the message without saving it into the history?").await;
|
||||
if ask_confirmation(rx, "Do you want this answer to be saved?").await {
|
||||
client.config.acknowledgements.auto_import_on_delete = true;
|
||||
client.config.general.auto_import_on_delete = auto_import;
|
||||
}
|
||||
auto_import
|
||||
};
|
||||
if auto_import {
|
||||
let history = client.get_new_pm_history().await;
|
||||
if let Ok(history) = history {
|
||||
let i = i + (history.len() as u8) - 1;
|
||||
handle_error(&client.update_new_pm_history().await);
|
||||
handle_error(&client.move_from_new_pm_history(i).await);
|
||||
}
|
||||
} else {
|
||||
handle_error(&client.delete_pm_msg(i, false).await);
|
||||
}
|
||||
} else {
|
||||
handle_error(&client.delete_pm_msg(i, false).await);
|
||||
}
|
||||
} else {
|
||||
let result = id.parse();
|
||||
if result.is_err() {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
return false;
|
||||
}
|
||||
let i: u8 = result.unwrap();
|
||||
handle_error(&client.move_from_new_pm_history(i).await);
|
||||
};
|
||||
} else {
|
||||
println!("You need to specify inbox or pm and a message id");
|
||||
}
|
||||
@@ -568,6 +737,7 @@ async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -
|
||||
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!("chat <username> - Chat with someone");
|
||||
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");
|
||||
@@ -584,8 +754,8 @@ async fn handle_line(client: &mut Client, rx: &Receiver<String>, line: String) -
|
||||
|
||||
async fn handle_message(client: &mut Client, response: ResponseMessage) {
|
||||
match response.clone().message {
|
||||
Response::DisplayMessage { message, id, .. } => match id.as_str() {
|
||||
"pay.received" => {
|
||||
Response::DisplayMessage { message, msg_type } => match msg_type {
|
||||
MsgType::PaymentReceived { amount: _, sender: _ } => {
|
||||
println!("{}", message);
|
||||
if client.config.general.show_balance_after_payment {
|
||||
let balance = client.get_balance().await;
|
||||
@@ -596,7 +766,7 @@ async fn handle_message(client: &mut Client, response: ResponseMessage) {
|
||||
}
|
||||
}
|
||||
}
|
||||
"pm_inbox.received" => {
|
||||
MsgType::PmReceived { sender: _, message: _ } => {
|
||||
println!("{}", message);
|
||||
}
|
||||
_ => { print_error(&report!(BankError::UnexpectedMessage(response))); }
|
||||
|
||||
Reference in new issue
Block a user