Introduce SecondaryAddrs config and make stuff work

This commit is contained in:
Hocuri
2022-04-13 10:03:49 +02:00
committed by holger krekel
parent 3ffc985968
commit 3b6fc9959f
16 changed files with 194 additions and 91 deletions

View File

@@ -6,6 +6,7 @@ use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString};
use crate::blob::BlobObject;
use crate::constants::DC_VERSION_STR;
use crate::contact::addr_normalize;
use crate::context::Context;
use crate::dc_tools::{dc_get_abs_path, improve_single_line_input};
use crate::events::EventType;
@@ -111,6 +112,7 @@ pub enum Config {
DeleteDeviceAfter,
SaveMimeHeaders,
/// The primary email address. Also see `SecondaryAddrs`.
ConfiguredAddr,
ConfiguredMailServer,
ConfiguredMailUser,
@@ -133,6 +135,10 @@ pub enum Config {
ConfiguredProvider,
Configured,
/// All secondary self addresses separated by spaces
/// (`addr1@example.org addr2@exapmle.org addr3@example.org`)
SecondaryAddrs,
#[strum(serialize = "sys.version")]
SysVersion,
@@ -226,12 +232,6 @@ impl Context {
Ok(self.get_config_int(key).await? != 0)
}
pub(crate) async fn get_configured_addr(&self) -> Result<String> {
self.get_config(Config::ConfiguredAddr)
.await?
.context("no address configured")
}
pub(crate) async fn should_watch_mvbox(&self) -> Result<bool> {
Ok(self.get_config_bool(Config::MvboxMove).await?
|| self.get_config_bool(Config::OnlyFetchMvbox).await?)
@@ -333,6 +333,70 @@ impl Context {
}
}
// Separate impl block for self address handling
impl Context {
/// determine whether the specified addr maps to the/a self addr
pub async fn is_self_addr(&self, addr: &str) -> Result<bool> {
let addr = addr_normalize(addr).to_lowercase();
// The addresses we get here are already normalized and lowercase TODO is this true?
Ok(
self.get_config(Config::ConfiguredAddr).await?.as_deref() == Some(&addr)
|| self.get_secondary_self_addrs().await?.contains(&addr),
)
}
/// Sets `primary_new` as the new primary self address and saves the old
/// primary address (if exists) as a secondary address.
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> {
let primary_new = addr_normalize(primary_new).to_lowercase(); // TODO check if this might make problems
// Get all self addrs, including the old primary addr:
let mut secondary_new = self.get_all_self_addrs().await?;
// Remove the new primary addr in case it was a secondary addr before:
secondary_new.retain(|a| a != &primary_new);
self.set_config(
Config::SecondaryAddrs,
Some(secondary_new.join(" ").as_str()),
)
.await?;
self.set_config(Config::ConfiguredAddr, Some(&primary_new))
.await?;
Ok(())
}
/// Returns all primary and secondary self addresses.
pub async fn get_all_self_addrs(&self) -> Result<Vec<String>> {
let mut ret = Vec::new();
ret.extend(self.get_config(Config::ConfiguredAddr).await?.into_iter());
ret.extend(self.get_secondary_self_addrs().await?.into_iter());
Ok(ret)
}
/// Returns all secondary self addresses.
pub async fn get_secondary_self_addrs(&self) -> Result<Vec<String>> {
let secondary_addrs = self
.get_config(Config::SecondaryAddrs)
.await?
.unwrap_or_default();
Ok(secondary_addrs
.split_ascii_whitespace()
.map(|s| s.to_string())
.collect())
}
/// Returns the primary self address.
pub async fn get_primary_self_addr(&self) -> Result<String> {
self.get_config(Config::ConfiguredAddr)
.await?
.context("No self addr configured")
}
}
/// Returns all available configuration keys concated together.
fn get_config_keys_string() -> String {
let keys = Config::iter().fold(String::new(), |mut acc, key| {