diff options
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..1b9a63c --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,76 @@ +use log::{debug, info, warn}; +use std::env; +use tdlib_rs::{Client, Error}; +use crate::client_ext::{ClientExt, AuthorizationState, TdlibParameters, PhoneNumberAuthenticationSettings}; + +fn make_lib_params() -> TdlibParameters { + let cache = env::current_dir().unwrap().join("cache").join("katya"); + let make_path = |p: &str| cache.join(p).to_str().unwrap().to_owned(); + let use_test_dc = env::var("TEST_DC").unwrap() == "yes"; + if !use_test_dc { + warn!("doin prod shit"); + } + TdlibParameters { + use_test_dc, + database_directory: make_path("database"), + files_directory: make_path("files"), + use_file_database: true, + use_chat_info_database: true, + use_message_database: true, + use_secret_chats: false, + api_id: env::var("API_ID").unwrap().parse::<i32>().unwrap(), + api_hash: env::var("API_HASH").unwrap(), + system_language_code: "en".to_owned(), + device_model: "mbia".to_owned(), + system_version: "15".to_owned(), + application_version: "0.1".to_owned(), + enable_storage_optimizer: false, + ignore_file_names: true + } +} + +pub async fn continue_auth(client: &Client, auth_state: AuthorizationState) -> Result<(), Error> { + match auth_state { + AuthorizationState::AuthorizationStateWaitTdlibParameters => { + let params = make_lib_params(); + client + .set_tdlib_parameters(params) + .await?; + debug!("sent tdlib params"); + } + AuthorizationState::AuthorizationStateWaitEncryptionKey(_) => { + client + .set_database_encryption_key("c2VjcmV0Cg==".to_owned() /* base64("secret") */ ) + .await?; + debug!("set encryption key"); + } + AuthorizationState::AuthorizationStateWaitPhoneNumber => { + let phone = env::var("PHONE").unwrap(); + warn!("logging with phone {}", phone); + client.set_authentication_phone_number(phone, PhoneNumberAuthenticationSettings { + allow_flash_call: false, + is_current_phone_number: false, + allow_sms_retriever_api: false, + }).await?; + debug!("send phone"); + } + AuthorizationState::AuthorizationStateWaitCode(_) => { + let code = { + let mut s = String::new(); + print!("code >>> "); + std::io::stdin() + .read_line(&mut s) + .expect("Could not read line"); + s + }; + client.check_authentication_code(code).await?; + debug!("send code"); + } + AuthorizationState::AuthorizationStateWaitPassword(_) => { + client.check_authentication_password(env::var("TG_PASS").unwrap()).await?; + debug!("send passwd") + } + _ => info!("auth state unknown: {:#?}", auth_state), + } + Ok(()) +} |