2017-06-03 23:37:46 +00:00
|
|
|
extern crate futures;
|
|
|
|
extern crate tokio_core;
|
|
|
|
extern crate tokio_xmpp;
|
2017-06-13 23:55:56 +00:00
|
|
|
extern crate jid;
|
2017-06-19 00:35:21 +00:00
|
|
|
extern crate xml;
|
2017-06-03 23:37:46 +00:00
|
|
|
|
|
|
|
use tokio_core::reactor::Core;
|
2017-06-20 19:26:51 +00:00
|
|
|
use futures::{Future, Stream, Sink, future};
|
2017-07-12 23:47:05 +00:00
|
|
|
use tokio_xmpp::Client;
|
2017-06-03 23:37:46 +00:00
|
|
|
|
|
|
|
fn main() {
|
2017-07-13 20:04:54 +00:00
|
|
|
// Tokio_core context
|
2017-06-20 19:26:51 +00:00
|
|
|
let mut core = Core::new().unwrap();
|
2017-07-13 20:04:54 +00:00
|
|
|
// Client instance
|
2017-06-20 19:26:51 +00:00
|
|
|
let client = Client::new("astrobot@example.org", "", &core.handle()).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.
|
2017-07-01 23:25:22 +00:00
|
|
|
let (sink, stream) = client.split();
|
2017-07-13 20:04:54 +00:00
|
|
|
// Wrap sink in Option so that we can take() it for the send(self)
|
|
|
|
// to consume and return it back when ready.
|
2017-07-01 23:25:22 +00:00
|
|
|
let mut sink = Some(sink);
|
2017-07-12 23:40:14 +00:00
|
|
|
let mut send = move |stanza| {
|
|
|
|
sink = Some(
|
|
|
|
sink.take().
|
|
|
|
expect("sink")
|
|
|
|
.send(stanza)
|
|
|
|
.wait()
|
|
|
|
.expect("sink.send")
|
|
|
|
);
|
|
|
|
};
|
2017-07-13 20:04:54 +00:00
|
|
|
// Main loop, processes events
|
2017-07-12 23:40:14 +00:00
|
|
|
let done = stream.for_each(|event| {
|
2017-07-13 20:04:54 +00:00
|
|
|
if event.is_online() {
|
|
|
|
println!("Online!");
|
2017-07-01 23:25:22 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
let presence = make_presence();
|
|
|
|
send(presence);
|
|
|
|
} else if let Some(stanza) = event.as_stanza() {
|
|
|
|
if stanza.name == "message" &&
|
|
|
|
stanza.get_attribute("type", None) != Some("error") {
|
|
|
|
// This is a message we'll echo
|
|
|
|
let from = stanza.get_attribute("from", None);
|
|
|
|
let body = stanza.get_child("body", Some("jabber:client"))
|
|
|
|
.map(|el| el.content_str());
|
2017-07-01 23:25:22 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
match (from.as_ref(), body) {
|
|
|
|
(Some(from), Some(body)) => {
|
|
|
|
let reply = make_reply(from, body);
|
|
|
|
send(reply);
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(future::ok(()))
|
2017-06-03 23:37:46 +00:00
|
|
|
});
|
2017-07-13 20:04:54 +00:00
|
|
|
|
|
|
|
// Start polling `done`
|
2017-06-20 19:26:51 +00:00
|
|
|
match core.run(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-01 23:25:22 +00:00
|
|
|
fn make_presence() -> xml::Element {
|
|
|
|
let mut presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
|
|
|
presence.tag(xml::Element::new("status".to_owned(), None, vec![]))
|
|
|
|
.text("chat".to_owned());
|
|
|
|
presence.tag(xml::Element::new("show".to_owned(), None, vec![]))
|
|
|
|
.text("Echoing messages".to_owned());
|
|
|
|
presence
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Construct a chat <message/>
|
2017-06-20 19:26:51 +00:00
|
|
|
fn make_reply(to: &str, body: String) -> xml::Element {
|
|
|
|
let mut message = xml::Element::new(
|
|
|
|
"message".to_owned(),
|
|
|
|
None,
|
|
|
|
vec![("type".to_owned(), None, "chat".to_owned()),
|
|
|
|
("to".to_owned(), None, to.to_owned())]
|
|
|
|
);
|
|
|
|
message.tag(xml::Element::new("body".to_owned(), None, vec![]))
|
|
|
|
.text(body);
|
|
|
|
message
|
|
|
|
}
|