summaryrefslogtreecommitdiffstats
path: root/src/auth.rs
blob: 9417a0c4cd24f209cda2f66c99fe01b13cd39de6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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(env::var("NAME").unwrap());
    let make_path = |p: &str| cache.join(p).to_str().unwrap().to_owned();
    let use_test_dc = false;
    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")
        }
        AuthorizationState::AuthorizationStateReady => {
            info!("!!!auth completed!!!");
        }
        _ => info!("auth state unknown: {:#?}", auth_state),
    }
    Ok(())
}