From 130d6bebfc820bcc69c9871748e759cf7a50dc15 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Mon, 3 Jun 2024 17:37:51 +0200 Subject: [PATCH] Start receive message --- src/main.rs | 14 +++++++++++++- src/xmpp_client.rs | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3a4f258..e5f5f14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ fn main() { let tabs_store = gio::ListStore::new::(); - let xmpp_receiver = xmpp_client::client(&username, &password); + let (xmpp_receiver, cmd_sender) = xmpp_client::client(&username, &password); let tabs_store_copy = tabs_store.clone(); glib::spawn_future_local(async move { while let Ok(event) = xmpp_receiver.recv().await { @@ -155,10 +155,22 @@ fn main() { }); let win2 = win.clone(); + let cmd_sender2 = cmd_sender.clone(); win.entry().connect_activate(move |entry| { let text = entry.text(); entry.set_text(""); println!("Send: {text}"); + let current_tab: Tab = win2 + .tabs_selection() + .selected_item() + .and_downcast() + .unwrap(); + cmd_sender2 + .send_blocking(xmpp_client::XMPPCommand::SendPM( + xmpp::BareJid::new(¤t_tab.jid()).unwrap(), + text.as_str().to_string(), + )) + .unwrap(); let message = Message::now(&get_own_nick(), &text); let selection = win2.selection(); let store = selection diff --git a/src/xmpp_client.rs b/src/xmpp_client.rs index e196d54..7fe350c 100644 --- a/src/xmpp_client.rs +++ b/src/xmpp_client.rs @@ -1,16 +1,22 @@ -use async_channel::Receiver; +use async_channel::{Receiver, Sender}; use tokio::runtime::Runtime; use xmpp::{BareJid, ClientBuilder, ClientFeature, Event}; use std::sync::OnceLock; +#[derive(Clone, Debug)] +pub enum XMPPCommand { + SendPM(BareJid, String), +} + fn tokio_runtime() -> &'static Runtime { static RUNTIME: OnceLock = OnceLock::new(); RUNTIME.get_or_init(|| Runtime::new().expect("Setting up tokio runtime needs to succeed.")) } -pub(crate) fn client(jid: &str, password: &str) -> Receiver { +pub(crate) fn client(jid: &str, password: &str) -> (Receiver, Sender) { let (event_sender, event_receiver) = async_channel::bounded::(1); + let (cmd_sender, cmd_receiver) = async_channel::bounded::(1); let jid = jid.to_string(); let password = password.to_string(); @@ -21,22 +27,34 @@ pub(crate) fn client(jid: &str, password: &str) -> Receiver { .enable_feature(ClientFeature::ContactList) .build(); - while let Some(events) = client.wait_for_events().await { - for event in events { - match event { - Event::ContactAdded(_) => { - event_sender.send(event).await.expect("BOOOOOOHOOOO"); + loop { + tokio::select! { + Some(events) = client.wait_for_events() => { + for event in events { + match event { + Event::ContactAdded(_) => { + event_sender.send(event).await.expect("BOOOOOOHOOOO"); + } + Event::ChatMessage(_, _, _, _) => { + event_sender.send(event).await.expect("BOOOHOOOO"); + } + _ => { + continue; + } + } } - Event::ChatMessage(_, _, _, _) => { - event_sender.send(event).await.expect("BOOOHOOOO"); - } - _ => { - continue; + } + + command = cmd_receiver.recv() => { + match command.unwrap() { + XMPPCommand::SendPM(jid, content) => { + client.send_message(jid.into(), xmpp::parsers::message::MessageType::Chat, "en", &content).await; + }, } } } } }); - event_receiver + (event_receiver, cmd_sender) }