Skip to content

Commit ec3f765

Browse files
committed
refactor(add_transport_from_qr): do not set deprecated config values
1 parent a743ad9 commit ec3f765

File tree

4 files changed

+95
-186
lines changed

4 files changed

+95
-186
lines changed

src/configure.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::login_param::{
3636
use crate::message::Message;
3737
use crate::oauth2::get_oauth2_addr;
3838
use crate::provider::{Protocol, Provider, Socket, UsernamePattern};
39+
use crate::qr::{login_param_from_account_qr, login_param_from_login_qr};
3940
use crate::smtp::Smtp;
4041
use crate::sync::Sync::*;
4142
use crate::tools::time;
@@ -117,7 +118,7 @@ impl Context {
117118
Ok(())
118119
}
119120

120-
async fn add_transport_inner(&self, param: &mut EnteredLoginParam) -> Result<()> {
121+
pub(crate) async fn add_transport_inner(&self, param: &mut EnteredLoginParam) -> Result<()> {
121122
ensure!(
122123
!self.scheduler.is_running().await,
123124
"cannot configure, already running"
@@ -162,20 +163,15 @@ impl Context {
162163
pub async fn add_transport_from_qr(&self, qr: &str) -> Result<()> {
163164
self.stop_io().await;
164165

165-
// This code first sets the deprecated Config::Addr, Config::MailPw, etc.
166-
// and then calls configure(), which loads them again.
167-
// At some point, we will remove configure()
168-
// and then simplify the code
169-
// to directly create an EnteredLoginParam.
170166
let result = async move {
171-
match crate::qr::check_qr(self, qr).await? {
172-
crate::qr::Qr::Account { .. } => crate::qr::set_account_from_qr(self, qr).await?,
167+
let mut param = match crate::qr::check_qr(self, qr).await? {
168+
crate::qr::Qr::Account { .. } => login_param_from_account_qr(self, qr).await?,
173169
crate::qr::Qr::Login { address, options } => {
174-
crate::qr::configure_from_login_qr(self, &address, options).await?
170+
login_param_from_login_qr(&address, options)?
175171
}
176172
_ => bail!("QR code does not contain account"),
177-
}
178-
self.configure().await?;
173+
};
174+
self.add_transport_inner(&mut param).await?;
179175
Ok(())
180176
}
181177
.await;

src/qr.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ use std::sync::LazyLock;
66

77
use anyhow::{Context as _, Result, anyhow, bail, ensure};
88
pub use dclogin_scheme::LoginOptions;
9+
pub(crate) use dclogin_scheme::login_param_from_login_qr;
910
use deltachat_contact_tools::{ContactAddress, addr_normalize, may_be_valid_addr};
1011
use percent_encoding::{NON_ALPHANUMERIC, percent_decode_str, percent_encode};
1112
use serde::Deserialize;
1213

13-
pub(crate) use self::dclogin_scheme::configure_from_login_qr;
1414
use crate::config::Config;
1515
use crate::contact::{Contact, ContactId, Origin};
1616
use crate::context::Context;
1717
use crate::events::EventType;
1818
use crate::key::Fingerprint;
19+
use crate::login_param::{EnteredCertificateChecks, EnteredLoginParam, EnteredServerLoginParam};
1920
use crate::net::http::post_empty;
2021
use crate::net::proxy::{DEFAULT_SOCKS_PORT, ProxyConfig};
2122
use crate::token;
@@ -651,10 +652,13 @@ struct CreateAccountErrorResponse {
651652
reason: String,
652653
}
653654

654-
/// take a qr of the type DC_QR_ACCOUNT, parse it's parameters,
655-
/// download additional information from the contained url and set the parameters.
656-
/// on success, a configure::configure() should be able to log in to the account
657-
pub(crate) async fn set_account_from_qr(context: &Context, qr: &str) -> Result<()> {
655+
/// Takes a QR with `DCACCOUNT:` scheme, parses its parameters,
656+
/// downloads additional information from the contained URL
657+
/// and returns the login parameters.
658+
pub(crate) async fn login_param_from_account_qr(
659+
context: &Context,
660+
qr: &str,
661+
) -> Result<EnteredLoginParam> {
658662
let url_str = qr
659663
.get(DCACCOUNT_SCHEME.len()..)
660664
.context("Invalid DCACCOUNT scheme")?;
@@ -669,14 +673,19 @@ pub(crate) async fn set_account_from_qr(context: &Context, qr: &str) -> Result<(
669673
.with_context(|| {
670674
format!("Cannot create account, response is malformed:\n{response_text:?}")
671675
})?;
672-
context
673-
.set_config_internal(Config::Addr, Some(&email))
674-
.await?;
675-
context
676-
.set_config_internal(Config::MailPw, Some(&password))
677-
.await?;
678676

679-
Ok(())
677+
let param = EnteredLoginParam {
678+
addr: email,
679+
imap: EnteredServerLoginParam {
680+
password,
681+
..Default::default()
682+
},
683+
smtp: Default::default(),
684+
certificate_checks: EnteredCertificateChecks::Strict,
685+
oauth2: false,
686+
};
687+
688+
Ok(param)
680689
} else {
681690
match serde_json::from_str::<CreateAccountErrorResponse>(&response_text) {
682691
Ok(error) => Err(anyhow!(error.reason)),
@@ -693,7 +702,10 @@ pub(crate) async fn set_account_from_qr(context: &Context, qr: &str) -> Result<(
693702
/// Sets configuration values from a QR code.
694703
pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<()> {
695704
match check_qr(context, qr).await? {
696-
Qr::Account { .. } => set_account_from_qr(context, qr).await?,
705+
Qr::Account { .. } => {
706+
let mut param = login_param_from_account_qr(context, qr).await?;
707+
context.add_transport_inner(&mut param).await?
708+
}
697709
Qr::Proxy { url, .. } => {
698710
let old_proxy_url_value = context
699711
.get_config(Config::ProxyUrl)
@@ -781,7 +793,8 @@ pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<()> {
781793
context.scheduler.interrupt_inbox().await;
782794
}
783795
Qr::Login { address, options } => {
784-
configure_from_login_qr(context, &address, options).await?
796+
let mut param = login_param_from_login_qr(&address, options)?;
797+
context.add_transport_inner(&mut param).await?
785798
}
786799
_ => bail!("QR code does not contain config"),
787800
}

src/qr/dclogin_scheme.rs

Lines changed: 24 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use std::collections::HashMap;
33
use anyhow::{Context as _, Result, bail};
44

55
use deltachat_contact_tools::may_be_valid_addr;
6-
use num_traits::cast::ToPrimitive;
76

87
use super::{DCLOGIN_SCHEME, Qr};
9-
use crate::config::Config;
10-
use crate::context::Context;
11-
use crate::login_param::EnteredCertificateChecks;
8+
use crate::login_param::{EnteredCertificateChecks, EnteredLoginParam, EnteredServerLoginParam};
129
use crate::provider::Socket;
1310

1411
/// Options for `dclogin:` scheme.
@@ -157,15 +154,10 @@ fn parse_certificate_checks(
157154
})
158155
}
159156

160-
pub(crate) async fn configure_from_login_qr(
161-
context: &Context,
162-
address: &str,
157+
pub(crate) fn login_param_from_login_qr(
158+
addr: &str,
163159
options: LoginOptions,
164-
) -> Result<()> {
165-
context
166-
.set_config_internal(Config::Addr, Some(address))
167-
.await?;
168-
160+
) -> Result<EnteredLoginParam> {
169161
match options {
170162
LoginOptions::V1 {
171163
mail_pw,
@@ -181,77 +173,26 @@ pub(crate) async fn configure_from_login_qr(
181173
smtp_security,
182174
certificate_checks,
183175
} => {
184-
context
185-
.set_config_internal(Config::MailPw, Some(&mail_pw))
186-
.await?;
187-
if let Some(value) = imap_host {
188-
context
189-
.set_config_internal(Config::MailServer, Some(&value))
190-
.await?;
191-
}
192-
if let Some(value) = imap_port {
193-
context
194-
.set_config_internal(Config::MailPort, Some(&value.to_string()))
195-
.await?;
196-
}
197-
if let Some(value) = imap_username {
198-
context
199-
.set_config_internal(Config::MailUser, Some(&value))
200-
.await?;
201-
}
202-
if let Some(value) = imap_password {
203-
context
204-
.set_config_internal(Config::MailPw, Some(&value))
205-
.await?;
206-
}
207-
if let Some(value) = imap_security {
208-
let code = value
209-
.to_u8()
210-
.context("could not convert imap security value to number")?;
211-
context
212-
.set_config_internal(Config::MailSecurity, Some(&code.to_string()))
213-
.await?;
214-
}
215-
if let Some(value) = smtp_host {
216-
context
217-
.set_config_internal(Config::SendServer, Some(&value))
218-
.await?;
219-
}
220-
if let Some(value) = smtp_port {
221-
context
222-
.set_config_internal(Config::SendPort, Some(&value.to_string()))
223-
.await?;
224-
}
225-
if let Some(value) = smtp_username {
226-
context
227-
.set_config_internal(Config::SendUser, Some(&value))
228-
.await?;
229-
}
230-
if let Some(value) = smtp_password {
231-
context
232-
.set_config_internal(Config::SendPw, Some(&value))
233-
.await?;
234-
}
235-
if let Some(value) = smtp_security {
236-
let code = value
237-
.to_u8()
238-
.context("could not convert smtp security value to number")?;
239-
context
240-
.set_config_internal(Config::SendSecurity, Some(&code.to_string()))
241-
.await?;
242-
}
243-
if let Some(value) = certificate_checks {
244-
let code = value
245-
.to_u32()
246-
.context("could not convert certificate checks value to number")?;
247-
context
248-
.set_config_internal(Config::ImapCertificateChecks, Some(&code.to_string()))
249-
.await?;
250-
context
251-
.set_config_internal(Config::SmtpCertificateChecks, Some(&code.to_string()))
252-
.await?;
253-
}
254-
Ok(())
176+
let param = EnteredLoginParam {
177+
addr: addr.to_string(),
178+
imap: EnteredServerLoginParam {
179+
server: imap_host.unwrap_or_default(),
180+
port: imap_port.unwrap_or_default(),
181+
security: imap_security.unwrap_or_default(),
182+
user: imap_username.unwrap_or_default(),
183+
password: imap_password.unwrap_or(mail_pw),
184+
},
185+
smtp: EnteredServerLoginParam {
186+
server: smtp_host.unwrap_or_default(),
187+
port: smtp_port.unwrap_or_default(),
188+
security: smtp_security.unwrap_or_default(),
189+
user: smtp_username.unwrap_or_default(),
190+
password: smtp_password.unwrap_or_default(),
191+
},
192+
certificate_checks: certificate_checks.unwrap_or_default(),
193+
oauth2: false,
194+
};
195+
Ok(param)
255196
}
256197
_ => bail!(
257198
"DeltaChat does not understand this QR Code yet, please update the app and try again."

0 commit comments

Comments
 (0)