1extern crate futures;
2extern crate tokio_core;
3extern crate tokio_xmpp;
4extern crate jid;
5
6use std::str::FromStr;
7use tokio_core::reactor::Core;
8use futures::{Future, Stream};
9use tokio_xmpp::TcpClient;
10use tokio_xmpp::xmpp_codec::Packet;
11use jid::Jid;
12
13fn main() {
14 let jid = Jid::from_str("astrobot@example.net").expect("JID");
15 let password = "".to_owned();
16
17 use std::net::ToSocketAddrs;
18 let addr = "[2a01:4f8:a0:33d0::5]:5222"
19 .to_socket_addrs().unwrap()
20 .next().unwrap();
21
22 let mut core = Core::new().unwrap();
23 let client = TcpClient::connect(
24 jid.clone(),
25 &addr,
26 &core.handle()
27 ).map_err(|e| format!("{}", e)
28 ).and_then(|stream| {
29 if stream.can_starttls() {
30 stream.starttls()
31 } else {
32 panic!("No STARTTLS")
33 }
34 }).and_then(|stream| {
35 let username = jid.node.as_ref().unwrap().to_owned();
36 stream.auth(username, password).expect("auth")
37 }).and_then(|stream| {
38 stream.for_each(|event| {
39 match event {
40 Packet::Stanza(el) => println!("<< {}", el),
41 _ => println!("!! {:?}", event),
42 }
43 Ok(())
44 }).map_err(|e| format!("{}", e))
45 });
46 match core.run(client) {
47 Ok(_) => (),
48 Err(e) => {
49 println!("Fatal: {}", e);
50 ()
51 }
52 }
53}