Add fetch-channel command

This commit is contained in:
Tobias Reisinger 2023-11-17 17:32:15 +01:00
parent 970c1ee2c2
commit 244a7073fe
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
9 changed files with 183 additions and 54 deletions

View file

@ -4,7 +4,7 @@ use std::time::Duration;
use telnet::Event::TimedOut;
use crate::{commands, parameter};
use crate::parameter::ParameterList;
use crate::parameter::{parameter_list_find_all, ParameterList};
use crate::response::Response;
use crate::response_classes::{ResponseChannel, ResponseClient};
@ -121,6 +121,24 @@ pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result<
}
}
pub fn get_channel_clients(connection: &mut Telnet, channel: &ResponseChannel) -> Result<Vec<ResponseClient>, String> {
match commands::clientlist(connection) {
Ok(response) => {
match response {
Response::DataList(parameter_lists) => {
let mut clients: Vec<ResponseClient> = Vec::new();
for client_params in parameter_list_find_all(&parameter_lists, "cid", &channel.cid.to_string(), true) {
clients.push(ResponseClient::from(client_params));
}
Ok(clients)
}
_ => Err(String::from("Received unexpected response from Teamspeak."))
}
}
Err(msg) => Err(msg)
}
}
fn get_self_clid(connection: &mut Telnet) -> Result<String, String> {
match commands::whoami(connection) {
Ok(response) => {
@ -168,6 +186,15 @@ pub fn fetch_client(connection: &mut Telnet, clients: &[ResponseClient]) -> Resu
commands::clientmove(connection, &cid, clid_list)
}
pub fn fetch_channel(connection: &mut Telnet, channel: ResponseChannel) -> Result<Response, String> {
let cid = find_self(connection)?.cid;
let clients = get_channel_clients(connection, &channel)?;
let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect();
commands::clientmove(connection, &cid, clid_list)
}
pub fn move_client(connection: &mut Telnet, channel: &ResponseChannel, clients: &[ResponseClient]) -> Result<Response, String> {
let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect();