From e953a03dda8beb3b0e668b66f13a7ecfacac001a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 31 May 2017 16:51:04 +0100 Subject: [PATCH 1/2] Basic MUC plugin --- src/plugins/mod.rs | 1 + src/plugins/muc.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/plugins/muc.rs diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index a6c8b7c..2a54781 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -8,3 +8,4 @@ pub mod ibb; pub mod stanza; pub mod stanza_debug; pub mod unhandled_iq; +pub mod muc; diff --git a/src/plugins/muc.rs b/src/plugins/muc.rs new file mode 100644 index 0000000..95b9aa7 --- /dev/null +++ b/src/plugins/muc.rs @@ -0,0 +1,53 @@ +use std::collections::BTreeMap; + +use jid::Jid; +use error::Error; +use plugin::PluginProxy; + +pub use xmpp_parsers::muc::Muc; +pub use xmpp_parsers::presence::{Presence, Type, Show}; + +pub struct MucPlugin { + proxy: PluginProxy, +} + +impl MucPlugin { + pub fn new() -> MucPlugin { + MucPlugin { + proxy: PluginProxy::new(), + } + } + + pub fn join_room(&self, room: Jid) -> Result<(), Error> { + let presence = Presence { + from: None, + to: Some(room), + id: None, + type_: Type::None, + show: Show::None, + priority: 0i8, + statuses: BTreeMap::new(), + payloads: vec![Muc.into()], + }; + self.proxy.send(presence.into()); + + Ok(()) + } + + pub fn leave_room(&self, room: Jid) -> Result<(), Error> { + let presence = Presence { + from: None, + to: Some(room), + id: None, + type_: Type::None, + show: Show::None, + priority: 0i8, + statuses: BTreeMap::new(), + payloads: vec![Muc.into()], + }; + self.proxy.send(presence.into()); + Ok(()) + } +} + +impl_plugin!(MucPlugin, proxy, []); From ac732f1b921c548a422bf2c76b74b0229411feaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 14 Jun 2017 23:38:00 +0100 Subject: [PATCH 2/2] listen for Presence and return MucPresence --- src/plugins/muc.rs | 49 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/plugins/muc.rs b/src/plugins/muc.rs index 95b9aa7..9d8d651 100644 --- a/src/plugins/muc.rs +++ b/src/plugins/muc.rs @@ -1,12 +1,27 @@ use std::collections::BTreeMap; +use std::convert::TryFrom; use jid::Jid; use error::Error; use plugin::PluginProxy; -pub use xmpp_parsers::muc::Muc; +use event::{Event, Propagation, Priority}; + +pub use xmpp_parsers::muc::{Muc, MucUser}; +pub use xmpp_parsers::muc::user::{Status, Affiliation, Role}; pub use xmpp_parsers::presence::{Presence, Type, Show}; +#[derive(Debug)] +pub struct MucPresence { + pub room: Jid, + pub nick: Option, + pub to: Jid, + pub type_: Type, + pub x: MucUser, +} + +impl Event for MucPresence {} + pub struct MucPlugin { proxy: PluginProxy, } @@ -19,6 +34,7 @@ impl MucPlugin { } pub fn join_room(&self, room: Jid) -> Result<(), Error> { + let x = Muc { password: None }; let presence = Presence { from: None, to: Some(room), @@ -27,7 +43,7 @@ impl MucPlugin { show: Show::None, priority: 0i8, statuses: BTreeMap::new(), - payloads: vec![Muc.into()], + payloads: vec![x.into()], }; self.proxy.send(presence.into()); @@ -35,6 +51,7 @@ impl MucPlugin { } pub fn leave_room(&self, room: Jid) -> Result<(), Error> { + let x = Muc { password: None }; let presence = Presence { from: None, to: Some(room), @@ -43,11 +60,35 @@ impl MucPlugin { show: Show::None, priority: 0i8, statuses: BTreeMap::new(), - payloads: vec![Muc.into()], + payloads: vec![x.into()], }; self.proxy.send(presence.into()); Ok(()) } + + fn handle_presence(&self, presence: &Presence) -> Propagation { + let from = presence.from.clone().unwrap(); + let room = from.clone().into_bare_jid(); + let nick = from.resource; + let to = presence.to.clone().unwrap(); + let type_ = presence.type_.clone(); + + for payload in presence.clone().payloads { + if let Ok(x) = MucUser::try_from(payload) { + self.proxy.dispatch(MucPresence { + room: room.clone(), + nick: nick.clone(), + to: to.clone(), + type_: type_.clone(), + x + }); + } + } + + Propagation::Stop + } } -impl_plugin!(MucPlugin, proxy, []); +impl_plugin!(MucPlugin, proxy, [ + (Presence, Priority::Default) => handle_presence, +]);