Refactor and add message command
This commit is contained in:
parent
e87bed00a2
commit
fa959136be
8 changed files with 370 additions and 263 deletions
237
src/utils.rs
237
src/utils.rs
|
|
@ -1,208 +1,39 @@
|
|||
use std::process::exit;
|
||||
use std::time::Duration;
|
||||
use crate::parameter::Parameter;
|
||||
|
||||
use telnet::Event::TimedOut;
|
||||
use telnet::Telnet;
|
||||
pub fn decode_value(value: &str) -> String {
|
||||
value
|
||||
.replace("\\s", " ")
|
||||
.replace("\\p", "|")
|
||||
}
|
||||
|
||||
use crate::{commands, parameter};
|
||||
use crate::parameter::{parameter_list_find_all, ParameterList};
|
||||
use crate::response::channel::ResponseChannel;
|
||||
use crate::response::client::ResponseClient;
|
||||
use crate::response::Response;
|
||||
pub fn encode_value(value: &str) -> String {
|
||||
value
|
||||
.replace(' ', "\\s")
|
||||
.replace('|', "\\p")
|
||||
}
|
||||
|
||||
pub fn skip_welcome(connection: &mut Telnet) {
|
||||
loop {
|
||||
let event_result = connection.read_timeout(Duration::from_millis(100));
|
||||
match event_result {
|
||||
Ok(event) => {
|
||||
if let TimedOut = event {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => println!("Failed to read from Teamspeak."),
|
||||
}
|
||||
pub enum SendTextMessageTarget {
|
||||
Client(i32),
|
||||
Channel,
|
||||
Server,
|
||||
}
|
||||
|
||||
impl From<SendTextMessageTarget> for String {
|
||||
fn from(value: SendTextMessageTarget) -> Self {
|
||||
let target_mode = match value {
|
||||
SendTextMessageTarget::Client(_) => "1",
|
||||
SendTextMessageTarget::Channel => "2",
|
||||
SendTextMessageTarget::Server => "3",
|
||||
};
|
||||
|
||||
let target = match value {
|
||||
SendTextMessageTarget::Client(id) => id.to_string(),
|
||||
_ => String::from("0"),
|
||||
};
|
||||
|
||||
Parameter::list_to_string(vec![
|
||||
Parameter::new("targetmode", target_mode),
|
||||
Parameter::new("target", &target)
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
pub fn login(connection: &mut Telnet) {
|
||||
// read api key from environment variable
|
||||
let apikey = std::env::var("TS3_CLIENT_API_KEY").unwrap_or_else(|_| {
|
||||
println!("No API key found in environment variable TS3_CLIENT_API_KEY.");
|
||||
exit(1);
|
||||
});
|
||||
|
||||
match commands::login(connection, &apikey) {
|
||||
Ok(_) => {}
|
||||
Err(msg) => {
|
||||
println!("Failed to authenticate with Teamspeak: {}", msg);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result<Vec<ResponseChannel>, String> {
|
||||
match commands::channellist(connection) {
|
||||
Ok(response) => {
|
||||
match response {
|
||||
Response::DataList(parameter_lists) => {
|
||||
let channels: Vec<ResponseChannel> = parameter_lists.iter()
|
||||
.map(|params| ResponseChannel::from(params.clone()))
|
||||
.collect();
|
||||
let mut channels = ResponseChannel::sort_list(channels);
|
||||
if !spacers {
|
||||
channels.retain(|c| !c.is_spacer());
|
||||
}
|
||||
Ok(channels)
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_channel(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<ResponseChannel>, String> {
|
||||
match commands::channellist(connection) {
|
||||
Ok(response) => {
|
||||
match response {
|
||||
Response::DataList(parameter_lists) => {
|
||||
match parameter::parameter_list_find(¶meter_lists, "channel_name", name, strict) {
|
||||
Some(params) => {
|
||||
Ok(Some(ResponseChannel::from(params)))
|
||||
}
|
||||
None => {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_clients(connection: &mut Telnet) -> Result<Vec<ResponseClient>, String> {
|
||||
match commands::clientlist(connection) {
|
||||
Ok(response) => {
|
||||
match response {
|
||||
Response::DataList(parameter_lists) => {
|
||||
let mut clients: Vec<ResponseClient> = parameter_lists.iter()
|
||||
.map(|params| ResponseClient::from(params.clone()))
|
||||
.collect();
|
||||
|
||||
clients.sort_by(|a, b| a.client_nickname.cmp(&b.client_nickname));
|
||||
|
||||
Ok(clients)
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<ResponseClient>, String> {
|
||||
match commands::clientlist(connection) {
|
||||
Ok(response) => {
|
||||
match response {
|
||||
Response::DataList(parameter_lists) => {
|
||||
match parameter::parameter_list_find(¶meter_lists, "client_nickname", name, strict) {
|
||||
Some(params) => {
|
||||
Ok(Some(ResponseClient::from(params)))
|
||||
}
|
||||
None => {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
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(¶meter_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) => {
|
||||
match response {
|
||||
Response::Data(params) => {
|
||||
match parameter::parameter_find(¶ms, "clid") {
|
||||
None => Err(String::from("Could not find clid in response from Teamspeak.")),
|
||||
Some(param) => Ok(param.value)
|
||||
}
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak for whoami."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_self(connection: &mut Telnet) -> Result<ResponseClient, String> {
|
||||
let clid = get_self_clid(connection)?;
|
||||
|
||||
match commands::clientlist(connection) {
|
||||
Ok(response) => {
|
||||
match response {
|
||||
Response::DataList(parameter_lists) => {
|
||||
match parameter::parameter_list_find(¶meter_lists, "clid", &clid, false) {
|
||||
Some(params) => {
|
||||
Ok(ResponseClient::from(params))
|
||||
}
|
||||
None => {
|
||||
Err(String::from("Could not find self in response from Teamspeak."))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(String::from("Received unexpected response from Teamspeak for clientlist."))
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch_client(connection: &mut Telnet, clients: &[ResponseClient]) -> Result<Response, String> {
|
||||
let cid = find_self(connection)?.cid;
|
||||
let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect();
|
||||
|
||||
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();
|
||||
|
||||
commands::clientmove(connection, &channel.cid, clid_list)
|
||||
}
|
||||
|
||||
pub fn update_client(connection: &mut Telnet, parameters: &ParameterList) -> Result<Response, String> {
|
||||
commands::clientupdate(connection, parameters)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue