2023-03-07 13:56:31 +00:00
|
|
|
use quick_xml::events::Event;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
use crate::util::xml::*;
|
|
|
|
|
2023-02-28 11:12:03 +00:00
|
|
|
pub static XMLNS: &'static str = "jabber:client";
|
2023-03-07 13:56:31 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
pub struct Message {
|
2023-03-07 15:27:09 +00:00
|
|
|
pub from: Option<String>,
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub to: Option<String>,
|
2023-03-07 13:56:31 +00:00
|
|
|
// default is Normal
|
2023-03-07 15:27:09 +00:00
|
|
|
pub r#type: MessageType,
|
|
|
|
pub lang: Option<String>,
|
2023-03-07 13:56:31 +00:00
|
|
|
|
2023-03-07 15:27:09 +00:00
|
|
|
pub subject: Option<String>,
|
|
|
|
pub body: String,
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
|
|
|
impl Message {
|
|
|
|
pub fn parse() -> impl Parser<Output = Result<Self>> {
|
|
|
|
MessageParser::Init
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
enum MessageParser {
|
|
|
|
#[default]
|
|
|
|
Init,
|
|
|
|
Outer(MessageParserState),
|
|
|
|
InSubject(MessageParserState),
|
|
|
|
InBody(MessageParserState),
|
|
|
|
}
|
|
|
|
#[derive(Default)]
|
|
|
|
struct MessageParserState {
|
|
|
|
from: Option<String>,
|
|
|
|
id: Option<String>,
|
|
|
|
to: Option<String>,
|
|
|
|
r#type: MessageType,
|
|
|
|
lang: Option<String>,
|
|
|
|
subject: Option<String>,
|
|
|
|
body: Option<String>,
|
|
|
|
}
|
|
|
|
impl Parser for MessageParser {
|
|
|
|
type Output = Result<Message>;
|
|
|
|
|
|
|
|
fn consume<'a>(self: Self, event: &Event<'a>) -> Continuation<Self, Self::Output> {
|
|
|
|
// TODO validate tag name and namespace at each stage
|
|
|
|
match self {
|
|
|
|
MessageParser::Init => {
|
|
|
|
if let Event::Start(ref bytes) = event {
|
|
|
|
let mut state: MessageParserState = Default::default();
|
|
|
|
for attr in bytes.attributes() {
|
|
|
|
let attr = fail_fast!(attr);
|
|
|
|
if attr.key.0 == b"from" {
|
|
|
|
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
|
|
|
state.from = Some(value.to_string())
|
|
|
|
} else if attr.key.0 == b"id" {
|
|
|
|
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
|
|
|
state.id = Some(value.to_string())
|
|
|
|
} else if attr.key.0 == b"to" {
|
|
|
|
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
|
|
|
state.to = Some(value.to_string())
|
2023-03-07 15:27:09 +00:00
|
|
|
} else if attr.key.0 == b"type" {
|
|
|
|
let value = fail_fast!(MessageType::from_str(&*attr.value));
|
|
|
|
state.r#type = value;
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Continuation::Continue(MessageParser::Outer(state))
|
|
|
|
} else {
|
|
|
|
Continuation::Final(Err(ffail!("Expected start")))
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 15:27:09 +00:00
|
|
|
MessageParser::Outer(state) => match event {
|
|
|
|
Event::Start(ref bytes) => {
|
|
|
|
if bytes.name().0 == b"subject" {
|
|
|
|
Continuation::Continue(MessageParser::InSubject(state))
|
|
|
|
} else if bytes.name().0 == b"body" {
|
|
|
|
Continuation::Continue(MessageParser::InBody(state))
|
|
|
|
} else {
|
|
|
|
Continuation::Final(Err(ffail!("Unexpected XML tag")))
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
2023-03-07 15:27:09 +00:00
|
|
|
}
|
|
|
|
Event::End(_) => {
|
|
|
|
if let Some(body) = state.body {
|
|
|
|
Continuation::Final(Ok(Message {
|
|
|
|
from: state.from,
|
|
|
|
id: state.id,
|
|
|
|
to: state.to,
|
|
|
|
r#type: state.r#type,
|
|
|
|
lang: state.lang,
|
|
|
|
subject: state.subject,
|
|
|
|
body,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Continuation::Final(Err(ffail!("Body not found")))
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-07 15:27:09 +00:00
|
|
|
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
|
2023-03-07 13:56:31 +00:00
|
|
|
},
|
2023-03-07 15:27:09 +00:00
|
|
|
MessageParser::InSubject(mut state) => match event {
|
|
|
|
Event::Text(ref bytes) => {
|
|
|
|
let subject = fail_fast!(std::str::from_utf8(&*bytes));
|
|
|
|
state.subject = Some(subject.to_string());
|
|
|
|
Continuation::Continue(MessageParser::InSubject(state))
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
2023-03-07 15:27:09 +00:00
|
|
|
Event::End(_) => Continuation::Continue(MessageParser::Outer(state)),
|
|
|
|
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
|
|
|
|
},
|
|
|
|
MessageParser::InBody(mut state) => match event {
|
|
|
|
Event::Text(ref bytes) => match std::str::from_utf8(&*bytes) {
|
|
|
|
Ok(subject) => {
|
|
|
|
state.body = Some(subject.to_string());
|
|
|
|
Continuation::Continue(MessageParser::InBody(state))
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
2023-03-07 15:27:09 +00:00
|
|
|
Err(err) => Continuation::Final(Err(err.into())),
|
|
|
|
},
|
|
|
|
Event::End(_) => Continuation::Continue(MessageParser::Outer(state)),
|
|
|
|
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
|
|
|
|
},
|
2023-03-07 13:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
pub enum MessageType {
|
|
|
|
Chat,
|
|
|
|
Error,
|
|
|
|
Groupchat,
|
|
|
|
Headline,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MessageType {
|
|
|
|
fn default() -> Self {
|
|
|
|
MessageType::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageType {
|
2023-03-07 15:27:09 +00:00
|
|
|
pub fn from_str(s: &[u8]) -> Result<MessageType> {
|
2023-03-07 13:56:31 +00:00
|
|
|
use MessageType::*;
|
2023-03-07 15:27:09 +00:00
|
|
|
let s = std::str::from_utf8(s)?;
|
2023-03-07 13:56:31 +00:00
|
|
|
match s {
|
|
|
|
"chat" => Ok(Chat),
|
|
|
|
"error" => Ok(Error),
|
|
|
|
"groupchat" => Ok(Groupchat),
|
|
|
|
"headline" => Ok(Headline),
|
|
|
|
"normal" => Ok(Normal),
|
|
|
|
t => Err(ffail!("Unknown message type: {t}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use quick_xml::NsReader;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn parse_message() {
|
|
|
|
let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body></message>"#;
|
|
|
|
let mut reader = NsReader::from_reader(input.as_bytes());
|
|
|
|
let mut buf = vec![];
|
|
|
|
let event = reader.read_event_into_async(&mut buf).await.unwrap();
|
|
|
|
let mut parser = Message::parse().consume(&event);
|
|
|
|
let result = loop {
|
|
|
|
match parser {
|
|
|
|
Continuation::Final(res) => break res,
|
|
|
|
Continuation::Continue(next) => {
|
|
|
|
parser = next.consume(&reader.read_event_into_async(&mut buf).await.unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Message {
|
|
|
|
from: None,
|
|
|
|
id: Some("aacea".to_string()),
|
|
|
|
to: Some("nikita@vlnv.dev".to_string()),
|
2023-03-07 15:27:09 +00:00
|
|
|
r#type: MessageType::Chat,
|
2023-03-07 13:56:31 +00:00
|
|
|
lang: None,
|
|
|
|
subject: Some("daa".to_string()),
|
|
|
|
body: "bbb".to_string(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|