simplify the presence plugin using xmpp-parsers
This commit is contained in:
parent
ca882fd13a
commit
8e7b7ae31c
2 changed files with 23 additions and 79 deletions
|
@ -4,7 +4,7 @@ use xmpp::jid::Jid;
|
|||
use xmpp::client::ClientBuilder;
|
||||
use xmpp::plugins::stanza::StanzaPlugin;
|
||||
use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent};
|
||||
use xmpp::plugins::presence::{PresencePlugin, Show};
|
||||
use xmpp::plugins::presence::{PresencePlugin, Type};
|
||||
use xmpp::plugins::ping::{PingPlugin, PingEvent};
|
||||
|
||||
use std::env;
|
||||
|
@ -20,7 +20,7 @@ fn main() {
|
|||
client.register_plugin(MessagingPlugin::new());
|
||||
client.register_plugin(PresencePlugin::new());
|
||||
client.register_plugin(PingPlugin::new());
|
||||
client.plugin::<PresencePlugin>().set_presence(Show::Available, None).unwrap();
|
||||
client.plugin::<PresencePlugin>().set_presence(Type::Available, None, None).unwrap();
|
||||
client.main().unwrap();
|
||||
/*loop {
|
||||
let event = client.next_event().unwrap();
|
||||
|
|
|
@ -1,56 +1,9 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use error::Error;
|
||||
use plugin::PluginProxy;
|
||||
|
||||
use minidom::Element;
|
||||
|
||||
use ns;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum Show {
|
||||
Available,
|
||||
Away,
|
||||
ExtendedAway,
|
||||
DoNotDisturb,
|
||||
Chat,
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
impl fmt::Display for Show {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Show::Away => write!(fmt, "away"),
|
||||
Show::ExtendedAway => write!(fmt, "xa"),
|
||||
Show::DoNotDisturb => write!(fmt, "dnd"),
|
||||
Show::Chat => write!(fmt, "chat"),
|
||||
|
||||
// will never be seen inside a <show>, maybe should crash?
|
||||
Show::Available => write!(fmt, "available"),
|
||||
Show::Unavailable => write!(fmt, "unavailable"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct InvalidShow;
|
||||
|
||||
impl FromStr for Show {
|
||||
type Err = InvalidShow;
|
||||
|
||||
fn from_str(s: &str) -> Result<Show, InvalidShow> {
|
||||
Ok(match s {
|
||||
"away" => Show::Away,
|
||||
"xa" => Show::ExtendedAway,
|
||||
"dnd" => Show::DoNotDisturb,
|
||||
"chat" => Show::Chat,
|
||||
|
||||
_ => { return Err(InvalidShow); }
|
||||
})
|
||||
}
|
||||
}
|
||||
pub use xmpp_parsers::presence::{Presence, PresenceType as Type, Show};
|
||||
|
||||
pub struct PresencePlugin {
|
||||
proxy: PluginProxy,
|
||||
|
@ -63,33 +16,24 @@ impl PresencePlugin {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_presence(&self, show: Show, status: Option<String>) -> Result<(), Error> {
|
||||
if show == Show::Unavailable {
|
||||
self.proxy.send(Element::builder("presence")
|
||||
.ns(ns::CLIENT)
|
||||
.attr("type", "unavailable")
|
||||
.build());
|
||||
}
|
||||
else {
|
||||
let mut stanza = Element::builder("presence")
|
||||
.ns(ns::CLIENT)
|
||||
.build();
|
||||
if let Some(stat) = status {
|
||||
let elem = Element::builder("status")
|
||||
.ns(ns::CLIENT)
|
||||
.append(stat)
|
||||
.build();
|
||||
stanza.append_child(elem);
|
||||
}
|
||||
if show != Show::Available {
|
||||
let mut elem = Element::builder("show")
|
||||
.ns(ns::CLIENT)
|
||||
.build();
|
||||
elem.append_text_node(show.to_string());
|
||||
stanza.append_child(elem);
|
||||
}
|
||||
self.proxy.send(stanza);
|
||||
}
|
||||
pub fn set_presence(&self, type_: Type, show: Option<Show>, status: Option<String>) -> Result<(), Error> {
|
||||
let presence = Presence {
|
||||
from: None,
|
||||
to: None,
|
||||
id: None,
|
||||
type_: type_,
|
||||
show: show,
|
||||
priority: 0i8,
|
||||
statuses: {
|
||||
let mut statuses = BTreeMap::new();
|
||||
if let Some(status) = status {
|
||||
statuses.insert(String::new(), status);
|
||||
}
|
||||
statuses
|
||||
},
|
||||
payloads: vec!(),
|
||||
};
|
||||
self.proxy.send(presence.into());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue