echo_bot.rs

 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.bind()
39    }).and_then(|stream| {
40        println!("Bound to {}", stream.jid);
41        stream.for_each(|event| {
42            match event {
43                Packet::Stanza(el) => println!("<< {}", el),
44                _ => println!("!! {:?}", event),
45            }
46            Ok(())
47        }).map_err(|e| format!("{}", e))
48    });
49    match core.run(client) {
50        Ok(_) => (),
51        Err(e) => {
52            println!("Fatal: {}", e);
53            ()
54        }
55    }
56}