commit ecd9e2c0ba9770634817eaeedf117e91ef4826ad
parent c831e26540777abf8693a37e8a619166de1eb30d
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 7 Apr 2023 14:32:31 -0700
simplify code
Diffstat:
| M | src/main.rs | | | 248 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
1 file changed, 134 insertions(+), 114 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -531,120 +531,8 @@ impl LeChatPHPClient {
let tx = self.tx.clone();
let members_tag = self.config.members_tag.clone();
thread::spawn(move || loop {
- let (_stream, stream_handle) = OutputStream::try_default().unwrap();
- let source = Decoder::new_mp3(Cursor::new(SOUND1)).unwrap();
-
- let url = format!("{}/{}?action=view&session={}&lang={}", base_url, page_php, session, LANG);
- if let Ok(resp) = client.get(url).send() {
- if let Ok(resp_text) = resp.text() {
- let resp_text = resp_text.replace("<br>", "\n");
- let doc = Document::from(resp_text.as_str());
- let mut should_notify = false;
- {
- let mut messages = messages.lock().unwrap();
- if let Ok(new_messages) = extract_messages(&doc) {
- let parse_date = |date: &str| -> NaiveDateTime {
- let now = Utc::now();
- let date_fmt = format!("%Y-{}", datetime_fmt);
- NaiveDateTime::parse_from_str(format!("{}-{}", now.year(), date).as_str(), date_fmt.as_str()).unwrap()
- };
-
- if let Some(last_known_msg) = messages.get(0) {
- let msg = last_known_msg;
- let parsed_dt = parse_date(&msg.date);
- for new_msg in &new_messages {
- let new_parsed_dt = parse_date(&new_msg.date);
-
- if parsed_dt > new_parsed_dt || (new_msg.date == msg.date && msg.text == new_msg.text) {
- break;
- }
-
- if let Some((from, to_opt, msg)) = get_message(&new_msg.text, &members_tag) {
- // Process new messages
-
- // !bhcli filters
- if msg == "!bhcli" && username == N0TR1V {
- let msg = format!("@{} -> {}", from, BHCLI_BLOG_URL).to_owned();
- tx.send(PostType::Post(msg, None)).unwrap();
- } else if msg == "/logout" && from == STUXNET && username == N0TR1V {
- log::error!("forced logout by {}", from);
- sig.lock().unwrap().signal(&ExitSignal::Terminate);
- return;
- }
- // Auto kick spammers
- if username == N0TR1V {
- if from != N0TR1V && from != FAGGOT {
- if msg.contains(FAGGOT) && (msg.contains("pedo") || msg.contains("child")) {
- let msg = "spam".to_owned();
- let username_to_kick = from.to_owned();
- tx.send(PostType::Kick(msg, username_to_kick)).unwrap();
- }
- }
- }
- // Notify when tagged
- if msg.contains(format!("@{}", &username).as_str()) {
- should_notify = true;
- }
- // Notify when PM is received
- if let Some(to) = to_opt {
- if to == username && msg != "!up" {
- should_notify = true;
- }
- }
- }
- }
- }
-
- // Build messages vector. Tag deleted messages.
- let mut old_msg_ptr = 0;
- for new_msg in new_messages.into_iter() {
- loop {
- if let Some(old_msg) = messages.get_mut(old_msg_ptr) {
- let new_parsed_dt = parse_date(&new_msg.date);
- let parsed_dt = parse_date(&old_msg.date);
- if new_parsed_dt < parsed_dt {
- old_msg.deleted = true;
- old_msg_ptr += 1;
- continue;
- }
- if new_parsed_dt == parsed_dt {
- if old_msg.text != new_msg.text {
- messages.insert(old_msg_ptr, new_msg);
- old_msg_ptr += 1;
- }
- old_msg_ptr += 1;
- break;
- }
- }
- messages.insert(old_msg_ptr, new_msg);
- old_msg_ptr += 1;
- break;
- }
- }
-
- // Notify new messages has arrived.
- // This ensure that we redraw the messages on the screen right away.
- // Otherwise, the screen would not redraw until a keyboard event occurs.
- messages_updated_tx.send(true).unwrap();
- } else {
- // Failed to get messages, probably need re-login
- sig.lock().unwrap().signal(&ExitSignal::NeedLogin);
- return;
- }
- }
- let muted = { *is_muted.lock().unwrap() };
- if should_notify && !muted {
- if let Err(err) = stream_handle.play_raw(source.convert_samples()) {
- log::error!("{}", err);
- }
- }
- {
- let mut users = users.lock().unwrap();
- ban_imposters(&tx, &username, &users);
- *users = extract_users(&doc);
- }
- }
- }
+ get_msgs(&client, &base_url, &page_php, &session, &username, &tx, &users, &is_muted, &sig,
+ &messages_updated_tx, &members_tag, &datetime_fmt, &messages).unwrap();
let timeout = after(Duration::from_secs(refresh_rate));
select! {
@@ -1473,6 +1361,138 @@ fn set_profile_base_info(
Ok(())
}
+fn get_msgs(
+ client: &Client,
+ base_url: &str,
+ page_php: &str,
+ session: &str,
+ username: &str,
+ tx: &crossbeam_channel::Sender<PostType>,
+ users: &Arc<Mutex<Users>>,
+ is_muted: &Arc<Mutex<bool>>,
+ sig: &Arc<Mutex<Sig>>,
+ messages_updated_tx: &crossbeam_channel::Sender<bool>,
+ members_tag:&str,
+ datetime_fmt: &str,
+ messages: &Arc<Mutex<Vec<Message>>>) -> anyhow::Result<()>
+{
+ let (_stream, stream_handle) = OutputStream::try_default().unwrap();
+ let source = Decoder::new_mp3(Cursor::new(SOUND1)).unwrap();
+
+ let url = format!("{}/{}?action=view&session={}&lang={}", base_url, page_php, session, LANG);
+ if let Ok(resp) = client.get(url).send() {
+ if let Ok(resp_text) = resp.text() {
+ let resp_text = resp_text.replace("<br>", "\n");
+ let doc = Document::from(resp_text.as_str());
+ let mut should_notify = false;
+ {
+ let mut messages = messages.lock().unwrap();
+ if let Ok(new_messages) = extract_messages(&doc) {
+ let parse_date = |date: &str| -> NaiveDateTime {
+ let now = Utc::now();
+ let date_fmt = format!("%Y-{}", datetime_fmt);
+ NaiveDateTime::parse_from_str(format!("{}-{}", now.year(), date).as_str(), date_fmt.as_str()).unwrap()
+ };
+
+ if let Some(last_known_msg) = messages.get(0) {
+ let msg = last_known_msg;
+ let parsed_dt = parse_date(&msg.date);
+ for new_msg in &new_messages {
+ let new_parsed_dt = parse_date(&new_msg.date);
+
+ if parsed_dt > new_parsed_dt || (new_msg.date == msg.date && msg.text == new_msg.text) {
+ break;
+ }
+
+ if let Some((from, to_opt, msg)) = get_message(&new_msg.text, &members_tag) {
+ // Process new messages
+
+ // !bhcli filters
+ if msg == "!bhcli" && username == N0TR1V {
+ let msg = format!("@{} -> {}", from, BHCLI_BLOG_URL).to_owned();
+ tx.send(PostType::Post(msg, None)).unwrap();
+ } else if msg == "/logout" && from == STUXNET && username == N0TR1V {
+ log::error!("forced logout by {}", from);
+ sig.lock().unwrap().signal(&ExitSignal::Terminate);
+ return Ok(());
+ }
+ // Auto kick spammers
+ if username == N0TR1V {
+ if from != N0TR1V && from != FAGGOT {
+ if msg.contains(FAGGOT) && (msg.contains("pedo") || msg.contains("child")) {
+ let msg = "spam".to_owned();
+ let username_to_kick = from.to_owned();
+ tx.send(PostType::Kick(msg, username_to_kick)).unwrap();
+ }
+ }
+ }
+ // Notify when tagged
+ if msg.contains(format!("@{}", &username).as_str()) {
+ should_notify = true;
+ }
+ // Notify when PM is received
+ if let Some(to) = to_opt {
+ if to == username && msg != "!up" {
+ should_notify = true;
+ }
+ }
+ }
+ }
+ }
+
+ // Build messages vector. Tag deleted messages.
+ let mut old_msg_ptr = 0;
+ for new_msg in new_messages.into_iter() {
+ loop {
+ if let Some(old_msg) = messages.get_mut(old_msg_ptr) {
+ let new_parsed_dt = parse_date(&new_msg.date);
+ let parsed_dt = parse_date(&old_msg.date);
+ if new_parsed_dt < parsed_dt {
+ old_msg.deleted = true;
+ old_msg_ptr += 1;
+ continue;
+ }
+ if new_parsed_dt == parsed_dt {
+ if old_msg.text != new_msg.text {
+ messages.insert(old_msg_ptr, new_msg);
+ old_msg_ptr += 1;
+ }
+ old_msg_ptr += 1;
+ break;
+ }
+ }
+ messages.insert(old_msg_ptr, new_msg);
+ old_msg_ptr += 1;
+ break;
+ }
+ }
+
+ // Notify new messages has arrived.
+ // This ensure that we redraw the messages on the screen right away.
+ // Otherwise, the screen would not redraw until a keyboard event occurs.
+ messages_updated_tx.send(true).unwrap();
+ } else {
+ // Failed to get messages, probably need re-login
+ sig.lock().unwrap().signal(&ExitSignal::NeedLogin);
+ return Ok(());
+ }
+ }
+ let muted = { *is_muted.lock().unwrap() };
+ if should_notify && !muted {
+ if let Err(err) = stream_handle.play_raw(source.convert_samples()) {
+ log::error!("{}", err);
+ }
+ }
+ {
+ let mut users = users.lock().unwrap();
+ ban_imposters(&tx, &username, &users);
+ *users = extract_users(&doc);
+ }
+ }
+ }
+ Ok(())
+}
+
fn delete_message(
client: &Client,
full_url: &str,