feat: use config file

This commit is contained in:
sinavir 2024-05-18 06:49:06 +02:00
parent fc6abbdae3
commit 7bfe133e05
2 changed files with 34 additions and 31 deletions

View file

@ -14,28 +14,16 @@ use {log, pretty_env_logger};
use crate::jsonrpc::RpcClient; use crate::jsonrpc::RpcClient;
const CHANNEL: &str = "#hackens-signal";
const MEDIA_DIR: &str = "./www";
const SIGNALCLI_DIR: &str = "./.config";
const SOCKET: &str = "./socket";
const URL_ROOT: &str = "https://hackens.org/irc-bridge/";
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), String> { async fn main() -> Result<(), String> {
pretty_env_logger::init(); pretty_env_logger::init();
log::info!("Starting..."); log::info!("Starting...");
// TODO: load config at runtime and add consts inside let config = Config::load("./config.toml").map_err(|e| format!("Error while loading config: {e}"))?;
let config = Config { check_config(&config);
nickname: Some("bottest".to_owned()),
server: Some("ulminfo.fr".to_owned()),
channels: vec![CHANNEL.to_owned()],
use_tls: Some(true),
..Config::default()
};
// Initialisation of components. TODO separate module // Initialisation of components. TODO separate module
let init_irc = async { let init_irc = async {
let mut client = Client::from_config(config).await.unwrap(); let mut client = Client::from_config(config.clone()).await.unwrap();
client.identify().unwrap(); client.identify().unwrap();
let sender = client.sender(); let sender = client.sender();
@ -55,7 +43,7 @@ async fn main() -> Result<(), String> {
let init_signal = async { let init_signal = async {
log::debug!("Initializing Signal"); log::debug!("Initializing Signal");
let signal_client = jsonrpc::connect_unix(SOCKET) let signal_client = jsonrpc::connect_unix(config.options.get("socket").unwrap())
.await .await
.map_err(|e| format!("failed to connect to signal socket: {e}"))?; .map_err(|e| format!("failed to connect to signal socket: {e}"))?;
let signal_stream = signal_client.subscribe_receive(None).await.unwrap(); let signal_stream = signal_client.subscribe_receive(None).await.unwrap();
@ -65,10 +53,11 @@ async fn main() -> Result<(), String> {
let ((sender, mut stream), (mut signal_stream, signal_client)) = let ((sender, mut stream), (mut signal_stream, signal_client)) =
try_join(init_irc, init_signal).await?; try_join(init_irc, init_signal).await?;
let bridge = Bridge::new( let bridge = Bridge::new(
&Path::new(MEDIA_DIR), &Path::new(config.options.get("media_dir").unwrap()),
&Path::new(SIGNALCLI_DIR), &Path::new(config.options.get("signal_cli_dir").unwrap()),
URL_ROOT, &config.options.get("url_root").unwrap(),
GROUPID, &config.options.get("groupid").unwrap(),
&config.channels[0],
); );
// Run !! // Run !!
@ -94,13 +83,13 @@ async fn handle_irc(
.transpose() .transpose()
.map_err(|e| format!("error while retrieving messages from irc: {e}"))? .map_err(|e| format!("error while retrieving messages from irc: {e}"))?
{ {
if let Some(msg) = bridge.irc2signal(message) { if let Some((groupid, msg)) = bridge.irc2signal(message) {
log::trace!("[SIGNAL SEND] {msg}"); log::trace!("[SIGNAL SEND] {msg}");
signal_client signal_client
.send( .send(
None, None,
vec![], vec![],
vec![String::from(GROUPID)], vec![String::from(groupid)],
false, false,
false, false,
msg, msg,
@ -141,9 +130,9 @@ async fn handle_signal(
} }
Some(Ok(val)) => { Some(Ok(val)) => {
log::trace!("[SIGNAL RCV] {:?}", val); log::trace!("[SIGNAL RCV] {:?}", val);
if let Some(msg) = bridge.signal2irc(val).await { if let Some((channel, msg)) = bridge.signal2irc(val).await {
sender sender
.send_privmsg(CHANNEL, msg) .send_privmsg(channel, msg)
.map_err(|e| format!("error while sending to irc: {e}"))?; .map_err(|e| format!("error while sending to irc: {e}"))?;
} }
} }
@ -155,3 +144,14 @@ async fn handle_signal(
} }
} }
} }
fn check_config(config: &Config) -> bool {
let keys = vec![
"groupid",
"media_dir",
"signal_cli_dir",
"socket",
"url_root",
];
keys.into_iter().fold(config.channels.len() >= 1, |b, k| b && config.options.contains_key(k))
}

View file

@ -54,6 +54,7 @@ pub struct Bridge<'a> {
signalcli_dir: &'a Path, signalcli_dir: &'a Path,
url_root: &'a str, url_root: &'a str,
groupid: &'a str, groupid: &'a str,
irc_channel: &'a str,
} }
impl<'a> Bridge<'a> { impl<'a> Bridge<'a> {
@ -62,12 +63,14 @@ impl<'a> Bridge<'a> {
signalcli_dir: &'a Path, signalcli_dir: &'a Path,
url_root: &'a str, url_root: &'a str,
groupid: &'a str, groupid: &'a str,
irc_channel: &'a str,
) -> Self { ) -> Self {
Self { Self {
media_dir, media_dir,
signalcli_dir, signalcli_dir,
url_root, url_root,
groupid, groupid,
irc_channel,
} }
} }
@ -120,7 +123,7 @@ impl<'a> Bridge<'a> {
} }
} }
pub async fn signal2irc(self: &Self, signal: Value) -> Option<String> { pub async fn signal2irc(self: &Self, signal: Value) -> Option<(&str, String)> {
let mut result: SignalMessageBuilder = SignalMessageBuilder::default(); let mut result: SignalMessageBuilder = SignalMessageBuilder::default();
if let Value::Object(map) = signal { if let Value::Object(map) = signal {
if let Some(Value::Object(envelope)) = map.get("envelope") { if let Some(Value::Object(envelope)) = map.get("envelope") {
@ -137,18 +140,18 @@ impl<'a> Bridge<'a> {
} }
} }
} }
result.build() result.build().map(|e| (self.irc_channel, e))
} }
pub fn irc2signal(self: &Self, message: Message) -> Option<String> { pub fn irc2signal(self: &Self, message: Message) -> Option<(&str, String)> {
match (message.prefix, message.command) { match (message.prefix, message.command) {
(Some(Prefix::Nickname(from, _, _)), Command::PRIVMSG(_, message)) => { (Some(Prefix::Nickname(from, _, _)), Command::PRIVMSG(_, message)) => {
return Some(format!("<{from}>: {message}")) Some(format!("<{from}>: {message}"))
} }
(Some(Prefix::Nickname(from, _, _)), Command::NOTICE(_, message)) => { (Some(Prefix::Nickname(from, _, _)), Command::NOTICE(_, message)) => {
return Some(format!("📜 {from} {message}")) Some(format!("📜 {from} {message}"))
}
_ => return None,
} }
_ => None,
}.map(|e| (self.groupid, e))
} }
} }