aboutsummaryrefslogtreecommitdiffstats
path: root/mumctl/src/main.rs
blob: eaad1d1651da078f8a1bd456ea8860ec61d0da46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use clap::{App, AppSettings, Arg, Shell, SubCommand};
use colored::Colorize;
use ipc_channel::ipc::{self, IpcSender};
use log::*;
use mumlib::command::{Command, CommandResponse};
use mumlib::config;
use mumlib::setup_logger;
use mumlib::state::Channel;
use std::{fs, io, iter};

const INDENTATION: &str = "  ";

macro_rules! err_print {
    ($func:expr) => {
        if let Err(e) = $func {
            println!("{} {}", "error:".red(), e);
        }
    };
}

fn main() {
    setup_logger(io::stderr(), true);
    let mut config = config::read_default_cfg();

    let mut app = App::new("mumctl")
        .setting(AppSettings::ArgRequiredElseHelp)
        .subcommand(
            SubCommand::with_name("server")
                .setting(AppSettings::ArgRequiredElseHelp)
                .subcommand(
                    SubCommand::with_name("connect")
                        .setting(AppSettings::ArgRequiredElseHelp)
                        .arg(Arg::with_name("host").required(true).index(1))
                        .arg(Arg::with_name("username").required(true).index(2))
                        .arg(Arg::with_name("port").short("p").long("port").takes_value(true)))
                .subcommand(SubCommand::with_name("disconnect"))
                .subcommand(
                    SubCommand::with_name("config")
                        .setting(AppSettings::ArgRequiredElseHelp)
                        .arg(Arg::with_name("server_name").required(true).index(1))
                        .arg(Arg::with_name("var_name").required(true).index(2))
                        .arg(Arg::with_name("var_value").required(true).index(3)))
                .subcommand(
                    SubCommand::with_name("rename")
                        .setting(AppSettings::ArgRequiredElseHelp)
                        .arg(Arg::with_name("prev_name").required(true).index(1))
                        .arg(Arg::with_name("next_name").required(true).index(2))))
        .subcommand(
            SubCommand::with_name("channel")
                .setting(AppSettings::ArgRequiredElseHelp)
                .subcommand(
                    SubCommand::with_name("list")
                        .arg(Arg::with_name("short").short("s").long("short")))
                .subcommand(
                    SubCommand::with_name("connect").arg(Arg::with_name("channel").required(true))))
        .subcommand(SubCommand::with_name("status"))
        .subcommand(SubCommand::with_name("config")
                    .arg(Arg::with_name("name")
                         .required(true))
                    .arg(Arg::with_name("value")
                         .required(true)))
        .subcommand(SubCommand::with_name("completions")
                    .arg(Arg::with_name("zsh")
                         .long("zsh"))
                    .arg(Arg::with_name("bash")
                         .long("bash"))
                    .arg(Arg::with_name("fish")
                         .long("fish")));

    let matches = app.clone().get_matches();

    if let Some(matches) = matches.subcommand_matches("server") {
        if let Some(matches) = matches.subcommand_matches("connect") {
            let host = matches.value_of("host").unwrap();
            let username = matches.value_of("username").unwrap();
            let port = match matches.value_of("port").map(|e| e.parse()) {
                None => Some(64738),
                Some(Err(_)) => None,
                Some(Ok(v)) => Some(v),
            };
            if let Some(port) = port {
                err_print!(send_command(Command::ServerConnect {
                    host: host.to_string(),
                    port,
                    username: username.to_string(),
                    accept_invalid_cert: true, //TODO
                }));
            }
        } else if let Some(_) = matches.subcommand_matches("disconnect") {
            err_print!(send_command(Command::ServerDisconnect));
        } else if let Some(matches) = matches.subcommand_matches("config") {
            let server_name = matches.value_of("server_name").unwrap();
            let var_name = matches.value_of("var_name").unwrap();
            let var_value = matches.value_of("var_value").unwrap();
            if let Some(ref mut servers) = config.servers {
                let server = servers
                    .iter_mut()
                    .find(
                        |s| s.name == server_name);
                if server.is_none() {
                    println!("{} server {} not found", "error:".red(), server_name);
                } else {
                    let server = server.unwrap();
                    match var_name {
                        "name" => {
                            println!("use mumctl server rename instead!");
                        },
                        "host" => {
                            server.host = var_value.to_string();
                        },
                        "port" => {
                            server.port = var_value.parse().unwrap();
                        },
                        "username" => {
                            server.username = Some(var_value.to_string());
                        },
                        "password" => {
                            server.password = Some(var_value.to_string()); //TODO ask stdin if empty
                        },
                        &_ => {
                            println!("{} variable {} not found", "error:".red(), var_name);
                        },
                    };
                }
            } else {
                println!("{} no servers found in configuration", "error:".red());
            }
        } else if let Some(matches) = matches.subcommand_matches("rename") {
            if let Some(ref mut servers) = config.servers {
                let prev_name = matches.value_of("prev_name").unwrap();
                let next_name = matches.value_of("next_name").unwrap();
                let server = servers
                    .iter_mut()
                    .find(
                        |s| s.name == prev_name
                    );
                if server.is_none() {
                    println!("{} server {} not found", "error:".red(), prev_name);
                } else {
                    server.unwrap().name = next_name.to_string();
                }
            }
        }
    } else if let Some(matches) = matches.subcommand_matches("channel") {
        if let Some(_matches) = matches.subcommand_matches("list") {
            match send_command(Command::ChannelList) {
                Ok(res) => match res {
                    Some(CommandResponse::ChannelList { channels }) => {
                        print_channel(&channels, 0);
                    }
                    _ => unreachable!(),
                },
                Err(e) => println!("{} {}", "error:".red(), e),
            }
        } else if let Some(matches) = matches.subcommand_matches("connect") {
            err_print!(send_command(Command::ChannelJoin {
                channel_identifier: matches.value_of("channel").unwrap().to_string()
            }));
        }
    } else if let Some(_matches) = matches.subcommand_matches("status") {
        match send_command(Command::Status) {
            Ok(res) => match res {
                Some(CommandResponse::Status { server_state }) => {
                    println!(
                        "Connected to {} as {}",
                        server_state.host, server_state.username
                    );
                    let own_channel = server_state
                        .channels
                        .iter()
                        .find(|e| e.users.iter().any(|e| e.name == server_state.username))
                        .unwrap();
                    println!(
                        "Currently in {} with {} other client{}:",
                        own_channel.name,
                        own_channel.users.len() - 1,
                        if own_channel.users.len() == 2 {
                            ""
                        } else {
                            "s"
                        }
                    );
                    println!("{}{}", INDENTATION, own_channel.name);
                    for user in &own_channel.users {
                        println!("{}{}{}", INDENTATION, INDENTATION, user);
                    }
                }
                _ => unreachable!(),
            },
            Err(e) => println!("{} {}", "error:".red(), e),
        }
    } else if let Some(matches) = matches.subcommand_matches("config") {
        let name = matches.value_of("name").unwrap();
        let value = matches.value_of("value").unwrap();
        match name {
            "audio.input_volume" => {
                if let Ok(volume) = value.parse() {
                    send_command(Command::InputVolumeSet(volume)).unwrap();
                }
            },
            _ => {
                println!("{} Unknown config value {}", "error:".red(), name);
            }
        }
    } else if let Some(matches) = matches.subcommand_matches("completions") {
        app.gen_completions_to(
            "mumctl",
            match matches.value_of("shell").unwrap_or("zsh") {
                "bash" => Shell::Bash,
                "fish" => Shell::Fish,
                _ => Shell::Zsh,
            },
            &mut io::stdout(),
        );
        return;
    };

    config.write_default_cfg();
}

fn send_command(command: Command) -> mumlib::error::Result<Option<CommandResponse>> {
    let (tx_client, rx_client) =
        ipc::channel::<mumlib::error::Result<Option<CommandResponse>>>().unwrap();

    let server_name = fs::read_to_string(mumlib::SOCKET_PATH).unwrap(); //TODO don't panic

    let tx0 = IpcSender::connect(server_name).unwrap();

    tx0.send((command, tx_client)).unwrap();

    rx_client.recv().unwrap()
}

fn print_channel(channel: &Channel, depth: usize) {
    println!(
        "{}{}{}",
        iter::repeat(INDENTATION).take(depth).collect::<String>(),
        channel.name.bold(),
        if channel.max_users != 0 {
            format!(" {}/{}", channel.users.len(), channel.max_users)
        } else {
            "".to_string()
        }
    );
    for user in &channel.users {
        println!(
            "{}-{}",
            iter::repeat(INDENTATION)
                .take(depth + 1)
                .collect::<String>(),
            user
        );
    }
    for child in &channel.children {
        print_channel(child, depth + 1);
    }
}