| | |
| |
|
| | use crate::provider::{Protocol, Socket}; |
| |
|
| | |
| | |
| | |
| | |
| | #[derive(Debug, Clone, PartialEq)] |
| | pub(crate) struct ServerParams { |
| | |
| | pub protocol: Protocol, |
| |
|
| | |
| | pub hostname: String, |
| |
|
| | |
| | pub port: u16, |
| |
|
| | |
| | pub socket: Socket, |
| |
|
| | |
| | pub username: String, |
| | } |
| |
|
| | impl ServerParams { |
| | fn expand_usernames(self, addr: &str) -> Vec<ServerParams> { |
| | if self.username.is_empty() { |
| | vec![Self { |
| | username: addr.to_string(), |
| | ..self.clone() |
| | }] |
| | } else { |
| | vec![self] |
| | } |
| | } |
| |
|
| | fn expand_hostnames(self, param_domain: &str) -> Vec<ServerParams> { |
| | if self.hostname.is_empty() { |
| | vec![ |
| | |
| | |
| | Self { |
| | hostname: match self.protocol { |
| | Protocol::Imap => "imap.".to_string() + param_domain, |
| | Protocol::Smtp => "smtp.".to_string() + param_domain, |
| | }, |
| | ..self.clone() |
| | }, |
| | Self { |
| | hostname: "mail.".to_string() + param_domain, |
| | ..self.clone() |
| | }, |
| | |
| | |
| | Self { |
| | hostname: param_domain.to_string(), |
| | ..self |
| | }, |
| | ] |
| | } else { |
| | vec![self] |
| | } |
| | } |
| |
|
| | fn expand_ports(mut self) -> Vec<ServerParams> { |
| | |
| | if self.port == 0 { |
| | self.port = match self.socket { |
| | Socket::Ssl => match self.protocol { |
| | Protocol::Imap => 993, |
| | Protocol::Smtp => 465, |
| | }, |
| | Socket::Starttls | Socket::Plain => match self.protocol { |
| | Protocol::Imap => 143, |
| | Protocol::Smtp => 587, |
| | }, |
| | Socket::Automatic => 0, |
| | } |
| | } |
| |
|
| | if self.port == 0 { |
| | |
| | |
| | |
| |
|
| | vec![ |
| | |
| | Self { |
| | socket: Socket::Ssl, |
| | port: match self.protocol { |
| | Protocol::Imap => 993, |
| | Protocol::Smtp => 465, |
| | }, |
| | ..self.clone() |
| | }, |
| | |
| | Self { |
| | socket: Socket::Starttls, |
| | port: match self.protocol { |
| | Protocol::Imap => 143, |
| | Protocol::Smtp => 587, |
| | }, |
| | ..self |
| | }, |
| | ] |
| | } else if self.socket == Socket::Automatic { |
| | vec![ |
| | |
| | Self { |
| | socket: Socket::Ssl, |
| | ..self.clone() |
| | }, |
| | |
| | Self { |
| | socket: Socket::Starttls, |
| | ..self |
| | }, |
| | ] |
| | } else { |
| | vec![self] |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | pub(crate) fn expand_param_vector( |
| | v: Vec<ServerParams>, |
| | addr: &str, |
| | domain: &str, |
| | ) -> Vec<ServerParams> { |
| | v.into_iter() |
| | |
| | |
| | |
| | .flat_map(|params| params.expand_usernames(addr).into_iter()) |
| | .flat_map(|params| params.expand_hostnames(domain).into_iter()) |
| | .flat_map(|params| params.expand_ports().into_iter()) |
| | .collect() |
| | } |
| |
|
| | #[cfg(test)] |
| | mod tests { |
| | use super::*; |
| |
|
| | #[test] |
| | fn test_expand_param_vector() { |
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 0, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| |
|
| | assert_eq!( |
| | v, |
| | vec![ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 993, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }], |
| | ); |
| |
|
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 123, |
| | socket: Socket::Automatic, |
| | username: "foobar".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| |
|
| | assert_eq!( |
| | v, |
| | vec![ |
| | ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 123, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }, |
| | ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 123, |
| | socket: Socket::Starttls, |
| | username: "foobar".to_string(), |
| | }, |
| | ], |
| | ); |
| |
|
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 123, |
| | socket: Socket::Plain, |
| | username: "foobar".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| | assert_eq!( |
| | v, |
| | vec![ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 123, |
| | socket: Socket::Plain, |
| | username: "foobar".to_string(), |
| | }], |
| | ); |
| |
|
| | |
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "".to_string(), |
| | port: 10480, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| | assert_eq!( |
| | v, |
| | vec![ |
| | ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "imap.example.net".to_string(), |
| | port: 10480, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }, |
| | ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "mail.example.net".to_string(), |
| | port: 10480, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }, |
| | ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 10480, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | } |
| | ], |
| | ); |
| |
|
| | |
| | |
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 0, |
| | socket: Socket::Automatic, |
| | username: "foobar".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| | assert_eq!( |
| | v, |
| | vec![ |
| | ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 465, |
| | socket: Socket::Ssl, |
| | username: "foobar".to_string(), |
| | }, |
| | ServerParams { |
| | protocol: Protocol::Smtp, |
| | hostname: "example.net".to_string(), |
| | port: 587, |
| | socket: Socket::Starttls, |
| | username: "foobar".to_string(), |
| | }, |
| | ], |
| | ); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | let v = expand_param_vector( |
| | vec![ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 0, |
| | socket: Socket::Automatic, |
| | username: "".to_string(), |
| | }], |
| | "foobar@example.net", |
| | "example.net", |
| | ); |
| | assert_eq!( |
| | v, |
| | vec![ |
| | ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 993, |
| | socket: Socket::Ssl, |
| | username: "foobar@example.net".to_string(), |
| | }, |
| | ServerParams { |
| | protocol: Protocol::Imap, |
| | hostname: "example.net".to_string(), |
| | port: 143, |
| | socket: Socket::Starttls, |
| | username: "foobar@example.net".to_string(), |
| | }, |
| | ], |
| | ); |
| | } |
| | } |
| |
|