From 2cf6fd57b7da57b116ca782130d97027a8eacb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 23 Dec 2020 19:41:54 +0100 Subject: [PATCH] improvement: don't send pdus to appservices if it isn't interested TODO: we need to send pdus if a user of the appservice is in the room but not the appservice user itself --- Cargo.lock | 37 +++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/database/rooms.rs | 59 ++++++++++++++++++++++++++++++++++++++++++- src/server_server.rs | 3 --- 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6566b108..b5be6aaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "aho-corasick" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +dependencies = [ + "memchr", +] + [[package]] name = "arrayref" version = "0.3.6" @@ -190,6 +199,7 @@ dependencies = [ "js_int", "log", "rand", + "regex", "reqwest", "ring", "rocket", @@ -1418,6 +1428,24 @@ dependencies = [ "syn", ] +[[package]] +name = "regex" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -2131,6 +2159,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "time" version = "0.2.23" diff --git a/Cargo.toml b/Cargo.toml index 1e4afe2a..4b871996 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,8 @@ base64 = "0.13.0" ring = "0.16.19" # Used when querying the SRV record of other servers trust-dns-resolver = "0.19.6" +# Used to find matching events for appservices +regex = "1.4.2" [features] default = ["conduit_bin"] diff --git a/src/database/rooms.rs b/src/database/rooms.rs index e59c77f4..a6d8fea5 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -4,6 +4,7 @@ pub use edus::RoomEdus; use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result}; use log::error; +use regex::Regex; use ring::digest; use ruma::{ api::client::error::ErrorKind, @@ -949,7 +950,63 @@ impl Rooms { } for appservice in appservice.iter_all().filter_map(|r| r.ok()) { - sending.send_pdu_appservice(&appservice.0, &pdu_id)?; + if let Some(namespaces) = appservice.1.get("namespaces") { + let users = namespaces + .get("users") + .and_then(|users| users.as_sequence()) + .map_or_else( + || Vec::new(), + |users| { + users + .iter() + .map(|users| { + users + .get("regex") + .and_then(|regex| regex.as_str()) + .and_then(|regex| Regex::new(regex).ok()) + }) + .filter_map(|o| o) + .collect::>() + }, + ); + let aliases = namespaces + .get("aliases") + .and_then(|users| users.get("regex")) + .and_then(|regex| regex.as_str()) + .and_then(|regex| Regex::new(regex).ok()); + let rooms = namespaces + .get("rooms") + .and_then(|rooms| rooms.as_sequence()); + + let room_aliases = self.room_aliases(&room_id); + + let bridge_user_id = appservice + .1 + .get("sender_localpart") + .and_then(|string| string.as_str()) + .and_then(|string| { + UserId::parse_with_server_name(string, globals.server_name()).ok() + }); + + if bridge_user_id.map_or(false, |bridge_user_id| { + self.is_joined(&bridge_user_id, room_id).unwrap_or(false) + }) || users.iter().any(|users| { + dbg!( + users.is_match(pdu.sender.as_str()) + || pdu.kind == EventType::RoomMember + && pdu.state_key.as_ref().map_or(false, |state_key| dbg!( + users.is_match(dbg!(&state_key)) + )) + ) + }) || aliases.map_or(false, |aliases| { + room_aliases + .filter_map(|r| r.ok()) + .any(|room_alias| aliases.is_match(room_alias.as_str())) + }) || rooms.map_or(false, |rooms| rooms.contains(&room_id.as_str().into())) + { + sending.send_pdu_appservice(&appservice.0, &pdu_id)?; + } + } } Ok(pdu.event_id) diff --git a/src/server_server.rs b/src/server_server.rs index c47afab1..eb6b2373 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -28,9 +28,6 @@ use std::{ time::{Duration, SystemTime}, }; - - - pub async fn send_request( globals: &crate::database::globals::Globals, destination: Box,