src/commands.rs

a67ad42e15d27546a73bef80a1049031245f6eb0
weechat-matrix-rs/src/commands.rs history · blame · raw

60 lines · 1802 bytes

 1use clap::App as Argparse;
 2use clap::AppSettings as ArgParseSettings;
 3use clap::SubCommand;
 4
 5use crate::plugin;
 6use weechat::Weechat;
 7use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook};
 8
 9pub struct Commands {
10    matrix: CommandHook<()>,
11}
12
13impl Commands {
14    pub fn hook_all(weechat: &Weechat) -> Commands {
15        let matrix_desc = CommandDescription {
16            name: "matrix",
17            ..Default::default()
18        };
19
20        let matrix = weechat.hook_command(
21            matrix_desc,
22            Commands::matrix_command_cb,
23            None,
24        );
25
26        Commands { matrix }
27    }
28
29    fn matrix_command_cb(_data: &(), buffer: Buffer, args: ArgsWeechat) {
30        let weechat = unsafe { Weechat::weechat() };
31        let argparse = Argparse::new("matrix")
32            .setting(ArgParseSettings::ColorNever)
33            .subcommand(SubCommand::with_name("connect"))
34            .subcommand(SubCommand::with_name("disconnect"));
35
36        let matches = match argparse.get_matches_from_safe(args) {
37            Ok(m) => m,
38            Err(e) => {
39                weechat
40                    .print(&format!("Error parsing command arguments {}", e));
41                return;
42            }
43        };
44        let mut plugin = plugin();
45
46        if let Some(matches) = matches.subcommand_matches("connect") {
47            weechat.print("Connecting");
48            for server in plugin.servers.values_mut() {
49                server.connect();
50            }
51        } else if let Some(matches) = matches.subcommand_matches("disconnect") {
52            weechat.print("Disconnecting");
53            for server in plugin.servers.values_mut() {
54                server.disconnect();
55            }
56        } else {
57            weechat.print("Unknown subcommand");
58        }
59    }
60}