src/commands.rs
177 lines · 6516 bytes
1use clap::App as Argparse;
2use clap::AppSettings as ArgParseSettings;
3use clap::{Arg, ArgMatches, SubCommand};
4use url::Url;
5
6use crate::config::{Config, ConfigHandle};
7use crate::{MatrixServer, Servers, ServersHandle};
8use weechat::Weechat;
9use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook};
10
11pub struct Commands {
12 _matrix: CommandHook<(ServersHandle, ConfigHandle)>,
13}
14
15impl Commands {
16 pub fn hook_all(
17 weechat: &Weechat,
18 servers: &Servers,
19 config: &Config,
20 ) -> Commands {
21 let matrix_desc = CommandDescription {
22 name: "matrix",
23 description: "Matrix chat protocol command.",
24 args: "server add <server-name> <hostname>[:<port>]||\
25 server delete|list|listfull <server-name> ||\
26 connect <server-name> ||\
27 disconnect <server-name> ||\
28 reconnect <server-name> ||\
29 help <matrix-command> [<matrix-subcommand>]",
30 args_description: " server: List, add, or remove Matrix servers.
31 connect: Connect to Matrix servers.
32 disconnect: Disconnect from one or all Matrix servers.
33 reconnect: Reconnect to server(s).
34 help: Show detailed command help.\n
35Use /matrix [command] help to find out more.\n",
36 completion: "server |add|delete|list|listfull ||
37 connect ||
38 disconnect ||
39 reconnect ||
40 help server|connect|disconnect|reconnect"
41 };
42
43 let matrix = weechat.hook_command(
44 matrix_desc,
45 Commands::matrix_command_cb,
46 Some((servers.clone_weak(), config.clone_weak())),
47 );
48
49 Commands { _matrix: matrix }
50 }
51
52 fn server_command(
53 buffer: &Buffer,
54 args: &ArgMatches,
55 servers: &Servers,
56 config: &ConfigHandle,
57 ) {
58 match args.subcommand() {
59 ("add", Some(subargs)) => {
60 let server_name = subargs
61 .value_of("name")
62 .expect("Server name not set but was required");
63 let homeserver = subargs
64 .value_of("homeserver")
65 .expect("Homeserver not set but was required");
66 let homeserver = Url::parse(homeserver)
67 .expect("Can't parse Homeserver even if validation passed");
68
69 let config = config.upgrade();
70 let mut config_borrow = config.borrow_mut();
71 let mut section = config_borrow
72 .search_section_mut("server")
73 .expect("Can't get server section");
74
75 let server =
76 MatrixServer::new(server_name, &config, &mut section);
77
78 let mut servers = servers.borrow_mut();
79 servers.insert(server_name.to_owned(), server);
80
81 let homeserver_option = section
82 .search_option(&format!("{}.homeserver", server_name))
83 .expect("Homeserver option wasn't created");
84 homeserver_option.set(homeserver.as_str(), true);
85 }
86 ("delete", Some(subargs)) => {
87 buffer.print("Deleting server");
88 }
89 _ => (),
90 }
91 }
92
93 fn matrix_command_cb(
94 data: &(ServersHandle, ConfigHandle),
95 buffer: Buffer,
96 args: ArgsWeechat,
97 ) {
98 let weechat = unsafe { Weechat::weechat() };
99 let server_command = SubCommand::with_name("server")
100 .about("List, add or delete Matrix servers.")
101 .subcommand(
102 SubCommand::with_name("add")
103 .about("Add a new Matrix server.")
104 .arg(
105 Arg::with_name("name")
106 .value_name("server-name")
107 .required(true),
108 )
109 .arg(
110 Arg::with_name("homeserver")
111 .required(true)
112 .validator(MatrixServer::parse_homeserver_url),
113 ),
114 )
115 .subcommand(
116 SubCommand::with_name("delete")
117 .about("Delete an existing Matrix server.")
118 .arg(
119 Arg::with_name("name")
120 .value_name("server-name")
121 .required(true),
122 ),
123 );
124
125 let argparse = Argparse::new("matrix")
126 .about("Matrix chat protocol command.")
127 .global_setting(ArgParseSettings::ColorNever)
128 .global_setting(ArgParseSettings::DisableHelpFlags)
129 .global_setting(ArgParseSettings::DisableVersion)
130 .global_setting(ArgParseSettings::VersionlessSubcommands)
131 .setting(ArgParseSettings::SubcommandRequiredElseHelp)
132 .subcommand(server_command)
133 .subcommand(
134 SubCommand::with_name("connect")
135 .about("Connect to Matrix servers."),
136 )
137 .subcommand(
138 SubCommand::with_name("disconnect")
139 .about("Disconnect from one or all Matrix servers"),
140 );
141
142 let matches = match argparse.get_matches_from_safe(args) {
143 Ok(m) => m,
144 Err(e) => {
145 weechat.print(&e.to_string());
146 return;
147 }
148 };
149 let (servers, config) = data;
150 let servers_ref = servers.upgrade();
151 let servers = servers_ref;
152
153 match matches.subcommand() {
154 ("connect", Some(subargs)) => {
155 weechat.print("Connecting");
156 let mut servers = servers.borrow_mut();
157 for server in servers.values_mut() {
158 match server.connect() {
159 Ok(_) => (),
160 Err(e) => weechat.print(&format!("{:?}", e)),
161 }
162 }
163 }
164 ("disconnect", Some(subargs)) => {
165 weechat.print("Disconnecting");
166 let mut servers = servers.borrow_mut();
167 for server in servers.values_mut() {
168 server.disconnect();
169 }
170 }
171 ("server", Some(subargs)) => {
172 Commands::server_command(&buffer, subargs, &servers, config);
173 }
174 _ => unreachable!(),
175 }
176 }
177}