session: implement filter_presence

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-12-27 00:21:10 +01:00
parent 1b74fce690
commit db764b18f7
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
3 changed files with 67 additions and 5 deletions

View file

@ -174,7 +174,7 @@ mod tests {
err => panic!("Err: {:?}", err),
}
assert_eq!(occupant.sessions[0].presence, presence1);
assert_eq!(occupant.sessions[0].presence.show, Some(PresenceShow::Away));
let presence2 = PresenceFull::try_from(
Presence::new(PresenceType::None)
@ -189,7 +189,7 @@ mod tests {
err => panic!("Error: {:?}", err),
}
assert_eq!(occupant.sessions[1].presence, presence2);
assert_eq!(occupant.sessions[1].presence.show, Some(PresenceShow::Xa));
let presence_leave_louise2 = PresenceFull::try_from(
Presence::new(PresenceType::Unavailable)

View file

@ -15,7 +15,7 @@
use crate::error::Error;
use std::convert::TryFrom;
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use xmpp_parsers::{presence::Presence, Jid};
/// Presence that has a FullJid as destination (to).
@ -44,3 +44,9 @@ impl Deref for PresenceFull {
&self.0
}
}
impl DerefMut for PresenceFull {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

View file

@ -16,7 +16,7 @@
use crate::error::Error;
use crate::presence::PresenceFull;
use xmpp_parsers::{presence::Presence, FullJid, Jid};
use xmpp_parsers::{presence::Presence, Element, FullJid, Jid};
pub type Nick = String;
@ -28,7 +28,16 @@ pub struct Session {
impl Session {
/// Ensure presence doesn't contain payloads that would impersonate us
fn filter_presence(presence: PresenceFull) -> PresenceFull {
fn filter_presence(mut presence: PresenceFull) -> PresenceFull {
presence.payloads.retain(|payload| {
// TODO: Add a ::is_a method in xmpp-parsers which takes a ref to avoid having to
// clone, and use this instead.
// TODO: Finish this list
!(Element::is(payload, "x", "http://jabber.org/protocol/muc")
|| Element::is(payload, "x", "http://jabber.org/protocol/muc#user")
|| Element::is(payload, "x", "http://jabber.org/protocol/muc#owner")
|| Element::is(payload, "occupant-id", "urn:xmpp:occupant-id:0"))
});
presence
}
@ -72,3 +81,50 @@ impl TryFrom<Presence> for Session {
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::templates::{LOUISE_FULL1, LOUISE_ROOM1_PART};
use chrono::{FixedOffset, Utc};
use xmpp_parsers::{
date::DateTime,
idle::Idle,
muc::{Muc, MucUser},
presence::{Presence, Type as PresenceType},
Jid,
};
#[test]
fn filter_presence() {
let idle = Idle {
since: DateTime(Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap())),
};
let presence = PresenceFull::try_from(
Presence::new(PresenceType::None)
.with_from(Jid::Full(LOUISE_FULL1.clone()))
.with_to(Jid::Full(LOUISE_ROOM1_PART.clone()))
.with_payloads(vec![
Muc::new().into(),
MucUser {
status: vec![],
items: vec![],
}
.into(),
idle.clone().into(),
]),
)
.unwrap();
let result = PresenceFull::try_from(
Presence::new(PresenceType::None)
.with_from(Jid::Full(LOUISE_FULL1.clone()))
.with_to(Jid::Full(LOUISE_ROOM1_PART.clone()))
.with_payloads(vec![idle.into()]),
)
.unwrap();
assert_eq!(Session::filter_presence(presence), result);
}
}