xmpp: Implement Message Archive Management stub for XEP-0313 (#60)

https://xmpp.org/extensions/xep-0313.html
Reviewed-on: lavina/lavina#60
Co-authored-by: Mikhail <mikhail@liamets.dev>
Co-committed-by: Mikhail <mikhail@liamets.dev>
This commit is contained in:
Mikhail 2024-05-05 15:12:58 +00:00 committed by Nikita Vilunov
parent 9a09ff717e
commit adf1d8c14c
9 changed files with 427 additions and 53 deletions

View File

@ -4,16 +4,16 @@ use quick_xml::events::Event;
use lavina_core::room::{RoomId, RoomRegistry}; use lavina_core::room::{RoomId, RoomRegistry};
use proto_xmpp::bind::{BindResponse, Jid, Name, Server}; use proto_xmpp::bind::{BindResponse, Jid, Name, Server};
use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType}; use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType, Message, MessageType};
use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery}; use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery};
use proto_xmpp::mam::{Fin, Set};
use proto_xmpp::roster::RosterQuery; use proto_xmpp::roster::RosterQuery;
use proto_xmpp::session::Session; use proto_xmpp::session::Session;
use proto_xmpp::xml::ToXml;
use crate::proto::IqClientBody; use crate::proto::IqClientBody;
use crate::XmppConnection; use crate::XmppConnection;
use proto_xmpp::xml::ToXml;
impl<'a> XmppConnection<'a> { impl<'a> XmppConnection<'a> {
pub async fn handle_iq(&self, output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) { pub async fn handle_iq(&self, output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
match iq.body { match iq.body {
@ -87,6 +87,18 @@ impl<'a> XmppConnection<'a> {
}; };
req.serialize(output); req.serialize(output);
} }
IqClientBody::MessageArchiveRequest(_) => {
let response = Iq {
from: iq.to,
id: iq.id,
to: None,
r#type: IqType::Result,
body: Fin {
set: Set { count: Some(0) },
},
};
response.serialize(output);
}
_ => { _ => {
let req = Iq { let req = Iq {
from: None, from: None,

View File

@ -7,6 +7,7 @@ use lavina_core::prelude::*;
use proto_xmpp::bind::BindRequest; use proto_xmpp::bind::BindRequest;
use proto_xmpp::client::{Iq, Message, Presence}; use proto_xmpp::client::{Iq, Message, Presence};
use proto_xmpp::disco::{InfoQuery, ItemQuery}; use proto_xmpp::disco::{InfoQuery, ItemQuery};
use proto_xmpp::mam::MessageArchiveRequest;
use proto_xmpp::roster::RosterQuery; use proto_xmpp::roster::RosterQuery;
use proto_xmpp::session::Session; use proto_xmpp::session::Session;
use proto_xmpp::xml::*; use proto_xmpp::xml::*;
@ -18,6 +19,7 @@ pub enum IqClientBody {
Roster(RosterQuery), Roster(RosterQuery),
DiscoInfo(InfoQuery), DiscoInfo(InfoQuery),
DiscoItem(ItemQuery), DiscoItem(ItemQuery),
MessageArchiveRequest(MessageArchiveRequest),
Unknown(Ignore), Unknown(Ignore),
} }
@ -38,6 +40,7 @@ impl FromXml for IqClientBody {
RosterQuery, RosterQuery,
InfoQuery, InfoQuery,
ItemQuery, ItemQuery,
MessageArchiveRequest,
{ {
delegate_parsing!(Ignore, namespace, event).into() delegate_parsing!(Ignore, namespace, event).into()
} }

View File

@ -1,4 +1,5 @@
use std::io::ErrorKind; use std::io::ErrorKind;
use std::str::from_utf8;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -6,6 +7,7 @@ use anyhow::Result;
use assert_matches::*; use assert_matches::*;
use prometheus::Registry as MetricsRegistry; use prometheus::Registry as MetricsRegistry;
use quick_xml::events::Event; use quick_xml::events::Event;
use quick_xml::name::LocalName;
use quick_xml::NsReader; use quick_xml::NsReader;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}; use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::io::{ReadHalf as GenericReadHalf, WriteHalf as GenericWriteHalf}; use tokio::io::{ReadHalf as GenericReadHalf, WriteHalf as GenericWriteHalf};
@ -22,6 +24,10 @@ use lavina_core::LavinaCore;
use projection_xmpp::{launch, RunningServer, ServerConfig}; use projection_xmpp::{launch, RunningServer, ServerConfig};
use proto_xmpp::xml::{Continuation, FromXml, Parser}; use proto_xmpp::xml::{Continuation, FromXml, Parser};
fn element_name<'a>(local_name: &LocalName<'a>) -> &'a str {
from_utf8(local_name.into_inner()).unwrap()
}
pub async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> { pub async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> {
let mut size = 0; let mut size = 0;
let res = reader.read_until(b'\n', buf).await?; let res = reader.read_until(b'\n', buf).await?;
@ -56,11 +62,11 @@ impl<'a> TestScope<'a> {
} }
async fn expect_starttls_required(&mut self) -> Result<()> { async fn expect_starttls_required(&mut self) -> Result<()> {
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features"));
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "starttls"));
assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"required")); assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "required"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "starttls"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features"));
Ok(()) Ok(())
} }
} }
@ -93,20 +99,20 @@ impl<'a> TestScopeTls<'a> {
} }
async fn expect_auth_mechanisms(&mut self) -> Result<()> { async fn expect_auth_mechanisms(&mut self) -> Result<()> {
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features"));
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "mechanisms"));
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanism")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "mechanism"));
assert_matches!(self.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN")); assert_matches!(self.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanism")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "mechanism"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "mechanisms"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features"));
Ok(()) Ok(())
} }
async fn expect_bind_feature(&mut self) -> Result<()> { async fn expect_bind_feature(&mut self) -> Result<()> {
assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features"));
assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"bind")); assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "bind"));
assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features"));
Ok(()) Ok(())
} }
@ -119,6 +125,7 @@ impl<'a> TestScopeTls<'a> {
} }
struct IgnoreCertVerification; struct IgnoreCertVerification;
impl ServerCertVerifier for IgnoreCertVerification { impl ServerCertVerifier for IgnoreCertVerification {
fn verify_server_cert( fn verify_server_cert(
&self, &self,
@ -139,6 +146,7 @@ struct TestServer {
core: LavinaCore, core: LavinaCore,
server: RunningServer, server: RunningServer,
} }
impl TestServer { impl TestServer {
async fn start() -> Result<TestServer> { async fn start() -> Result<TestServer> {
let _ = tracing_subscriber::fmt::try_init(); let _ = tracing_subscriber::fmt::try_init();
@ -187,10 +195,10 @@ async fn scenario_basic() -> Result<()> {
s.send(r#"<?xml version="1.0"?>"#).await?; s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_starttls_required().await?; s.expect_starttls_required().await?;
s.send(r#"<starttls/>"#).await?; s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed"));
let buffer = s.buffer; let buffer = s.buffer;
tracing::info!("TLS feature negotiation complete"); tracing::info!("TLS feature negotiation complete");
@ -209,26 +217,26 @@ async fn scenario_basic() -> Result<()> {
s.send(r#"<?xml version="1.0"?>"#).await?; s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_auth_mechanisms().await?; s.expect_auth_mechanisms().await?;
// base64-encoded b"\x00tester\x00password" // base64-encoded "\x00tester\x00password"
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZA==</auth>"#) s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZA==</auth>"#)
.await?; .await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"success")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "success"));
s.send(r#"<?xml version="1.0"?>"#).await?; s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_bind_feature().await?; s.expect_bind_feature().await?;
s.send(r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>kek</resource></bind></iq>"#).await?; s.send(r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>kek</resource></bind></iq>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"iq")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "iq"));
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"bind")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "bind"));
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"jid")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "jid"));
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester")); assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"jid")); assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "jid"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"bind")); assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "bind"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"iq")); assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "iq"));
s.send(r#"<presence xmlns="jabber:client" type="unavailable"><status>Logged out</status></presence>"#).await?; s.send(r#"<presence xmlns="jabber:client" type="unavailable"><status>Logged out</status></presence>"#).await?;
stream.shutdown().await?; stream.shutdown().await?;
@ -255,10 +263,10 @@ async fn scenario_wrong_password() -> Result<()> {
s.send(r#"<?xml version="1.0"?>"#).await?; s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_starttls_required().await?; s.expect_starttls_required().await?;
s.send(r#"<starttls/>"#).await?; s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed"));
let buffer = s.buffer; let buffer = s.buffer;
tracing::info!("TLS feature negotiation complete"); tracing::info!("TLS feature negotiation complete");
@ -277,14 +285,14 @@ async fn scenario_wrong_password() -> Result<()> {
s.send(r#"<?xml version="1.0"?>"#).await?; s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_auth_mechanisms().await?; s.expect_auth_mechanisms().await?;
// base64-encoded b"\x00tester\x00password2" // base64-encoded "\x00tester\x00password2"
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZDI=</auth>"#) s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZDI=</auth>"#)
.await?; .await?;
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"failure")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "failure"));
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"not-authorized")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "not-authorized"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"failure")); assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "failure"));
let _ = stream.shutdown().await; let _ = stream.shutdown().await;
@ -309,10 +317,10 @@ async fn scenario_basic_without_headers() -> Result<()> {
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_starttls_required().await?; s.expect_starttls_required().await?;
s.send(r#"<starttls/>"#).await?; s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed"));
let buffer = s.buffer; let buffer = s.buffer;
tracing::info!("TLS feature negotiation complete"); tracing::info!("TLS feature negotiation complete");
@ -330,7 +338,7 @@ async fn scenario_basic_without_headers() -> Result<()> {
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
stream.shutdown().await?; stream.shutdown().await?;
@ -357,10 +365,10 @@ async fn terminate_socket() -> Result<()> {
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?; s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_starttls_required().await?; s.expect_starttls_required().await?;
s.send(r#"<starttls/>"#).await?; s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed"));
let connector = TlsConnector::from(Arc::new( let connector = TlsConnector::from(Arc::new(
ClientConfig::builder() ClientConfig::builder()
@ -379,3 +387,89 @@ async fn terminate_socket() -> Result<()> {
Ok(()) Ok(())
} }
#[tokio::test]
async fn test_message_archive_request() -> Result<()> {
let mut server = TestServer::start().await?;
// test scenario
server.storage.create_user("tester").await?;
Authenticator::new(&server.storage).set_password("tester", "password").await?;
let mut stream = TcpStream::connect(server.server.addr).await?;
let mut s = TestScope::new(&mut stream);
tracing::info!("TCP connection established");
s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_starttls_required().await?;
s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed"));
let buffer = s.buffer;
tracing::info!("TLS feature negotiation complete");
let connector = TlsConnector::from(Arc::new(
ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(Arc::new(IgnoreCertVerification))
.with_no_client_auth(),
));
tracing::info!("Initiating TLS connection...");
let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
tracing::info!("TLS connection established");
let mut s = TestScopeTls::new(&mut stream, buffer);
s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_auth_mechanisms().await?;
// base64-encoded "\x00tester\x00password"
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZA==</auth>"#)
.await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "success"));
s.send(r#"<?xml version="1.0"?>"#).await?;
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream"));
s.expect_bind_feature().await?;
s.send(r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>kek</resource></bind></iq>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "iq"));
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "bind"));
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "jid"));
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "jid"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "bind"));
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "iq"));
s.send(r#"<iq type='get' id='juliet1'><query xmlns='urn:xmpp:mam:2' queryid='f27'/></iq>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Start(b) => {
assert_eq!(element_name(&b.local_name()), "iq")
});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => {
assert_eq!(element_name(&b.local_name()), "fin")
});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => {
assert_eq!(element_name(&b.local_name()), "set")
});
assert_matches!(s.next_xml_event().await?, Event::Start(b) => {
assert_eq!(element_name(&b.local_name()), "count")
});
assert_matches!(s.next_xml_event().await?, Event::Text(b) => {
assert_eq!(&*b, b"0")
});
s.send(r#"<presence xmlns="jabber:client" type="unavailable"><status>Logged out</status></presence>"#).await?;
stream.shutdown().await?;
// wrap up
server.shutdown().await?;
Ok(())
}

