2019-09-29 01:59:44 +00:00
|
|
|
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
use super::Agent;
|
2019-09-29 01:59:44 +00:00
|
|
|
use crate::Event;
|
|
|
|
use std::str::FromStr;
|
2023-12-31 06:17:29 +00:00
|
|
|
use tokio_xmpp::{
|
|
|
|
connect::ServerConnector,
|
|
|
|
parsers::{
|
|
|
|
bookmarks2::{self, Autojoin},
|
|
|
|
ns,
|
|
|
|
pubsub::event::PubSubEvent,
|
|
|
|
pubsub::pubsub::PubSub,
|
|
|
|
BareJid, Element, Jid,
|
|
|
|
},
|
2019-09-29 01:59:44 +00:00
|
|
|
};
|
|
|
|
|
2019-09-29 02:02:32 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
2019-09-29 01:59:44 +00:00
|
|
|
pub(crate) mod avatar;
|
|
|
|
|
2023-12-31 06:17:29 +00:00
|
|
|
pub(crate) async fn handle_event<C: ServerConnector>(
|
2024-01-18 14:18:33 +00:00
|
|
|
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] from: &Jid,
|
2023-12-31 06:17:29 +00:00
|
|
|
elem: Element,
|
2024-01-18 14:18:33 +00:00
|
|
|
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] agent: &mut Agent<C>,
|
2023-12-31 06:17:29 +00:00
|
|
|
) -> Vec<Event> {
|
2019-09-29 01:59:44 +00:00
|
|
|
let mut events = Vec::new();
|
2019-09-29 02:08:56 +00:00
|
|
|
let event = PubSubEvent::try_from(elem);
|
|
|
|
trace!("PubSub event: {:#?}", event);
|
|
|
|
match event {
|
2019-09-29 01:59:44 +00:00
|
|
|
Ok(PubSubEvent::PublishedItems { node, items }) => {
|
|
|
|
match node.0 {
|
2019-09-29 02:02:32 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::AVATAR_METADATA => {
|
2020-03-08 18:57:59 +00:00
|
|
|
let new_events =
|
|
|
|
avatar::handle_metadata_pubsub_event(&from, agent, items).await;
|
2019-09-29 01:59:44 +00:00
|
|
|
events.extend(new_events);
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::BOOKMARKS2 => {
|
2019-09-29 01:59:44 +00:00
|
|
|
// TODO: Check that our bare JID is the sender.
|
|
|
|
assert_eq!(items.len(), 1);
|
|
|
|
let item = items.clone().pop().unwrap();
|
|
|
|
let jid = BareJid::from_str(&item.id.clone().unwrap().0).unwrap();
|
|
|
|
let payload = item.payload.clone().unwrap();
|
2023-12-16 15:06:38 +00:00
|
|
|
match bookmarks2::Conference::try_from(payload) {
|
2019-09-29 01:59:44 +00:00
|
|
|
Ok(conference) => {
|
|
|
|
if conference.autojoin == Autojoin::True {
|
|
|
|
events.push(Event::JoinRoom(jid, conference));
|
|
|
|
} else {
|
|
|
|
events.push(Event::LeaveRoom(jid));
|
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
Err(err) => println!("not bookmark: {}", err),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
ref node => unimplemented!("node {}", node),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(PubSubEvent::RetractedItems { node, items }) => {
|
|
|
|
match node.0 {
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::BOOKMARKS2 => {
|
2019-09-29 01:59:44 +00:00
|
|
|
// TODO: Check that our bare JID is the sender.
|
|
|
|
assert_eq!(items.len(), 1);
|
|
|
|
let item = items.clone().pop().unwrap();
|
|
|
|
let jid = BareJid::from_str(&item.0).unwrap();
|
|
|
|
events.push(Event::LeaveRoom(jid));
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
ref node => unimplemented!("node {}", node),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(PubSubEvent::Purge { node }) => {
|
|
|
|
match node.0 {
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::BOOKMARKS2 => {
|
2019-09-29 01:59:44 +00:00
|
|
|
// TODO: Check that our bare JID is the sender.
|
|
|
|
events.push(Event::LeaveAllRooms);
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
ref node => unimplemented!("node {}", node),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
2023-12-04 01:01:19 +00:00
|
|
|
}
|
2023-12-03 13:37:46 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!("Error parsing PubSub event: {}", e);
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
2023-12-03 13:37:46 +00:00
|
|
|
_ => unimplemented!("PubSub event: {:#?}", event),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
|
|
|
events
|
|
|
|
}
|
|
|
|
|
2024-01-18 14:18:33 +00:00
|
|
|
pub(crate) fn handle_iq_result(
|
|
|
|
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] from: &Jid,
|
|
|
|
elem: Element,
|
|
|
|
) -> impl IntoIterator<Item = Event> {
|
2019-09-29 01:59:44 +00:00
|
|
|
let mut events = Vec::new();
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
2019-09-29 02:08:56 +00:00
|
|
|
trace!("PubSub: {:#?}", pubsub);
|
2019-09-29 01:59:44 +00:00
|
|
|
if let PubSub::Items(items) = pubsub {
|
2019-09-29 02:02:32 +00:00
|
|
|
match items.node.0.clone() {
|
|
|
|
#[cfg(feature = "avatars")]
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::AVATAR_DATA => {
|
2019-09-29 02:02:32 +00:00
|
|
|
let new_events = avatar::handle_data_pubsub_iq(&from, &items);
|
|
|
|
events.extend(new_events);
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2019-10-22 22:46:26 +00:00
|
|
|
ref node if node == ns::BOOKMARKS2 => {
|
2019-09-29 02:02:32 +00:00
|
|
|
events.push(Event::LeaveAllRooms);
|
|
|
|
for item in items.items {
|
|
|
|
let item = item.0;
|
|
|
|
let jid = BareJid::from_str(&item.id.clone().unwrap().0).unwrap();
|
|
|
|
let payload = item.payload.clone().unwrap();
|
2023-12-16 15:06:38 +00:00
|
|
|
match bookmarks2::Conference::try_from(payload) {
|
2019-09-29 02:02:32 +00:00
|
|
|
Ok(conference) => {
|
|
|
|
if let Autojoin::True = conference.autojoin {
|
|
|
|
events.push(Event::JoinRoom(jid, conference));
|
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2019-09-29 02:02:32 +00:00
|
|
|
Err(err) => panic!("Wrong payload type in bookmarks 2 item: {}", err),
|
|
|
|
}
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
2019-09-29 01:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
events
|
|
|
|
}
|