Thumbnail

steew/belltoll.git

Clone URL: https://git.buni.party/steew/belltoll.git

commit 85c6ff1a8e6d5678135a269faff7d913b3895ab8 Author: Daniel Pérez <steew@psi.my.domain> Date: Wed Jan 07 19:56:02 2026 +0000 Working associations file diff --git a/src/main.rs b/src/main.rs index e559a45..8509a0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -569 +566 @@ async fn main() { });   // TODO: remove and read from file - let webhook = Webhook::from_url(&shared_http, - "").await.unwrap(); - // ====================================================================================== // IRC initialization let config = Config { @@ -8913 +8628 @@ async fn main() { let irc_chan = String::from_str(i_channel).expect("Channel name is not valid! {i_channel}"); let mut ircs = Vec::new(); ircs.push(irc_chan); - assoc.bridge_assoc.insert(ChannelId::new(chid), ircs); + assoc.bridge_assoc.insert(ChannelId::new(chid), ircs.clone()); } }, None => { panic!("Expected an [assoc] section with bridge associations.") }, } + + // find the webhook urls now + match ini.section(Some("webhook")) { + Some(section_contents) => { + for (chid_str, webhook_url) in section_contents.iter() { + let chid = u64::from_str(chid_str).expect("Channel ID is not valid! {chid_str}"); + let webhook_url = String::from_str(webhook_url).expect("Webhook URL is not valid! {webhook_url}"); + assoc.chid_webhook_assoc.insert(ChannelId::new(chid), webhook_url); + } + }, + None => { panic!("Expected a [webhook] section with webhook associations.") }, + } + println!("{:?}", assoc.bridge_assoc.keys()); println!("{:?}", assoc.bridge_assoc.values()); + println!("Webhooks:"); + println!("{:?}", assoc.chid_webhook_assoc.keys());   // ====================================================================================== // Relay consumer thread spawn @@ -10410 +11611 @@ async fn main() { buffer_reference, notify, shared_http, - webhook, irc_sender, assoc ) .await; }); + + loop {}  } diff --git a/src/relay.rs b/src/relay.rs index b6ebad5..ab7048c 100644 --- a/src/relay.rs +++ b/src/relay.rs @@ -207 +207 @@ pub enum RelayDirection {  pub struct RelayMessage { pub contents: String, pub direction: RelayDirection, - pub author: String, + pub author: String  }    pub struct MessageBuffer { @@ -466 +4628 @@ pub struct RelayAssoc { pub chid_webhook_assoc: HashMap<ChannelId, String>,  }   +impl RelayAssoc { + pub fn find_target(&self, source: RelayDirection) -> RelayDirection { + match source { + RelayDirection::IRC2DIS(chan) => { + for (chid, chan_vec) in self.bridge_assoc.iter() { + for c in chan_vec.iter() { + if c.eq_ignore_ascii_case(&chan) { return RelayDirection::DIS2IRC(*chid) }; + } + } + return RelayDirection::INVALID; + }, + RelayDirection::DIS2IRC(chan) => { + if let Some(target) = self.bridge_assoc.get(&chan) { + let irc_target = target.first().unwrap(); + return RelayDirection::IRC2DIS(irc_target.clone()); + } else { return RelayDirection::INVALID } + }, + _ => { panic!("Invalid source of message! {source:?}") } + } + } +} +  impl Default for RelayAssoc { fn default() -> Self { RelayAssoc { @@ -867 +1086 @@ pub async fn relay_consumer( buffer: Arc<RwLock<MessageBuffer>>, notify: Arc<RelayNotify>, http: Arc<Http>, - webhook: Webhook, sender: Sender, assoc: RelayAssoc  ) { @@ -10619 +12736 @@ pub async fn relay_consumer( RelayDirection::IRC2DIS(chan) => { // let chanid = ChannelId::new(591954698664149044); // chanid.say(http.clone(), pending.contents).await.unwrap(); - let builder = ExecuteWebhook::new().content(pending.contents).username(pending.author); - webhook.execute(&http, false, builder).await.expect("Could not execute webhook."); + let target = assoc.find_target(RelayDirection::IRC2DIS(chan)); + match target { + RelayDirection::DIS2IRC(t) => { + let webhook = Webhook::from_url(&http, assoc.chid_webhook_assoc.get(&t).expect("Expected a webhook url for channel {t.get()}")).await.unwrap(); + let builder = ExecuteWebhook::new().content(pending.contents).username(pending.author); + webhook.execute(&http, false, builder).await.expect("Could not execute webhook."); + }, + _ => { panic!("Found no target to send the message to!") } + } } RelayDirection::DIS2IRC(chan) => { let unpingable_name = pending.author.clone(); let (first, rest) = unpingable_name.split_at(1); let mut unpingable_name = String::new(); + unpingable_name.push(char::from_u32(0x03).unwrap()); + unpingable_name.push_str("04"); unpingable_name.push_str(first); unpingable_name.push_str("​"); unpingable_name.push_str(rest); + unpingable_name.push(char::from_u32(0x03).unwrap()); - let response = format!("<{}>: {}", unpingable_name, pending.contents); - sender.send_privmsg("##steew", response).unwrap(); + let target = assoc.find_target(RelayDirection::DIS2IRC(chan)); + + match target { + RelayDirection::IRC2DIS(t) => { + let response = format!("<{}>: {}", unpingable_name, pending.contents); + sender.send_privmsg(t, response).unwrap(); + }, + _ => { panic!("Found no target to send the message to!") } + } } _ => {} }