xmpp-rs-mirror/tokio-xmpp/examples/echo_component.rs

100 lines
3.2 KiB
Rust
Raw Normal View History

2018-12-18 18:04:31 +00:00
use futures::{future, Sink, Stream};
2019-09-08 19:28:44 +00:00
use std::convert::TryFrom;
2017-07-22 00:59:51 +00:00
use std::env::args;
use std::process::exit;
use std::str::FromStr;
2018-09-01 19:59:02 +00:00
use tokio::runtime::current_thread::Runtime;
2017-07-22 00:59:51 +00:00
use tokio_xmpp::Component;
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};
use xmpp_parsers::{Element, Jid};
2017-07-22 00:59:51 +00:00
fn main() {
let args: Vec<String> = args().collect();
if args.len() < 3 || args.len() > 5 {
println!("Usage: {} <jid> <password> [server] [port]", args[0]);
exit(1);
}
let jid = &args[1];
let password = &args[2];
2018-12-18 18:04:31 +00:00
let server = &args
.get(3)
.unwrap()
.parse()
.unwrap_or("127.0.0.1".to_owned());
2017-07-22 00:59:51 +00:00
let port: u16 = args.get(4).unwrap().parse().unwrap_or(5347u16);
// tokio_core context
2018-09-01 19:59:02 +00:00
let mut rt = Runtime::new().unwrap();
2017-07-22 00:59:51 +00:00
// Component instance
2018-09-01 19:59:02 +00:00
println!("{} {} {} {}", jid, password, server, port);
let component = Component::new(jid, password, server, port).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.
2019-09-08 19:28:44 +00:00
println!("Got it: {}", component.jid.clone());
let (mut sink, stream) = component.split();
2017-07-22 00:59:51 +00:00
// Wrap sink in Option so that we can take() it for the send(self)
// to consume and return it back when ready.
let mut send = move |stanza| {
sink.start_send(stanza).expect("start_send");
2017-07-22 00:59:51 +00:00
};
// Main loop, processes events
let done = stream.for_each(|event| {
if event.is_online() {
println!("Online!");
2017-07-23 00:47:07 +00:00
// TODO: replace these hardcoded JIDs
2018-12-18 18:04:31 +00:00
let presence = make_presence(
Jid::from_str("test@component.linkmauve.fr/coucou").unwrap(),
Jid::from_str("linkmauve@linkmauve.fr").unwrap(),
);
2017-07-22 00:59:51 +00:00
send(presence);
2018-12-18 18:04:31 +00:00
} else if let Some(message) = event
.into_stanza()
2017-07-22 00:59:51 +00:00
.and_then(|stanza| Message::try_from(stanza).ok())
{
// This is a message we'll echo
match (message.from, message.bodies.get("")) {
2018-12-18 18:04:31 +00:00
(Some(from), Some(body)) => {
2017-07-22 00:59:51 +00:00
if message.type_ != MessageType::Error {
2017-08-24 17:54:33 +00:00
let reply = make_reply(from, &body.0);
2017-07-22 00:59:51 +00:00
send(reply);
2018-12-18 18:04:31 +00:00
}
}
2017-07-22 00:59:51 +00:00
_ => (),
}
}
Box::new(future::ok(()))
});
// Start polling `done`
2018-09-01 19:59:02 +00:00
match rt.block_on(done) {
2017-07-22 00:59:51 +00:00
Ok(_) => (),
Err(e) => {
println!("Fatal: {}", e);
()
}
}
}
// 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()
}