View File

@ -74,8 +74,8 @@ impl Jid {
pub struct BindRequest(pub Resource); pub struct BindRequest(pub Resource);
impl FromXmlTag for BindRequest { impl FromXmlTag for BindRequest {
const NS: &'static str = XMLNS;
const NAME: &'static str = "bind"; const NAME: &'static str = "bind";
const NS: &'static str = XMLNS;
} }
impl FromXml for BindRequest { impl FromXml for BindRequest {

View File

@ -658,7 +658,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn parse_message() { async fn parse_message() {
let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body><unknown-stuff></unknown-stuff></message>"#; let input = r#"<message id="aacea" type="chat" to="chelik@xmpp.ru"><subject>daa</subject><body>bbb</body><unknown-stuff></unknown-stuff></message>"#;
let result: Message<Ignore> = crate::xml::parse(input).unwrap(); let result: Message<Ignore> = crate::xml::parse(input).unwrap();
assert_eq!( assert_eq!(
result, result,
@ -666,8 +666,8 @@ mod tests {
from: None, from: None,
id: Some("aacea".to_string()), id: Some("aacea".to_string()),
to: Some(Jid { to: Some(Jid {
name: Some(Name("nikita".into())), name: Some(Name("chelik".into())),
server: Server("vlnv.dev".into()), server: Server("xmpp.ru".into()),
resource: None resource: None
}), }),
r#type: MessageType::Chat, r#type: MessageType::Chat,
@ -681,7 +681,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn parse_message_empty_custom() { async fn parse_message_empty_custom() {
let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body><unknown-stuff/></message>"#; let input = r#"<message id="aacea" type="chat" to="chelik@xmpp.ru"><subject>daa</subject><body>bbb</body><unknown-stuff/></message>"#;
let result: Message<Ignore> = crate::xml::parse(input).unwrap(); let result: Message<Ignore> = crate::xml::parse(input).unwrap();
assert_eq!( assert_eq!(
result, result,
@ -689,8 +689,8 @@ mod tests {
from: None, from: None,
id: Some("aacea".to_string()), id: Some("aacea".to_string()),
to: Some(Jid { to: Some(Jid {
name: Some(Name("nikita".into())), name: Some(Name("chelik".into())),
server: Server("vlnv.dev".into()), server: Server("xmpp.ru".into()),
resource: None resource: None
}), }),
r#type: MessageType::Chat, r#type: MessageType::Chat,

View File

@ -3,6 +3,7 @@
pub mod bind; pub mod bind;
pub mod client; pub mod client;
pub mod disco; pub mod disco;
pub mod mam;
pub mod muc; pub mod muc;
mod prelude; mod prelude;
pub mod roster; pub mod roster;

View File

@ -0,0 +1,225 @@
use anyhow::{anyhow, Result};
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::name::{Namespace, ResolveResult};
use std::io::Read;
use crate::xml::*;
pub const MAM_XMLNS: &'static str = "urn:xmpp:mam:2";
pub const DATA_XMLNS: &'static str = "jabber:x:data";
pub const RESULT_SET_XMLNS: &'static str = "http://jabber.org/protocol/rsm";
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct MessageArchiveRequest {
pub x: Option<X>,
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct X {
pub fields: Vec<Field>,
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Field {
pub values: Vec<String>,
}
// Message archive response styled as a result set.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Fin {
pub set: Set,
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Set {
pub count: Option<i32>,
}
impl ToXml for Fin {
fn serialize(&self, events: &mut Vec<Event<'static>>) {
let fin_bytes = BytesStart::new(format!(r#"fin xmlns="{}" complete=True"#, MAM_XMLNS));
let set_bytes = BytesStart::new(format!(r#"set xmlns="{}""#, RESULT_SET_XMLNS));
events.push(Event::Start(fin_bytes));
events.push(Event::Start(set_bytes));
if let &Some(count) = &self.set.count {
events.push(Event::Start(BytesStart::new("count")));
events.push(Event::Text(BytesText::new(count.to_string().as_str()).into_owned()));
events.push(Event::End(BytesEnd::new("count")));
}
events.push(Event::End(BytesEnd::new("set")));
events.push(Event::End(BytesEnd::new("fin")));
}
}
impl FromXmlTag for X {
const NAME: &'static str = "x";
const NS: &'static str = DATA_XMLNS;
}
impl FromXmlTag for MessageArchiveRequest {
const NAME: &'static str = "query";
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(_) => {
// 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:?}")),
}
}
}
}
}
impl FromXml for MessageArchiveRequest {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(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:?}")),
};
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"));
};
if ns != MAM_XMLNS.as_bytes() {
return Err(anyhow!("Incorrect namespace"));
}
(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) })
}
_ => Err(anyhow!("Unexpected XML event: {event:?}")),
}
}
}
}
impl MessageArchiveRequest {}
#[cfg(test)]
mod tests {
use super::*;
use crate::bind::{Jid, Name, Server};
use crate::client::{Iq, IqType};
#[test]
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>"#;
let result: Iq<MessageArchiveRequest> = parse(input).unwrap();
assert_eq!(
result,
Iq {
from: None,
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(),
to: None,
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()],
},
]
})
},
}
);
}
#[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())
}
}

