set up imap connection

This commit is contained in:
Clément DOUIN
2020-12-25 22:39:16 +01:00
parent 37a78c3783
commit 2a567040ac
6 changed files with 552 additions and 12 deletions
+26 -8
View File
@@ -7,18 +7,36 @@ use toml;
#[derive(Debug, Deserialize)]
pub struct ServerInfo {
host: String,
port: usize,
login: String,
password: String,
pub host: String,
pub port: u16,
pub login: String,
pub password: String,
}
impl ServerInfo {
pub fn get_host(&self) -> &str {
&self.host[..]
}
pub fn get_addr(&self) -> (&str, u16) {
(&self.host[..], self.port)
}
pub fn get_login(&self) -> &str {
&self.login[..]
}
pub fn get_password(&self) -> &str {
&self.password[..]
}
}
#[derive(Debug, Deserialize)]
pub struct Config {
name: String,
email: String,
imap: ServerInfo,
smtp: ServerInfo,
pub name: String,
pub email: String,
pub imap: ServerInfo,
pub smtp: ServerInfo,
}
pub fn from_xdg() -> Option<PathBuf> {
+45
View File
@@ -0,0 +1,45 @@
use imap;
use native_tls::{TlsConnector, TlsStream};
use std::net::TcpStream;
use crate::config::{Config, ServerInfo};
type ImapClient = imap::Client<TlsStream<TcpStream>>;
type ImapSession = imap::Session<TlsStream<TcpStream>>;
pub fn create_tls_connector() -> TlsConnector {
match native_tls::TlsConnector::new() {
Ok(connector) => connector,
Err(err) => {
println!("The TLS connector could not be created.");
panic!(err);
}
}
}
pub fn create_imap_client(server: &ServerInfo, tls: &TlsConnector) -> ImapClient {
match imap::connect(server.get_addr(), server.get_host(), &tls) {
Ok(client) => client,
Err(err) => {
println!("The IMAP socket could not be opened.");
panic!(err);
}
}
}
pub fn create_imap_sess(client: ImapClient, server: &ServerInfo) -> ImapSession {
match client.login(server.get_login(), server.get_password()) {
Ok(sess) => sess,
Err((err, _)) => {
println!("The IMAP connection could not be established.");
panic!(err);
}
}
}
pub fn login(config: &Config) -> ImapSession {
let tls = create_tls_connector();
let client = create_imap_client(&config.imap, &tls);
let sess = create_imap_sess(client, &config.imap);
sess
}
+4 -1
View File
@@ -1,5 +1,8 @@
mod config;
mod imap;
fn main() {
println!("{:?}", config::read_file());
let config = config::read_file();
let sess = imap::login(&config);
println!("{:?}", sess);
}