2023-09-30 23:34:35 +00:00
|
|
|
use anyhow::anyhow;
|
2023-03-11 17:36:38 +00:00
|
|
|
use derive_more::From;
|
|
|
|
use quick_xml::events::Event;
|
|
|
|
use quick_xml::name::{Namespace, ResolveResult};
|
|
|
|
|
2023-09-30 23:34:35 +00:00
|
|
|
use lavina_core::prelude::*;
|
2023-09-30 15:43:46 +00:00
|
|
|
use proto_xmpp::bind::BindRequest;
|
|
|
|
use proto_xmpp::client::{Iq, Message, Presence};
|
|
|
|
use proto_xmpp::disco::{InfoQuery, ItemQuery};
|
|
|
|
use proto_xmpp::roster::RosterQuery;
|
|
|
|
use proto_xmpp::session::Session;
|
|
|
|
use proto_xmpp::xml::*;
|
2023-03-11 17:36:38 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, From)]
|
|
|
|
pub enum IqClientBody {
|
|
|
|
Bind(BindRequest),
|
2023-03-12 13:15:13 +00:00
|
|
|
Session(Session),
|
2023-03-12 21:50:28 +00:00
|
|
|
Roster(RosterQuery),
|
2023-03-27 21:52:31 +00:00
|
|
|
DiscoInfo(InfoQuery),
|
|
|
|
DiscoItem(ItemQuery),
|
2023-03-21 00:16:02 +00:00
|
|
|
Unknown(Ignore),
|
2023-03-11 17:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromXml for IqClientBody {
|
2023-03-23 00:37:02 +00:00
|
|
|
type P = impl Parser<Output = Result<Self>>;
|
2023-03-11 17:36:38 +00:00
|
|
|
|
|
|
|
fn parse() -> Self::P {
|
2023-03-23 00:37:02 +00:00
|
|
|
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
|
|
|
let bytes = match event {
|
|
|
|
Event::Start(bytes) => bytes,
|
|
|
|
Event::Empty(bytes) => bytes,
|
2023-09-30 23:34:35 +00:00
|
|
|
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
2023-03-23 00:37:02 +00:00
|
|
|
};
|
|
|
|
let name = bytes.name();
|
|
|
|
match_parser!(name, namespace, event;
|
|
|
|
BindRequest,
|
|
|
|
Session,
|
|
|
|
RosterQuery,
|
2023-03-27 21:52:31 +00:00
|
|
|
InfoQuery,
|
|
|
|
ItemQuery,
|
2023-03-23 00:37:02 +00:00
|
|
|
{
|
|
|
|
delegate_parsing!(Ignore, namespace, event).into()
|
|
|
|
}
|
|
|
|
)
|
2023-03-11 17:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 23:30:48 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, From)]
|
|
|
|
pub enum ClientPacket {
|
|
|
|
Iq(Iq<IqClientBody>),
|
2023-10-01 23:16:25 +00:00
|
|
|
Message(Message<Ignore>),
|
2023-03-20 16:25:14 +00:00
|
|
|
Presence(Presence<Ignore>),
|
2023-04-05 16:57:35 +00:00
|
|
|
StreamEnd,
|
2023-03-11 23:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromXml for ClientPacket {
|
2023-03-23 00:37:02 +00:00
|
|
|
type P = impl Parser<Output = Result<Self>>;
|
2023-03-11 23:30:48 +00:00
|
|
|
|
|
|
|
fn parse() -> Self::P {
|
2023-03-23 00:37:02 +00:00
|
|
|
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
2023-04-05 16:57:35 +00:00
|
|
|
match event {
|
2023-04-09 21:31:43 +00:00
|
|
|
Event::Start(bytes) | Event::Empty(bytes) => {
|
2023-04-05 16:57:35 +00:00
|
|
|
let name = bytes.name();
|
|
|
|
match_parser!(name, namespace, event;
|
|
|
|
Iq::<IqClientBody>,
|
|
|
|
Presence::<Ignore>,
|
2023-10-01 23:16:25 +00:00
|
|
|
Message::<Ignore>,
|
2023-04-05 16:57:35 +00:00
|
|
|
{
|
2023-09-30 23:34:35 +00:00
|
|
|
Err(anyhow!(
|
2023-04-05 16:57:35 +00:00
|
|
|
"Unexpected XML event of name {:?} in namespace {:?}",
|
|
|
|
name,
|
|
|
|
namespace
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
2023-03-23 00:37:02 +00:00
|
|
|
}
|
2023-04-05 16:57:35 +00:00
|
|
|
Event::End(bytes) => {
|
|
|
|
let name = bytes.name();
|
|
|
|
if name.local_name().as_ref() == b"stream" {
|
|
|
|
return Ok(ClientPacket::StreamEnd);
|
|
|
|
} else {
|
2023-09-30 23:34:35 +00:00
|
|
|
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
2023-04-05 16:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2023-09-30 23:34:35 +00:00
|
|
|
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
2023-04-05 16:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-11 23:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|