View File

@ -2,7 +2,7 @@ use quick_xml::events::{BytesStart, Event};
use crate::xml::*; use crate::xml::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use quick_xml::name::ResolveResult; use quick_xml::name::{Namespace, ResolveResult};
pub const XMLNS: &'static str = "jabber:iq:roster"; pub const XMLNS: &'static str = "jabber:iq:roster";
@ -14,6 +14,9 @@ impl FromXml for RosterQuery {
fn parse() -> Self::P { fn parse() -> Self::P {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> { |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let ResolveResult::Bound(Namespace(ns)) = namespace else {
return Err(anyhow!("No namespace provided"));
};
match event { match event {
Event::Start(_) => (), Event::Start(_) => (),
Event::Empty(_) => return Ok(RosterQuery), Event::Empty(_) => return Ok(RosterQuery),
@ -38,3 +41,39 @@ impl ToXml for RosterQuery {
events.push(Event::Empty(BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS)))); events.push(Event::Empty(BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS))));
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::bind::{Jid, Name, Resource, Server};
use crate::client::{Iq, IqType};
#[test]
fn test_parse() {
let input =
r#"<iq from='juliet@example.com/balcony' id='bv1bs71f' type='get'><query xmlns='jabber:iq:roster'/></iq>"#;
let result: Iq<RosterQuery> = parse(input).unwrap();
assert_eq!(
result,
Iq {
from: Option::from(Jid {
name: Option::from(Name("juliet".into())),
server: Server("example.com".into()),
resource: Option::from(Resource("balcony".into())),
}),
id: "bv1bs71f".to_string(),
to: None,
r#type: IqType::Get,
body: RosterQuery,
}
)
}
#[test]
fn test_missing_namespace() {
let input = r#"<iq from='juliet@example.com/balcony' id='bv1bs71f' type='get'><query/></iq>"#;
assert!(parse::<Iq<RosterQuery>>(input).is_err());
}
}

View File

@ -170,14 +170,14 @@ mod test {
#[tokio::test] #[tokio::test]
async fn client_stream_start_correct_parse() { async fn client_stream_start_correct_parse() {
let input = r###"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="vlnv.dev" version="1.0" xmlns="jabber:client" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">"###; let input = r###"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="xmpp.ru" version="1.0" xmlns="jabber:client" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">"###;
let mut reader = NsReader::from_reader(input.as_bytes()); let mut reader = NsReader::from_reader(input.as_bytes());
let mut buf = vec![]; let mut buf = vec![];
let res = ClientStreamStart::parse(&mut reader, &mut buf).await.unwrap(); let res = ClientStreamStart::parse(&mut reader, &mut buf).await.unwrap();
assert_eq!( assert_eq!(
res, res,
ClientStreamStart { ClientStreamStart {
to: "vlnv.dev".to_owned(), to: "xmpp.ru".to_owned(),
lang: Some("en".to_owned()), lang: Some("en".to_owned()),
version: "1.0".to_owned() version: "1.0".to_owned()
} }
@ -187,12 +187,12 @@ mod test {
#[tokio::test] #[tokio::test]
async fn server_stream_start_write() { async fn server_stream_start_write() {
let input = ServerStreamStart { let input = ServerStreamStart {
from: "vlnv.dev".to_owned(), from: "xmpp.ru".to_owned(),
lang: "en".to_owned(), lang: "en".to_owned(),
id: "stream_id".to_owned(), id: "stream_id".to_owned(),
version: "1.0".to_owned(), version: "1.0".to_owned(),
}; };
let expected = r###"<stream:stream from="vlnv.dev" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en" id="stream_id">"###; let expected = r###"<stream:stream from="xmpp.ru" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en" id="stream_id">"###;
let mut output: Vec<u8> = vec![]; let mut output: Vec<u8> = vec![];
let mut writer = Writer::new(&mut output); let mut writer = Writer::new(&mut output);
input.write_xml(&mut writer).await.unwrap(); input.write_xml(&mut writer).await.unwrap();