/*!
* matrix-lolopener v0.1.0
*
* Copyright (C) 2026 The 1312 Media Collective
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
mod config;
mod matrix;
mod mqtt;
use tokio::{
sync::{Notify, broadcast},
task::JoinHandle,
};
use crate::{
config::Config,
matrix::login_and_sync,
mqtt::{MQTT, StatusMessage},
};
use std::sync::Arc;
use tracing::{ info, debug };
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let config = Config::load("config.yml").unwrap_or_else(|e| {
panic!("Failed to load config: {e:?}")
});
let config = Arc::new(config);
let matrix_ready = Arc::new(Notify::new());
// This channel is used for sending messages to the matrix module,
// it holds a tuple with an HTML message and a list of rooms to post to
let (bcast_tx, bcast_rx) = broadcast::channel::<(String, Vec)>(1024);
let handle: JoinHandle<_> = (|| {
let matrix_ready = Arc::clone(&matrix_ready);
let config = Arc::clone(&config);
tokio::spawn(async move {
debug!("Waiting until matrix is ready");
matrix_ready.notified().await;
info!("Matrix is ready");
let mut mqtt = MQTT::new(config.mqtt.clone()).await;
mqtt.subscribe(|status: StatusMessage| {
let msg = match status {
StatusMessage { contact: true, .. } => "🔴 Le LOL est fermé :(",
StatusMessage { contact: false, .. } => "🟢 Le LOL est ouvert :)",
};
debug!("Sending msg {}", msg);
//XXX: Panic if the matrix module can't get the message
bcast_tx.send((msg.into(), config.rooms.clone())).unwrap();
()
}).await.unwrap(); //XXX: Panic if the mqtt broker fails
})
})();
login_and_sync(
config.homeserver_uri.clone().into(),
&config.auth,
matrix_ready,
bcast_rx
).await?;
handle.abort();
handle.await?;
Ok(())
}