Skip to main content

tuwunel_service/appservice/
registration_info.rs

1use ruma::{OwnedUserId, ServerName, UserId, api::appservice::Registration};
2use tuwunel_core::Result;
3
4use super::NamespaceRegex;
5
6/// Appservice registration combined with its compiled regular expressions.
7#[derive(Clone, Debug)]
8pub struct RegistrationInfo {
9	pub aliases: NamespaceRegex,
10	pub users: NamespaceRegex,
11	pub rooms: NamespaceRegex,
12	pub sender: OwnedUserId,
13	pub registration: Registration,
14}
15
16impl RegistrationInfo {
17	pub fn new(registration: Registration, server_name: &ServerName) -> Result<Self> {
18		Ok(Self {
19			aliases: NamespaceRegex::new(false, registration.namespaces.aliases.iter())?,
20			users: NamespaceRegex::new(false, registration.namespaces.users.iter())?,
21			rooms: NamespaceRegex::new(true, registration.namespaces.rooms.iter())?,
22			sender: OwnedUserId::from_parts(
23				'@',
24				registration.sender_localpart.as_ref(),
25				server_name.as_str().into(),
26			)?,
27
28			registration,
29		})
30	}
31
32	/// MSC3905: the `users` regex matches local users only.
33	#[inline]
34	#[must_use]
35	pub fn is_user_match(&self, user_id: &UserId) -> bool {
36		user_id == self.sender
37			|| (self.users.is_match(user_id.as_str())
38				&& user_id.server_name() == self.sender.server_name())
39	}
40
41	/// MSC3905: the `users` regex matches local users only.
42	#[inline]
43	#[must_use]
44	pub fn is_exclusive_user_match(&self, user_id: &UserId) -> bool {
45		user_id == self.sender
46			|| (self.users.is_exclusive_match(user_id.as_str())
47				&& user_id.server_name() == self.sender.server_name())
48	}
49}