Skip to content

Commit

Permalink
Merge pull request #8 from lazytanuki/daemon_rooms
Browse files Browse the repository at this point in the history
feat: translate possible room name from configuration for the daemon
  • Loading branch information
arcuru authored Sep 16, 2024
2 parents 6b89c4e + a24d6ce commit 9e630a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
use matrix_sdk::ruma::events::tag::TagInfo;
use matrix_sdk::Room;

use tokio::sync::RwLock;
use tracing::error;

use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;

use http_body_util::BodyExt;
use http_body_util::Full;
Expand All @@ -24,9 +27,12 @@ use tokio::net::TcpListener;

/// Run in daemon mode
/// This binds to a port and listens for incoming requests, and sends them to the Matrix room
pub async fn daemon(config: &Option<DaemonConfig>) -> anyhow::Result<()> {
pub async fn daemon(
config: Option<DaemonConfig>,
rooms: Option<HashMap<String, String>>,
) -> anyhow::Result<()> {
let addr = {
if let Some(daemon) = config {
if let Some(daemon) = &config {
let ip_addr: IpAddr = daemon
.addr
.clone()
Expand Down Expand Up @@ -162,6 +168,7 @@ pub async fn daemon(config: &Option<DaemonConfig>) -> anyhow::Result<()> {
.await;

// Spawn a tokio task to continuously accept incoming connections
let rooms = Arc::new(RwLock::new(rooms));
tokio::task::spawn(async move {
// We start a loop to continuously accept incoming connections
loop {
Expand All @@ -179,9 +186,10 @@ pub async fn daemon(config: &Option<DaemonConfig>) -> anyhow::Result<()> {
let io = TokioIo::new(stream);

// Spawn a tokio task to serve each connection concurrently
let cloned_rooms = rooms.clone();
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(io, service_fn(daemon_poke))
.serve_connection(io, service_fn(|req| daemon_poke(req, cloned_rooms.clone())))
.await
{
eprintln!("Error serving connection: {:?}", err);
Expand Down Expand Up @@ -264,15 +272,23 @@ Current values:\n- block: {}{}",
/// Poke the room from an http request
async fn daemon_poke(
request: Request<hyper::body::Incoming>,
rooms: Arc<RwLock<Option<HashMap<String, String>>>>,
) -> Result<Response<Full<Bytes>>, hyper::Error> {
// The uri without the leading / will be the room id
let room_id = request.uri().path().trim_start_matches('/').to_string();
// The room_id may be URI encoded
let room_id = match urlencoding::decode(&room_id) {
let mut room_id = match urlencoding::decode(&room_id) {
Ok(room) => room.to_string(),
Err(_) => room_id,
};

// If the room is a room name in the config, we'll transform it to the room id
room_id = if let Some(room_id) = &rooms.read().await.as_ref().and_then(|r| r.get(&room_id)) {
room_id.to_string()
} else {
room_id
};

let headers = request.headers().clone();

// If it's a GET request, we'll serve a WebUI
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn main() -> anyhow::Result<()> {
if args.daemon {
// Daemon mode ignores all the other arguments
info!("Running in daemon mode");
return daemon(&config.daemon).await;
return daemon(config.daemon, config.rooms).await;
}

let headers = {
Expand Down

0 comments on commit 9e630a3

Please sign in to comment.