Initial commit

This commit is contained in:
Leon Grünewald 2024-09-17 02:37:04 +02:00
commit 50ea5ee632
5 changed files with 2374 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
.idea
config.toml

2230
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

20
Cargo.toml Normal file
View file

@ -0,0 +1,20 @@
[package]
name = "momb-notifier-rs"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0"
figment = {version = "0.10", features = ["toml"]}
bytes = "1"
prost = "0.13"
tokio = { version = "1.38", features = ["full"] }
tokio-stream = "0.1"
tokio-util = { version = "0.7", features = ["codec"] }
tokio-rustls = { version = "0.26" }
webpki-roots = "0.26"
rustls-pki-types = { version = "1.7.0" , features = ["alloc", "std"]}
futures = "0.3.30"
mumble-rs = { path = "/home/dhalucario/workspace/mumble-rs" }
teloxide = { version = "0.13", features = ["macros"] }
serde = { version = "1.0", features = ["derive"] }

20
src/config.rs Normal file
View file

@ -0,0 +1,20 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct MumbleConfig {
pub host: String,
pub username: String,
pub channel_id: u32
}
#[derive(Serialize, Deserialize)]
pub struct TelegramConfig {
pub token: String,
pub chat_id: i64
}
#[derive(Serialize, Deserialize)]
pub struct Config {
pub mumble: MumbleConfig,
pub telegram: TelegramConfig
}

101
src/main.rs Normal file
View file

@ -0,0 +1,101 @@
mod config;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use figment::{Figment, providers::{Toml, Format}};
use futures::{SinkExt, StreamExt};
use mumble_rs::{MumbleClient, MumbleMessage};
use mumble_rs::proto::Ping;
use tokio::task::JoinHandle;
use teloxide::prelude::*;
use tokio::sync::Mutex;
use crate::config::Config;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config: Config = Figment::new()
.merge(Toml::file("config.toml"))
.extract()?;
let joined_users = Arc::new(Mutex::new(Vec::new()));
let telegram_bot = Arc::new(Mutex::new(Bot::new(config.telegram.token)));
let mut client = MumbleClient::new(config.mumble.host, None, config.mumble.username, None);
let (mut mumble_send, mut mumble_recv) = client.connect().await?.split();
let mut server_synced = false;
let (conn_send_channel_send, mut conn_send_channel_recv) = tokio::sync::mpsc::channel::<MumbleMessage>(10);
/*
let udp_connection = client.create_udp_connection().await?;
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
loop {
let mut _data: [u8; 1024] = [0; 1024];
let len = udp_connection.recv(&mut _data).await?;
println!("UDP Data: {len}");
}
Ok(())
});*/
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
while !conn_send_channel_send.is_closed() {
conn_send_channel_send.send(MumbleMessage::Ping {data: Ping {
good: None,
late: None,
lost: None,
resync: None,
tcp_packets: None,
tcp_ping_avg: None,
tcp_ping_var: None,
timestamp: Some(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs()),
udp_packets: None,
udp_ping_avg: None,
udp_ping_var: None
}}).await?;
tokio::time::sleep(Duration::from_secs(10)).await;
}
Ok(())
});
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
while let Some(message) = conn_send_channel_recv.recv().await {
mumble_send.send(message).await?;
}
Ok(())
});
while let Some(frame_result) = mumble_recv.next().await {
if let Ok(message) = frame_result {
match message {
MumbleMessage::UserState {data} => {
if !server_synced { continue }
if let (Some(mumble_user_id), Some(name)) = (data.user_id, data.name) {
let message;
{
let telegram_bot = telegram_bot.lock().await;
let mut joined_users = joined_users.lock().await;
message = telegram_bot.send_message(ChatId(config.telegram.chat_id), format!("A momber just joined: {name}")).await?;
joined_users.push(mumble_user_id);
}
let telegram_bot = telegram_bot.clone();
let joined_users = joined_users.clone();
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(60*60)).await;
{
let telegram_bot = telegram_bot.lock().await;
let mut joined_users = joined_users.lock().await;
joined_users.retain(|user| user != &mumble_user_id);
telegram_bot.delete_message(message.chat.id, message.id).await?;
}
Ok(())
});
}
}
MumbleMessage::ServerSync { data: _ } => {
server_synced = true;
}
_ => {}
}
}
}
Ok(())
}