Skip to content

Commit 6032a5c

Browse files
author
Alexander Krotov
committed
Do not pass IMAP params to smtp module
Also introduce Smtp.connect_configured.
1 parent a23b04d commit 6032a5c

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

src/configure/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,15 @@ async fn try_smtp_one_param(context: &Context, param: &LoginParam, smtp: &mut Sm
669669
);
670670
info!(context, "Trying: {}", inf);
671671

672-
if let Err(err) = smtp.connect(context, &param).await {
672+
if let Err(err) = smtp
673+
.connect(
674+
context,
675+
&param.smtp,
676+
&param.addr,
677+
param.server_flags & DC_LP_AUTH_OAUTH2 != 0,
678+
)
679+
.await
680+
{
673681
bail!("could not connect: {}", err);
674682
}
675683

src/job.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use crate::error::{bail, ensure, format_err, Error, Result};
2626
use crate::events::EventType;
2727
use crate::imap::*;
2828
use crate::location;
29-
use crate::login_param::LoginParam;
3029
use crate::message::MsgId;
3130
use crate::message::{self, Message, MessageState};
3231
use crate::mimefactory::MimeFactory;
@@ -338,12 +337,9 @@ impl Job {
338337

339338
pub(crate) async fn send_msg_to_smtp(&mut self, context: &Context, smtp: &mut Smtp) -> Status {
340339
// SMTP server, if not yet done
341-
if !smtp.is_connected().await {
342-
let loginparam = LoginParam::from_database(context, "configured_").await;
343-
if let Err(err) = smtp.connect(context, &loginparam).await {
344-
warn!(context, "SMTP connection failure: {:?}", err);
345-
return Status::RetryLater;
346-
}
340+
if let Err(err) = smtp.connect_configured(context).await {
341+
warn!(context, "SMTP connection failure: {:?}", err);
342+
return Status::RetryLater;
347343
}
348344

349345
let filename = job_try!(job_try!(self
@@ -486,12 +482,9 @@ impl Job {
486482
let recipients = vec![recipient];
487483

488484
// connect to SMTP server, if not yet done
489-
if !smtp.is_connected().await {
490-
let loginparam = LoginParam::from_database(context, "configured_").await;
491-
if let Err(err) = smtp.connect(context, &loginparam).await {
492-
warn!(context, "SMTP connection failure: {:?}", err);
493-
return Status::RetryLater;
494-
}
485+
if let Err(err) = smtp.connect_configured(context).await {
486+
warn!(context, "SMTP connection failure: {:?}", err);
487+
return Status::RetryLater;
495488
}
496489

497490
self.smtp_send(context, recipients, body, self.job_id, smtp, || {

src/smtp/mod.rs

+38-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use async_smtp::*;
1010
use crate::constants::*;
1111
use crate::context::Context;
1212
use crate::events::EventType;
13-
use crate::login_param::{dc_build_tls, CertificateChecks, LoginParam};
13+
use crate::login_param::{dc_build_tls, CertificateChecks, LoginParam, ServerLoginParam};
1414
use crate::oauth2::*;
1515
use crate::provider::{get_provider_info, Socket};
1616
use crate::stock::StockMessage;
@@ -94,31 +94,53 @@ impl Smtp {
9494
.unwrap_or_default()
9595
}
9696

97+
/// Connect using configured parameters.
98+
pub async fn connect_configured(&mut self, context: &Context) -> Result<()> {
99+
if self.is_connected().await {
100+
return Ok(());
101+
}
102+
103+
let lp = LoginParam::from_database(context, "configured_").await;
104+
self.connect(
105+
context,
106+
&lp.smtp,
107+
&lp.addr,
108+
lp.server_flags & DC_LP_AUTH_OAUTH2 != 0,
109+
)
110+
.await
111+
}
112+
97113
/// Connect using the provided login params.
98-
pub async fn connect(&mut self, context: &Context, lp: &LoginParam) -> Result<()> {
114+
pub async fn connect(
115+
&mut self,
116+
context: &Context,
117+
lp: &ServerLoginParam,
118+
addr: &str,
119+
oauth2: bool,
120+
) -> Result<()> {
99121
if self.is_connected().await {
100122
warn!(context, "SMTP already connected.");
101123
return Ok(());
102124
}
103125

104-
if lp.smtp.server.is_empty() || lp.smtp.port == 0 {
126+
if lp.server.is_empty() || lp.port == 0 {
105127
context.emit_event(EventType::ErrorNetwork("SMTP bad parameters.".into()));
106128
return Err(Error::BadParameters);
107129
}
108130

109131
let from =
110-
EmailAddress::new(lp.addr.clone()).map_err(|err| Error::InvalidLoginAddress {
111-
address: lp.addr.clone(),
132+
EmailAddress::new(addr.to_string()).map_err(|err| Error::InvalidLoginAddress {
133+
address: addr.to_string(),
112134
error: err,
113135
})?;
114136

115137
self.from = Some(from);
116138

117-
let domain = &lp.smtp.server;
118-
let port = lp.smtp.port;
139+
let domain = &lp.server;
140+
let port = lp.port;
119141

120-
let provider = get_provider_info(&lp.addr);
121-
let strict_tls = match lp.smtp.certificate_checks {
142+
let provider = get_provider_info(addr);
143+
let strict_tls = match lp.certificate_checks {
122144
CertificateChecks::Automatic => provider.map_or(false, |provider| provider.strict_tls),
123145
CertificateChecks::Strict => true,
124146
CertificateChecks::AcceptInvalidCertificates
@@ -127,17 +149,16 @@ impl Smtp {
127149
let tls_config = dc_build_tls(strict_tls);
128150
let tls_parameters = ClientTlsParameters::new(domain.to_string(), tls_config);
129151

130-
let (creds, mechanism) = if 0 != lp.server_flags & (DC_LP_AUTH_OAUTH2 as i32) {
152+
let (creds, mechanism) = if oauth2 {
131153
// oauth2
132-
let addr = &lp.addr;
133-
let send_pw = &lp.smtp.password;
154+
let send_pw = &lp.password;
134155
let access_token = dc_get_oauth2_access_token(context, addr, send_pw, false).await;
135156
if access_token.is_none() {
136157
return Err(Error::Oauth2Error {
137158
address: addr.to_string(),
138159
});
139160
}
140-
let user = &lp.smtp.user;
161+
let user = &lp.user;
141162
(
142163
smtp::authentication::Credentials::new(
143164
user.to_string(),
@@ -147,8 +168,8 @@ impl Smtp {
147168
)
148169
} else {
149170
// plain
150-
let user = lp.smtp.user.clone();
151-
let pw = lp.smtp.password.clone();
171+
let user = lp.user.clone();
172+
let pw = lp.password.clone();
152173
(
153174
smtp::authentication::Credentials::new(user, pw),
154175
vec![
@@ -158,7 +179,7 @@ impl Smtp {
158179
)
159180
};
160181

161-
let security = match lp.smtp.security {
182+
let security = match lp.security {
162183
Socket::STARTTLS | Socket::Plain => smtp::ClientSecurity::Opportunistic(tls_parameters),
163184
_ => smtp::ClientSecurity::Wrapper(tls_parameters),
164185
};
@@ -193,7 +214,7 @@ impl Smtp {
193214

194215
context.emit_event(EventType::SmtpConnected(format!(
195216
"SMTP-LOGIN as {} ok",
196-
lp.smtp.user,
217+
lp.user,
197218
)));
198219

199220
Ok(())

0 commit comments

Comments
 (0)