phoebe/src/main.rs

102 lines
2.5 KiB
Rust
Raw Normal View History

2021-09-12 02:59:13 +00:00
use std::sync::Arc;
2021-09-10 04:44:00 +00:00
use discord::create_discord_client;
use matrix::create_matrix_client;
2021-08-12 09:47:38 +00:00
2021-09-12 02:59:13 +00:00
mod bridgers;
mod channels;
2021-09-12 02:59:13 +00:00
mod message_ast;
mod messages;
2021-09-10 04:44:00 +00:00
2021-09-12 02:59:13 +00:00
pub mod discord;
pub mod matrix;
2021-09-10 04:44:00 +00:00
2021-09-12 02:59:13 +00:00
use bridgers::Bridgers;
use tokio::sync::mpsc;
2021-09-10 04:44:00 +00:00
2021-09-13 02:08:35 +00:00
use crate::messages::MessageEvent;
2021-09-10 04:44:00 +00:00
async fn setup_discord(
token: String,
bridgers: Arc<Bridgers>,
2021-09-13 02:08:35 +00:00
discord_tx: mpsc::UnboundedSender<MessageEvent>,
2021-09-10 04:44:00 +00:00
) {
let (discord_ctx_tx, mut discord_ctx_rx) = mpsc::unbounded_channel::<discord::Context>();
tokio::spawn(async move {
let mut discord = create_discord_client(discord_ctx_tx, discord_tx, &token).await;
discord.start().await.unwrap();
});
// Hack to grab the Context object when discord is ready
tokio::spawn(async move {
while let Some(discord) = discord_ctx_rx.recv().await {
bridgers.discord.lock().unwrap().replace(Some(discord));
}
});
}
async fn setup_matrix(
homeserver_url: String,
username: String,
password: String,
bridgers: Arc<Bridgers>,
2021-09-13 02:08:35 +00:00
event_tx: mpsc::UnboundedSender<MessageEvent>,
2021-09-10 04:44:00 +00:00
) {
2021-09-13 02:08:35 +00:00
let client = create_matrix_client(homeserver_url, username, password, event_tx).await;
2021-09-10 04:44:00 +00:00
bridgers
.matrix
.lock()
.unwrap()
.replace(Some(client.clone()));
2021-09-12 02:59:13 +00:00
let settings = matrix_sdk::SyncSettings::default().token(client.sync_token().await.unwrap());
2021-09-10 04:44:00 +00:00
tokio::spawn(async move {
client.sync(settings).await;
});
}
#[tokio::main]
async fn main() {
2021-09-12 02:59:13 +00:00
env_logger::init();
2021-09-10 04:44:00 +00:00
let bridgers = Arc::new(Bridgers::new());
2021-09-13 02:08:35 +00:00
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel::<MessageEvent>();
2021-09-10 04:44:00 +00:00
2021-09-12 02:59:13 +00:00
#[inline]
fn get_env_var(key: &str) -> String {
std::env::var(key).expect("DISCORD_TOKEN not set in environment")
}
2021-09-10 04:44:00 +00:00
setup_discord(
2021-09-12 02:59:13 +00:00
get_env_var("DISCORD_TOKEN"),
2021-09-10 04:44:00 +00:00
Arc::clone(&bridgers),
2021-09-13 02:08:35 +00:00
event_tx.clone(),
2021-09-10 04:44:00 +00:00
)
.await;
setup_matrix(
2021-09-12 02:59:13 +00:00
get_env_var("MATRIX_HOMESERVER"),
get_env_var("MATRIX_USERNAME"),
get_env_var("MATRIX_PASSWORD"),
2021-09-10 04:44:00 +00:00
Arc::clone(&bridgers),
2021-09-13 02:08:35 +00:00
event_tx.clone(),
2021-09-10 04:44:00 +00:00
)
.await;
2021-09-13 02:08:35 +00:00
while let Some(event) = event_rx.recv().await {
match event {
MessageEvent::Send(sent_message) => {
let _ = bridgers.send_message(sent_message).await;
}
MessageEvent::Edit(_edited_message) => {
2021-09-13 02:08:35 +00:00
todo!();
}
MessageEvent::AdminLinkChannels(channels) => {
bridgers.link_channels(&channels);
}
2021-09-13 02:08:35 +00:00
}
2021-09-10 04:44:00 +00:00
}
2021-08-12 09:47:38 +00:00
}