Compare commits

...

2 Commits

Author SHA1 Message Date
Bizcochito 42caf9fa77 uncomplete recive stuff 2023-01-06 12:22:47 +01:00
Bizcochito d1f1159c20 modified some stuff 2023-01-06 10:44:43 +01:00
3 changed files with 100 additions and 24 deletions

1
Cargo.lock generated
View File

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

View File

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

View File

@ -1,40 +1,114 @@
use std::fs;
use tokio_xmpp::SimpleClient;
use std::{fs, thread, env::args};
use tokio_xmpp::{SimpleClient, AsyncClient, Event};
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";
#[tokio::main]
async fn main() {
let creds = fs::read_to_string(CREDS)
.expect("Should have been able to read the file");
let creds = fs::read_to_string(CREDS).expect("Should have been able to read the file");
let mut creds = creds.lines();
let (jid, password) = (creds.next().expect("No JID"), creds.next().expect("No Pass"));
let recipients = vec!["suguivy@fai.st", "bizcochito@fai.st", "bizcochito@cronut.cafe"];
xmp(jid, password, "Hi from the main", recipients).await;
let (jid, password) = (
creds.next().expect("No JID"),
creds.next().expect("No Pass"),
);
let recipients = vec!["suguivy@fai.st"];
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::<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();
if data.is_empty() {/* don't send empty stanzas*/ return;}
let mut client = SimpleClient::new(jid, password).await.expect("could not connect to xmpp server");
for recipient in recipients{
let recipient = recipient.parse::<Jid>().unwrap();
let reply = make_reply(recipient.clone(), &data, false);
client.send_stanza(reply).await.expect("sending message failed");
}
if data.is_empty() {return;} // don't send empty stanzas
let mut client = SimpleClient::new(jid, password)
.await
.expect("could not connect to xmpp server");
for recipient in recipients {
let recipient = recipient.parse::<Jid>().unwrap();
let reply = make_reply(recipient.clone(), &data);
client
.send_stanza(reply)
.await
.expect("sending message failed");
}
// Close SimpleClient connection
client.end().await.ok(); // ignore errors here, I guess
}
fn make_reply(to: Jid, body: &str, groupchat: bool) -> Element {
fn make_reply(to: Jid, body: &str) -> Element {
let mut message = Message::new(Some(to));
if groupchat {
message.type_ = MessageType::Groupchat;
}
message.bodies.insert(String::new(), Body(body.to_owned()));
message.into()
}