| 1 | use std::str::FromStr; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use futures_util::StreamExt; |
| 5 | use irc::proto::Command; |
| 6 | use serenity::prelude::*; |
| 7 | |
| 8 | use crate::relay::*; |
| 9 | |
| 10 | pub async fn irc_producer( |
| 11 | mut irc_client: irc::client::Client, |
| 12 | buffer_reference: Arc<RwLock<MessageBuffer>>, |
| 13 | notify: Arc<RelayNotify> |
| 14 | ) { |
| 15 | let mut irc_stream: irc::client::ClientStream = irc_client.stream().unwrap(); |
| 16 | |
| 17 | while let Ok(Some(message)) = irc_stream.next().await.transpose() { |
| 18 | let msg_clone = message.clone(); |
| 19 | match message.command { |
| 20 | Command::PRIVMSG(_, contents) => { |
| 21 | { |
| 22 | println!("IRC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
| 23 | let uname = msg_clone.source_nickname().unwrap(); |
| 24 | println!("username: {uname}, current nick: {}", irc_client.current_nickname()); |
| 25 | if uname.eq_ignore_ascii_case(irc_client.current_nickname()) { |
| 26 | println!("Ignoring message from myself!"); |
| 27 | return; |
| 28 | }; |
| 29 | let mut buffer = buffer_reference.write().await; |
| 30 | let mut new_message = RelayMessage::default(); |
| 31 | new_message.contents = contents; |
| 32 | new_message.direction = RelayDirection::IRC2DIS(String::from_str(msg_clone.response_target().unwrap()).unwrap()); |
| 33 | new_message.author.push_str(uname); |
| 34 | buffer.pending_relay_messages.push_back(new_message); |
| 35 | notify.notify.notify_one(); |
| 36 | } |
| 37 | println!("Added IRC message to buffer"); |
| 38 | } |
| 39 | _ => {} |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | |