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
|
|
|
|
2017-06-13 23:55:56 +00:00
|
|
|
use std::str::FromStr;
|
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};
|
|
|
|
use tokio_xmpp::{Client, ClientEvent};
|
2017-06-05 00:50:22 +00:00
|
|
|
use tokio_xmpp::xmpp_codec::Packet;
|
2017-06-03 23:37:46 +00:00
|
|
|
|
|
|
|
fn main() {
|
2017-06-20 19:26:51 +00:00
|
|
|
let mut core = Core::new().unwrap();
|
|
|
|
let client = Client::new("astrobot@example.org", "", &core.handle()).unwrap();
|
|
|
|
// let client = TcpClient::connect(
|
|
|
|
// jid.clone(),
|
|
|
|
// &addr,
|
|
|
|
// &core.handle()
|
|
|
|
// ).map_err(|e| format!("{}", e)
|
|
|
|
// ).and_then(|stream| {
|
|
|
|
// if stream.can_starttls() {
|
|
|
|
// stream.starttls()
|
|
|
|
// } else {
|
|
|
|
// panic!("No STARTTLS")
|
|
|
|
// }
|
|
|
|
// }).and_then(|stream| {
|
|
|
|
// let username = jid.node.as_ref().unwrap().to_owned();
|
|
|
|
// stream.auth(username, password).expect("auth")
|
|
|
|
// }).and_then(|stream| {
|
|
|
|
// stream.bind()
|
|
|
|
// }).and_then(|stream| {
|
|
|
|
// println!("Bound to {}", stream.jid);
|
2017-06-13 23:55:56 +00:00
|
|
|
|
2017-06-20 19:26:51 +00:00
|
|
|
// let presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
|
|
|
// stream.send(Packet::Stanza(presence))
|
|
|
|
// .map_err(|e| format!("{}", e))
|
|
|
|
// }).and_then(|stream| {
|
|
|
|
// let main_loop = |stream| {
|
|
|
|
// stream.into_future()
|
|
|
|
// .and_then(|(event, stream)| {
|
|
|
|
// stream.send(Packet::Stanza(unreachable!()))
|
|
|
|
// }).and_then(main_loop)
|
|
|
|
// };
|
|
|
|
// main_loop(stream)
|
|
|
|
// }).and_then(|(event, stream)| {
|
|
|
|
// let (mut sink, stream) = stream.split();
|
|
|
|
// stream.for_each(move |event| {
|
|
|
|
// match event {
|
|
|
|
// Packet::Stanza(ref message)
|
|
|
|
// if message.name == "message" => {
|
|
|
|
// let ty = message.get_attribute("type", None);
|
|
|
|
// let body = message.get_child("body", Some("jabber:client"))
|
|
|
|
// .map(|body_el| body_el.content_str());
|
|
|
|
// match ty {
|
|
|
|
// None | Some("normal") | Some("chat")
|
|
|
|
// if body.is_some() => {
|
|
|
|
// let from = message.get_attribute("from", None).unwrap();
|
|
|
|
// println!("Got message from {}: {:?}", from, body);
|
|
|
|
// let reply = make_reply(from, body.unwrap());
|
|
|
|
// sink.send(Packet::Stanza(reply))
|
|
|
|
// .and_then(|_| Ok(()))
|
|
|
|
// },
|
|
|
|
// _ => future::ok(()),
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// _ => future::ok(()),
|
|
|
|
// }
|
|
|
|
// }).map_err(|e| format!("{}", e))
|
|
|
|
// });
|
2017-06-03 23:37:46 +00:00
|
|
|
|
2017-06-20 19:26:51 +00:00
|
|
|
let done = client.for_each(|event| {
|
|
|
|
match event {
|
|
|
|
ClientEvent::Online => {
|
|
|
|
println!("Online!");
|
|
|
|
},
|
|
|
|
ClientEvent::Stanza(stanza) => {
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
println!("Event: {:?}", event);
|
|
|
|
},
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
2017-06-20 19:26:51 +00:00
|
|
|
|
|
|
|
Ok(())
|
2017-06-03 23:37:46 +00:00
|
|
|
});
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|