bhcli

A TUI for chatting on LE PHP Chats (onion)
git clone https://git.dasho.dev/n0tr1v/bhcli.git
Log | Files | Refs | README

commit 0fbfdb5b9a8da5cf07e1d4242b3e5b9475999205
parent a59e01d48120dc33cffa33122f7a1cd785ed6200
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri,  7 Apr 2023 16:34:39 -0700

cleanup

Diffstat:
Msrc/main.rs | 343++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 174 insertions(+), 169 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -334,175 +334,7 @@ impl LeChatPHPClient { } } else if oper.index() == oper1 { if let Ok(post_type_recv) = oper.recv(&rx) { - let mut should_reset_keepalive_timer = false; - loop { - let post_type = post_type_recv.clone(); - let resp = match client.get(url.clone()).send() { - Ok(r) => r, - Err(e) => { - log::error!("failed to send request: {:?}", e); - continue; - } - }; - let resp_text = resp.text().unwrap(); - let doc = Document::from(resp_text.as_str()); - let nc = doc.find(Attr("name", "nc")).next().unwrap(); - let nc_value = nc.attr("value").unwrap().to_owned(); - let postid = match doc.find(Attr("name", "postid")).next() { - Some(v) => v, - None => { - log::error!("failed to get postid"); - continue; - }, - }; - let postid_value = postid.attr("value").unwrap().to_owned(); - let mut params: Vec<(&str, String)> = vec![ - ("lang", LANG.to_owned()), - ("nc", nc_value.to_owned()), - ("session", session.clone()), - ]; - - if let PostType::Clean(date, text) = post_type { - if let Err(e) = delete_message(&client, &full_url, &mut params, date, text) { - log::error!("failed to delete message: {:?}", e); - continue; - } - break; - } - - let mut req = client.post(&full_url); - let mut form: Option<multipart::Form> = None; - - match post_type { - PostType::Post(msg, send_to) => { - should_reset_keepalive_timer = true; - params.extend(vec![ - ("action", "post".to_owned()), - ("postid", postid_value.to_owned()), - ("message", msg.clone()), - ("sendto", send_to.unwrap_or(SEND_TO_ALL.to_owned())), - ]); - } - PostType::NewNickname(new_nickname) => { - if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) { - log::error!("{:?}", e); - continue; - } - params.extend(vec![ - ("do", "save".to_owned()), - ("timestamps", "on".to_owned()), - ("newnickname", new_nickname), - ]); - } - PostType::NewColor(new_color) => { - if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) { - log::error!("{:?}", e); - continue; - } - params.extend(vec![ - ("do", "save".to_owned()), - ("timestamps", "on".to_owned()), - ("colour", new_color), - ]); - } - PostType::Ignore(username) => { - if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) { - log::error!("{:?}", e); - continue; - } - params.extend(vec![ - ("do", "save".to_owned()), - ("timestamps", "on".to_owned()), - ("ignore", username), - ]); - } - PostType::Unignore(username) => { - if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) { - log::error!("{:?}", e); - continue; - } - params.extend(vec![ - ("do", "save".to_owned()), - ("timestamps", "on".to_owned()), - ("unignore", username), - ]); - } - PostType::Profile(new_color, new_nickname) => { - if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) { - log::error!("{:?}", e); - continue; - } - params.extend(vec![ - ("do", "save".to_owned()), - ("timestamps", "on".to_owned()), - ("colour", new_color), - ("newnickname", new_nickname), - ]); - } - PostType::Kick(msg, send_to) => { - params.extend(vec![ - ("action", "post".to_owned()), - ("postid", postid_value.to_owned()), - ("message", msg), - ("sendto", send_to), - ("kick", "kick".to_owned()), - ("what", "purge".to_owned()), - ]); - } - PostType::DeleteLast | PostType::DeleteAll => { - params.extend(vec![("action", "delete".to_owned())]); - if let PostType::DeleteAll = post_type { - params.extend(vec![ - ("sendto", SEND_TO_ALL.to_owned()), - ("confirm", "yes".to_owned()), - ("what", "all".to_owned()), - ]); - } else { - params.extend(vec![ - ("sendto", "".to_owned()), - ("what", "last".to_owned()), - ]); - } - } - PostType::Upload(file_path, send_to, msg) => { - form = Some( - match multipart::Form::new() - .text("lang", LANG.to_owned()) - .text("nc", nc_value.to_owned()) - .text("session", session.clone()) - .text("action", "post".to_owned()) - .text("postid", postid_value.to_owned()) - .text("message", msg) - .text("sendto", send_to.to_owned()) - .text("what", "purge".to_owned()) - .file("file", file_path) { - Ok(f) => f, - Err(e) => { - log::error!("{:?}", e); - break; - }, - } - ); - } - PostType::Clean(_, _) => {} - } - - if let Some(form_content) = form { - req = req.multipart(form_content); - } else { - req = req.form(&params); - } - if let Err(err) = req.send() { - log::error!("{:?}", err.to_string()); - if err.is_timeout() { - continue; - } - } - break; - } - if should_reset_keepalive_timer { - last_post_tx.send(true).unwrap(); - } + post_msg(&client, post_type_recv, &full_url, session.clone(), &url, &last_post_tx); } } }) @@ -1374,6 +1206,179 @@ fn set_profile_base_info( Ok(()) } + +fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session: String, url: &str, last_post_tx: &crossbeam_channel::Sender<bool>) { + let mut should_reset_keepalive_timer = false; + loop { + let post_type = post_type_recv.clone(); + let resp = match client.get(url.clone()).send() { + Ok(r) => r, + Err(e) => { + log::error!("failed to send request: {:?}", e); + continue; + } + }; + let resp_text = resp.text().unwrap(); + let doc = Document::from(resp_text.as_str()); + let nc = doc.find(Attr("name", "nc")).next().unwrap(); + let nc_value = nc.attr("value").unwrap().to_owned(); + let postid = match doc.find(Attr("name", "postid")).next() { + Some(v) => v, + None => { + log::error!("failed to get postid"); + continue; + }, + }; + let postid_value = postid.attr("value").unwrap().to_owned(); + let mut params: Vec<(&str, String)> = vec![ + ("lang", LANG.to_owned()), + ("nc", nc_value.to_owned()), + ("session", session.clone()), + ]; + + if let PostType::Clean(date, text) = post_type { + if let Err(e) = delete_message(&client, full_url, &mut params, date, text) { + log::error!("failed to delete message: {:?}", e); + continue; + } + break; + } + + let mut req = client.post(full_url); + let mut form: Option<multipart::Form> = None; + + match post_type { + PostType::Post(msg, send_to) => { + should_reset_keepalive_timer = true; + params.extend(vec![ + ("action", "post".to_owned()), + ("postid", postid_value.to_owned()), + ("message", msg.clone()), + ("sendto", send_to.unwrap_or(SEND_TO_ALL.to_owned())), + ]); + } + PostType::NewNickname(new_nickname) => { + if let Err(e) = set_profile_base_info(&client, full_url, &mut params) { + log::error!("{:?}", e); + continue; + } + params.extend(vec![ + ("do", "save".to_owned()), + ("timestamps", "on".to_owned()), + ("newnickname", new_nickname), + ]); + } + PostType::NewColor(new_color) => { + if let Err(e) = set_profile_base_info(&client, full_url, &mut params) { + log::error!("{:?}", e); + continue; + } + params.extend(vec![ + ("do", "save".to_owned()), + ("timestamps", "on".to_owned()), + ("colour", new_color), + ]); + } + PostType::Ignore(username) => { + if let Err(e) = set_profile_base_info(&client, full_url, &mut params) { + log::error!("{:?}", e); + continue; + } + params.extend(vec![ + ("do", "save".to_owned()), + ("timestamps", "on".to_owned()), + ("ignore", username), + ]); + } + PostType::Unignore(username) => { + if let Err(e) = set_profile_base_info(&client, full_url, &mut params) { + log::error!("{:?}", e); + continue; + } + params.extend(vec![ + ("do", "save".to_owned()), + ("timestamps", "on".to_owned()), + ("unignore", username), + ]); + } + PostType::Profile(new_color, new_nickname) => { + if let Err(e) = set_profile_base_info(&client, full_url, &mut params) { + log::error!("{:?}", e); + continue; + } + params.extend(vec![ + ("do", "save".to_owned()), + ("timestamps", "on".to_owned()), + ("colour", new_color), + ("newnickname", new_nickname), + ]); + } + PostType::Kick(msg, send_to) => { + params.extend(vec![ + ("action", "post".to_owned()), + ("postid", postid_value.to_owned()), + ("message", msg), + ("sendto", send_to), + ("kick", "kick".to_owned()), + ("what", "purge".to_owned()), + ]); + } + PostType::DeleteLast | PostType::DeleteAll => { + params.extend(vec![("action", "delete".to_owned())]); + if let PostType::DeleteAll = post_type { + params.extend(vec![ + ("sendto", SEND_TO_ALL.to_owned()), + ("confirm", "yes".to_owned()), + ("what", "all".to_owned()), + ]); + } else { + params.extend(vec![ + ("sendto", "".to_owned()), + ("what", "last".to_owned()), + ]); + } + } + PostType::Upload(file_path, send_to, msg) => { + form = Some( + match multipart::Form::new() + .text("lang", LANG.to_owned()) + .text("nc", nc_value.to_owned()) + .text("session", session.clone()) + .text("action", "post".to_owned()) + .text("postid", postid_value.to_owned()) + .text("message", msg) + .text("sendto", send_to.to_owned()) + .text("what", "purge".to_owned()) + .file("file", file_path) { + Ok(f) => f, + Err(e) => { + log::error!("{:?}", e); + break; + }, + } + ); + } + PostType::Clean(_, _) => {} + } + + if let Some(form_content) = form { + req = req.multipart(form_content); + } else { + req = req.form(&params); + } + if let Err(err) = req.send() { + log::error!("{:?}", err.to_string()); + if err.is_timeout() { + continue; + } + } + break; + } + if should_reset_keepalive_timer { + last_post_tx.send(true).unwrap(); + } +} + fn parse_date(date: &str, datetime_fmt: &str) -> NaiveDateTime { let now = Utc::now(); let date_fmt = format!("%Y-{}", datetime_fmt);