echo_bot.rs

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