2020-03-05 00:25:24 +00:00
|
|
|
use futures::stream::StreamExt;
|
2024-07-24 18:39:27 +00:00
|
|
|
use minidom::Element;
|
2017-07-22 00:59:51 +00:00
|
|
|
use std::env::args;
|
|
|
|
use std::process::exit;
|
|
|
|
use std::str::FromStr;
|
2024-07-24 18:39:27 +00:00
|
|
|
use xmpp_parsers::jid::Jid;
|
2018-12-18 18:04:31 +00:00
|
|
|
use xmpp_parsers::message::{Body, Message, MessageType};
|
|
|
|
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
|
2017-07-22 00:59:51 +00:00
|
|
|
|
2024-08-06 18:40:24 +00:00
|
|
|
use tokio_xmpp::{connect::DnsConfig, Component};
|
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-06-05 11:06:52 +00:00
|
|
|
env_logger::init();
|
|
|
|
|
2017-07-22 00:59:51 +00:00
|
|
|
let args: Vec<String> = args().collect();
|
2024-01-01 06:13:51 +00:00
|
|
|
if args.len() < 3 || args.len() > 4 {
|
|
|
|
println!("Usage: {} <jid> <password> [server:port]", args[0]);
|
2017-07-22 00:59:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let jid = &args[1];
|
|
|
|
let password = &args[2];
|
2024-08-06 18:40:24 +00:00
|
|
|
|
|
|
|
let server = if let Some(server) = args.get(3) {
|
|
|
|
DnsConfig::addr(server)
|
|
|
|
} else {
|
|
|
|
DnsConfig::no_srv("127.0.0.1", 5347)
|
|
|
|
};
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
|
|
// Component instance
|
2024-01-01 06:13:51 +00:00
|
|
|
println!("{} {} {}", jid, password, server);
|
2024-08-06 18:40:24 +00:00
|
|
|
|
|
|
|
// If you don't need a custom server but default localhost:5347, you can use
|
|
|
|
// Component::new() directly
|
|
|
|
let mut component = Component::new_plaintext(jid, password, server)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
|
|
// Make the two interfaces for sending and receiving independent
|
|
|
|
// of each other so we can move one into a closure.
|
2020-03-05 00:25:24 +00:00
|
|
|
println!("Online: {}", component.jid);
|
|
|
|
|
|
|
|
// TODO: replace these hardcoded JIDs
|
|
|
|
let presence = make_presence(
|
|
|
|
Jid::from_str("test@component.linkmauve.fr/coucou").unwrap(),
|
|
|
|
Jid::from_str("linkmauve@linkmauve.fr").unwrap(),
|
|
|
|
);
|
|
|
|
component.send_stanza(presence).await.unwrap();
|
2017-07-22 00:59:51 +00:00
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
// Main loop, processes events
|
|
|
|
loop {
|
|
|
|
if let Some(stanza) = component.next().await {
|
|
|
|
if let Some(message) = Message::try_from(stanza).ok() {
|
|
|
|
// This is a message we'll echo
|
|
|
|
match (message.from, message.bodies.get("")) {
|
|
|
|
(Some(from), Some(body)) => {
|
|
|
|
if message.type_ != MessageType::Error {
|
|
|
|
let reply = make_reply(from, &body.0);
|
|
|
|
component.send_stanza(reply).await.unwrap();
|
|
|
|
}
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
_ => (),
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2017-07-22 00:59:51 +00:00
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2017-07-22 00:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a <presence/>
|
|
|
|
fn make_presence(from: Jid, to: Jid) -> Element {
|
|
|
|
let mut presence = Presence::new(PresenceType::None);
|
|
|
|
presence.from = Some(from);
|
|
|
|
presence.to = Some(to);
|
2019-09-08 19:28:44 +00:00
|
|
|
presence.show = Some(PresenceShow::Chat);
|
2018-12-18 18:04:31 +00:00
|
|
|
presence
|
|
|
|
.statuses
|
|
|
|
.insert(String::from("en"), String::from("Echoing messages."));
|
2017-07-22 00:59:51 +00:00
|
|
|
presence.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a chat <message/>
|
|
|
|
fn make_reply(to: Jid, body: &str) -> Element {
|
|
|
|
let mut message = Message::new(Some(to));
|
2017-08-24 17:54:33 +00:00
|
|
|
message.bodies.insert(String::new(), Body(body.to_owned()));
|
2017-07-22 00:59:51 +00:00
|
|
|
message.into()
|
|
|
|
}
|