Skip to main content

tuwunel_service/membership/
mod.rs

1mod auto_accept;
2mod ban;
3mod invite;
4mod join;
5mod kick;
6mod knock;
7mod leave;
8mod stripped_state;
9mod unban;
10
11use std::sync::Arc;
12
13use async_trait::async_trait;
14use loole::{Receiver, Sender, unbounded};
15use ruma::{
16	CanonicalJsonObject, OwnedEventId, RoomId, RoomVersionId, UserId,
17	events::room::member::MembershipState, room_version_rules::RoomVersionRules,
18	signatures::Verified,
19};
20use serde_json::value::RawValue as RawJsonValue;
21use tuwunel_core::{Result, err, implement};
22use tuwunel_matrix::pdu::Pdu;
23
24use self::auto_accept::Pending;
25pub use self::{
26	join::{Join, is_reserved_join_key},
27	stripped_state::{
28		StrippedCreateVerdict, dedup_stripped_state, enforce_stripped_create,
29		into_client_stripped, v12_room_ids, without_member,
30	},
31};
32
33/// A verified federation event bound to its room.
34///
35/// It carries the event ID, the parsed PDU, the stored form and how it verified.
36type VerifiedPdu = (OwnedEventId, Pdu, CanonicalJsonObject, Verified);
37
38pub struct Service {
39	services: Arc<crate::services::OnceServices>,
40	queue: (Sender<Pending>, Receiver<Pending>),
41}
42
43#[async_trait]
44impl crate::Service for Service {
45	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
46		Ok(Arc::new(Self {
47			services: args.services.clone(),
48			queue: unbounded(),
49		}))
50	}
51
52	async fn worker(self: Arc<Self>) -> Result {
53		self.accept_worker().await;
54
55		Ok(())
56	}
57
58	async fn interrupt(&self) {
59		let (sender, _) = &self.queue;
60
61		if !sender.is_closed() {
62			sender.close();
63		}
64	}
65
66	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
67}
68
69/// Whether a room this server is in holds a ban on the user.
70///
71/// A room we left can lift a ban without telling us, so its frozen state
72/// answers for nothing here and the room itself refuses or admits the user. A
73/// failed residency read keeps the state's answer.
74#[implement(Service)]
75async fn banned_from_resident_room(&self, user_id: &UserId, room_id: &RoomId) -> bool {
76	let resident = self
77		.services
78		.state_cache
79		.server_in_room_result(self.services.globals.server_name(), room_id)
80		.await
81		.unwrap_or(true);
82
83	resident
84		&& self
85			.services
86			.state_accessor
87			.get_member(room_id, user_id)
88			.await
89			.is_ok_and(|content| content.membership == MembershipState::Ban)
90}
91
92/// Verifies a federation event and binds it to the room as a PDU.
93///
94/// Only signing keys already stored are used. The event comes back in the form
95/// this server stores it, with its verification classification.
96#[implement(Service)]
97async fn verified_pdu(
98	&self,
99	room_id: &RoomId,
100	room_version_id: &RoomVersionId,
101	room_version_rules: &RoomVersionRules,
102	pdu: &RawJsonValue,
103) -> Result<VerifiedPdu> {
104	let (event_id, value, verified) = self
105		.services
106		.server_keys
107		.validate_and_add_event_id_no_fetch(pdu, room_version_id)
108		.await?;
109
110	let (pdu, value) = Pdu::from_object_federation(room_id, &event_id, value, room_version_rules)
111		.map_err(|error| err!("Invalid PDU {event_id}: {error}"))?;
112
113	Ok((event_id, pdu, value, verified))
114}