forked from lavina/lavina
1
0
Fork 0

xmpp: fix message parsing when unknown elements are present

This commit is contained in:
Nikita Vilunov 2023-10-01 17:58:59 +02:00
parent 2f034284cf
commit 7c89936a87
1 changed files with 11 additions and 10 deletions

View File

@ -14,25 +14,25 @@ use super::bind::Jid;
pub const XMLNS: &'static str = "jabber:client"; pub const XMLNS: &'static str = "jabber:client";
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub struct Message { pub struct Message<T> {
pub from: Option<Jid>, pub from: Option<Jid>,
pub id: Option<String>, pub id: Option<String>,
pub to: Option<Jid>, pub to: Option<Jid>,
// default is Normal // default is Normal
pub r#type: MessageType, pub r#type: MessageType,
pub lang: Option<Str>, pub lang: Option<Str>,
pub subject: Option<Str>, pub subject: Option<Str>,
pub body: Str, pub body: Str,
pub custom: Vec<T>,
} }
impl FromXmlTag for Message { impl<T> FromXmlTag for Message<T> {
const NS: &'static str = XMLNS; const NS: &'static str = XMLNS;
const NAME: &'static str = "message"; const NAME: &'static str = "message";
} }
impl FromXml for Message { impl<T> FromXml for Message<T> {
type P = MessageParser; type P = MessageParser<T>;
fn parse() -> Self::P { fn parse() -> Self::P {
MessageParserInner::Init.into() MessageParserInner::Init.into()
@ -40,15 +40,16 @@ impl FromXml for Message {
} }
#[derive(From)] #[derive(From)]
pub struct MessageParser(MessageParserInner); pub struct MessageParser<T: FromXml>(MessageParserInner<T>);
#[derive(Default)] #[derive(Default)]
enum MessageParserInner { enum MessageParserInner<T: FromXml> {
#[default] #[default]
Init, Init,
Outer(MessageParserState), Outer(MessageParserState),
InSubject(MessageParserState), InSubject(MessageParserState),
InBody(MessageParserState), InBody(MessageParserState),
InCustom(T::P),
} }
#[derive(Default)] #[derive(Default)]
struct MessageParserState { struct MessageParserState {
@ -60,8 +61,8 @@ struct MessageParserState {
subject: Option<Str>, subject: Option<Str>,
body: Option<Str>, body: Option<Str>,
} }
impl Parser for MessageParser { impl<T> Parser for MessageParser<T> {
type Output = Result<Message>; type Output = Result<Message<T>>;
fn consume<'a>( fn consume<'a>(
self: Self, self: Self,
@ -148,7 +149,7 @@ impl Parser for MessageParser {
} }
} }
impl ToXml for Message { impl<T: ToXml> ToXml for Message<T> {
fn serialize(&self, events: &mut Vec<Event<'static>>) { fn serialize(&self, events: &mut Vec<Event<'static>>) {
let mut bytes = BytesStart::new(format!(r#"message xmlns="{}""#, XMLNS)); let mut bytes = BytesStart::new(format!(r#"message xmlns="{}""#, XMLNS));
if let Some(from) = &self.from { if let Some(from) = &self.from {