Refactor and add message command
This commit is contained in:
parent
e87bed00a2
commit
fa959136be
8 changed files with 370 additions and 263 deletions
src
213
src/wrappers.rs
Normal file
213
src/wrappers.rs
Normal file
|
@ -0,0 +1,213 @@
|
|||
use std::process::exit;
|
||||
use std::time::Duration;
|
||||
|
||||
use telnet::Event::TimedOut;
|
||||
use telnet::Telnet;
|
||||
|
||||
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;
|
||||
use crate::utils::SendTextMessageTarget;
|
||||
|
||||
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 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)
|
||||
}
|
||||
|
||||
pub fn send_text_message(connection: &mut Telnet, target: SendTextMessageTarget, msg: String) -> Result<Response, String> {
|
||||
commands::sendtextmessage(connection, target, msg)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue