From 5e28b9158f587cae9fa6cb428c39c5ada7cfd1bc Mon Sep 17 00:00:00 2001 From: avdb13 Date: Sat, 6 Jul 2024 22:26:33 +0200 Subject: [PATCH] ok --- docs/configuration.md | 9 ++++--- src/config/mod.rs | 8 +++++-- src/service/globals/mod.rs | 49 +++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 2036744e..1810583f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -114,12 +114,11 @@ exclude = ["*.clearnet.onion"] ### Default rooms -Since the validation of room aliases and IDs does validate the presence of a colon separator, -this symbol including the server name can be ommitted for local rooms. +This list should contain either room identifiers or aliases. +Opaque identifiers will be interpreted as incomplete, local aliases. #### Example -In this example, the two configuration options are equivalent: +In this example, the opaque identifier and alias are the same: ```toml -default_rooms = ["#home", "!abc123", "#conduit:fachschaften.org"] -default_rooms = ["#home:conduit.rs", "!abc123:conduit.rs", "#conduit:fachschaften.org"] +default_rooms = ["home", "#home:conduit.rs"] ``` diff --git a/src/config/mod.rs b/src/config/mod.rs index 71aa6176..4ab328cf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,15 +2,18 @@ use std::{ collections::BTreeMap, fmt, net::{IpAddr, Ipv4Addr}, + str::FromStr, }; -use ruma::{OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; +use ruma::{serde::Raw, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; use serde::{de::IgnoredAny, Deserialize}; use tracing::warn; use url::Url; mod proxy; +use crate::Error; + use self::proxy::ProxyConfig; #[derive(Clone, Debug, Deserialize)] @@ -105,7 +108,8 @@ pub struct WellKnownConfig { #[derive(Clone, Debug, Deserialize, Default)] pub struct DefaultRoomsConfig { - pub rooms: Vec, + #[serde(default = "Vec::new")] + pub rooms: Vec, pub join_reason: Option, } diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 2ff2ff41..90d6df4a 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -1,9 +1,10 @@ mod data; pub use data::{Data, SigningKeys}; +use hmac::digest::typenum::Pow; use ruma::{ serde::Base64, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomAliasId, - OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, + OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, RoomId, }; use crate::api::{client_server::get_alias_helper, server_server::DestinationResponse}; @@ -24,6 +25,7 @@ use std::{ future::{self, Future}, iter, net::{IpAddr, SocketAddr}, + ops::Deref, path::PathBuf, str::FromStr, sync::{ @@ -507,32 +509,31 @@ impl Service { } pub async fn default_rooms(&self) -> Result> { - let mut default_rooms = BTreeSet::new(); + let server_name = &self.config.server_name; - for id_or_alias in &self.config.default_rooms.rooms { - // // `OwnedRoomOrAliasId` always starts with a '#' or '!', presence of a ':' is not validated - // let localpart = id_or_alias - // .as_str() - // .split_once(':') - // .map(|(localpart, _)| localpart) - // .unwrap_or(id_or_alias.as_str()); - // let server_name = id_or_alias - // .server_name() - // .map(ToOwned::to_owned) - // .unwrap_or(self.config.server_name.clone()); + let fallback = || { + format!("#{s}:{server_name}") + .parse() + .map_err(|_| Error::bad_config("Invalid opaque identifier.")) + }; - let room_id = match id_or_alias.try_into() - // OwnedRoomOrAliasId::from_str(&format!("{localpart}:{server_name}")) - // .expect("this should always be valid") - // .try_into() - { - Ok(room_id) => room_id, - Err(room_alias) => get_alias_helper(room_alias).await.map(|res| res.room_id)?, - }; - default_rooms.insert(room_id); - } + let f = |s| match OwnedRoomOrAliasId::from_str(s).map(OwnedRoomId::try_from) { + Ok(Ok(room_id)) => room_id + .server_name() + .ok_or_else(|| Error::bad_config("Invalid room ID due to missing server name.")) + .map(|_| room_id), + result => get_alias_helper(result.map(Result::unwrap_err).or_else(fallback)?) + .await + .map(|response| response.room_id), + }; - Ok(default_rooms) + self.config + .default_rooms + .rooms + .iter() + .map(String::as_str) + .map(|s| f) + .collect::>() } pub fn shutdown(&self) {