From 4661ffb76218aee1d4dd8e8d3bdbea47b341e287 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 13 Apr 2022 20:38:00 +0200 Subject: [PATCH] introduce get_primary_addr() shortcutting access the configured address for an account. --- src/chat.rs | 16 +++------------- src/config.rs | 8 ++++++++ src/contact.rs | 24 +++++++----------------- src/dc_receive_imf.rs | 18 +++--------------- src/e2ee.rs | 18 +++--------------- src/imap.rs | 6 +----- src/imex.rs | 5 ++--- src/key.rs | 5 +---- src/location.rs | 6 +----- src/login_param.rs | 4 ++-- src/mimefactory.rs | 17 +++-------------- src/scheduler/connectivity.rs | 8 +------- src/securejoin.rs | 14 +------------- src/sql/migrations.rs | 2 +- src/test_utils.rs | 7 +------ 15 files changed, 38 insertions(+), 120 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 26c4c48de..2c3e68679 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1254,11 +1254,7 @@ impl Chat { } } - let from = context - .get_config(Config::ConfiguredAddr) - .await? - .context("Cannot prepare message for sending, address is not configured.")?; - + let from = context.get_primary_addr().await?; let new_rfc724_mid = { let grpid = match self.typ { Chattype::Group => Some(self.grpid.as_str()), @@ -2047,10 +2043,7 @@ async fn create_send_msg_job(context: &Context, msg_id: MsgId) -> Result Result { + use anyhow::Context; + self.get_config(Config::ConfiguredAddr) + .await? + .context("no address configured") + } + pub(crate) async fn should_watch_mvbox(&self) -> Result { Ok(self.get_config_bool(Config::MvboxMove).await? || self.get_config_bool(Config::OnlyFetchMvbox).await?) diff --git a/src/contact.rs b/src/contact.rs index cdcf4363e..0b6116fc4 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -296,10 +296,7 @@ impl Contact { .await?; if contact_id == ContactId::SELF { contact.name = stock_str::self_msg(context).await; - contact.addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); + contact.addr = context.get_primary_addr().await.unwrap_or_default(); contact.status = context .get_config(Config::Selfstatus) .await? @@ -398,10 +395,8 @@ impl Contact { let addr_normalized = addr_normalize(addr); - if let Some(addr_self) = context.get_config(Config::ConfiguredAddr).await? { - if addr_cmp(addr_normalized, &addr_self) { - return Ok(Some(ContactId::SELF)); - } + if context.is_self_addr(addr_normalized).await? { + return Ok(Some(ContactId::SELF)); } let id = context .sql @@ -452,11 +447,10 @@ impl Contact { ensure!(origin != Origin::Unknown, "Missing valid origin"); let addr = addr_normalize(addr).to_string(); - let addr_self = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); + // during configuration process some chats are added and addr + // might not be configured yet + let addr_self = context.get_primary_addr().await.unwrap_or_default(); if addr_cmp(&addr, &addr_self) { return Ok((ContactId::SELF, sth_modified)); } @@ -692,11 +686,7 @@ impl Contact { listflags: u32, query: Option>, ) -> Result> { - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); - + let self_addr = context.get_primary_addr().await.unwrap_or_default(); let mut add_self = false; let mut ret = Vec::new(); let flag_verified_only = (listflags & DC_GCL_VERIFIED_ONLY) != 0; diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index a5a00fe8e..8f11374d9 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1451,10 +1451,7 @@ async fn create_or_lookup_group( ProtectionStatus::Unprotected }; - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context("no address configured")?; + let self_addr = context.get_primary_addr().await?; if chat_id.is_none() && !mime_parser.is_mailinglist_message() && !grpid.is_empty() @@ -1550,11 +1547,7 @@ async fn apply_group_changes( return Ok(None); } - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context("no address configured")?; - + let self_addr = context.get_primary_addr().await?; let mut recreate_member_list = false; let mut send_event_chat_modified = false; @@ -1998,12 +1991,7 @@ async fn create_adhoc_group( /// are hidden in BCC. This group ID is sent by DC in the messages sent to this chat, /// so having the same ID prevents group split. async fn create_adhoc_grp_id(context: &Context, member_ids: &[ContactId]) -> Result { - let member_cs = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_else(|| "no-self".to_string()) - .to_lowercase(); - + let member_cs = context.get_primary_addr().await?.to_lowercase(); let query = format!( "SELECT addr FROM contacts WHERE id IN({}) AND id!=?", sql::repeat_vars(member_ids.len())? diff --git a/src/e2ee.rs b/src/e2ee.rs index 4fe3d2ebc..75d54b8e2 100644 --- a/src/e2ee.rs +++ b/src/e2ee.rs @@ -2,7 +2,7 @@ use std::collections::HashSet; -use anyhow::{bail, format_err, Context as _, Result}; +use anyhow::{format_err, Context as _, Result}; use mailparse::ParsedMail; use num_traits::FromPrimitive; @@ -28,13 +28,7 @@ impl EncryptHelper { let prefer_encrypt = EncryptPreference::from_i32(context.get_config_int(Config::E2eeEnabled).await?) .unwrap_or_default(); - let addr = match context.get_config(Config::ConfiguredAddr).await? { - None => { - bail!("addr not configured!"); - } - Some(addr) => addr, - }; - + let addr = context.get_primary_addr().await?; let public_key = SignedPublicKey::load_self(context).await?; Ok(EncryptHelper { @@ -387,13 +381,7 @@ fn contains_report(mail: &ParsedMail<'_>) -> bool { /// [Config::ConfiguredAddr] is configured, this address is returned. // TODO, remove this once deltachat::key::Key no longer exists. pub async fn ensure_secret_key_exists(context: &Context) -> Result { - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context(concat!( - "Failed to get self address, ", - "cannot ensure secret key if not configured." - ))?; + let self_addr = context.get_primary_addr().await?; SignedPublicKey::load_self(context).await?; Ok(self_addr) } diff --git a/src/imap.rs b/src/imap.rs index cc64914f3..13d2b4bf2 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -1117,11 +1117,7 @@ impl Imap { .session .as_mut() .context("IMAP No Connection established")?; - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context("not configured")?; - + let self_addr = context.get_primary_addr().await?; let search_command = format!("FROM \"{}\"", self_addr); let uids = session .uid_search(search_command) diff --git a/src/imex.rs b/src/imex.rs index 3dc07e9f3..1377d14ff 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -351,9 +351,8 @@ async fn set_self_key( } }; - let self_addr = context.get_config(Config::ConfiguredAddr).await?; - ensure!(self_addr.is_some(), "Missing self addr"); - let addr = EmailAddress::new(&self_addr.unwrap_or_default())?; + let self_addr = context.get_primary_addr().await?; + let addr = EmailAddress::new(&self_addr)?; let keypair = pgp::KeyPair { addr, public: public_key, diff --git a/src/key.rs b/src/key.rs index ee1aa298f..3c876ee51 100644 --- a/src/key.rs +++ b/src/key.rs @@ -198,10 +198,7 @@ impl DcSecretKey for SignedSecretKey { } async fn generate_keypair(context: &Context) -> Result { - let addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context("no address configured")?; + let addr = context.get_primary_addr().await?; let addr = EmailAddress::new(&addr)?; let _guard = context.generating_key_mutex.lock().await; diff --git a/src/location.rs b/src/location.rs index 9b18f6cc6..45d617e47 100644 --- a/src/location.rs +++ b/src/location.rs @@ -6,7 +6,6 @@ use bitflags::bitflags; use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use crate::chat::{self, ChatId}; -use crate::config::Config; use crate::contact::ContactId; use crate::context::Context; use crate::dc_tools::time; @@ -424,10 +423,7 @@ pub async fn delete_all(context: &Context) -> Result<()> { pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<(String, u32)> { let mut last_added_location_id = 0; - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); + let self_addr = context.get_primary_addr().await?; let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row( "SELECT locations_send_begin, locations_send_until, locations_last_sent FROM chats WHERE id=?;", diff --git a/src/login_param.rs b/src/login_param.rs index 5b1d78e88..0debe2828 100644 --- a/src/login_param.rs +++ b/src/login_param.rs @@ -139,12 +139,12 @@ pub struct LoginParam { } impl LoginParam { - /// Load entered (candidate) account settings + /// Load entered (candidate) account settings pub async fn load_candidate_params(context: &Context) -> Result { LoginParam::from_database(context, "").await } - /// Load configured (working) account settings + /// Load configured (working) account settings pub async fn load_configured_params(context: &Context) -> Result { LoginParam::from_database(context, "configured_").await } diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 675005ade..bf6f12919 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -133,11 +133,7 @@ impl<'a> MimeFactory<'a> { ) -> Result> { let chat = Chat::load_from_db(context, msg.chat_id).await?; - let from_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); - + let from_addr = context.get_primary_addr().await.unwrap_or_default(); let config_displayname = context .get_config(Config::Displayname) .await? @@ -237,10 +233,7 @@ impl<'a> MimeFactory<'a> { ensure!(!msg.chat_id.is_special(), "Invalid chat id"); let contact = Contact::load_from_db(context, msg.from_id).await?; - let from_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); + let from_addr = context.get_primary_addr().await?; let from_displayname = context .get_config(Config::Displayname) .await? @@ -278,11 +271,7 @@ impl<'a> MimeFactory<'a> { &self, context: &Context, ) -> Result, &str)>> { - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .context("not configured")?; - + let self_addr = context.get_primary_addr().await?; let mut res = Vec::new(); for (_, addr) in self .recipients diff --git a/src/scheduler/connectivity.rs b/src/scheduler/connectivity.rs index 6d412171a..dd71d14b0 100644 --- a/src/scheduler/connectivity.rs +++ b/src/scheduler/connectivity.rs @@ -449,13 +449,7 @@ impl Context { // [======67%===== ] // ============================================================================================= - let domain = dc_tools::EmailAddress::new( - &self - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(), - )? - .domain; + let domain = dc_tools::EmailAddress::new(&self.get_primary_addr().await?)?.domain; ret += &format!( "

{}

    ", stock_str::storage_on_domain(self, domain).await diff --git a/src/securejoin.rs b/src/securejoin.rs index c2ff41615..62b16fae1 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -66,19 +66,7 @@ pub async fn dc_get_securejoin_qr(context: &Context, group: Option) -> R .is_none(); let invitenumber = token::lookup_or_new(context, Namespace::InviteNumber, group).await; let auth = token::lookup_or_new(context, Namespace::Auth, group).await; - let self_addr = match context.get_config(Config::ConfiguredAddr).await { - Ok(Some(addr)) => addr, - Ok(None) => { - bail!("Not configured, cannot generate QR code."); - } - Err(err) => { - bail!( - "Unable to retrieve configuration, cannot generate QR code: {:?}", - err - ); - } - }; - + let self_addr = context.get_primary_addr().await?; let self_name = context .get_config(Config::Displayname) .await? diff --git a/src/sql/migrations.rs b/src/sql/migrations.rs index 2e009721b..7291a7518 100644 --- a/src/sql/migrations.rs +++ b/src/sql/migrations.rs @@ -395,7 +395,7 @@ UPDATE chats SET protected=1, type=120 WHERE type=130;"#, if dbversion < 71 { info!(context, "[migration] v71"); - if let Some(addr) = context.get_config(Config::ConfiguredAddr).await? { + if let Ok(addr) = context.get_primary_addr().await { if let Ok(domain) = addr.parse::().map(|email| email.domain) { context .set_config( diff --git a/src/test_utils.rs b/src/test_utils.rs index 1d4b1f2e0..b59147890 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -407,12 +407,7 @@ impl TestContext { .await .unwrap_or_default() .unwrap_or_default(); - let addr = other - .ctx - .get_config(Config::ConfiguredAddr) - .await - .unwrap() - .unwrap(); + let addr = other.ctx.get_primary_addr().await.unwrap(); // MailinglistAddress is the lowest allowed origin, we'd prefer to not modify the // origin when creating this contact. let (contact_id, modified) =