mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
smtp: replace thiserror with anyhow
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
### Changes
|
### Changes
|
||||||
- don't watch Sent folder by default #3025
|
- don't watch Sent folder by default #3025
|
||||||
- use webxdc app name in chatlist/quotes/replies etc. #3027
|
- use webxdc app name in chatlist/quotes/replies etc. #3027
|
||||||
|
- refactorings #3023
|
||||||
|
|
||||||
|
|
||||||
## 1.72.0
|
## 1.72.0
|
||||||
|
|||||||
43
src/smtp.rs
43
src/smtp.rs
@@ -4,10 +4,10 @@ pub mod send;
|
|||||||
|
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
use anyhow::{format_err, Context as _};
|
use anyhow::{bail, format_err, Context as _, Result};
|
||||||
use async_smtp::smtp::client::net::ClientTlsParameters;
|
use async_smtp::smtp::client::net::ClientTlsParameters;
|
||||||
use async_smtp::smtp::response::{Category, Code, Detail};
|
use async_smtp::smtp::response::{Category, Code, Detail};
|
||||||
use async_smtp::{error, smtp, EmailAddress, ServerAddress};
|
use async_smtp::{smtp, EmailAddress, ServerAddress};
|
||||||
|
|
||||||
use crate::constants::DC_LP_AUTH_OAUTH2;
|
use crate::constants::DC_LP_AUTH_OAUTH2;
|
||||||
use crate::events::EventType;
|
use crate::events::EventType;
|
||||||
@@ -23,28 +23,6 @@ use crate::{context::Context, scheduler::connectivity::ConnectivityStore};
|
|||||||
/// SMTP write and read timeout in seconds.
|
/// SMTP write and read timeout in seconds.
|
||||||
const SMTP_TIMEOUT: u64 = 30;
|
const SMTP_TIMEOUT: u64 = 30;
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
|
||||||
pub enum Error {
|
|
||||||
#[error("Bad parameters")]
|
|
||||||
BadParameters,
|
|
||||||
#[error("Invalid login address {address}: {error}")]
|
|
||||||
InvalidLoginAddress {
|
|
||||||
address: String,
|
|
||||||
#[source]
|
|
||||||
error: error::Error,
|
|
||||||
},
|
|
||||||
#[error("SMTP failed to connect: {0}")]
|
|
||||||
ConnectionFailure(#[source] smtp::error::Error),
|
|
||||||
#[error("SMTP oauth2 error {address}")]
|
|
||||||
Oauth2 { address: String },
|
|
||||||
#[error("TLS error {0}")]
|
|
||||||
Tls(#[from] async_native_tls::Error),
|
|
||||||
#[error("{0}")]
|
|
||||||
Other(#[from] anyhow::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(crate) struct Smtp {
|
pub(crate) struct Smtp {
|
||||||
transport: Option<smtp::SmtpTransport>,
|
transport: Option<smtp::SmtpTransport>,
|
||||||
@@ -135,14 +113,11 @@ impl Smtp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if lp.server.is_empty() || lp.port == 0 {
|
if lp.server.is_empty() || lp.port == 0 {
|
||||||
return Err(Error::BadParameters);
|
bail!("bad connection parameters");
|
||||||
}
|
}
|
||||||
|
|
||||||
let from =
|
let from = EmailAddress::new(addr.to_string())
|
||||||
EmailAddress::new(addr.to_string()).map_err(|err| Error::InvalidLoginAddress {
|
.with_context(|| format!("invalid login address {}", addr))?;
|
||||||
address: addr.to_string(),
|
|
||||||
error: err,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
self.from = Some(from);
|
self.from = Some(from);
|
||||||
|
|
||||||
@@ -163,9 +138,7 @@ impl Smtp {
|
|||||||
let send_pw = &lp.password;
|
let send_pw = &lp.password;
|
||||||
let access_token = dc_get_oauth2_access_token(context, addr, send_pw, false).await?;
|
let access_token = dc_get_oauth2_access_token(context, addr, send_pw, false).await?;
|
||||||
if access_token.is_none() {
|
if access_token.is_none() {
|
||||||
return Err(Error::Oauth2 {
|
bail!("SMTP OAuth 2 error {}", addr);
|
||||||
address: addr.to_string(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
let user = &lp.user;
|
let user = &lp.user;
|
||||||
(
|
(
|
||||||
@@ -209,9 +182,7 @@ impl Smtp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut trans = client.into_transport();
|
let mut trans = client.into_transport();
|
||||||
if let Err(err) = trans.connect().await {
|
trans.connect().await.context("SMTP failed to connect")?;
|
||||||
return Err(Error::ConnectionFailure(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.transport = Some(trans);
|
self.transport = Some(trans);
|
||||||
self.last_success = Some(SystemTime::now());
|
self.last_success = Some(SystemTime::now());
|
||||||
|
|||||||
Reference in New Issue
Block a user