2021-06-04 12:16:24 +00:00
|
|
|
use std::{convert::TryFrom, env};
|
|
|
|
|
|
|
|
use matrix_sdk_appservice::{
|
2021-06-08 09:18:56 +00:00
|
|
|
matrix_sdk::{
|
2021-06-04 12:16:24 +00:00
|
|
|
async_trait,
|
|
|
|
room::Room,
|
2021-06-23 10:06:28 +00:00
|
|
|
ruma::{
|
|
|
|
events::{
|
|
|
|
room::member::{MemberEventContent, MembershipState},
|
|
|
|
SyncStateEvent,
|
|
|
|
},
|
|
|
|
UserId,
|
|
|
|
},
|
2021-06-04 12:16:24 +00:00
|
|
|
EventHandler,
|
|
|
|
},
|
2021-06-15 10:09:01 +00:00
|
|
|
AppService, AppServiceRegistration,
|
2021-06-04 12:16:24 +00:00
|
|
|
};
|
|
|
|
use tracing::{error, trace};
|
|
|
|
|
2021-06-15 10:09:01 +00:00
|
|
|
struct AppServiceEventHandler {
|
|
|
|
appservice: AppService,
|
2021-06-04 12:16:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 10:09:01 +00:00
|
|
|
impl AppServiceEventHandler {
|
|
|
|
pub fn new(appservice: AppService) -> Self {
|
2021-06-04 12:16:24 +00:00
|
|
|
Self { appservice }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_room_member(
|
|
|
|
&self,
|
|
|
|
room: Room,
|
|
|
|
event: &SyncStateEvent<MemberEventContent>,
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
if !self.appservice.user_id_is_in_namespace(&event.state_key)? {
|
|
|
|
trace!("not an appservice user: {}", event.state_key);
|
|
|
|
} else if let MembershipState::Invite = event.content.membership {
|
|
|
|
let user_id = UserId::try_from(event.state_key.clone())?;
|
|
|
|
|
|
|
|
let appservice = self.appservice.clone();
|
|
|
|
appservice.register_virtual_user(user_id.localpart()).await?;
|
|
|
|
|
|
|
|
let client = appservice.virtual_user_client(user_id.localpart()).await?;
|
|
|
|
|
|
|
|
client.join_room_by_id(room.room_id()).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2021-06-15 10:09:01 +00:00
|
|
|
impl EventHandler for AppServiceEventHandler {
|
2021-06-04 12:16:24 +00:00
|
|
|
async fn on_room_member(&self, room: Room, event: &SyncStateEvent<MemberEventContent>) {
|
|
|
|
match self.handle_room_member(room, event).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(error) => error!("{:?}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
env::set_var("RUST_LOG", "matrix_sdk=debug,matrix_sdk_appservice=debug");
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
let homeserver_url = "http://localhost:8008";
|
|
|
|
let server_name = "localhost";
|
2021-06-15 10:09:01 +00:00
|
|
|
let registration = AppServiceRegistration::try_from_yaml_file("./tests/registration.yaml")?;
|
2021-06-04 12:16:24 +00:00
|
|
|
|
2021-06-15 10:09:01 +00:00
|
|
|
let mut appservice = AppService::new(homeserver_url, server_name, registration).await?;
|
|
|
|
appservice.set_event_handler(Box::new(AppServiceEventHandler::new(appservice.clone()))).await?;
|
2021-06-04 12:16:24 +00:00
|
|
|
|
|
|
|
let (host, port) = appservice.registration().get_host_and_port()?;
|
|
|
|
appservice.run(host, port).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|