aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Queseth <eskilq@kth.se>2021-05-19 00:20:33 +0200
committerEskil Queseth <eskilq@kth.se>2021-05-19 00:20:33 +0200
commit7ac57f3803bcf0f357ee307a6f0daf0783efbf92 (patch)
treedb2be03071cb4efd4802d8c274f7e61b4c62f7e6
parent6fa328db646a0e1c33b883d387ad95ec971cf2e0 (diff)
downloadmum-7ac57f3803bcf0f357ee307a6f0daf0783efbf92.tar.gz
add backend support for sending messages
-rw-r--r--mumd/src/state.rs94
-rw-r--r--mumd/src/state/channel.rs2
2 files changed, 56 insertions, 40 deletions
diff --git a/mumd/src/state.rs b/mumd/src/state.rs
index 0f608d7..bffc082 100644
--- a/mumd/src/state.rs
+++ b/mumd/src/state.rs
@@ -14,9 +14,8 @@ use mumble_protocol::control::msgs;
use mumble_protocol::control::ControlPacket;
use mumble_protocol::ping::PongPacket;
use mumble_protocol::voice::Serverbound;
-use mumlib::command::{Command, CommandResponse};
+use mumlib::command::{Command, CommandResponse, MessageTarget};
use mumlib::config::Config;
-use mumlib::error::ChannelIdentifierError;
use mumlib::Error;
use crate::state::user::UserDiff;
use std::net::{SocketAddr, ToSocketAddrs};
@@ -99,43 +98,9 @@ impl State {
return now!(Err(Error::Disconnected));
}
- let channels = self.server().unwrap().channels();
-
- let matches = channels
- .iter()
- .map(|e| (e.0, e.1.path(channels)))
- .filter(|e| e.1.ends_with(&channel_identifier))
- .collect::<Vec<_>>();
- let id = match matches.len() {
- 0 => {
- let soft_matches = channels
- .iter()
- .map(|e| (e.0, e.1.path(channels).to_lowercase()))
- .filter(|e| e.1.ends_with(&channel_identifier.to_lowercase()))
- .collect::<Vec<_>>();
- match soft_matches.len() {
- 0 => {
- return now!(Err(Error::ChannelIdentifierError(
- channel_identifier,
- ChannelIdentifierError::Invalid
- )))
- }
- 1 => *soft_matches.get(0).unwrap().0,
- _ => {
- return now!(Err(Error::ChannelIdentifierError(
- channel_identifier,
- ChannelIdentifierError::Invalid
- )))
- }
- }
- }
- 1 => *matches.get(0).unwrap().0,
- _ => {
- return now!(Err(Error::ChannelIdentifierError(
- channel_identifier,
- ChannelIdentifierError::Ambiguous
- )))
- }
+ let id = match self.server().unwrap().channel_name(&channel_identifier) {
+ Ok((id, _)) => id,
+ Err(e) => return now!(Err(Error::ChannelIdentifierError(channel_identifier, e))),
};
let mut msg = msgs::UserState::new();
@@ -438,6 +403,57 @@ impl State {
now!(Ok(Some(CommandResponse::PastMessages { messages })))
}
+ Command::SendMessage { message, targets } => {
+ if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected(_)) {
+ return now!(Err(Error::Disconnected));
+ }
+
+ let mut msg = msgs::TextMessage::new();
+
+ msg.set_message(message);
+
+ for target in targets {
+ match target {
+ MessageTarget::Channel { recursive, name } => {
+ let channel_id = self
+ .server()
+ .unwrap()
+ .channel_name(&name);
+
+ let channel_id = match channel_id {
+ Ok(id) => id,
+ Err(e) => return now!(Err(Error::ChannelIdentifierError(name, e))),
+ }.0;
+
+ if recursive {
+ msg.mut_tree_id()
+ } else {
+ msg.mut_channel_id()
+ }.push(channel_id);
+ }
+ MessageTarget::User { name } => {
+ let id = self
+ .server()
+ .unwrap()
+ .users()
+ .iter()
+ .find(|(_, user)| user.name() == &name)
+ .map(|(e, _)| *e);
+
+ let id = match id {
+ Some(id) => id,
+ None => return now!(Err(Error::InvalidUsername(name))),
+ };
+
+ msg.mut_session().push(id);
+ }
+ }
+ }
+
+ packet_sender.send(msg.into()).unwrap();
+
+ now!(Ok(None))
+ }
}
}
diff --git a/mumd/src/state/channel.rs b/mumd/src/state/channel.rs
index 5b6d669..f58ed15 100644
--- a/mumd/src/state/channel.rs
+++ b/mumd/src/state/channel.rs
@@ -4,7 +4,7 @@ use mumble_protocol::control::msgs;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
-#[derive(Clone, Debug, Deserialize, Serialize)]
+#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Channel {
description: Option<String>,
links: Vec<u32>,