diff --git a/Cargo.lock b/Cargo.lock index 7739f0c..581f73a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,6 +331,7 @@ dependencies = [ name = "hixmpp" version = "0.1.0" dependencies = [ + "futures", "tokio", "tokio-xmpp", "xmpp-parsers", diff --git a/Cargo.toml b/Cargo.toml index 644f7ec..1abd21b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] tokio = "1" tokio-xmpp = "3.2.0" -xmpp-parsers = "0.19.2" \ No newline at end of file +xmpp-parsers = "0.19.2" +futures = "0.3.25" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4fbf432..463e986 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ -use std::{fs, thread}; -use tokio_xmpp::SimpleClient; -use xmpp_parsers::message::{Body, Message}; -use xmpp_parsers::{Element, Jid}; +use std::{fs, thread, env::args}; +use tokio_xmpp::{SimpleClient, AsyncClient, Event}; +use xmpp_parsers::message::{Body, Message, MessageType}; +use xmpp_parsers::{Element, Jid, BareJid}; +use futures::StreamExt; + const CREDS: &str = "creds"; #[tokio::main] @@ -13,17 +15,81 @@ async fn main() { creds.next().expect("No Pass"), ); let recipients = vec!["suguivy@fai.st"]; - - xmp( - jid, - password, - format!("Hi from the {}", thread::current().name().unwrap()).as_str(), - recipients, - ) - .await; + let argus = args().count(); + if argus > 1{ + let mut cli = AsyncClient::new(jid, password).unwrap(); + cli.set_reconnect(true); + loop{ + if let Some(evs) = wait_for_events(&mut cli).await{ + for (s, m) in evs{ + println!("{} says: {}", s, m); + } + }; + //cli.send_end().await.ok(); + cli.send_stanza(make_reply("suguivy@fai.st".parse::().unwrap(), "Hi from the async")).await.ok(); + } + }else{ + xmp_send( + jid, + password, + format!("Hi from the {}", thread::current().name().unwrap()).as_str(), + recipients, + ).await; + } } -async fn xmp(jid: &str, password: &str, data: &str, recipients: Vec<&str>) { +async fn handle_message(message: Message) -> Vec<(String, String)> { + let mut events = vec![]; + let from = message.from.clone().unwrap(); + match message.get_best_body(vec!["es","en"]) { + Some((_lang, body)) => match message.type_ { + MessageType::Groupchat => { + + } + MessageType::Chat | MessageType::Normal => { + let event = (from.clone().to_string(), body.clone().0); + events.push(event) + } + _ => (), + }, + None => (), + } + events +} + + + +async fn wait_for_events(client: &mut AsyncClient) -> Option> { + if let Some(event) = client.next().await { + let mut events = Vec::new(); + println!("{:?}", event); + match event { + Event::Stanza(elem) => { + if elem.is("iq", "jabber:client") { + + } else if elem.is("message", "jabber:client") { + let message = Message::try_from(elem).unwrap(); + let new_events = handle_message(message).await; + events.extend(new_events); + } else if elem.is("presence", "jabber:client") { + + } else if elem.is("error", "http://etherx.jabber.org/streams") { + println!("Received a fatal stream error: {}", String::from(&elem)); + } else { + panic!("Unknown stanza: {}", String::from(&elem)); + } + }, + _ => (), + }; + + Some(events) + } else { + None + } +} + + +async fn xmp_send(jid: &str, password: &str, data: &str, recipients: Vec<&str>) { let data = data.trim(); if data.is_empty() {return;} // don't send empty stanzas let mut client = SimpleClient::new(jid, password)