commit 48f43e1a6e0b2fc4f03a615d6decf7d1453638e5
parent d61ca3d5b54ec562208b34bf233a4b298e547e2c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 27 Mar 2023 17:59:07 -0700
simplify code
Diffstat:
| M | src/main.rs | | | 101 | +++++++++++++++++++++++++------------------------------------------------------ |
1 file changed, 32 insertions(+), 69 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -1804,35 +1804,31 @@ fn ban_imposters(tx: &crossbeam_channel::Sender<PostType>, account_username: &st
}
}
-struct CustomClient {
- le_chat_php_client: LeChatPHPClient,
-}
-
-impl ChatClient for CustomClient {
- fn run_forever(&mut self) {
- self.le_chat_php_client.run_forever();
- }
-}
-
-impl CustomClient {
+impl ChatClient {
fn new(params: Params) -> Self {
let mut c = new_default_le_chat_php_client(params.clone());
- c.config.url = params.url.unwrap_or("".to_owned());
- c.config.page_php = params.page_php.unwrap_or("chat.php".to_owned());
- c.config.datetime_fmt = params.datetime_fmt.unwrap_or("%m-%d %H:%M:%S".to_owned());
- c.config.members_tag = params.members_tag.unwrap_or("[M] ".to_owned());
- c.config.keepalive_send_to = None;
+ match params.chat_type {
+ ClientType::Custom => {
+ c.config.url = params.url.unwrap_or("".to_owned());
+ c.config.page_php = params.page_php.unwrap_or("chat.php".to_owned());
+ c.config.datetime_fmt = params.datetime_fmt.unwrap_or("%m-%d %H:%M:%S".to_owned());
+ c.config.members_tag = params.members_tag.unwrap_or("[M] ".to_owned());
+ c.config.keepalive_send_to = None;
+ },
+ ClientType::Dan => {
+ c.config = LeChatPHPConfig::new_dans_chat_config();
+ c.manual_captcha = true;
+ },
+ ClientType::BHC => {
+ c.config = LeChatPHPConfig::new_black_hat_chat_config();
+ c.manual_captcha = true;
+ }
+ }
Self {
le_chat_php_client: c,
}
}
-}
-struct BHClient {
- le_chat_php_client: LeChatPHPClient,
-}
-
-impl ChatClient for BHClient {
fn run_forever(&mut self) {
self.le_chat_php_client.run_forever();
}
@@ -1865,44 +1861,13 @@ fn new_default_le_chat_php_client(params: Params) -> LeChatPHPClient {
}
}
-impl BHClient {
- fn new(params: Params) -> Self {
- let mut c = new_default_le_chat_php_client(params);
- c.config = LeChatPHPConfig::new_black_hat_chat_config();
- c.manual_captcha = true;
- Self {
- le_chat_php_client: c,
- }
- }
-}
-
-trait ChatClient {
- fn run_forever(&mut self);
-}
-
-struct DanClient {
+struct ChatClient {
le_chat_php_client: LeChatPHPClient,
}
-impl ChatClient for DanClient {
- fn run_forever(&mut self) {
- self.le_chat_php_client.run_forever();
- }
-}
-
-impl DanClient {
- fn new(params: Params) -> Self {
- let mut c = new_default_le_chat_php_client(params);
- c.config = LeChatPHPConfig::new_dans_chat_config();
- c.manual_captcha = true;
- Self {
- le_chat_php_client: c,
- }
- }
-}
-
#[derive(Debug, Clone)]
struct Params {
+ chat_type: ClientType,
url: Option<String>,
page_php: Option<String>,
datetime_fmt: Option<String>,
@@ -2021,6 +1986,7 @@ fn ask_password(password: Option<String>) -> String {
})
}
+#[derive(Debug, Clone)]
enum ClientType {
BHC,
Dan,
@@ -2138,7 +2104,17 @@ fn main() -> Result<()> {
let guest_color = get_guest_color(opts.guest_color);
let username = ask_username(opts.username);
let password = ask_password(opts.password);
+
+ let chat_type = if opts.url.is_some() {
+ ClientType::Custom
+ } else if opts.dan {
+ ClientType::Dan
+ } else {
+ ClientType::BHC
+ };
+
let params = Params {
+ chat_type,
url: opts.url,
page_php: opts.page_php,
datetime_fmt: opts.datetime_fmt,
@@ -2153,20 +2129,7 @@ fn main() -> Result<()> {
max_login_retry: opts.max_login_retry,
};
- let chat_type = if params.url.is_some() {
- ClientType::Custom
- } else if opts.dan {
- ClientType::Dan
- } else {
- ClientType::BHC
- };
-
- let mut chat_client: Box<dyn ChatClient> = match chat_type {
- ClientType::Custom => Box::new(CustomClient::new(params)),
- ClientType::BHC => Box::new(BHClient::new(params)),
- ClientType::Dan => Box::new(DanClient::new(params)),
- };
- chat_client.run_forever();
+ ChatClient::new(params).run_forever();
Ok(())
}