uncomplete recive stuff

This commit is contained in:
Bizcochito 2023-01-06 12:22:47 +01:00
parent d1f1159c20
commit 42caf9fa77
3 changed files with 82 additions and 14 deletions

1
Cargo.lock generated
View File

@ -331,6 +331,7 @@ dependencies = [
name = "hixmpp" name = "hixmpp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"futures",
"tokio", "tokio",
"tokio-xmpp", "tokio-xmpp",
"xmpp-parsers", "xmpp-parsers",

View File

@ -8,4 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
tokio = "1" tokio = "1"
tokio-xmpp = "3.2.0" tokio-xmpp = "3.2.0"
xmpp-parsers = "0.19.2" xmpp-parsers = "0.19.2"
futures = "0.3.25"

View File

@ -1,7 +1,9 @@
use std::{fs, thread}; use std::{fs, thread, env::args};
use tokio_xmpp::SimpleClient; use tokio_xmpp::{SimpleClient, AsyncClient, Event};
use xmpp_parsers::message::{Body, Message}; use xmpp_parsers::message::{Body, Message, MessageType};
use xmpp_parsers::{Element, Jid}; use xmpp_parsers::{Element, Jid, BareJid};
use futures::StreamExt;
const CREDS: &str = "creds"; const CREDS: &str = "creds";
#[tokio::main] #[tokio::main]
@ -13,17 +15,81 @@ async fn main() {
creds.next().expect("No Pass"), creds.next().expect("No Pass"),
); );
let recipients = vec!["suguivy@fai.st"]; let recipients = vec!["suguivy@fai.st"];
let argus = args().count();
xmp( if argus > 1{
jid, let mut cli = AsyncClient::new(jid, password).unwrap();
password, cli.set_reconnect(true);
format!("Hi from the {}", thread::current().name().unwrap()).as_str(), loop{
recipients, if let Some(evs) = wait_for_events(&mut cli).await{
) for (s, m) in evs{
.await; println!("{} says: {}", s, m);
}
};
//cli.send_end().await.ok();
cli.send_stanza(make_reply("suguivy@fai.st".parse::<Jid>().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<Vec<(String, String)>> {
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(); let data = data.trim();
if data.is_empty() {return;} // don't send empty stanzas if data.is_empty() {return;} // don't send empty stanzas
let mut client = SimpleClient::new(jid, password) let mut client = SimpleClient::new(jid, password)