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