echo_bot: use xmpp-parsers to parse message
This commit is contained in:
parent
fc0973539f
commit
00e2daaecd
2 changed files with 20 additions and 13 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
#![feature(try_from)]
|
||||||
|
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_xmpp;
|
extern crate tokio_xmpp;
|
||||||
|
@ -7,12 +9,14 @@ extern crate xmpp_parsers;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
use std::convert::TryFrom;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
use futures::{Future, Stream, Sink, future};
|
use futures::{Future, Stream, Sink, future};
|
||||||
use tokio_xmpp::Client;
|
use tokio_xmpp::Client;
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
use xmpp_parsers::presence::{Presence, Type as PresenceType, Show as PresenceShow};
|
use xmpp_parsers::presence::{Presence, Type as PresenceType, Show as PresenceShow};
|
||||||
use xmpp_parsers::message::Message;
|
use xmpp_parsers::message::{Message, MessageType};
|
||||||
|
use jid::Jid;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = args().collect();
|
let args: Vec<String> = args().collect();
|
||||||
|
@ -50,15 +54,11 @@ fn main() {
|
||||||
|
|
||||||
let presence = make_presence();
|
let presence = make_presence();
|
||||||
send(presence);
|
send(presence);
|
||||||
} else if let Some(stanza) = event.as_stanza() {
|
} else if let Some(stanza) = event.into_stanza() {
|
||||||
if stanza.name() == "message" &&
|
if let Ok(message) = Message::try_from(stanza) {
|
||||||
stanza.attr("type") != Some("error") {
|
if message.type_ != MessageType::Error {
|
||||||
// This is a message we'll echo
|
// This is a message we'll echo
|
||||||
let from = stanza.attr("from");
|
match (message.from, message.bodies.get("")) {
|
||||||
let body = stanza.get_child("body", "jabber:client")
|
|
||||||
.map(|el| el.text());
|
|
||||||
|
|
||||||
match (from, body) {
|
|
||||||
(Some(from), Some(body)) => {
|
(Some(from), Some(body)) => {
|
||||||
let reply = make_reply(from, body);
|
let reply = make_reply(from, body);
|
||||||
send(reply);
|
send(reply);
|
||||||
|
@ -66,6 +66,7 @@ fn main() {
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::new(future::ok(()))
|
Box::new(future::ok(()))
|
||||||
|
@ -90,9 +91,8 @@ fn make_presence() -> Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct a chat <message/>
|
// Construct a chat <message/>
|
||||||
fn make_reply(to: &str, body: String) -> Element {
|
fn make_reply(to: Jid, body: &str) -> Element {
|
||||||
let jid = to.parse().unwrap();
|
let mut message = Message::new(Some(to));
|
||||||
let mut message = Message::new(Some(jid));
|
message.bodies.insert(String::new(), body.to_owned());
|
||||||
message.bodies.insert(String::new(), body);
|
|
||||||
message.into()
|
message.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,4 +28,11 @@ impl Event {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_stanza(self) -> Option<Element> {
|
||||||
|
match self {
|
||||||
|
Event::Stanza(stanza) => Some(stanza),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue