xmpp-rs/examples/echo_bot.rs

99 lines
2.8 KiB
Rust
Raw Normal View History

#![feature(try_from)]
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;
extern crate minidom;
extern crate xmpp_parsers;
2017-06-03 23:37:46 +00:00
use std::env::args;
use std::process::exit;
use std::convert::TryFrom;
2017-06-03 23:37:46 +00:00
use tokio_core::reactor::Core;
2017-06-20 19:26:51 +00:00
use futures::{Future, Stream, Sink, future};
2017-07-12 23:47:05 +00:00
use tokio_xmpp::Client;
use minidom::Element;
use xmpp_parsers::presence::{Presence, Type as PresenceType, Show as PresenceShow};
use xmpp_parsers::message::{Message, MessageType};
use jid::Jid;
2017-06-03 23:37:46 +00:00
fn main() {
let args: Vec<String> = args().collect();
if args.len() != 3 {
println!("Usage: {} <jid> <password>", args[0]);
exit(1);
}
let jid = &args[1];
let password = &args[2];
// tokio_core context
2017-06-20 19:26:51 +00:00
let mut core = Core::new().unwrap();
2017-07-13 20:04:54 +00:00
// Client instance
let client = Client::new(jid, password, core.handle()).unwrap();
2017-06-13 23:55:56 +00:00
2017-07-13 20:04:54 +00:00
// Make the two interfaces for sending and receiving independent
// of each other so we can move one into a closure.
let (sink, stream) = client.split();
2017-07-13 20:04:54 +00:00
// Wrap sink in Option so that we can take() it for the send(self)
// to consume and return it back when ready.
let mut sink = Some(sink);
2017-07-12 23:40:14 +00:00
let mut send = move |stanza| {
sink = Some(
sink.take().
expect("sink")
.send(stanza)
.wait()
.expect("sink.send")
);
};
2017-07-13 20:04:54 +00:00
// Main loop, processes events
2017-07-12 23:40:14 +00:00
let done = stream.for_each(|event| {
2017-07-13 20:04:54 +00:00
if event.is_online() {
println!("Online!");
2017-07-13 20:04:54 +00:00
let presence = make_presence();
send(presence);
} else if let Some(stanza) = event.into_stanza() {
if let Ok(message) = Message::try_from(stanza) {
if message.type_ != MessageType::Error {
2017-07-13 20:04:54 +00:00
// This is a message we'll echo
match (message.from, message.bodies.get("")) {
2017-07-13 20:04:54 +00:00
(Some(from), Some(body)) => {
let reply = make_reply(from, body);
send(reply);
},
_ => (),
};
}
}
2017-07-13 20:04:54 +00:00
}
Box::new(future::ok(()))
2017-06-03 23:37:46 +00:00
});
2017-07-13 20:04:54 +00:00
// Start polling `done`
2017-06-20 19:26:51 +00:00
match core.run(done) {
2017-06-04 22:42:35 +00:00
Ok(_) => (),
Err(e) => {
println!("Fatal: {}", e);
()
}
}
2017-06-03 23:37:46 +00:00
}
2017-06-20 19:26:51 +00:00
2017-07-13 20:04:54 +00:00
// Construct a <presence/>
fn make_presence() -> Element {
let mut presence = Presence::new(PresenceType::None);
presence.show = PresenceShow::Chat;
presence.statuses.insert(String::from("en"), String::from("Echoing messages."));
presence.into()
}
2017-07-13 20:04:54 +00:00
// Construct a chat <message/>
fn make_reply(to: Jid, body: &str) -> Element {
let mut message = Message::new(Some(to));
message.bodies.insert(String::new(), body.to_owned());
message.into()
2017-06-20 19:26:51 +00:00
}