From 9543c0985b3cd4a502bd1df22eae64a450f60f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Thu, 22 Aug 2024 00:59:36 +0200 Subject: [PATCH] Update foo example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- xmpp/examples/foo.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/xmpp/examples/foo.rs b/xmpp/examples/foo.rs index 74d01aac..349b8dc4 100644 --- a/xmpp/examples/foo.rs +++ b/xmpp/examples/foo.rs @@ -1,17 +1,18 @@ -// Copyright (c) 2019 Emmanuel Gil Peyrot +// Copyright (c) 2024 The crate authors // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use env_logger; +use log::debug; use std::env::args; use std::str::FromStr; -use tokio_xmpp::parsers::{message::MessageType, BareJid, Jid}; -use tokio_xmpp::Element; +use tokio_xmpp::{minidom::Element, parsers::message::Message}; use xmpp::{ - stream::{IqRequest, IqRequestType}, - ClientBuilder, ClientFeature, ClientType, Event, + jid::{BareJid, Jid}, + stream::{IqRequest, IqRequestType, NonTransactional}, + ClientBuilder, ClientFeature, ClientType, }; #[tokio::main] @@ -28,13 +29,10 @@ async fn main() -> Result<(), Option> { let password = &args[2]; // Client instance - let mut client = ClientBuilder::new(jid, password) + let client = ClientBuilder::new(jid, password) .set_client(ClientType::Bot, "xmpp-rs") .set_website("https://gitlab.com/xmpp-rs/xmpp-rs") .set_default_nick("bot") - .enable_feature(ClientFeature::Avatars) - .enable_feature(ClientFeature::ContactList) - .enable_feature(ClientFeature::JoinRooms) .build()?; println!("FOO0: {:?}", client); @@ -44,11 +42,28 @@ async fn main() -> Result<(), Option> { to: Jid::new("bouah.net").unwrap(), data: IqRequestType::Get(elem), }; - let resp = client.send_iq(req).await?; + let resp = client.send_iq(req).await.unwrap(); println!("Response: {:?}", resp); - let _ = client.disconnect().await?; + let receiver = client.misc_receiver().await; - // tokio::time::sleep(tokio::time::Duration::from_millis(10000)).await; + loop { + match receiver.lock().await.recv().await { + Some(NonTransactional::Presence(presence)) => debug!("Presence: {:?}", presence), + Some(NonTransactional::Message(message)) => { + debug!("Message: {:?}", message); + let (lang, body) = + if let Some((lang, body)) = message.get_best_body(vec!["en", "fr"]) { + (lang.to_string(), format!("Echo: {}", body.0)) + } else { + (format!("en"), format!("Echo?!")) + }; + let echo_jid = message.from.unwrap(); + let _echo = Message::groupchat(echo_jid).with_body(lang, body); + // client.send_message(echo).await? + } + None => break, + } + } Ok(()) }