matrix-google-calendar

Bot to manage meetings, pushrules, matrix status as well as showing daily agenda and allowing to shedule stuff.
git clone git://archive.git.mtrnord.blog/MTRNord/matrix-google-calendar.git
Log | Files | Refs

mod.rs (2752B)


      1 use crate::config::Config;
      2 use matrix_sdk::{Client, ClientConfig, Session as SDKSession, SyncSettings};
      3 use mrsbfh::url::Url;
      4 use mrsbfh::utils::Session;
      5 use std::convert::TryFrom;
      6 use std::error::Error;
      7 use std::fs;
      8 use std::path::Path;
      9 use tracing::*;
     10 
     11 mod sync;
     12 
     13 pub async fn setup(config: Config<'_>) -> Result<Client, Box<dyn Error>> {
     14     info!("Beginning Matrix Setup");
     15     let store_path_string = config.store_path.to_string();
     16     let store_path = Path::new(&store_path_string);
     17     if !store_path.exists() {
     18         fs::create_dir_all(store_path)?;
     19     }
     20     let client_config = ClientConfig::new().store_path(fs::canonicalize(&store_path)?);
     21 
     22     let homeserver_url =
     23         Url::parse(&config.homeserver_url).expect("Couldn't parse the homeserver URL");
     24 
     25     let client = Client::new_with_config(homeserver_url, client_config).unwrap();
     26 
     27     if let Some(session) = Session::load(config.session_path.parse().unwrap()) {
     28         info!("Starting relogin");
     29 
     30         let session = SDKSession {
     31             access_token: session.access_token,
     32             device_id: session.device_id.into(),
     33             user_id: matrix_sdk::identifiers::UserId::try_from(session.user_id.as_str()).unwrap(),
     34         };
     35 
     36         if let Err(e) = client.restore_login(session).await {
     37             error!("{}", e);
     38         };
     39         info!("Finished relogin");
     40     } else {
     41         info!("Starting login");
     42         let login_response = client
     43             .login(
     44                 &config.mxid,
     45                 &config.password,
     46                 None,
     47                 Some(&"matrix-calendar-bot".to_string()),
     48             )
     49             .await;
     50         match login_response {
     51             Ok(login_response) => {
     52                 info!("Session: {:#?}", login_response);
     53                 let session = Session {
     54                     homeserver: client.homeserver().await.to_string(),
     55                     user_id: login_response.user_id.to_string(),
     56                     access_token: login_response.access_token,
     57                     device_id: login_response.device_id.into(),
     58                 };
     59                 session.save(config.session_path.parse().unwrap())?;
     60             }
     61             Err(e) => error!("Error while login: {}", e),
     62         }
     63         info!("Finished login");
     64     }
     65 
     66     info!("logged in as {}", config.mxid);
     67     if crate::MATRIX_CLIENT.set(client.clone()).is_err() {
     68         error!("Failed to globally set matrix client");
     69     };
     70 
     71     Ok(client)
     72 }
     73 
     74 pub async fn start_sync(client: Client, config: Config<'static>) -> Result<(), Box<dyn Error>> {
     75     client
     76         .set_event_handler(Box::new(sync::Bot::new(client.clone(), config.clone())))
     77         .await;
     78 
     79     info!("Starting Sync...");
     80     client.sync(SyncSettings::default()).await;
     81     Ok(())
     82 }