echo_bot.rs

 1extern crate futures;
 2extern crate tokio_core;
 3extern crate tokio_xmpp;
 4extern crate rustls;
 5
 6use std::sync::Arc;
 7use std::io::BufReader;
 8use std::fs::File;
 9use tokio_core::reactor::Core;
10use futures::{Future, Stream};
11use tokio_xmpp::TcpClient;
12use tokio_xmpp::xmpp_codec::Packet;
13use rustls::ClientConfig;
14
15fn main() {
16    use std::net::ToSocketAddrs;
17    let addr = "[2a01:4f8:a0:33d0::5]:5222"
18        .to_socket_addrs().unwrap()
19        .next().unwrap();
20
21    let mut config = ClientConfig::new();
22    let mut certfile = BufReader::new(File::open("/usr/share/ca-certificates/CAcert/root.crt").unwrap());
23    config.root_store.add_pem_file(&mut certfile).unwrap();
24    let arc_config = Arc::new(config);
25
26    let mut core = Core::new().unwrap();
27    let client = TcpClient::connect(
28        &addr,
29        &core.handle()
30    ).and_then(|stream| {
31        if stream.can_starttls() {
32            stream.starttls(arc_config)
33        } else {
34            panic!("No STARTTLS")
35        }
36    }).map_err(|e| format!("{}", e)
37    ).and_then(|stream| {
38        stream.auth("astrobot", "").expect("auth")
39    }).and_then(|stream| {
40        stream.for_each(|event| {
41            match event {
42                Packet::Stanza(el) => println!("<< {}", el),
43                _ => println!("!! {:?}", event),
44            }
45            Ok(())
46        }).map_err(|e| format!("{}", e))
47    });
48    match core.run(client) {
49        Ok(_) => (),
50        Err(e) => {
51            println!("Fatal: {}", e);
52            ()
53        }
54    }
55}