more of a ClientEvent api
This commit is contained in:
parent
e2a4f609fb
commit
7f667041d9
3 changed files with 52 additions and 29 deletions
|
@ -6,7 +6,7 @@ extern crate xml;
|
||||||
|
|
||||||
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, ClientEvent};
|
use tokio_xmpp::Client;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
@ -24,8 +24,8 @@ fn main() {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
let done = stream.for_each(|event| {
|
let done = stream.for_each(|event| {
|
||||||
let result: Box<Future<Item=(), Error=String>> = match event {
|
let result: Box<Future<Item=(), Error=String>> =
|
||||||
ClientEvent::Online => {
|
if event.is_online() {
|
||||||
println!("Online!");
|
println!("Online!");
|
||||||
|
|
||||||
let presence = make_presence();
|
let presence = make_presence();
|
||||||
|
@ -33,28 +33,25 @@ fn main() {
|
||||||
Box::new(
|
Box::new(
|
||||||
future::ok(())
|
future::ok(())
|
||||||
)
|
)
|
||||||
},
|
} else if let Some(stanza) = event.as_stanza() {
|
||||||
ClientEvent::Stanza(ref stanza)
|
if stanza.name == "message" &&
|
||||||
if stanza.name == "message"
|
stanza.get_attribute("type", None) != Some("error") {
|
||||||
&& stanza.get_attribute("type", None) != Some("error") =>
|
let from = stanza.get_attribute("from", None);
|
||||||
{
|
let body = stanza.get_child("body", Some("jabber:client"))
|
||||||
let from = stanza.get_attribute("from", None);
|
.map(|el| el.content_str());
|
||||||
let body = stanza.get_child("body", Some("jabber:client"))
|
|
||||||
.map(|el| el.content_str());
|
|
||||||
|
|
||||||
match (from.as_ref(), body) {
|
match (from.as_ref(), 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);
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
}
|
||||||
Box::new(future::ok(()))
|
Box::new(future::ok(()))
|
||||||
},
|
} else {
|
||||||
_ => {
|
|
||||||
Box::new(future::ok(()))
|
Box::new(future::ok(()))
|
||||||
},
|
};
|
||||||
};
|
|
||||||
result
|
result
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
31
src/client/event.rs
Normal file
31
src/client/event.rs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use xml;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Event {
|
||||||
|
Online,
|
||||||
|
Disconnected,
|
||||||
|
Stanza(xml::Element),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Event {
|
||||||
|
pub fn is_online(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
&Event::Online => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_stanza(&self, name: &str) -> bool {
|
||||||
|
match self {
|
||||||
|
&Event::Stanza(ref stanza) => stanza.name == name,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_stanza(&self) -> Option<&xml::Element> {
|
||||||
|
match self {
|
||||||
|
&Event::Stanza(ref stanza) => Some(stanza),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,8 @@ mod auth;
|
||||||
use self::auth::*;
|
use self::auth::*;
|
||||||
mod bind;
|
mod bind;
|
||||||
use self::bind::*;
|
use self::bind::*;
|
||||||
|
mod event;
|
||||||
|
pub use self::event::Event as ClientEvent;
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub jid: Jid,
|
pub jid: Jid,
|
||||||
|
@ -99,13 +101,6 @@ impl Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ClientEvent {
|
|
||||||
Online,
|
|
||||||
Disconnected,
|
|
||||||
Stanza(xml::Element),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Stream for Client {
|
impl Stream for Client {
|
||||||
type Item = ClientEvent;
|
type Item = ClientEvent;
|
||||||
type Error = String;
|
type Error = String;
|
||||||
|
|
Loading…
Reference in a new issue