echo_bot: document, simplify
This commit is contained in:
parent
e2c6a6ed37
commit
7e18db0717
1 changed files with 33 additions and 28 deletions
|
@ -9,10 +9,16 @@ use futures::{Future, Stream, Sink, future};
|
||||||
use tokio_xmpp::Client;
|
use tokio_xmpp::Client;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Tokio_core context
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
// Client instance
|
||||||
let client = Client::new("astrobot@example.org", "", &core.handle()).unwrap();
|
let client = Client::new("astrobot@example.org", "", &core.handle()).unwrap();
|
||||||
|
|
||||||
|
// 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();
|
let (sink, stream) = client.split();
|
||||||
|
// 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);
|
let mut sink = Some(sink);
|
||||||
let mut send = move |stanza| {
|
let mut send = move |stanza| {
|
||||||
sink = Some(
|
sink = Some(
|
||||||
|
@ -23,19 +29,17 @@ fn main() {
|
||||||
.expect("sink.send")
|
.expect("sink.send")
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
// Main loop, processes events
|
||||||
let done = stream.for_each(|event| {
|
let done = stream.for_each(|event| {
|
||||||
let result: Box<Future<Item=(), Error=String>> =
|
|
||||||
if event.is_online() {
|
if event.is_online() {
|
||||||
println!("Online!");
|
println!("Online!");
|
||||||
|
|
||||||
let presence = make_presence();
|
let presence = make_presence();
|
||||||
send(presence);
|
send(presence);
|
||||||
Box::new(
|
|
||||||
future::ok(())
|
|
||||||
)
|
|
||||||
} else if let Some(stanza) = event.as_stanza() {
|
} else if let Some(stanza) = event.as_stanza() {
|
||||||
if stanza.name == "message" &&
|
if stanza.name == "message" &&
|
||||||
stanza.get_attribute("type", None) != Some("error") {
|
stanza.get_attribute("type", None) != Some("error") {
|
||||||
|
// This is a message we'll echo
|
||||||
let from = stanza.get_attribute("from", None);
|
let from = stanza.get_attribute("from", None);
|
||||||
let body = stanza.get_child("body", Some("jabber:client"))
|
let body = stanza.get_child("body", Some("jabber:client"))
|
||||||
.map(|el| el.content_str());
|
.map(|el| el.content_str());
|
||||||
|
@ -48,13 +52,12 @@ fn main() {
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Box::new(future::ok(()))
|
Box::new(future::ok(()))
|
||||||
} else {
|
|
||||||
Box::new(future::ok(()))
|
|
||||||
};
|
|
||||||
result
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Start polling `done`
|
||||||
match core.run(done) {
|
match core.run(done) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -64,6 +67,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct a <presence/>
|
||||||
fn make_presence() -> xml::Element {
|
fn make_presence() -> xml::Element {
|
||||||
let mut presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
let mut presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
||||||
presence.tag(xml::Element::new("status".to_owned(), None, vec![]))
|
presence.tag(xml::Element::new("status".to_owned(), None, vec![]))
|
||||||
|
@ -73,6 +77,7 @@ fn make_presence() -> xml::Element {
|
||||||
presence
|
presence
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct a chat <message/>
|
||||||
fn make_reply(to: &str, body: String) -> xml::Element {
|
fn make_reply(to: &str, body: String) -> xml::Element {
|
||||||
let mut message = xml::Element::new(
|
let mut message = xml::Element::new(
|
||||||
"message".to_owned(),
|
"message".to_owned(),
|
||||||
|
|
Loading…
Reference in a new issue