parsers: port Iq to use the derive macros
This commit is contained in:
parent
8d8a19380a
commit
dc8c3eac4c
8 changed files with 486 additions and 312 deletions
|
|
@ -18,10 +18,7 @@ use std::sync::Mutex;
|
|||
use futures::Stream;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
use xmpp_parsers::{
|
||||
iq::{Iq, IqType},
|
||||
stanza_error::StanzaError,
|
||||
};
|
||||
use xmpp_parsers::{iq::Iq, stanza_error::StanzaError};
|
||||
|
||||
use crate::{
|
||||
event::make_id,
|
||||
|
|
@ -40,11 +37,21 @@ pub enum IqRequest {
|
|||
Set(Element),
|
||||
}
|
||||
|
||||
impl From<IqRequest> for IqType {
|
||||
fn from(other: IqRequest) -> IqType {
|
||||
match other {
|
||||
IqRequest::Get(v) => Self::Get(v),
|
||||
IqRequest::Set(v) => Self::Set(v),
|
||||
impl IqRequest {
|
||||
fn into_iq(self, from: Option<Jid>, to: Option<Jid>, id: String) -> Iq {
|
||||
match self {
|
||||
Self::Get(payload) => Iq::Get {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
payload,
|
||||
},
|
||||
Self::Set(payload) => Iq::Set {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
payload,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -59,11 +66,22 @@ pub enum IqResponse {
|
|||
Error(StanzaError),
|
||||
}
|
||||
|
||||
impl From<IqResponse> for IqType {
|
||||
fn from(other: IqResponse) -> IqType {
|
||||
match other {
|
||||
IqResponse::Result(v) => Self::Result(v),
|
||||
IqResponse::Error(v) => Self::Error(v),
|
||||
impl IqResponse {
|
||||
fn into_iq(self, from: Option<Jid>, to: Option<Jid>, id: String) -> Iq {
|
||||
match self {
|
||||
Self::Error(error) => Iq::Error {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
error,
|
||||
payload: None,
|
||||
},
|
||||
Self::Result(payload) => Iq::Result {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
payload,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -251,22 +269,28 @@ impl IqResponseTracker {
|
|||
/// Returns the IQ stanza unharmed if it is not an IQ response matching
|
||||
/// any request which is still being tracked.
|
||||
pub fn handle_iq(&self, iq: Iq) -> ControlFlow<(), Iq> {
|
||||
let payload = match iq.payload {
|
||||
IqType::Error(error) => IqResponse::Error(error),
|
||||
IqType::Result(result) => IqResponse::Result(result),
|
||||
let (from, to, id, payload) = match iq {
|
||||
Iq::Error {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
error,
|
||||
payload: _,
|
||||
} => (from, to, id, IqResponse::Error(error)),
|
||||
Iq::Result {
|
||||
from,
|
||||
to,
|
||||
id,
|
||||
payload,
|
||||
} => (from, to, id, IqResponse::Result(payload)),
|
||||
_ => return ControlFlow::Continue(iq),
|
||||
};
|
||||
let key = (iq.from, iq.id);
|
||||
let key = (from, id);
|
||||
let mut map = self.map.lock().unwrap();
|
||||
match map.remove(&key) {
|
||||
None => {
|
||||
log::trace!("not handling IQ response from {:?} with id {:?}: no active tracker for this tuple", key.0, key.1);
|
||||
ControlFlow::Continue(Iq {
|
||||
from: key.0,
|
||||
id: key.1,
|
||||
to: iq.to,
|
||||
payload: payload.into(),
|
||||
})
|
||||
ControlFlow::Continue(payload.into_iq(key.0, to, key.1))
|
||||
}
|
||||
Some(sink) => {
|
||||
sink.complete(payload);
|
||||
|
|
@ -298,14 +322,6 @@ impl IqResponseTracker {
|
|||
inner: rx,
|
||||
};
|
||||
map.insert(key.clone(), sink);
|
||||
(
|
||||
Iq {
|
||||
from,
|
||||
to: key.0,
|
||||
id: key.1,
|
||||
payload: req.into(),
|
||||
},
|
||||
token,
|
||||
)
|
||||
(req.into_iq(from, key.0, key.1), token)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,10 +43,11 @@ impl Stanza {
|
|||
pub fn ensure_id(&mut self) -> &str {
|
||||
match self {
|
||||
Self::Iq(iq) => {
|
||||
if iq.id.is_empty() {
|
||||
iq.id = make_id();
|
||||
let id = iq.id_mut();
|
||||
if id.is_empty() {
|
||||
*id = make_id();
|
||||
}
|
||||
&iq.id
|
||||
id
|
||||
}
|
||||
Self::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(),
|
||||
Self::Presence(presence) => presence.id.get_or_insert_with(make_id),
|
||||
|
|
@ -97,7 +98,7 @@ impl TryFrom<Stanza> for Presence {
|
|||
impl TryFrom<Stanza> for Iq {
|
||||
type Error = Stanza;
|
||||
|
||||
fn try_from(other: Stanza) -> Result<Self, Self::Error> {
|
||||
fn try_from(other: Stanza) -> Result<Self, Stanza> {
|
||||
match other {
|
||||
Stanza::Iq(st) => Ok(st),
|
||||
other => Err(other),
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use futures::{ready, Sink, Stream};
|
|||
|
||||
use xmpp_parsers::{
|
||||
bind::{BindQuery, BindResponse},
|
||||
iq::{Iq, IqType},
|
||||
iq::Iq,
|
||||
jid::{FullJid, Jid},
|
||||
sm,
|
||||
stream_error::{DefinedCondition, StreamError},
|
||||
|
|
@ -200,31 +200,34 @@ impl NegotiationState {
|
|||
|
||||
match item {
|
||||
Ok(XmppStreamElement::Stanza(data)) => match data {
|
||||
Stanza::Iq(iq) if iq.id == BIND_REQ_ID => {
|
||||
let error = match iq.payload {
|
||||
IqType::Result(Some(payload)) => {
|
||||
match BindResponse::try_from(payload) {
|
||||
Ok(v) => {
|
||||
let bound_jid = v.into();
|
||||
if *sm_supported {
|
||||
*self = Self::SendSmRequest {
|
||||
Stanza::Iq(iq) if iq.id() == BIND_REQ_ID => {
|
||||
let error = match iq {
|
||||
Iq::Result {
|
||||
payload: Some(payload),
|
||||
..
|
||||
} => match BindResponse::try_from(payload) {
|
||||
Ok(v) => {
|
||||
let bound_jid = v.into();
|
||||
if *sm_supported {
|
||||
*self = Self::SendSmRequest {
|
||||
sm_state: None,
|
||||
bound_jid: Some(bound_jid),
|
||||
};
|
||||
return Poll::Ready(Continue(None));
|
||||
} else {
|
||||
return Poll::Ready(Break(
|
||||
NegotiationResult::StreamReset {
|
||||
sm_state: None,
|
||||
bound_jid: Some(bound_jid),
|
||||
};
|
||||
return Poll::Ready(Continue(None));
|
||||
} else {
|
||||
return Poll::Ready(Break(
|
||||
NegotiationResult::StreamReset {
|
||||
sm_state: None,
|
||||
bound_jid: Jid::from(bound_jid),
|
||||
},
|
||||
));
|
||||
}
|
||||
bound_jid: Jid::from(bound_jid),
|
||||
},
|
||||
));
|
||||
}
|
||||
Err(e) => e.to_string(),
|
||||
}
|
||||
Err(e) => e.to_string(),
|
||||
},
|
||||
Iq::Result { payload: None, .. } => {
|
||||
"Bind response has no payload".to_owned()
|
||||
}
|
||||
IqType::Result(None) => "Bind response has no payload".to_owned(),
|
||||
_ => "Unexpected IQ type in response to bind request".to_owned(),
|
||||
};
|
||||
log::warn!("Received IQ matching the bind request, but parsing failed ({error})! Emitting stream error.");
|
||||
|
|
|
|||
Loading…
Reference in a new issue