Commit 51099ecc4c
Unsigned
Layout: unified · split
src/server.rs +52 −2
| @@ -102,6 +102,8 @@ pub struct ServerSettings { | |||
| 102 | homeserver: Option<Url>, | 102 | homeserver: Option<Url>, |
| 103 | proxy: Option<Url>, | 103 | proxy: Option<Url>, |
| 104 | autoconnect: bool, | 104 | autoconnect: bool, |
| 105 | username: String, | ||
| 106 | password: String, | ||
| 105 | } | 107 | } |
| 106 | 108 | ||
| 107 | impl ServerSettings { | 109 | impl ServerSettings { |
| @@ -253,6 +255,7 @@ impl MatrixServer { | |||
| 253 | .expect("Can't create homeserver option"); | 255 | .expect("Can't create homeserver option"); |
| 254 | 256 | ||
| 255 | let server = server_copy; | 257 | let server = server_copy; |
| 258 | let server_copy = server.clone(); | ||
| 256 | 259 | ||
| 257 | let proxy = StringOptionSettings::new(format!("{}.proxy", server_name)) | 260 | let proxy = StringOptionSettings::new(format!("{}.proxy", server_name)) |
| 258 | .set_check_callback(|_, _, value| { | 261 | .set_check_callback(|_, _, value| { |
| @@ -280,6 +283,45 @@ impl MatrixServer { | |||
| 280 | server_section | 283 | server_section |
| 281 | .new_string_option(proxy) | 284 | .new_string_option(proxy) |
| 282 | .expect("Can't create proxy option"); | 285 | .expect("Can't create proxy option"); |
| 286 | |||
| 287 | let server = server_copy; | ||
| 288 | let server_copy = server.clone(); | ||
| 289 | |||
| 290 | let username = | ||
| 291 | StringOptionSettings::new(format!("{}.username", server_name)) | ||
| 292 | .set_change_callback(move |_, option| { | ||
| 293 | let server = server.clone(); | ||
| 294 | |||
| 295 | let server_ref = server.upgrade().expect( | ||
| 296 | "Server got deleted while server config is alive" | ||
| 297 | ); | ||
| 298 | |||
| 299 | let mut server = server_ref.borrow_mut(); | ||
| 300 | server.settings.username = option.value().to_string(); | ||
| 301 | }); | ||
| 302 | |||
| 303 | server_section | ||
| 304 | .new_string_option(username) | ||
| 305 | .expect("Can't create username option"); | ||
| 306 | |||
| 307 | let server = server_copy; | ||
| 308 | |||
| 309 | let password = | ||
| 310 | StringOptionSettings::new(format!("{}.password", server_name)) | ||
| 311 | .set_change_callback(move |_, option| { | ||
| 312 | let server = server.clone(); | ||
| 313 | |||
| 314 | let server_ref = server.upgrade().expect( | ||
| 315 | "Server got deleted while server config is alive" | ||
| 316 | ); | ||
| 317 | |||
| 318 | let mut server = server_ref.borrow_mut(); | ||
| 319 | server.settings.password = option.value().to_string(); | ||
| 320 | }); | ||
| 321 | |||
| 322 | server_section | ||
| 323 | .new_string_option(password) | ||
| 324 | .expect("Can't create password option"); | ||
| 283 | } | 325 | } |
| 284 | 326 | ||
| 285 | pub fn connected(&self) -> bool { | 327 | pub fn connected(&self) -> bool { |
| @@ -424,7 +466,13 @@ impl MatrixServer { | |||
| 424 | }; | 466 | }; |
| 425 | 467 | ||
| 426 | let (tx, rx) = async_channel(1000); | 468 | let (tx, rx) = async_channel(1000); |
| 427 | runtime.spawn(MatrixServer::sync_loop(client.clone(), tx)); | 469 | runtime.spawn( |
| 470 | MatrixServer::sync_loop( | ||
| 471 | client.clone(), | ||
| 472 | tx, | ||
| 473 | server.settings.username.to_string(), | ||
| 474 | server.settings.password.to_string(), | ||
| 475 | )); | ||
| 428 | let response_receiver = Weechat::spawn( | 476 | let response_receiver = Weechat::spawn( |
| 429 | MatrixServer::response_receiver(rx, Rc::downgrade(&self.inner)), | 477 | MatrixServer::response_receiver(rx, Rc::downgrade(&self.inner)), |
| 430 | ); | 478 | ); |
| @@ -475,9 +523,11 @@ impl MatrixServer { | |||
| 475 | pub async fn sync_loop( | 523 | pub async fn sync_loop( |
| 476 | mut client: AsyncClient, | 524 | mut client: AsyncClient, |
| 477 | channel: Sender<Result<ThreadMessage, String>>, | 525 | channel: Sender<Result<ThreadMessage, String>>, |
| 526 | username: String, | ||
| 527 | password: String, | ||
| 478 | ) { | 528 | ) { |
| 479 | if !client.logged_in().await { | 529 | if !client.logged_in().await { |
| 480 | let ret = client.login("example", "wordpass", None).await; | 530 | let ret = client.login(username, password, None).await; |
| 481 | 531 | ||
| 482 | match ret { | 532 | match ret { |
| 483 | Ok(response) => { | 533 | Ok(response) => { |