2024-04-20 18:14:12 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use quick_xml::events::Event;
|
|
|
|
use quick_xml::name::{Namespace, ResolveResult};
|
2024-04-30 12:16:52 +00:00
|
|
|
use std::io::Read;
|
2024-04-20 18:14:12 +00:00
|
|
|
|
|
|
|
use crate::xml::*;
|
|
|
|
|
2024-04-30 12:16:52 +00:00
|
|
|
pub const MAM_XMLNS: &'static str = "urn:xmpp:mam:2";
|
|
|
|
pub const DATA_XMLNS: &'static str = "jabber:x:data";
|
2024-04-20 18:14:12 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
2024-04-30 12:16:52 +00:00
|
|
|
pub struct MessageArchiveRequest {
|
2024-05-02 17:19:35 +00:00
|
|
|
pub x: Option<X>,
|
2024-04-30 12:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
|
|
|
pub struct X {
|
2024-05-02 17:19:35 +00:00
|
|
|
pub fields: Vec<Field>,
|
2024-04-30 12:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
|
|
|
pub struct Field {
|
2024-05-02 17:19:35 +00:00
|
|
|
pub values: Vec<String>,
|
2024-04-30 12:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromXmlTag for X {
|
|
|
|
const NAME: &'static str = "x";
|
|
|
|
const NS: &'static str = DATA_XMLNS;
|
|
|
|
}
|
2024-04-20 18:14:12 +00:00
|
|
|
|
|
|
|
impl FromXmlTag for MessageArchiveRequest {
|
|
|
|
const NAME: &'static str = "query";
|
2024-04-30 12:16:52 +00:00
|
|
|
const NS: &'static str = MAM_XMLNS;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromXml for X {
|
|
|
|
type P = impl Parser<Output = Result<Self>>;
|
|
|
|
|
|
|
|
fn parse() -> Self::P {
|
|
|
|
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
|
|
|
println!("X::parse {:?}", event);
|
|
|
|
|
|
|
|
let bytes = match event {
|
|
|
|
Event::Start(bytes) if bytes.name().0 == X::NAME.as_bytes() => bytes,
|
|
|
|
Event::Empty(bytes) if bytes.name().0 == X::NAME.as_bytes() => return Ok(X { fields: vec![] }),
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
|
|
|
};
|
|
|
|
let mut fields = vec![];
|
|
|
|
loop {
|
|
|
|
(namespace, event) = yield;
|
|
|
|
match event {
|
|
|
|
Event::Start(bytes) => {
|
|
|
|
// start of <field>
|
|
|
|
let mut values = vec![];
|
|
|
|
loop {
|
|
|
|
(namespace, event) = yield;
|
|
|
|
match event {
|
|
|
|
Event::Start(bytes) if bytes.name().0 == b"value" => {
|
|
|
|
// start of <value>
|
|
|
|
}
|
|
|
|
Event::End(bytes) if bytes.name().0 == b"field" => {
|
|
|
|
// end of </field>
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
|
|
|
}
|
|
|
|
(namespace, event) = yield;
|
|
|
|
let text: String = match event {
|
|
|
|
Event::Text(bytes) => {
|
|
|
|
// text inside <value></value>
|
|
|
|
String::from_utf8(bytes.to_vec())?
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
|
|
|
};
|
|
|
|
(namespace, event) = yield;
|
|
|
|
match event {
|
|
|
|
Event::End(bytes) if bytes.name().0 == b"value" => {
|
|
|
|
// end of </value>
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
|
|
|
}
|
|
|
|
values.push(text);
|
|
|
|
}
|
|
|
|
fields.push(Field { values })
|
|
|
|
}
|
|
|
|
Event::End(bytes) if bytes.name().0 == X::NAME.as_bytes() => {
|
|
|
|
// end of <x/>
|
|
|
|
return Ok(X { fields });
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-20 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromXml for MessageArchiveRequest {
|
|
|
|
type P = impl Parser<Output = Result<Self>>;
|
|
|
|
|
|
|
|
fn parse() -> Self::P {
|
2024-04-30 12:16:52 +00:00
|
|
|
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
|
|
|
println!("MessageArchiveRequest::parse {:?}", event);
|
|
|
|
|
|
|
|
let bytes = match event {
|
|
|
|
Event::Empty(_) => return Ok(MessageArchiveRequest { x: None }),
|
|
|
|
Event::Start(bytes) => bytes,
|
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
2024-04-20 18:14:12 +00:00
|
|
|
};
|
|
|
|
if bytes.name().0 != MessageArchiveRequest::NAME.as_bytes() {
|
|
|
|
return Err(anyhow!("Unexpected XML tag: {:?}", bytes.name()));
|
|
|
|
}
|
|
|
|
let ResolveResult::Bound(Namespace(ns)) = namespace else {
|
|
|
|
return Err(anyhow!("No namespace provided"));
|
|
|
|
};
|
2024-04-30 12:16:52 +00:00
|
|
|
if ns != MAM_XMLNS.as_bytes() {
|
2024-04-20 18:14:12 +00:00
|
|
|
return Err(anyhow!("Incorrect namespace"));
|
|
|
|
}
|
2024-04-30 12:16:52 +00:00
|
|
|
(namespace, event) = yield;
|
|
|
|
match event {
|
|
|
|
Event::End(bytes) if bytes.name().0 == MessageArchiveRequest::NAME.as_bytes() => {
|
|
|
|
Ok(MessageArchiveRequest { x: None })
|
|
|
|
}
|
|
|
|
Event::Start(bytes) | Event::Empty(bytes) if bytes.name().0 == X::NAME.as_bytes() => {
|
|
|
|
let x = delegate_parsing!(X, namespace, event)?;
|
|
|
|
Ok(MessageArchiveRequest { x: Some(x) })
|
2024-04-20 18:14:12 +00:00
|
|
|
}
|
2024-04-30 12:16:52 +00:00
|
|
|
_ => Err(anyhow!("Unexpected XML event: {event:?}")),
|
2024-04-20 18:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageArchiveRequest {}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2024-04-30 12:16:52 +00:00
|
|
|
use crate::bind::{Jid, Name, Server};
|
2024-04-20 18:14:12 +00:00
|
|
|
use crate::client::{Iq, IqType};
|
|
|
|
|
|
|
|
#[test]
|
2024-04-30 12:16:52 +00:00
|
|
|
fn test_parse_archive_query() {
|
|
|
|
let input = r#"<iq to='pubsub.shakespeare.lit' type='set' id='juliet1'><query xmlns='urn:xmpp:mam:2' queryid='f28'/></iq>"#;
|
2024-04-20 18:14:12 +00:00
|
|
|
|
|
|
|
let result: Iq<MessageArchiveRequest> = parse(input).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Iq {
|
|
|
|
from: None,
|
2024-04-30 12:16:52 +00:00
|
|
|
id: "juliet1".to_string(),
|
|
|
|
to: Option::from(Jid {
|
|
|
|
name: None,
|
|
|
|
server: Server("pubsub.shakespeare.lit".into()),
|
|
|
|
resource: None,
|
|
|
|
}),
|
|
|
|
r#type: IqType::Set,
|
|
|
|
body: MessageArchiveRequest { x: None },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_query_messages_from_jid() {
|
|
|
|
let input = r#"<iq type='set' id='juliet1'><query xmlns='urn:xmpp:mam:2'><x xmlns='jabber:x:data' type='submit'><field var='FORM_TYPE' type='hidden'><value>value1</value></field><field var='with'><value>juliet@capulet.lit</value></field></x></query></iq>"#;
|
|
|
|
|
|
|
|
let result: Iq<MessageArchiveRequest> = parse(input).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
id: "juliet1".to_string(),
|
2024-04-20 18:14:12 +00:00
|
|
|
to: None,
|
2024-04-30 12:16:52 +00:00
|
|
|
r#type: IqType::Set,
|
|
|
|
body: MessageArchiveRequest {
|
|
|
|
x: Some(X {
|
|
|
|
fields: vec![
|
|
|
|
Field {
|
|
|
|
values: vec!["value1".to_string()],
|
|
|
|
},
|
|
|
|
Field {
|
|
|
|
values: vec!["juliet@capulet.lit".to_string()],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
|
|
|
},
|
2024-04-20 18:14:12 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-04-30 12:16:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_query_messages_from_jid_with_unclosed_tag() {
|
|
|
|
let input = r#"<iq type='set' id='juliet1'><query xmlns='urn:xmpp:mam:2'><x xmlns='jabber:x:data' type='submit'><field var='FORM_TYPE' type='hidden'><value>value1</value></field><field var='with'><value>juliet@capulet.lit</value></field></query></iq>"#;
|
|
|
|
|
|
|
|
assert!(parse::<Iq<MessageArchiveRequest>>(input).is_err())
|
|
|
|
}
|
2024-04-20 18:14:12 +00:00
|
|
|
}
|