Commit 31c94e4b19
Unsigned
Layout: unified · split
src/server.rs +55 −23
| @@ -91,13 +91,27 @@ pub enum ServerError { | |||
| 91 | IoError(String), | 91 | IoError(String), |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | #[derive(Default)] | 94 | #[derive(Clone, Debug, PartialEq)] |
| 95 | pub struct ServerSettings { | 95 | pub struct ServerSettings { |
| 96 | pub homeserver: Option<Url>, | 96 | pub homeserver: Option<Url>, |
| 97 | pub proxy: Option<Url>, | 97 | pub proxy: Option<Url>, |
| 98 | pub autoconnect: bool, | 98 | pub autoconnect: bool, |
| 99 | pub username: String, | 99 | pub username: String, |
| 100 | pub password: String, | 100 | pub password: String, |
| 101 | pub ssl_verify: bool, | ||
| 102 | } | ||
| 103 | |||
| 104 | impl Default for ServerSettings { | ||
| 105 | fn default() -> Self { | ||
| 106 | Self { | ||
| 107 | ssl_verify: true, | ||
| 108 | proxy: None, | ||
| 109 | autoconnect: false, | ||
| 110 | homeserver: None, | ||
| 111 | username: "".to_owned(), | ||
| 112 | password: "".to_owned(), | ||
| 113 | } | ||
| 114 | } | ||
| 101 | } | 115 | } |
| 102 | 116 | ||
| 103 | impl ServerSettings { | 117 | impl ServerSettings { |
| @@ -129,6 +143,7 @@ pub struct InnerServer { | |||
| 129 | server_name: Rc<String>, | 143 | server_name: Rc<String>, |
| 130 | rooms: HashMap<RoomId, RoomHandle>, | 144 | rooms: HashMap<RoomId, RoomHandle>, |
| 131 | settings: ServerSettings, | 145 | settings: ServerSettings, |
| 146 | current_settings: ServerSettings, | ||
| 132 | config: ConfigHandle, | 147 | config: ConfigHandle, |
| 133 | client: Option<Client>, | 148 | client: Option<Client>, |
| 134 | login_state: Option<LoginInfo>, | 149 | login_state: Option<LoginInfo>, |
| @@ -148,6 +163,7 @@ impl MatrixServer { | |||
| 148 | server_name: server_name.clone(), | 163 | server_name: server_name.clone(), |
| 149 | rooms: HashMap::new(), | 164 | rooms: HashMap::new(), |
| 150 | settings: ServerSettings::new(), | 165 | settings: ServerSettings::new(), |
| 166 | current_settings: ServerSettings::new(), | ||
| 151 | config: config.clone(), | 167 | config: config.clone(), |
| 152 | client: None, | 168 | client: None, |
| 153 | login_state: None, | 169 | login_state: None, |
| @@ -402,7 +418,6 @@ impl MatrixServer { | |||
| 402 | let autoconnect = | 418 | let autoconnect = |
| 403 | BooleanOptionSettings::new(format!("{}.autoconnect", server_name)) | 419 | BooleanOptionSettings::new(format!("{}.autoconnect", server_name)) |
| 404 | .set_change_callback(move |_, option| { | 420 | .set_change_callback(move |_, option| { |
| 405 | let server = server.clone(); | ||
| 406 | let value = option.value(); | 421 | let value = option.value(); |
| 407 | 422 | ||
| 408 | let server_ref = server.upgrade().expect( | 423 | let server_ref = server.upgrade().expect( |
| @@ -426,7 +441,6 @@ impl MatrixServer { | |||
| 426 | MatrixServer::is_url_valid(&value) | 441 | MatrixServer::is_url_valid(&value) |
| 427 | }) | 442 | }) |
| 428 | .set_change_callback(move |_, option| { | 443 | .set_change_callback(move |_, option| { |
| 429 | let server = server.clone(); | ||
| 430 | let server_ref = server.upgrade().expect( | 444 | let server_ref = server.upgrade().expect( |
| 431 | "Server got deleted while server config is alive", | 445 | "Server got deleted while server config is alive", |
| 432 | ); | 446 | ); |
| @@ -449,7 +463,6 @@ impl MatrixServer { | |||
| 449 | MatrixServer::is_url_valid(&value) | 463 | MatrixServer::is_url_valid(&value) |
| 450 | }) | 464 | }) |
| 451 | .set_change_callback(move |_, option| { | 465 | .set_change_callback(move |_, option| { |
| 452 | let server = server.clone(); | ||
| 453 | let server_ref = server | 466 | let server_ref = server |
| 454 | .upgrade() | 467 | .upgrade() |
| 455 | .expect("Server got deleted while server config is alive"); | 468 | .expect("Server got deleted while server config is alive"); |
| @@ -469,8 +482,6 @@ impl MatrixServer { | |||
| 469 | let username = | 482 | let username = |
| 470 | StringOptionSettings::new(format!("{}.username", server_name)) | 483 | StringOptionSettings::new(format!("{}.username", server_name)) |
| 471 | .set_change_callback(move |_, option| { | 484 | .set_change_callback(move |_, option| { |
| 472 | let server = server.clone(); | ||
| 473 | |||
| 474 | let server_ref = server.upgrade().expect( | 485 | let server_ref = server.upgrade().expect( |
| 475 | "Server got deleted while server config is alive", | 486 | "Server got deleted while server config is alive", |
| 476 | ); | 487 | ); |
| @@ -484,12 +495,11 @@ impl MatrixServer { | |||
| 484 | .expect("Can't create username option"); | 495 | .expect("Can't create username option"); |
| 485 | 496 | ||
| 486 | let server = server_copy; | 497 | let server = server_copy; |
| 498 | let server_copy = server.clone(); | ||
| 487 | 499 | ||
| 488 | let password = | 500 | let password = |
| 489 | StringOptionSettings::new(format!("{}.password", server_name)) | 501 | StringOptionSettings::new(format!("{}.password", server_name)) |
| 490 | .set_change_callback(move |_, option| { | 502 | .set_change_callback(move |_, option| { |
| 491 | let server = server.clone(); | ||
| 492 | |||
| 493 | let server_ref = server.upgrade().expect( | 503 | let server_ref = server.upgrade().expect( |
| 494 | "Server got deleted while server config is alive", | 504 | "Server got deleted while server config is alive", |
| 495 | ); | 505 | ); |
| @@ -501,6 +511,26 @@ impl MatrixServer { | |||
| 501 | server_section | 511 | server_section |
| 502 | .new_string_option(password) | 512 | .new_string_option(password) |
| 503 | .expect("Can't create password option"); | 513 | .expect("Can't create password option"); |
| 514 | |||
| 515 | let server = server_copy; | ||
| 516 | |||
| 517 | let ssl_verify = | ||
| 518 | BooleanOptionSettings::new(format!("{}.ssl_verify", server_name)) | ||
| 519 | .default_value(true) | ||
| 520 | .set_change_callback(move |_, option| { | ||
| 521 | let value = option.value(); | ||
| 522 | |||
| 523 | let server_ref = server.upgrade().expect( | ||
| 524 | "Server got deleted while server config is alive", | ||
| 525 | ); | ||
| 526 | |||
| 527 | let mut server = server_ref.borrow_mut(); | ||
| 528 | server.settings.ssl_verify = value; | ||
| 529 | }); | ||
| 530 | |||
| 531 | server_section | ||
| 532 | .new_boolean_option(ssl_verify) | ||
| 533 | .expect("Can't create autoconnect option"); | ||
| 504 | } | 534 | } |
| 505 | 535 | ||
| 506 | pub fn connected(&self) -> bool { | 536 | pub fn connected(&self) -> bool { |
| @@ -511,6 +541,10 @@ impl MatrixServer { | |||
| 511 | self.inner.borrow().settings.autoconnect | 541 | self.inner.borrow().settings.autoconnect |
| 512 | } | 542 | } |
| 513 | 543 | ||
| 544 | pub fn ssl_verify(&self) -> bool { | ||
| 545 | self.inner.borrow().settings.ssl_verify | ||
| 546 | } | ||
| 547 | |||
| 514 | pub fn connect(&self) -> Result<(), ServerError> { | 548 | pub fn connect(&self) -> Result<(), ServerError> { |
| 515 | if self.connected() { | 549 | if self.connected() { |
| 516 | self.print_error(&format!( | 550 | self.print_error(&format!( |
| @@ -741,15 +775,9 @@ impl InnerServer { | |||
| 741 | 775 | ||
| 742 | // Check if the homeserver setting changed and swap our client if it | 776 | // Check if the homeserver setting changed and swap our client if it |
| 743 | // did. | 777 | // did. |
| 744 | if client.homeserver() | 778 | if self.current_settings != self.settings { |
| 745 | != self | 779 | // TODO if the homeserver changed close all the room buffers of the |
| 746 | .settings | 780 | // server here, they don't belong to our client anymore. |
| 747 | .homeserver | ||
| 748 | .as_ref() | ||
| 749 | .expect("Homeserver URL isn't set") | ||
| 750 | { | ||
| 751 | // TODO close all the room buffers of the server here, they don't | ||
| 752 | // belong to our client anymore. | ||
| 753 | self.create_client() | 781 | self.create_client() |
| 754 | } else { | 782 | } else { |
| 755 | Ok(client) | 783 | Ok(client) |
| @@ -848,8 +876,10 @@ impl InnerServer { | |||
| 848 | } | 876 | } |
| 849 | 877 | ||
| 850 | pub fn create_client(&mut self) -> Result<Client, ServerError> { | 878 | pub fn create_client(&mut self) -> Result<Client, ServerError> { |
| 879 | let settings = self.settings.clone(); | ||
| 880 | |||
| 851 | let homeserver = | 881 | let homeserver = |
| 852 | self.settings.homeserver.as_ref().ok_or_else(|| { | 882 | settings.homeserver.as_ref().ok_or_else(|| { |
| 853 | ServerError::StartError("Homeserver not configured".to_owned()) | 883 | ServerError::StartError("Homeserver not configured".to_owned()) |
| 854 | })?; | 884 | })?; |
| 855 | 885 | ||
| @@ -863,15 +893,17 @@ impl InnerServer { | |||
| 863 | let mut client_config = | 893 | let mut client_config = |
| 864 | ClientConfig::new().store_path(self.get_server_path()); | 894 | ClientConfig::new().store_path(self.get_server_path()); |
| 865 | 895 | ||
| 866 | if let Some(proxy) = &self.settings.proxy { | 896 | if let Some(proxy) = settings.proxy.as_ref() { |
| 867 | client_config = client_config | 897 | client_config = client_config.proxy(proxy.as_str()).unwrap(); |
| 868 | .proxy(proxy.as_str()) | 898 | } |
| 869 | .unwrap() | 899 | |
| 870 | .disable_ssl_verification(); | 900 | if !settings.ssl_verify { |
| 901 | client_config = client_config.disable_ssl_verification(); | ||
| 871 | } | 902 | } |
| 872 | 903 | ||
| 873 | let client = | 904 | let client = |
| 874 | Client::new_with_config(homeserver.clone(), client_config).unwrap(); | 905 | Client::new_with_config(homeserver.clone(), client_config).unwrap(); |
| 906 | self.current_settings = settings; | ||
| 875 | self.client = Some(client.clone()); | 907 | self.client = Some(client.clone()); |
| 876 | 908 | ||
| 877 | Ok(client) | 909 | Ok(client) |