|
|
|
@ -2,7 +2,7 @@ mod edus; |
|
|
|
|
|
|
|
|
|
|
|
pub use edus::RoomEdus; |
|
|
|
pub use edus::RoomEdus; |
|
|
|
|
|
|
|
|
|
|
|
use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result}; |
|
|
|
use crate::{pdu::PduBuilder, utils, Database, Error, PduEvent, Result}; |
|
|
|
use log::error; |
|
|
|
use log::error; |
|
|
|
use regex::Regex; |
|
|
|
use regex::Regex; |
|
|
|
use ring::digest; |
|
|
|
use ring::digest; |
|
|
|
@ -447,9 +447,7 @@ impl Rooms { |
|
|
|
mut pdu_json: CanonicalJsonObject, |
|
|
|
mut pdu_json: CanonicalJsonObject, |
|
|
|
count: u64, |
|
|
|
count: u64, |
|
|
|
pdu_id: IVec, |
|
|
|
pdu_id: IVec, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
db: &Database, |
|
|
|
account_data: &super::account_data::AccountData, |
|
|
|
|
|
|
|
admin: &super::admin::Admin, |
|
|
|
|
|
|
|
) -> Result<()> { |
|
|
|
) -> Result<()> { |
|
|
|
// Make unsigned fields correct. This is not properly documented in the spec, but state
|
|
|
|
// Make unsigned fields correct. This is not properly documented in the spec, but state
|
|
|
|
// events need to have previous content in the unsigned field, so clients can easily
|
|
|
|
// events need to have previous content in the unsigned field, so clients can easily
|
|
|
|
@ -486,7 +484,7 @@ impl Rooms { |
|
|
|
// Mark as read first so the sending client doesn't get a notification even if appending
|
|
|
|
// Mark as read first so the sending client doesn't get a notification even if appending
|
|
|
|
// fails
|
|
|
|
// fails
|
|
|
|
self.edus |
|
|
|
self.edus |
|
|
|
.private_read_set(&pdu.room_id, &pdu.sender, count, &globals)?; |
|
|
|
.private_read_set(&pdu.room_id, &pdu.sender, count, &db.globals)?; |
|
|
|
|
|
|
|
|
|
|
|
self.pduid_pdu.insert( |
|
|
|
self.pduid_pdu.insert( |
|
|
|
&pdu_id, |
|
|
|
&pdu_id, |
|
|
|
@ -521,8 +519,8 @@ impl Rooms { |
|
|
|
) |
|
|
|
) |
|
|
|
})?, |
|
|
|
})?, |
|
|
|
&pdu.sender, |
|
|
|
&pdu.sender, |
|
|
|
account_data, |
|
|
|
&db.account_data, |
|
|
|
globals, |
|
|
|
&db.globals, |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -540,10 +538,10 @@ impl Rooms { |
|
|
|
self.tokenids.insert(key, &[])?; |
|
|
|
self.tokenids.insert(key, &[])?; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if body.starts_with(&format!("@conduit:{}: ", globals.server_name())) |
|
|
|
if body.starts_with(&format!("@conduit:{}: ", db.globals.server_name())) |
|
|
|
&& self |
|
|
|
&& self |
|
|
|
.id_from_alias( |
|
|
|
.id_from_alias( |
|
|
|
&format!("#admins:{}", globals.server_name()) |
|
|
|
&format!("#admins:{}", db.globals.server_name()) |
|
|
|
.try_into() |
|
|
|
.try_into() |
|
|
|
.expect("#admins:server_name is a valid room alias"), |
|
|
|
.expect("#admins:server_name is a valid room alias"), |
|
|
|
)? |
|
|
|
)? |
|
|
|
@ -570,10 +568,11 @@ impl Rooms { |
|
|
|
); |
|
|
|
); |
|
|
|
match parsed_config { |
|
|
|
match parsed_config { |
|
|
|
Ok(yaml) => { |
|
|
|
Ok(yaml) => { |
|
|
|
admin.send(AdminCommand::RegisterAppservice(yaml)); |
|
|
|
db.admin |
|
|
|
|
|
|
|
.send(AdminCommand::RegisterAppservice(yaml)); |
|
|
|
} |
|
|
|
} |
|
|
|
Err(e) => { |
|
|
|
Err(e) => { |
|
|
|
admin.send(AdminCommand::SendMessage( |
|
|
|
db.admin.send(AdminCommand::SendMessage( |
|
|
|
message::MessageEventContent::text_plain( |
|
|
|
message::MessageEventContent::text_plain( |
|
|
|
format!( |
|
|
|
format!( |
|
|
|
"Could not parse appservice config: {}", |
|
|
|
"Could not parse appservice config: {}", |
|
|
|
@ -584,7 +583,7 @@ impl Rooms { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
admin.send(AdminCommand::SendMessage( |
|
|
|
db.admin.send(AdminCommand::SendMessage( |
|
|
|
message::MessageEventContent::text_plain( |
|
|
|
message::MessageEventContent::text_plain( |
|
|
|
"Expected code block in command body.", |
|
|
|
"Expected code block in command body.", |
|
|
|
), |
|
|
|
), |
|
|
|
@ -592,10 +591,10 @@ impl Rooms { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
"list_appservices" => { |
|
|
|
"list_appservices" => { |
|
|
|
admin.send(AdminCommand::ListAppservices); |
|
|
|
db.admin.send(AdminCommand::ListAppservices); |
|
|
|
} |
|
|
|
} |
|
|
|
_ => { |
|
|
|
_ => { |
|
|
|
admin.send(AdminCommand::SendMessage( |
|
|
|
db.admin.send(AdminCommand::SendMessage( |
|
|
|
message::MessageEventContent::text_plain(format!( |
|
|
|
message::MessageEventContent::text_plain(format!( |
|
|
|
"Command: {}, Args: {:?}", |
|
|
|
"Command: {}, Args: {:?}", |
|
|
|
command, args |
|
|
|
command, args |
|
|
|
@ -696,17 +695,12 @@ impl Rooms { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new persisted data unit and adds it to a room.
|
|
|
|
/// Creates a new persisted data unit and adds it to a room.
|
|
|
|
#[allow(clippy::too_many_arguments)] |
|
|
|
|
|
|
|
pub fn build_and_append_pdu( |
|
|
|
pub fn build_and_append_pdu( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
pdu_builder: PduBuilder, |
|
|
|
pdu_builder: PduBuilder, |
|
|
|
sender: &UserId, |
|
|
|
sender: &UserId, |
|
|
|
room_id: &RoomId, |
|
|
|
room_id: &RoomId, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
db: &Database, |
|
|
|
sending: &super::sending::Sending, |
|
|
|
|
|
|
|
admin: &super::admin::Admin, |
|
|
|
|
|
|
|
account_data: &super::account_data::AccountData, |
|
|
|
|
|
|
|
appservice: &super::appservice::Appservice, |
|
|
|
|
|
|
|
) -> Result<EventId> { |
|
|
|
) -> Result<EventId> { |
|
|
|
let PduBuilder { |
|
|
|
let PduBuilder { |
|
|
|
event_type, |
|
|
|
event_type, |
|
|
|
@ -789,7 +783,7 @@ impl Rooms { |
|
|
|
if !match event_type { |
|
|
|
if !match event_type { |
|
|
|
EventType::RoomEncryption => { |
|
|
|
EventType::RoomEncryption => { |
|
|
|
// Only allow encryption events if it's allowed in the config
|
|
|
|
// Only allow encryption events if it's allowed in the config
|
|
|
|
globals.allow_encryption() |
|
|
|
db.globals.allow_encryption() |
|
|
|
} |
|
|
|
} |
|
|
|
EventType::RoomMember => { |
|
|
|
EventType::RoomMember => { |
|
|
|
let prev_event = self |
|
|
|
let prev_event = self |
|
|
|
@ -895,13 +889,13 @@ impl Rooms { |
|
|
|
// Add origin because synapse likes that (and it's required in the spec)
|
|
|
|
// Add origin because synapse likes that (and it's required in the spec)
|
|
|
|
pdu_json.insert( |
|
|
|
pdu_json.insert( |
|
|
|
"origin".to_owned(), |
|
|
|
"origin".to_owned(), |
|
|
|
to_canonical_value(globals.server_name()) |
|
|
|
to_canonical_value(db.globals.server_name()) |
|
|
|
.expect("server name is a valid CanonicalJsonValue"), |
|
|
|
.expect("server name is a valid CanonicalJsonValue"), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
ruma::signatures::hash_and_sign_event( |
|
|
|
ruma::signatures::hash_and_sign_event( |
|
|
|
globals.server_name().as_str(), |
|
|
|
db.globals.server_name().as_str(), |
|
|
|
globals.keypair(), |
|
|
|
db.globals.keypair(), |
|
|
|
&mut pdu_json, |
|
|
|
&mut pdu_json, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
) |
|
|
|
) |
|
|
|
@ -922,24 +916,16 @@ impl Rooms { |
|
|
|
|
|
|
|
|
|
|
|
// Increment the last index and use that
|
|
|
|
// Increment the last index and use that
|
|
|
|
// This is also the next_batch/since value
|
|
|
|
// This is also the next_batch/since value
|
|
|
|
let count = globals.next_count()?; |
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
|
|
// We append to state before appending the pdu, so we don't have a moment in time with the
|
|
|
|
// We append to state before appending the pdu, so we don't have a moment in time with the
|
|
|
|
// pdu without it's state. This is okay because append_pdu can't fail.
|
|
|
|
// pdu without it's state. This is okay because append_pdu can't fail.
|
|
|
|
let statehashid = self.append_to_state(&pdu_id, &pdu, &globals)?; |
|
|
|
let statehashid = self.append_to_state(&pdu_id, &pdu, &db.globals)?; |
|
|
|
|
|
|
|
|
|
|
|
self.append_pdu( |
|
|
|
self.append_pdu(&pdu, pdu_json, count, pdu_id.clone().into(), db)?; |
|
|
|
&pdu, |
|
|
|
|
|
|
|
pdu_json, |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.clone().into(), |
|
|
|
|
|
|
|
globals, |
|
|
|
|
|
|
|
account_data, |
|
|
|
|
|
|
|
admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We set the room state after inserting the pdu, so that we never have a moment in time
|
|
|
|
// We set the room state after inserting the pdu, so that we never have a moment in time
|
|
|
|
// where events in the current room state do not exist
|
|
|
|
// where events in the current room state do not exist
|
|
|
|
@ -948,19 +934,17 @@ impl Rooms { |
|
|
|
for server in self |
|
|
|
for server in self |
|
|
|
.room_servers(room_id) |
|
|
|
.room_servers(room_id) |
|
|
|
.filter_map(|r| r.ok()) |
|
|
|
.filter_map(|r| r.ok()) |
|
|
|
.filter(|server| &**server != globals.server_name()) |
|
|
|
.filter(|server| &**server != db.globals.server_name()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
sending.send_pdu(&server, &pdu_id)?; |
|
|
|
db.sending.send_pdu(&server, &pdu_id)?; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for appservice in appservice.iter_all().filter_map(|r| r.ok()) { |
|
|
|
for appservice in db.appservice.iter_all().filter_map(|r| r.ok()) { |
|
|
|
if let Some(namespaces) = appservice.1.get("namespaces") { |
|
|
|
if let Some(namespaces) = appservice.1.get("namespaces") { |
|
|
|
let users = namespaces |
|
|
|
let users = namespaces |
|
|
|
.get("users") |
|
|
|
.get("users") |
|
|
|
.and_then(|users| users.as_sequence()) |
|
|
|
.and_then(|users| users.as_sequence()) |
|
|
|
.map_or_else( |
|
|
|
.map_or_else(Vec::new, |users| { |
|
|
|
|| Vec::new(), |
|
|
|
|
|
|
|
|users| { |
|
|
|
|
|
|
|
users |
|
|
|
users |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|users| { |
|
|
|
.map(|users| { |
|
|
|
@ -971,8 +955,7 @@ impl Rooms { |
|
|
|
}) |
|
|
|
}) |
|
|
|
.filter_map(|o| o) |
|
|
|
.filter_map(|o| o) |
|
|
|
.collect::<Vec<_>>() |
|
|
|
.collect::<Vec<_>>() |
|
|
|
}, |
|
|
|
}); |
|
|
|
); |
|
|
|
|
|
|
|
let aliases = namespaces |
|
|
|
let aliases = namespaces |
|
|
|
.get("aliases") |
|
|
|
.get("aliases") |
|
|
|
.and_then(|users| users.get("regex")) |
|
|
|
.and_then(|users| users.get("regex")) |
|
|
|
@ -989,29 +972,31 @@ impl Rooms { |
|
|
|
.get("sender_localpart") |
|
|
|
.get("sender_localpart") |
|
|
|
.and_then(|string| string.as_str()) |
|
|
|
.and_then(|string| string.as_str()) |
|
|
|
.and_then(|string| { |
|
|
|
.and_then(|string| { |
|
|
|
UserId::parse_with_server_name(string, globals.server_name()).ok() |
|
|
|
UserId::parse_with_server_name(string, db.globals.server_name()).ok() |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if bridge_user_id.map_or(false, |bridge_user_id| { |
|
|
|
let user_is_joined = |
|
|
|
self.is_joined(&bridge_user_id, room_id).unwrap_or(false) |
|
|
|
|bridge_user_id| self.is_joined(&bridge_user_id, room_id).unwrap_or(false); |
|
|
|
}) || users.iter().any(|users| { |
|
|
|
let matching_users = |users: &Regex| { |
|
|
|
users.is_match(pdu.sender.as_str()) |
|
|
|
users.is_match(pdu.sender.as_str()) |
|
|
|
|| pdu.kind == EventType::RoomMember |
|
|
|
|| pdu.kind == EventType::RoomMember |
|
|
|
&& pdu |
|
|
|
&& pdu |
|
|
|
.state_key |
|
|
|
.state_key |
|
|
|
.as_ref() |
|
|
|
.as_ref() |
|
|
|
.map_or(false, |state_key| users.is_match(&state_key)) |
|
|
|
.map_or(false, |state_key| users.is_match(&state_key)) |
|
|
|
}) || aliases.map_or(false, |aliases| { |
|
|
|
}; |
|
|
|
|
|
|
|
let matching_aliases = |aliases: Regex| { |
|
|
|
room_aliases |
|
|
|
room_aliases |
|
|
|
.filter_map(|r| r.ok()) |
|
|
|
.filter_map(|r| r.ok()) |
|
|
|
.any(|room_alias| aliases.is_match(room_alias.as_str())) |
|
|
|
.any(|room_alias| aliases.is_match(room_alias.as_str())) |
|
|
|
}) || rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into())) |
|
|
|
}; |
|
|
|
|| self |
|
|
|
|
|
|
|
.room_members(&room_id) |
|
|
|
if bridge_user_id.map_or(false, user_is_joined) |
|
|
|
.filter_map(|r| r.ok()) |
|
|
|
|| users.iter().any(matching_users) |
|
|
|
.any(|member| users.iter().any(|regex| regex.is_match(member.as_str()))) |
|
|
|
|| aliases.map_or(false, matching_aliases) |
|
|
|
|
|
|
|
|| rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into())) |
|
|
|
{ |
|
|
|
{ |
|
|
|
sending.send_pdu_appservice(&appservice.0, &pdu_id)?; |
|
|
|
db.sending.send_pdu_appservice(&appservice.0, &pdu_id)?; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|