Skip to content
Extraits de code Groupes Projets
Vérifiée Valider a1c6fffe rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Use the new Config module

parent 9fb998b3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#![allow(unused_imports)] #![allow(unused_imports)]
extern crate ini; mod config;
use crate::config::Config;
use ini::Ini;
use matrix_sdk::{ use matrix_sdk::{
ruma::{ ruma::{
events::{room::message::MessageEventContent, SyncMessageEvent}, events::{room::message::MessageEventContent, SyncMessageEvent},
...@@ -10,48 +10,62 @@ use matrix_sdk::{ ...@@ -10,48 +10,62 @@ use matrix_sdk::{
}, },
Client, Result, SyncSettings, Client, Result, SyncSettings,
}; };
use std::path::Path;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::{env, process::exit}; use std::{env, process::exit};
use url::Url;
/* a def conf creation (TODO):
let mut conf = Ini::new();
conf.with_section(None::<String>)
.set("encoding", "utf-8");
conf.with_section(Some("User"))
.set("given_name", "Tommy")
.set("family_name", "Green")
.set("unicode", "Raspberry树莓");
conf.with_section(Some("Book"))
.set("name", "Rust cool");
conf.write_to_file("conf.ini").unwrap();
*/
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let config_file = "conf.ini"; // TODO if env::args().len() != 2 {
let default_file = env::args()
let conf = Ini::load_from_file(config_file).unwrap(); .nth(0)
let user_conf = conf.section(Some("user")).unwrap(); .unwrap()
let user_id = user_conf.get("id").unwrap(); .split(std::path::MAIN_SEPARATOR)
let user_token = user_conf.get("password").unwrap(); .last()
let homeserver = user_conf.get("homeserver").unwrap(); .unwrap()
.to_string()
+ ".yml";
if Path::new(&default_file).exists() {
panic!(
"Args count is {}, you need to only specify the config file for the bot",
env::args().len() - 1
);
} else {
match config::write_default(&default_file) {
Ok(()) => panic!(
"Args count is {}, you need to only specify the config file for the bot. \
The default config file was written to '{}'",
env::args().len() - 1,
default_file
),
Err(e) => panic!(
"Args count is {}, you need to only specify the config file for the bot. \
The default config file '{}' could not be written: {}",
env::args().len() - 1,
default_file,
e
),
}
}
}
let alice = UserId::try_from(user_id)?; let config_file = env::args().last().unwrap();
println!("User id created: {}", alice);
return match Url::parse(homeserver) { return match config::from_file(&config_file) {
Err(e) => panic!("Invalid homeserver url: {}", e), Ok(config) => {
Ok(homeserver_url) => { let alice = UserId::try_from(config.user_name)?;
println!("Try to login to: {}", homeserver_url); let client = Client::new(config.homeserver_url)?;
let client = Client::new(homeserver_url)?;
client client
.login(alice.localpart(), user_token, None, Some("Test BOT")) .login(
alice.localpart(),
config.user_password.as_str(),
None,
Some(config.display_name.as_str()),
)
.await?; .await?;
println!("Logged in! Try to register event handler");
client client
.register_event_handler(|ev: SyncMessageEvent<MessageEventContent>| async move { .register_event_handler(|ev: SyncMessageEvent<MessageEventContent>| async move {
println!("Received a message {:?}", ev); println!("Received a message {:?}", ev);
...@@ -61,5 +75,9 @@ async fn main() -> Result<()> { ...@@ -61,5 +75,9 @@ async fn main() -> Result<()> {
client.sync(SyncSettings::default()).await; // Should never return client.sync(SyncSettings::default()).await; // Should never return
Ok(()) Ok(())
} }
Err(e) => panic!(
"Failed to read config file '{}' with error: {}",
config_file, e
),
}; };
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter