2021-04-29 11:28:08 +00:00
|
|
|
// Copyright 2021 Famedly GmbH
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Matrix [Application Service] library
|
|
|
|
//!
|
2021-05-17 09:38:28 +00:00
|
|
|
//! The appservice crate aims to provide a batteries-included experience by
|
|
|
|
//! being a thin wrapper around the [`matrix_sdk`]. That means that we
|
|
|
|
//!
|
2021-05-12 17:20:52 +00:00
|
|
|
//! * ship with functionality to configure your webserver crate or simply run
|
|
|
|
//! the webserver for you
|
2021-05-10 06:43:06 +00:00
|
|
|
//! * receive and validate requests from the homeserver correctly
|
|
|
|
//! * allow calling the homeserver with proper virtual user identity assertion
|
2021-05-13 15:42:06 +00:00
|
|
|
//! * have consistent room state by leveraging matrix-sdk's state store
|
|
|
|
//! * provide E2EE support by leveraging matrix-sdk's crypto store
|
|
|
|
//!
|
|
|
|
//! # Status
|
|
|
|
//!
|
|
|
|
//! The crate is in an experimental state. Follow
|
|
|
|
//! [matrix-org/matrix-rust-sdk#228] for progress.
|
2021-05-10 06:43:06 +00:00
|
|
|
//!
|
2021-04-29 11:28:08 +00:00
|
|
|
//! # Quickstart
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
|
|
|
//! # async {
|
2021-05-13 15:42:06 +00:00
|
|
|
//! #
|
|
|
|
//! # use matrix_sdk::{async_trait, EventHandler};
|
|
|
|
//! #
|
|
|
|
//! # struct AppserviceEventHandler;
|
|
|
|
//! #
|
|
|
|
//! # #[async_trait]
|
|
|
|
//! # impl EventHandler for AppserviceEventHandler {}
|
|
|
|
//! #
|
2021-04-29 11:28:08 +00:00
|
|
|
//! use matrix_sdk_appservice::{Appservice, AppserviceRegistration};
|
|
|
|
//!
|
2021-05-10 06:43:06 +00:00
|
|
|
//! let homeserver_url = "http://127.0.0.1:8008";
|
2021-04-29 11:28:08 +00:00
|
|
|
//! let server_name = "localhost";
|
|
|
|
//! let registration = AppserviceRegistration::try_from_yaml_str(
|
|
|
|
//! r"
|
|
|
|
//! id: appservice
|
2021-05-10 06:43:06 +00:00
|
|
|
//! url: http://127.0.0.1:9009
|
2021-04-29 11:28:08 +00:00
|
|
|
//! as_token: as_token
|
|
|
|
//! hs_token: hs_token
|
|
|
|
//! sender_localpart: _appservice
|
|
|
|
//! namespaces:
|
|
|
|
//! users:
|
|
|
|
//! - exclusive: true
|
|
|
|
//! regex: '@_appservice_.*'
|
2021-05-13 15:42:06 +00:00
|
|
|
//! ")?;
|
2021-04-29 11:28:08 +00:00
|
|
|
//!
|
2021-05-26 12:00:59 +00:00
|
|
|
//! let mut appservice = Appservice::new(homeserver_url, server_name, registration).await?;
|
2021-05-13 15:42:06 +00:00
|
|
|
//! appservice.set_event_handler(Box::new(AppserviceEventHandler)).await?;
|
|
|
|
//!
|
|
|
|
//! let (host, port) = appservice.registration().get_host_and_port()?;
|
|
|
|
//! appservice.run(host, port).await?;
|
|
|
|
//! #
|
|
|
|
//! # Ok::<(), Box<dyn std::error::Error + 'static>>(())
|
2021-04-29 11:28:08 +00:00
|
|
|
//! # };
|
|
|
|
//! ```
|
|
|
|
//!
|
2021-05-13 15:42:06 +00:00
|
|
|
//! Check the [examples directory] for fully working examples.
|
|
|
|
//!
|
2021-04-29 11:28:08 +00:00
|
|
|
//! [Application Service]: https://matrix.org/docs/spec/application_service/r0.1.2
|
2021-05-13 15:42:06 +00:00
|
|
|
//! [matrix-org/matrix-rust-sdk#228]: https://github.com/matrix-org/matrix-rust-sdk/issues/228
|
|
|
|
//! [examples directory]: https://github.com/matrix-org/matrix-rust-sdk/tree/master/matrix_sdk_appservice/examples
|
2021-04-29 11:28:08 +00:00
|
|
|
|
2021-05-10 07:04:51 +00:00
|
|
|
#[cfg(not(any(feature = "actix",)))]
|
|
|
|
compile_error!("one webserver feature must be enabled. available ones: `actix`");
|
|
|
|
|
2021-04-29 11:28:08 +00:00
|
|
|
use std::{
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
fs::File,
|
|
|
|
ops::Deref,
|
|
|
|
path::PathBuf,
|
2021-05-17 10:02:17 +00:00
|
|
|
sync::Arc,
|
2021-04-29 11:28:08 +00:00
|
|
|
};
|
|
|
|
|
2021-05-26 12:00:59 +00:00
|
|
|
use dashmap::DashMap;
|
2021-04-29 11:28:08 +00:00
|
|
|
use http::Uri;
|
2021-05-12 10:56:29 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use matrix_sdk::api_appservice as api;
|
2021-04-29 11:28:08 +00:00
|
|
|
use matrix_sdk::{
|
|
|
|
api::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::{
|
|
|
|
account::register::{LoginType, Request as RegistrationRequest},
|
|
|
|
uiaa::UiaaResponse,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
api_appservice::Registration,
|
|
|
|
assign,
|
|
|
|
identifiers::{self, DeviceId, ServerNameBox, UserId},
|
|
|
|
reqwest::Url,
|
2021-05-26 12:00:59 +00:00
|
|
|
Client, ClientConfig, EventHandler, FromHttpResponseError, HttpError, ServerError, Session,
|
2021-04-29 11:28:08 +00:00
|
|
|
};
|
|
|
|
use regex::Regex;
|
|
|
|
use tracing::warn;
|
|
|
|
|
|
|
|
#[cfg(feature = "actix")]
|
|
|
|
mod actix;
|
|
|
|
mod error;
|
|
|
|
|
|
|
|
pub use error::Error;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
pub type Host = String;
|
|
|
|
pub type Port = u16;
|
|
|
|
|
|
|
|
/// Appservice Registration
|
2021-05-13 15:42:06 +00:00
|
|
|
///
|
|
|
|
/// Wrapper around [`Registration`]
|
2021-04-29 11:28:08 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct AppserviceRegistration {
|
|
|
|
inner: Registration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AppserviceRegistration {
|
|
|
|
/// Try to load registration from yaml string
|
2021-05-10 06:43:06 +00:00
|
|
|
///
|
|
|
|
/// See the fields of [`Registration`] for the required format
|
2021-04-29 11:28:08 +00:00
|
|
|
pub fn try_from_yaml_str(value: impl AsRef<str>) -> Result<Self> {
|
2021-05-12 15:00:47 +00:00
|
|
|
Ok(Self { inner: serde_yaml::from_str(value.as_ref())? })
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to load registration from yaml file
|
2021-05-10 06:43:06 +00:00
|
|
|
///
|
|
|
|
/// See the fields of [`Registration`] for the required format
|
2021-04-29 11:28:08 +00:00
|
|
|
pub fn try_from_yaml_file(path: impl Into<PathBuf>) -> Result<Self> {
|
|
|
|
let file = File::open(path.into())?;
|
|
|
|
|
2021-05-12 15:00:47 +00:00
|
|
|
Ok(Self { inner: serde_yaml::from_reader(file)? })
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
2021-05-13 15:42:06 +00:00
|
|
|
|
|
|
|
/// Get the host and port from the registration URL
|
|
|
|
///
|
|
|
|
/// If no port is found it falls back to scheme defaults: 80 for http and
|
|
|
|
/// 443 for https
|
|
|
|
pub fn get_host_and_port(&self) -> Result<(Host, Port)> {
|
|
|
|
let uri = Uri::try_from(&self.inner.url)?;
|
|
|
|
|
|
|
|
let host = uri.host().ok_or(Error::MissingRegistrationHost)?.to_owned();
|
|
|
|
let port = match uri.port() {
|
|
|
|
Some(port) => Ok(port.as_u16()),
|
|
|
|
None => match uri.scheme_str() {
|
|
|
|
Some("http") => Ok(80),
|
|
|
|
Some("https") => Ok(443),
|
|
|
|
_ => Err(Error::MissingRegistrationPort),
|
|
|
|
},
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Ok((host, port))
|
|
|
|
}
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Registration> for AppserviceRegistration {
|
|
|
|
fn from(value: Registration) -> Self {
|
|
|
|
Self { inner: value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for AppserviceRegistration {
|
|
|
|
type Target = Registration;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:00:59 +00:00
|
|
|
type Localpart = String;
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
/// The `localpart` of the user associated with the application service via
|
|
|
|
/// `sender_localpart` in [`AppserviceRegistration`].
|
2021-05-26 12:00:59 +00:00
|
|
|
///
|
|
|
|
/// Dummy type for shared documentation
|
|
|
|
#[allow(dead_code)]
|
2021-06-01 08:48:01 +00:00
|
|
|
pub type MainUser = ();
|
|
|
|
|
|
|
|
/// The application service may specify the virtual user to act as through use
|
|
|
|
/// of a user_id query string parameter on the request. The user specified in
|
|
|
|
/// the query string must be covered by one of the [`AppserviceRegistration`]'s
|
|
|
|
/// `users` namespaces.
|
|
|
|
///
|
|
|
|
/// Dummy type for shared documentation
|
|
|
|
pub type VirtualUser = ();
|
2021-04-29 11:28:08 +00:00
|
|
|
|
|
|
|
/// Appservice
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Appservice {
|
|
|
|
homeserver_url: Url,
|
|
|
|
server_name: ServerNameBox,
|
2021-05-17 10:02:17 +00:00
|
|
|
registration: Arc<AppserviceRegistration>,
|
2021-05-26 12:00:59 +00:00
|
|
|
clients: Arc<DashMap<Localpart, Client>>,
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Appservice {
|
|
|
|
/// Create new Appservice
|
2021-05-10 06:43:06 +00:00
|
|
|
///
|
2021-06-01 08:48:01 +00:00
|
|
|
/// Also creates and caches a [`Client`] for the [`MainUser`].
|
2021-05-31 10:50:53 +00:00
|
|
|
/// The default [`ClientConfig`] is used, if you want to customize it
|
2021-06-01 08:48:01 +00:00
|
|
|
/// use [`Self::new_with_config()`] instead.
|
2021-05-31 10:50:53 +00:00
|
|
|
///
|
2021-05-10 06:43:06 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `homeserver_url` - The homeserver that the client should connect to.
|
2021-05-12 17:20:52 +00:00
|
|
|
/// * `server_name` - The server name to use when constructing user ids from
|
|
|
|
/// the localpart.
|
|
|
|
/// * `registration` - The [Appservice Registration] to use when interacting
|
2021-06-05 12:35:20 +00:00
|
|
|
/// with the homeserver.
|
2021-05-10 06:43:06 +00:00
|
|
|
///
|
|
|
|
/// [Appservice Registration]: https://matrix.org/docs/spec/application_service/r0.1.2#registration
|
2021-04-29 11:28:08 +00:00
|
|
|
pub async fn new(
|
|
|
|
homeserver_url: impl TryInto<Url, Error = url::ParseError>,
|
|
|
|
server_name: impl TryInto<ServerNameBox, Error = identifiers::Error>,
|
|
|
|
registration: AppserviceRegistration,
|
|
|
|
) -> Result<Self> {
|
2021-06-01 08:48:01 +00:00
|
|
|
let appservice = Self::new_with_config(
|
2021-05-31 10:50:53 +00:00
|
|
|
homeserver_url,
|
|
|
|
server_name,
|
|
|
|
registration,
|
|
|
|
ClientConfig::default(),
|
2021-05-13 15:42:06 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-04-29 11:28:08 +00:00
|
|
|
|
2021-05-31 10:50:53 +00:00
|
|
|
Ok(appservice)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as [`Self::new()`] but lets you provide a [`ClientConfig`] for the
|
|
|
|
/// [`Client`]
|
2021-06-01 08:48:01 +00:00
|
|
|
pub async fn new_with_config(
|
2021-05-31 10:50:53 +00:00
|
|
|
homeserver_url: impl TryInto<Url, Error = url::ParseError>,
|
|
|
|
server_name: impl TryInto<ServerNameBox, Error = identifiers::Error>,
|
|
|
|
registration: AppserviceRegistration,
|
|
|
|
client_config: ClientConfig,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let homeserver_url = homeserver_url.try_into()?;
|
|
|
|
let server_name = server_name.try_into()?;
|
2021-05-17 10:02:17 +00:00
|
|
|
let registration = Arc::new(registration);
|
2021-05-31 10:50:53 +00:00
|
|
|
let clients = Arc::new(DashMap::new());
|
2021-06-01 08:48:01 +00:00
|
|
|
let sender_localpart = registration.sender_localpart.clone();
|
2021-05-31 10:50:53 +00:00
|
|
|
|
|
|
|
let appservice = Appservice { homeserver_url, server_name, registration, clients };
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
// we cache the [`MainUser`] by default
|
|
|
|
appservice.virtual_user_with_config(sender_localpart, client_config).await?;
|
2021-05-17 10:02:17 +00:00
|
|
|
|
2021-05-31 10:50:53 +00:00
|
|
|
Ok(appservice)
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
/// Create a [`Client`] for the given [`VirtualUser`]'s `localpart`
|
2021-05-13 15:42:06 +00:00
|
|
|
///
|
2021-05-26 12:00:59 +00:00
|
|
|
/// Will create and return a [`Client`] that's configured to [assert the
|
|
|
|
/// identity] on all outgoing homeserver requests if `localpart` is
|
2021-06-01 08:48:01 +00:00
|
|
|
/// given.
|
2021-05-26 12:00:59 +00:00
|
|
|
///
|
|
|
|
/// This method is a singleton that saves the client internally for re-use
|
2021-06-01 08:48:01 +00:00
|
|
|
/// based on the `localpart`. The cached [`Client`] can be retrieved either
|
|
|
|
/// by calling this method again or by calling [`Self::get_cached_client()`]
|
|
|
|
/// which is non-async convenience wrapper.
|
2021-05-13 15:42:06 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
2021-04-29 11:28:08 +00:00
|
|
|
///
|
2021-05-13 15:42:06 +00:00
|
|
|
/// * `localpart` - The localpart of the user we want assert our identity to
|
2021-04-29 11:28:08 +00:00
|
|
|
///
|
|
|
|
/// [registration]: https://matrix.org/docs/spec/application_service/r0.1.2#registration
|
2021-05-13 15:42:06 +00:00
|
|
|
/// [assert the identity]: https://matrix.org/docs/spec/application_service/r0.1.2#identity-assertion
|
2021-06-01 08:48:01 +00:00
|
|
|
pub async fn virtual_user(&self, localpart: impl AsRef<str>) -> Result<Client> {
|
|
|
|
let client = self.virtual_user_with_config(localpart, ClientConfig::default()).await?;
|
2021-05-26 12:00:59 +00:00
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
/// Same as [`Self::virtual_user()`] but with the ability to pass in a
|
2021-05-26 12:00:59 +00:00
|
|
|
/// [`ClientConfig`]
|
|
|
|
///
|
|
|
|
/// Since this method is a singleton follow-up calls with different
|
|
|
|
/// [`ClientConfig`]s will be ignored.
|
2021-06-01 08:48:01 +00:00
|
|
|
pub async fn virtual_user_with_config(
|
2021-05-31 10:50:53 +00:00
|
|
|
&self,
|
2021-06-01 08:48:01 +00:00
|
|
|
localpart: impl AsRef<str>,
|
2021-05-26 12:00:59 +00:00
|
|
|
config: ClientConfig,
|
|
|
|
) -> Result<Client> {
|
2021-06-01 08:48:01 +00:00
|
|
|
// TODO: check if localpart is covered by namespace?
|
|
|
|
let localpart = localpart.as_ref();
|
2021-05-13 15:42:06 +00:00
|
|
|
|
2021-05-26 12:00:59 +00:00
|
|
|
let client = if let Some(client) = self.clients.get(localpart) {
|
|
|
|
client.clone()
|
2021-05-13 15:42:06 +00:00
|
|
|
} else {
|
2021-05-26 12:00:59 +00:00
|
|
|
let user_id = UserId::parse_with_server_name(localpart, &self.server_name)?;
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
// The `as_token` in the `Session` maps to the [`MainUser`]
|
2021-05-26 12:00:59 +00:00
|
|
|
// (`sender_localpart`) by default, so we don't need to assert identity
|
|
|
|
// in that case
|
|
|
|
if localpart != self.registration.sender_localpart {
|
|
|
|
config.get_request_config().assert_identity();
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:42:06 +00:00
|
|
|
let client = Client::new_with_config(self.homeserver_url.clone(), config)?;
|
|
|
|
|
2021-05-26 12:00:59 +00:00
|
|
|
let session = Session {
|
|
|
|
access_token: self.registration.as_token.clone(),
|
|
|
|
user_id: user_id.clone(),
|
|
|
|
// TODO: expose & proper E2EE
|
|
|
|
device_id: DeviceId::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
client.restore_login(session).await?;
|
|
|
|
self.clients.insert(localpart.to_owned(), client.clone());
|
2021-05-13 15:42:06 +00:00
|
|
|
|
|
|
|
client
|
|
|
|
};
|
2021-04-29 11:28:08 +00:00
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:00:59 +00:00
|
|
|
/// Get cached [`Client`]
|
|
|
|
///
|
|
|
|
/// Will return the client for the given `localpart` if previously
|
2021-06-01 08:48:01 +00:00
|
|
|
/// constructed with [`Self::virtual_user()`] or
|
|
|
|
/// [`Self::virtual_user_with_config()`].
|
|
|
|
///
|
|
|
|
/// If no `localpart` is given it assumes the [`MainUser`]'s `localpart`. If
|
|
|
|
/// no client for `localpart` is found it will return an Error.
|
2021-05-26 12:00:59 +00:00
|
|
|
pub fn get_cached_client(&self, localpart: Option<&str>) -> Result<Client> {
|
|
|
|
let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref());
|
|
|
|
|
|
|
|
let entry = self.clients.get(localpart).ok_or(Error::NoClientForLocalpart)?;
|
|
|
|
|
|
|
|
Ok(entry.value().clone())
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:42:06 +00:00
|
|
|
/// Convenience wrapper around [`Client::set_event_handler()`]
|
2021-05-26 12:00:59 +00:00
|
|
|
///
|
2021-06-01 08:48:01 +00:00
|
|
|
/// Attaches the event handler to the [`MainUser`]'s [`Client`]
|
2021-05-26 12:00:59 +00:00
|
|
|
pub async fn set_event_handler(&mut self, handler: Box<dyn EventHandler>) -> Result<()> {
|
2021-06-01 08:48:01 +00:00
|
|
|
let client = self.get_cached_client(None)?;
|
2021-05-26 12:00:59 +00:00
|
|
|
|
2021-05-13 15:42:06 +00:00
|
|
|
client.set_event_handler(handler).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a virtual user by sending a [`RegistrationRequest`] to the
|
|
|
|
/// homeserver
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `localpart` - The localpart of the user to register. Must be covered
|
|
|
|
/// by the namespaces in the [`Registration`] in order to succeed.
|
2021-05-26 12:00:59 +00:00
|
|
|
pub async fn register(&mut self, localpart: impl AsRef<str>) -> Result<()> {
|
2021-04-29 11:28:08 +00:00
|
|
|
let request = assign!(RegistrationRequest::new(), {
|
|
|
|
username: Some(localpart.as_ref()),
|
|
|
|
login_type: Some(&LoginType::ApplicationService),
|
|
|
|
});
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
let client = self.get_cached_client(None)?;
|
2021-05-13 15:42:06 +00:00
|
|
|
match client.register(request).await {
|
2021-04-29 11:28:08 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(error) => match error {
|
|
|
|
matrix_sdk::Error::Http(HttpError::UiaaError(FromHttpResponseError::Http(
|
|
|
|
ServerError::Known(UiaaResponse::MatrixError(ref matrix_error)),
|
|
|
|
))) => {
|
|
|
|
match matrix_error.kind {
|
|
|
|
ErrorKind::UserInUse => {
|
|
|
|
// TODO: persist the fact that we registered that user
|
|
|
|
warn!("{}", matrix_error.message);
|
|
|
|
}
|
|
|
|
_ => return Err(error.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(error.into()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the Appservice [registration]
|
|
|
|
///
|
|
|
|
/// [registration]: https://matrix.org/docs/spec/application_service/r0.1.2#registration
|
2021-05-13 15:42:06 +00:00
|
|
|
pub fn registration(&self) -> &AppserviceRegistration {
|
2021-04-29 11:28:08 +00:00
|
|
|
&self.registration
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compare the given `hs_token` against `registration.hs_token`
|
2021-05-10 06:43:06 +00:00
|
|
|
///
|
|
|
|
/// Returns `true` if the tokens match, `false` otherwise.
|
2021-05-13 15:42:06 +00:00
|
|
|
pub fn compare_hs_token(&self, hs_token: impl AsRef<str>) -> bool {
|
2021-04-29 11:28:08 +00:00
|
|
|
self.registration.hs_token == hs_token.as_ref()
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:48:01 +00:00
|
|
|
/// Check if given `user_id` is in any of the [`AppserviceRegistration`]'s
|
|
|
|
/// `users` namespaces
|
2021-04-29 11:28:08 +00:00
|
|
|
pub fn user_id_is_in_namespace(&self, user_id: impl AsRef<str>) -> Result<bool> {
|
|
|
|
for user in &self.registration.namespaces.users {
|
|
|
|
// TODO: precompile on Appservice construction
|
|
|
|
let re = Regex::new(&user.regex)?;
|
|
|
|
if re.is_match(user_id.as_ref()) {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Service to register on an Actix `App`
|
|
|
|
#[cfg(feature = "actix")]
|
|
|
|
#[cfg_attr(docs, doc(cfg(feature = "actix")))]
|
|
|
|
pub fn actix_service(&self) -> actix::Scope {
|
|
|
|
actix::get_scope().data(self.clone())
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:20:52 +00:00
|
|
|
/// Convenience method that runs an http server depending on the selected
|
|
|
|
/// server feature
|
2021-04-29 11:28:08 +00:00
|
|
|
///
|
2021-05-12 17:20:52 +00:00
|
|
|
/// This is a blocking call that tries to listen on the provided host and
|
|
|
|
/// port
|
2021-04-29 11:28:08 +00:00
|
|
|
pub async fn run(&self, host: impl AsRef<str>, port: impl Into<u16>) -> Result<()> {
|
|
|
|
#[cfg(feature = "actix")]
|
|
|
|
{
|
|
|
|
actix::run_server(self.clone(), host, port).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-10 07:04:51 +00:00
|
|
|
#[cfg(not(any(feature = "actix",)))]
|
|
|
|
unreachable!()
|
2021-04-29 11:28:08 +00:00
|
|
|
}
|
|
|
|
}
|