2019-01-26 22:59:06 +00:00
|
|
|
use futures::{future, Future, Sink, Stream};
|
2017-07-15 20:35:50 +00:00
|
|
|
use std::env::args;
|
|
|
|
use std::process::exit;
|
2018-09-01 19:59:02 +00:00
|
|
|
use tokio::runtime::current_thread::Runtime;
|
2019-01-29 00:52:07 +00:00
|
|
|
use tokio_xmpp::{Client, Packet};
|
2019-01-16 13:35:38 +00:00
|
|
|
use xmpp_parsers::{Jid, Element, TryFrom};
|
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-06-03 23:37:46 +00:00
|
|
|
|
|
|
|
fn main() {
|
2017-07-15 20:35:50 +00:00
|
|
|
let args: Vec<String> = args().collect();
|
|
|
|
if args.len() != 3 {
|
|
|
|
println!("Usage: {} <jid> <password>", args[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let jid = &args[1];
|
|
|
|
let password = &args[2];
|
|
|
|
|
|
|
|
// tokio_core context
|
2018-09-01 19:59:02 +00:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2017-07-13 20:04:54 +00:00
|
|
|
// Client instance
|
2018-09-01 19:59:02 +00:00
|
|
|
let client = Client::new(jid, password).unwrap();
|
2017-06-13 23:55:56 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Make the two interfaces for sending and receiving independent
|
|
|
|
// of each other so we can move one into a closure.
|
2019-01-26 22:59:06 +00:00
|
|
|
let (sink, stream) = client.split();
|
2019-01-29 22:04:42 +00:00
|
|
|
|
|
|
|
// Create outgoing pipe
|
|
|
|
let (mut tx, rx) = futures::unsync::mpsc::unbounded();
|
|
|
|
rt.spawn(
|
|
|
|
rx.forward(
|
|
|
|
sink.sink_map_err(|_| panic!("Pipe"))
|
|
|
|
)
|
|
|
|
.map(|(rx, mut sink)| {
|
|
|
|
drop(rx);
|
|
|
|
let _ = sink.close();
|
|
|
|
})
|
|
|
|
.map_err(|e| {
|
|
|
|
panic!("Send error: {:?}", e);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Main loop, processes events
|
2019-01-29 22:04:42 +00:00
|
|
|
let mut wait_for_stream_end = false;
|
2019-01-26 22:59:06 +00:00
|
|
|
let done = stream.for_each(move |event| {
|
2019-01-29 22:04:42 +00:00
|
|
|
if wait_for_stream_end {
|
|
|
|
/* Do nothing */
|
|
|
|
} else if event.is_online() {
|
2017-07-13 20:04:54 +00:00
|
|
|
println!("Online!");
|
2017-07-01 23:25:22 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
let presence = make_presence();
|
2019-01-29 22:04:42 +00:00
|
|
|
tx.start_send(Packet::Stanza(presence)).unwrap();
|
2018-12-18 18:04:31 +00:00
|
|
|
} else if let Some(message) = event
|
|
|
|
.into_stanza()
|
2017-07-20 22:19:08 +00:00
|
|
|
.and_then(|stanza| Message::try_from(stanza).ok())
|
|
|
|
{
|
|
|
|
match (message.from, message.bodies.get("")) {
|
2019-01-26 22:59:06 +00:00
|
|
|
(Some(ref from), Some(ref body)) if body.0 == "die" => {
|
|
|
|
println!("Secret die command triggered by {}", from);
|
2019-01-29 22:04:42 +00:00
|
|
|
wait_for_stream_end = true;
|
|
|
|
tx.start_send(Packet::StreamEnd).unwrap();
|
2019-01-26 22:59:06 +00:00
|
|
|
}
|
|
|
|
(Some(ref from), Some(ref body)) => {
|
2017-07-20 22:19:08 +00:00
|
|
|
if message.type_ != MessageType::Error {
|
2019-01-26 22:59:06 +00:00
|
|
|
// This is a message we'll echo
|
|
|
|
let reply = make_reply(from.clone(), &body.0);
|
2019-01-29 22:04:42 +00:00
|
|
|
tx.start_send(Packet::Stanza(reply)).unwrap();
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 22:59:06 +00:00
|
|
|
_ => {}
|
2017-07-19 23:07:07 +00:00
|
|
|
}
|
2019-01-29 22:04:42 +00:00
|
|
|
}
|
2017-07-13 20:04:54 +00:00
|
|
|
|
2019-01-29 22:04:42 +00:00
|
|
|
future::ok(())
|
2017-06-03 23:37:46 +00:00
|
|
|
});
|
2017-07-13 20:04:54 +00:00
|
|
|
|
|
|
|
// Start polling `done`
|
2018-09-01 19:59:02 +00:00
|
|
|
match rt.block_on(done) {
|
2017-06-04 22:42:35 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
println!("Fatal: {}", e);
|
|
|
|
()
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 23:37:46 +00:00
|
|
|
}
|
2017-06-20 19:26:51 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Construct a <presence/>
|
2017-07-17 18:53:00 +00:00
|
|
|
fn make_presence() -> Element {
|
2017-07-18 19:41:57 +00:00
|
|
|
let mut presence = Presence::new(PresenceType::None);
|
|
|
|
presence.show = PresenceShow::Chat;
|
2018-12-18 18:04:31 +00:00
|
|
|
presence
|
|
|
|
.statuses
|
|
|
|
.insert(String::from("en"), String::from("Echoing messages."));
|
2017-07-18 23:00:17 +00:00
|
|
|
presence.into()
|
2017-07-01 23:25:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Construct a chat <message/>
|
2017-07-19 23:07:07 +00:00
|
|
|
fn make_reply(to: Jid, body: &str) -> Element {
|
|
|
|
let mut message = Message::new(Some(to));
|
2017-08-14 22:27:40 +00:00
|
|
|
message.bodies.insert(String::new(), Body(body.to_owned()));
|
2017-07-18 23:00:17 +00:00
|
|
|
message.into()
|
2017-06-20 19:26:51 +00:00
|
|
|
}
|