mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Move handle_message to message::receive module
This commit is contained in:
parent
0e31739934
commit
12e5299abf
3 changed files with 82 additions and 75 deletions
|
@ -57,7 +57,7 @@ pub async fn wait_for_events(agent: &mut Agent) -> Option<Vec<Event>> {
|
||||||
events.extend(new_events);
|
events.extend(new_events);
|
||||||
} else if elem.is("message", "jabber:client") {
|
} else if elem.is("message", "jabber:client") {
|
||||||
let message = Message::try_from(elem).unwrap();
|
let message = Message::try_from(elem).unwrap();
|
||||||
let new_events = message::handle_message(agent, message).await;
|
let new_events = message::receive::handle_message(agent, message).await;
|
||||||
events.extend(new_events);
|
events.extend(new_events);
|
||||||
} else if elem.is("presence", "jabber:client") {
|
} else if elem.is("presence", "jabber:client") {
|
||||||
let presence = Presence::try_from(elem).unwrap();
|
let presence = Presence::try_from(elem).unwrap();
|
||||||
|
|
|
@ -4,77 +4,4 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use tokio_xmpp::{
|
pub mod receive;
|
||||||
parsers::{
|
|
||||||
message::{Message, MessageType},
|
|
||||||
muc::user::MucUser,
|
|
||||||
ns,
|
|
||||||
},
|
|
||||||
Jid,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{pubsub, Agent, Event};
|
|
||||||
|
|
||||||
pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec<Event> {
|
|
||||||
let mut events = vec![];
|
|
||||||
let from = message.from.clone().unwrap();
|
|
||||||
let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
|
|
||||||
match message.get_best_body(langs) {
|
|
||||||
Some((_lang, body)) => match message.type_ {
|
|
||||||
MessageType::Groupchat => {
|
|
||||||
let event = match from.clone() {
|
|
||||||
Jid::Full(full) => Event::RoomMessage(
|
|
||||||
message.id.clone(),
|
|
||||||
from.to_bare(),
|
|
||||||
full.resource_str().to_owned(),
|
|
||||||
body.clone(),
|
|
||||||
),
|
|
||||||
Jid::Bare(bare) => {
|
|
||||||
Event::ServiceMessage(message.id.clone(), bare, body.clone())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
events.push(event)
|
|
||||||
}
|
|
||||||
MessageType::Chat | MessageType::Normal => {
|
|
||||||
let mut found_special_message = false;
|
|
||||||
|
|
||||||
for payload in &message.payloads {
|
|
||||||
if let Ok(_) = MucUser::try_from(payload.clone()) {
|
|
||||||
let event = match from.clone() {
|
|
||||||
Jid::Bare(bare) => {
|
|
||||||
// TODO: Can a service message be of type Chat/Normal and not Groupchat?
|
|
||||||
warn!("Received misformed MessageType::Chat in muc#user namespace from a bare JID.");
|
|
||||||
Event::ServiceMessage(message.id.clone(), bare, body.clone())
|
|
||||||
}
|
|
||||||
Jid::Full(full) => Event::RoomPrivateMessage(
|
|
||||||
message.id.clone(),
|
|
||||||
full.to_bare(),
|
|
||||||
full.resource_str().to_owned(),
|
|
||||||
body.clone(),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
found_special_message = true;
|
|
||||||
events.push(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found_special_message {
|
|
||||||
let event =
|
|
||||||
Event::ChatMessage(message.id.clone(), from.to_bare(), body.clone());
|
|
||||||
events.push(event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
None => (),
|
|
||||||
}
|
|
||||||
for child in message.payloads {
|
|
||||||
if child.is("event", ns::PUBSUB_EVENT) {
|
|
||||||
let new_events = pubsub::handle_event(&from, child, agent).await;
|
|
||||||
events.extend(new_events);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
events
|
|
||||||
}
|
|
||||||
|
|
80
xmpp/src/message/receive.rs
Normal file
80
xmpp/src/message/receive.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
// Copyright (c) 2023 xmpp-rs contributors.
|
||||||
|
//
|
||||||
|
// 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/.
|
||||||
|
|
||||||
|
use tokio_xmpp::{
|
||||||
|
parsers::{
|
||||||
|
message::{Message, MessageType},
|
||||||
|
muc::user::MucUser,
|
||||||
|
ns,
|
||||||
|
},
|
||||||
|
Jid,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{pubsub, Agent, Event};
|
||||||
|
|
||||||
|
pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec<Event> {
|
||||||
|
let mut events = vec![];
|
||||||
|
let from = message.from.clone().unwrap();
|
||||||
|
let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect();
|
||||||
|
match message.get_best_body(langs) {
|
||||||
|
Some((_lang, body)) => match message.type_ {
|
||||||
|
MessageType::Groupchat => {
|
||||||
|
let event = match from.clone() {
|
||||||
|
Jid::Full(full) => Event::RoomMessage(
|
||||||
|
message.id.clone(),
|
||||||
|
from.to_bare(),
|
||||||
|
full.resource_str().to_owned(),
|
||||||
|
body.clone(),
|
||||||
|
),
|
||||||
|
Jid::Bare(bare) => {
|
||||||
|
Event::ServiceMessage(message.id.clone(), bare, body.clone())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
events.push(event)
|
||||||
|
}
|
||||||
|
MessageType::Chat | MessageType::Normal => {
|
||||||
|
let mut found_special_message = false;
|
||||||
|
|
||||||
|
for payload in &message.payloads {
|
||||||
|
if let Ok(_) = MucUser::try_from(payload.clone()) {
|
||||||
|
let event = match from.clone() {
|
||||||
|
Jid::Bare(bare) => {
|
||||||
|
// TODO: Can a service message be of type Chat/Normal and not Groupchat?
|
||||||
|
warn!("Received misformed MessageType::Chat in muc#user namespace from a bare JID.");
|
||||||
|
Event::ServiceMessage(message.id.clone(), bare, body.clone())
|
||||||
|
}
|
||||||
|
Jid::Full(full) => Event::RoomPrivateMessage(
|
||||||
|
message.id.clone(),
|
||||||
|
full.to_bare(),
|
||||||
|
full.resource_str().to_owned(),
|
||||||
|
body.clone(),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
found_special_message = true;
|
||||||
|
events.push(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found_special_message {
|
||||||
|
let event =
|
||||||
|
Event::ChatMessage(message.id.clone(), from.to_bare(), body.clone());
|
||||||
|
events.push(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
for child in message.payloads {
|
||||||
|
if child.is("event", ns::PUBSUB_EVENT) {
|
||||||
|
let new_events = pubsub::handle_event(&from, child, agent).await;
|
||||||
|
events.extend(new_events);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
events
|
||||||
|
}
|
Loading…
Reference in a new issue