Compare commits

...
11 Commits
4 changed files with 33 additions and 38 deletions

No files matched your search

+9 -9
View File
@@ -1,4 +1,4 @@
/** /*!
* matrix-lolopener v0.1.0 * matrix-lolopener v0.1.0
* *
* Copyright (C) 2026 The 1312 Media Collective * Copyright (C) 2026 The 1312 Media Collective
@@ -56,8 +56,8 @@ impl Config {
let serialized_config = fs::read_to_string(config_file).map_err(|e| { let serialized_config = fs::read_to_string(config_file).map_err(|e| {
match e.kind() { match e.kind() {
io::ErrorKind::NotFound => Error::FileNotFoundError(config_file.into()), io::ErrorKind::NotFound => Error::FileNotFound(config_file.into()),
io::ErrorKind::PermissionDenied => Error::PermissionDeniedError(), io::ErrorKind::PermissionDenied => Error::PermissionDenied(),
_ => e.into(), _ => e.into(),
} }
})?; })?;
@@ -71,21 +71,21 @@ impl Config {
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] // we use Debug, so inner fields are never read #[allow(dead_code)] // we use Debug, so inner fields are never read
pub enum Error { pub enum Error {
FileNotFoundError(String), FileNotFound(String),
PermissionDeniedError(), PermissionDenied(),
InvalidFormatError(serde_yaml::Error), InvalidFormat(serde_yaml::Error),
IoError(io::Error), Io(io::Error),
} }
impl From<serde_yaml::Error> for Error { impl From<serde_yaml::Error> for Error {
fn from(e: serde_yaml::Error) -> Self { fn from(e: serde_yaml::Error) -> Self {
Self::InvalidFormatError(e) Self::InvalidFormat(e)
} }
} }
impl From<io::Error> for Error { impl From<io::Error> for Error {
fn from(e: io::Error) -> Self { fn from(e: io::Error) -> Self {
Self::IoError(e) Self::Io(e)
} }
} }
+4 -6
View File
@@ -1,4 +1,4 @@
/** /*!
* matrix-lolopener v0.1.0 * matrix-lolopener v0.1.0
* *
* Copyright (C) 2026 The 1312 Media Collective * Copyright (C) 2026 The 1312 Media Collective
@@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
// it holds a tuple with an HTML message and a list of rooms to post to // 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<String>)>(1024); let (bcast_tx, bcast_rx) = broadcast::channel::<(String, Vec<String>)>(1024);
let handle: JoinHandle<_> = (|| { let handle: JoinHandle<_> = {
let matrix_ready = Arc::clone(&matrix_ready); let matrix_ready = Arc::clone(&matrix_ready);
let config = Arc::clone(&config); let config = Arc::clone(&config);
@@ -68,14 +68,12 @@ async fn main() -> anyhow::Result<()> {
debug!("Sending msg {}", msg); debug!("Sending msg {}", msg);
//XXX: Panic if the matrix module can't get the message //XXX: Panic if the matrix module can't get the message
bcast_tx.send((msg.into(), config.rooms.clone())).unwrap(); bcast_tx.send((msg.into(), config.rooms.clone())).unwrap();
()
}).await.unwrap(); //XXX: Panic if the mqtt broker fails }).await.unwrap(); //XXX: Panic if the mqtt broker fails
}) })
})(); };
login_and_sync( login_and_sync(
config.homeserver_uri.clone().into(), config.homeserver_uri.clone(),
&config.auth, &config.auth,
matrix_ready, matrix_ready,
bcast_rx bcast_rx
+4 -3
View File
@@ -1,4 +1,4 @@
/** /*!
* matrix-lolopener v0.1.0 * matrix-lolopener v0.1.0
* *
* Copyright (C) 2024 The 1312 Media Collective * Copyright (C) 2024 The 1312 Media Collective
@@ -87,10 +87,12 @@ async fn send_to_room(
Ok(_) => { Ok(_) => {
info!("Posting to room {}", room.room_id()); info!("Posting to room {}", room.room_id());
room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await?; room.send(RoomMessageEventContent::text_html(msg.clone(), msg)).await?;
return Ok(()); return Ok(());
}, },
Err(e) => { Err(e) => {
error!("Failed to join room {e:?}"); error!("Failed to join room {e:?}");
return Err(e.into()); return Err(e.into());
} }
}; };
@@ -158,7 +160,6 @@ pub async fn login_and_sync(
Ok(_) => return Ok(()), Ok(_) => return Ok(()),
Err(e) => { Err(e) => {
error!("Sync error: {e:?}"); error!("Sync error: {e:?}");
()
} }
}, },
recv = rx.recv() => match recv { recv = rx.recv() => match recv {
@@ -179,7 +180,7 @@ pub async fn login_and_sync(
} }
}, },
Err(e) => { Err(e) => {
panic!("Broadcast channel is lagging: {e:?}"); panic!("Broken channel: {e:?}");
} }
} }
} }
+16 -20
View File
@@ -1,4 +1,4 @@
/** /*!
* matrix-lolopener v0.1.0 * matrix-lolopener v0.1.0
* *
* Copyright (C) 2026 The 1312 Media Collective * Copyright (C) 2026 The 1312 Media Collective
@@ -54,13 +54,12 @@ impl MQTT {
mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_keep_alive(Duration::from_secs(5));
let (client, eventloop) = AsyncClient::new(mqttoptions, 1); let (client, eventloop) = AsyncClient::new(mqttoptions, 1);
let mqtt = MQTT {
config: config,
client: client,
eventloop: eventloop,
};
mqtt MQTT {
config,
client,
eventloop,
}
} }
pub async fn subscribe<F: Fn(StatusMessage)>(&mut self, callback: F) -> Result<()> { pub async fn subscribe<F: Fn(StatusMessage)>(&mut self, callback: F) -> Result<()> {
@@ -71,13 +70,10 @@ impl MQTT {
let notification = self.eventloop.poll().await?; let notification = self.eventloop.poll().await?;
debug!("Notification = {:?}", notification); debug!("Notification = {:?}", notification);
match notification { if let Event::Incoming(Incoming::Publish(p)) = notification {
Event::Incoming(Incoming::Publish(p)) => { let msg: StatusMessage = serde_json::from_slice(p.payload.as_ref())?;
let msg: StatusMessage = serde_json::from_slice(p.payload.as_ref())?; info!("Message: {:?}", msg);
info!("Message: {:?}", msg); callback(msg);
callback(msg);
},
_ => (),
} }
} }
@@ -89,26 +85,26 @@ impl MQTT {
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] // we use Debug, so inner fields are never read #[allow(dead_code)] // we use Debug, so inner fields are never read
pub enum Error { pub enum Error {
MQTTClientError(rumqttc::ClientError), MQTTClient(rumqttc::ClientError),
MQTTConnectionError(rumqttc::ConnectionError), MQTTConnection(rumqttc::ConnectionError),
InvalidFormatError(serde_json::Error), InvalidFormat(serde_json::Error),
} }
impl From<rumqttc::ClientError> for Error { impl From<rumqttc::ClientError> for Error {
fn from(e: rumqttc::ClientError) -> Self { fn from(e: rumqttc::ClientError) -> Self {
Self::MQTTClientError(e) Self::MQTTClient(e)
} }
} }
impl From<rumqttc::ConnectionError> for Error { impl From<rumqttc::ConnectionError> for Error {
fn from(e: rumqttc::ConnectionError) -> Self { fn from(e: rumqttc::ConnectionError) -> Self {
Self::MQTTConnectionError(e) Self::MQTTConnection(e)
} }
} }
impl From<serde_json::Error> for Error { impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self { fn from(e: serde_json::Error) -> Self {
Self::InvalidFormatError(e) Self::InvalidFormat(e)
} }
} }