commit 8ff4a1300d4d7aa95c73be5340fd4c8b4ba4852c
parent 809d0a35581fe5a27e45d9911e61d1ed6179e09c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 7 Apr 2023 17:54:49 -0700
retry function instead of loop
Diffstat:
| M | src/main.rs | | | 93 | +++++++++++++++++++++++++++++++++++-------------------------------------------- |
1 file changed, 41 insertions(+), 52 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -2,7 +2,7 @@ mod util;
mod lechatphp;
mod bhc;
-use anyhow::anyhow;
+use anyhow::{anyhow, Context};
use log;
use log::LevelFilter;
use log4rs::append::file::FileAppender;
@@ -1203,30 +1203,34 @@ fn set_profile_base_info(
Ok(())
}
+enum RetryErr {
+ Retry,
+ Exit,
+}
+
+fn retry_fn<F>(mut clb: F) where F: FnMut() -> anyhow::Result<RetryErr> {
+ loop {
+ match clb() {
+ Ok(RetryErr::Retry) => continue,
+ Ok(RetryErr::Exit) => return,
+ Err(err) => {
+ log::error!("{}", err);
+ continue;
+ },
+ }
+ }
+}
fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session: String, url: &str, last_post_tx: &crossbeam_channel::Sender<()>) {
let mut should_reset_keepalive_timer = false;
- loop {
+ retry_fn(|| -> anyhow::Result<RetryErr> {
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 resp_text = client.get(url.clone()).send()?.text()?;
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 nc = doc.find(Attr("name", "nc")).next().context("nc not found")?;
+ let nc_value = nc.attr("value").context("nc value not found")?.to_owned();
+ let postid = doc.find(Attr("name", "postid")).next().context("failed to get postid")?;
+ let postid_value = postid.attr("value").context("failed to get postid value")?.to_owned();
let mut params: Vec<(&str, String)> = vec![
("lang", LANG.to_owned()),
("nc", nc_value.to_owned()),
@@ -1236,9 +1240,9 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
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;
+ return Ok(RetryErr::Retry);
}
- break;
+ return Ok(RetryErr::Exit);
}
let mut req = client.post(full_url);
@@ -1255,10 +1259,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
]);
}
PostType::NewNickname(new_nickname) => {
- if let Err(e) = set_profile_base_info(&client, full_url, &mut params) {
- log::error!("{:?}", e);
- continue;
- }
+ set_profile_base_info(&client, full_url, &mut params)?;
params.extend(vec![
("do", "save".to_owned()),
("timestamps", "on".to_owned()),
@@ -1266,10 +1267,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
]);
}
PostType::NewColor(new_color) => {
- if let Err(e) = set_profile_base_info(&client, full_url, &mut params) {
- log::error!("{:?}", e);
- continue;
- }
+ set_profile_base_info(&client, full_url, &mut params)?;
params.extend(vec![
("do", "save".to_owned()),
("timestamps", "on".to_owned()),
@@ -1277,10 +1275,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
]);
}
PostType::Ignore(username) => {
- if let Err(e) = set_profile_base_info(&client, full_url, &mut params) {
- log::error!("{:?}", e);
- continue;
- }
+ set_profile_base_info(&client, full_url, &mut params)?;
params.extend(vec![
("do", "save".to_owned()),
("timestamps", "on".to_owned()),
@@ -1288,10 +1283,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
]);
}
PostType::Unignore(username) => {
- if let Err(e) = set_profile_base_info(&client, full_url, &mut params) {
- log::error!("{:?}", e);
- continue;
- }
+ set_profile_base_info(&client, full_url, &mut params)?;
params.extend(vec![
("do", "save".to_owned()),
("timestamps", "on".to_owned()),
@@ -1299,10 +1291,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
]);
}
PostType::Profile(new_color, new_nickname) => {
- if let Err(e) = set_profile_base_info(&client, full_url, &mut params) {
- log::error!("{:?}", e);
- continue;
- }
+ set_profile_base_info(&client, full_url, &mut params)?;
params.extend(vec![
("do", "save".to_owned()),
("timestamps", "on".to_owned()),
@@ -1350,7 +1339,7 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
Ok(f) => f,
Err(e) => {
log::error!("{:?}", e);
- break;
+ return Ok(RetryErr::Exit);
},
}
);
@@ -1366,11 +1355,11 @@ fn post_msg(client: &Client, post_type_recv: PostType, full_url: &str, session:
if let Err(err) = req.send() {
log::error!("{:?}", err.to_string());
if err.is_timeout() {
- continue;
+ return Ok(RetryErr::Retry);
}
}
- break;
- }
+ return Ok(RetryErr::Exit);
+ });
if should_reset_keepalive_timer {
last_post_tx.send(()).unwrap();
}
@@ -1518,17 +1507,17 @@ fn delete_message(
("do", "clean".to_owned()),
("what", "choose".to_owned()),
]);
- let clean_resp = client.post(full_url).form(¶ms).send()?;
- let clean_resp_txt = clean_resp.text().unwrap();
+ let clean_resp_txt = client.post(full_url).form(¶ms).send()?.text()?;
let doc = Document::from(clean_resp_txt.as_str());
- let nc = doc.find(Attr("name", "nc")).next().unwrap();
- let nc_value = nc.attr("value").unwrap().to_owned();
- let msgs = extract_messages(&doc).unwrap();
+ let nc = doc.find(Attr("name", "nc")).next().context("nc not found")?;
+ let nc_value = nc.attr("value").context("nc value not found")?.to_owned();
+ let msgs = extract_messages(&doc)?;
if let Some(msg) = msgs.iter().find(|m| m.date == date && m.text.text() == text) {
+ let msg_id = msg.id.context("msg id not found")?;
params.extend(vec![
("nc", nc_value.to_owned()),
("what", "selected".to_owned()),
- ("mid[]", format!("{}", msg.id.unwrap())),
+ ("mid[]", format!("{}", msg_id)),
]);
client.post(full_url).form(¶ms).send()?;
}