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