Refactor and add message command

This commit is contained in:
Tobias Reisinger 2023-11-26 22:30:59 +01:00
parent e87bed00a2
commit fa959136be
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 370 additions and 263 deletions

View file

@ -1,7 +1,8 @@
use crate::utils::SendTextMessageTarget;
use telnet::Event::Data;
use telnet::Telnet;
use crate::parameter::ParameterList;
use crate::parameter::{Parameter, ParameterList};
use crate::response::Response;
fn to_single_response(resp: Response) -> Response {
@ -41,6 +42,7 @@ fn read_response_buffer(connection: &mut Telnet, buffer: &mut String) -> Result<
}
fn send_command(connection: &mut Telnet, command: &str, skip_ok: bool) -> Result<Response, String> {
let command = format!("{}\n\r", command);
connection.write(command.as_bytes()).map_err(|_| "Failed to write to Teamspeak.")?;
read_response(connection, skip_ok, String::new())
@ -68,40 +70,40 @@ fn read_response(connection: &mut Telnet, skip_ok: bool, mut buffer: String) ->
}
pub fn login(connection: &mut Telnet, apikey: &str) -> Result<Response, String> {
send_command(connection, &format!("auth apikey={}\n", apikey), false)
send_command(connection, &format!("auth apikey={}", apikey), false)
}
#[allow(dead_code)]
pub fn set_name(connection: &mut Telnet, name: &str) -> Result<Response, String> {
send_command(connection, &format!("clientupdate client_nickname={}\n", name), true)
send_command(connection, &format!("clientupdate client_nickname={}", name), true)
}
pub fn channellist(connection: &mut Telnet) -> Result<Response, String> {
send_command(connection, "channellist\n", true)
send_command(connection, "channellist", true)
}
pub fn clientlist(connection: &mut Telnet) -> Result<Response, String> {
send_command(connection, "clientlist\n", true)
send_command(connection, "clientlist", true)
}
pub fn whoami(connection: &mut Telnet) -> Result<Response, String> {
send_command(connection, "whoami\n", true).map(to_single_response)
send_command(connection, "whoami", true).map(to_single_response)
}
pub fn clientmove(connection: &mut Telnet, cid: &i32, clid_list: Vec<&i32>) -> Result<Response, String> {
let clid_str = clid_list
.iter()
.map(|clid| format!("clid={}", clid))
.collect::<Vec<String>>()
.join("|");
send_command(connection, &format!("clientmove cid={} {}\n", cid, clid_str), false)
let clid_param_list = clid_list
.into_iter()
.map(|clid| Parameter::new("clid", &clid.to_string()))
.collect::<Vec<Parameter>>();
let clid_str = Parameter::list_to_string_sep(clid_param_list, "|");
send_command(connection, &format!("clientmove cid={} {}", cid, clid_str), false)
}
pub fn clientupdate(connection: &mut Telnet, parameters: &ParameterList) -> Result<Response, String> {
let parameters_str = parameters
.iter()
.map(|param| format!("{}={}", param.name, param.value))
.collect::<Vec<String>>()
.join(" ");
send_command(connection, &format!("clientupdate {}\n", parameters_str), false)
}
pub fn clientupdate(connection: &mut Telnet, parameters: ParameterList) -> Result<Response, String> {
let parameters_str = Parameter::list_to_string(parameters);
send_command(connection, &format!("clientupdate {}", parameters_str), false)
}
pub fn sendtextmessage(connection: &mut Telnet, target: SendTextMessageTarget, msg: String) -> Result<Response, String> {
let msg = String::from(Parameter::new("msg", &msg));
send_command(connection, &format!("sendtextmessage {} {}", msg, String::from(target)), false) }