From e1f2e2653a9c14409a04bc4fcd7b10ce842871ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 7 Sep 2020 11:41:40 +0200 Subject: [PATCH] xmpp: re-add bits that weren't moved from wait_for_events to handle_iq MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- xmpp/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index 758cda5b..82c5df4e 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -236,7 +236,12 @@ impl Agent { presence } - async fn handle_iq(&mut self, iq: Iq) -> () { + async fn handle_iq(&mut self, iq: Iq) -> Vec { + let mut events = vec![]; + let from = iq + .from + .clone() + .unwrap_or_else(|| self.client.bound_jid().unwrap().clone()); if let IqType::Get(payload) = iq.payload { if payload.is("query", ns::DISCO_INFO) { let query = DiscoInfoQuery::try_from(payload); @@ -275,7 +280,33 @@ impl Agent { .into(); let _ = self.client.send_stanza(iq).await; } + } else if let IqType::Result(Some(payload)) = iq.payload { + // TODO: move private iqs like this one somewhere else, for + // security reasons. + if payload.is("query", ns::ROSTER) && iq.from.is_none() { + let roster = Roster::try_from(payload).unwrap(); + for item in roster.items.into_iter() { + events.push(Event::ContactAdded(item)); + } + } else if payload.is("pubsub", ns::PUBSUB) { + let new_events = pubsub::handle_iq_result(&from, payload); + events.extend(new_events); + } + } else if let IqType::Set(_) = iq.payload { + // We MUST answer unhandled set iqs with a service-unavailable error. + let error = StanzaError::new( + ErrorType::Cancel, + DefinedCondition::ServiceUnavailable, + "en", + "No handler defined for this kind of iq.", + ); + let iq = Iq::from_error(iq.id, error) + .with_to(iq.from.unwrap()) + .into(); + let _ = self.client.send_stanza(iq).await; } + + events } async fn handle_message(&mut self, message: Message) -> Vec { @@ -363,7 +394,8 @@ impl Agent { TokioXmppEvent::Stanza(elem) => { if elem.is("iq", "jabber:client") { let iq = Iq::try_from(elem).unwrap(); - self.handle_iq(iq).await; + let new_events = self.handle_iq(iq).await; + events.extend(new_events); } else if elem.is("message", "jabber:client") { let message = Message::try_from(elem).unwrap(); let new_events = self.handle_message(message).await;