xmpp-rs-mirror/examples/echo_bot.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

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-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;
use futures::{Future, Stream};
use tokio_xmpp::TcpClient;
use tokio_xmpp::xmpp_codec::Packet;
2017-06-13 23:55:56 +00:00
use jid::Jid;
2017-06-03 23:37:46 +00:00
fn main() {
2017-06-13 23:55:56 +00:00
let jid = Jid::from_str("astrobot@example.net").expect("JID");
let password = "".to_owned();
2017-06-03 23:37:46 +00:00
use std::net::ToSocketAddrs;
let addr = "[2a01:4f8:a0:33d0::5]:5222"
.to_socket_addrs().unwrap()
.next().unwrap();
let mut core = Core::new().unwrap();
let client = TcpClient::connect(
2017-06-13 23:55:56 +00:00
jid.clone(),
2017-06-03 23:37:46 +00:00
&addr,
&core.handle()
2017-06-06 00:03:38 +00:00
).map_err(|e| format!("{}", e)
2017-06-03 23:37:46 +00:00
).and_then(|stream| {
if stream.can_starttls() {
2017-06-06 00:03:38 +00:00
stream.starttls()
} else {
panic!("No STARTTLS")
}
2017-06-06 00:03:38 +00:00
}).and_then(|stream| {
2017-06-13 23:55:56 +00:00
let username = jid.node.as_ref().unwrap().to_owned();
stream.auth(username, password).expect("auth")
}).and_then(|stream| {
2017-06-19 00:16:47 +00:00
stream.bind()
}).and_then(|stream| {
println!("Bound to {}", stream.jid);
2017-06-03 23:37:46 +00:00
stream.for_each(|event| {
match event {
Packet::Stanza(el) => println!("<< {}", el),
_ => println!("!! {:?}", event),
}
Ok(())
2017-06-05 23:29:20 +00:00
}).map_err(|e| format!("{}", e))
2017-06-03 23:37:46 +00:00
});
2017-06-04 22:42:35 +00:00
match core.run(client) {
Ok(_) => (),
Err(e) => {
println!("Fatal: {}", e);
()
}
}
2017-06-03 23:37:46 +00:00
}