| @@ -3,56 +3,24 @@ use clap::AppSettings as ArgParseSettings; |
| 3 | use clap::{Arg, ArgMatches, SubCommand}; |
3 | use clap::{Arg, ArgMatches, SubCommand}; |
| 4 | use url::Url; |
4 | use url::Url; |
| 5 | |
5 | |
| 6 | use crate::config::{Config, ConfigHandle}; |
6 | use crate::config::Config; |
| 7 | use crate::PLUGIN_NAME; |
7 | use crate::PLUGIN_NAME; |
| 8 | use crate::{MatrixServer, Servers, ServersHandle}; |
8 | use crate::{MatrixServer, Servers}; |
| 9 | use weechat::buffer::Buffer; |
9 | use weechat::buffer::Buffer; |
| 10 | use weechat::hooks::{CommandDescription, CommandHook}; |
10 | use weechat::hooks::{CommandSettings, Command, CommandCallback}; |
| 11 | use weechat::{ArgsWeechat, Weechat}; |
11 | use weechat::{ArgsWeechat, Weechat}; |
| 12 | |
12 | |
| 13 | pub struct Commands { |
13 | pub struct Commands { |
| 14 | _matrix: CommandHook<(ServersHandle, ConfigHandle)>, |
14 | _matrix: Command, |
| 15 | } |
15 | } |
| 16 | |
16 | |
| 17 | impl Commands { |
17 | struct MatrixCommand { |
| 18 | pub fn hook_all( |
18 | servers: Servers, |
| 19 | weechat: &Weechat, |
19 | config: Config, |
| 20 | servers: &Servers, |
20 | } |
| 21 | config: &Config, |
| |
| 22 | ) -> Commands { |
| |
| 23 | let matrix_desc = CommandDescription { |
| |
| 24 | name: "matrix", |
| |
| 25 | description: "Matrix chat protocol command.", |
| |
| 26 | args: "server add <server-name> <hostname>[:<port>]||\ |
| |
| 27 | server delete|list|listfull <server-name> ||\ |
| |
| 28 | connect <server-name> ||\ |
| |
| 29 | disconnect <server-name> ||\ |
| |
| 30 | reconnect <server-name> ||\ |
| |
| 31 | help <matrix-command> [<matrix-subcommand>]", |
| |
| 32 | args_description: |
| |
| 33 | " server: List, add, or remove Matrix servers. |
| |
| 34 | connect: Connect to Matrix servers. |
| |
| 35 | disconnect: Disconnect from one or all Matrix servers. |
| |
| 36 | reconnect: Reconnect to server(s). |
| |
| 37 | help: Show detailed command help.\n |
| |
| 38 | Use /matrix [command] help to find out more.\n", |
| |
| 39 | completion: "server |add|delete|list|listfull || |
| |
| 40 | connect || |
| |
| 41 | disconnect || |
| |
| 42 | reconnect || |
| |
| 43 | help server|connect|disconnect|reconnect", |
| |
| 44 | }; |
| |
| 45 | |
| |
| 46 | let matrix = weechat.hook_command( |
| |
| 47 | matrix_desc, |
| |
| 48 | Commands::matrix_command_cb, |
| |
| 49 | Some((servers.clone_weak(), config.clone_weak())), |
| |
| 50 | ); |
| |
| 51 | |
| |
| 52 | Commands { _matrix: matrix } |
| |
| 53 | } |
| |
| 54 | |
21 | |
| 55 | fn add_server(args: &ArgMatches, servers: &Servers, config: &ConfigHandle) { |
22 | impl MatrixCommand { |
| |
23 | fn add_server(&self, args: &ArgMatches) { |
| 56 | let server_name = args |
24 | let server_name = args |
| 57 | .value_of("name") |
25 | .value_of("name") |
| 58 | .expect("Server name not set but was required"); |
26 | .expect("Server name not set but was required"); |
| @@ -62,15 +30,14 @@ Use /matrix [command] help to find out more.\n", |
| 62 | let homeserver = Url::parse(homeserver) |
30 | let homeserver = Url::parse(homeserver) |
| 63 | .expect("Can't parse Homeserver even if validation passed"); |
31 | .expect("Can't parse Homeserver even if validation passed"); |
| 64 | |
32 | |
| 65 | let config = config.upgrade(); |
33 | let mut config_borrow = self.config.borrow_mut(); |
| 66 | let mut config_borrow = config.borrow_mut(); |
| |
| 67 | let mut section = config_borrow |
34 | let mut section = config_borrow |
| 68 | .search_section_mut("server") |
35 | .search_section_mut("server") |
| 69 | .expect("Can't get server section"); |
36 | .expect("Can't get server section"); |
| 70 | |
37 | |
| 71 | let server = MatrixServer::new(server_name, &config, &mut section); |
38 | let server = MatrixServer::new(server_name, &self.config, &mut section); |
| 72 | |
39 | |
| 73 | let mut servers = servers.borrow_mut(); |
40 | let mut servers = self.servers.borrow_mut(); |
| 74 | servers.insert(server_name.to_owned(), server); |
41 | servers.insert(server_name.to_owned(), server); |
| 75 | |
42 | |
| 76 | let homeserver_option = section |
43 | let homeserver_option = section |
| @@ -87,12 +54,12 @@ Use /matrix [command] help to find out more.\n", |
| 87 | )); |
54 | )); |
| 88 | } |
55 | } |
| 89 | |
56 | |
| 90 | fn delete_server(args: &ArgMatches, servers: &Servers) { |
57 | fn delete_server(&self, args: &ArgMatches) { |
| 91 | let server_name = args |
58 | let server_name = args |
| 92 | .value_of("name") |
59 | .value_of("name") |
| 93 | .expect("Server name not set but was required"); |
60 | .expect("Server name not set but was required"); |
| 94 | |
61 | |
| 95 | let mut servers = servers.borrow_mut(); |
62 | let mut servers = self.servers.borrow_mut(); |
| 96 | |
63 | |
| 97 | let connected = { |
64 | let connected = { |
| 98 | let server = servers.get(server_name); |
65 | let server = servers.get(server_name); |
| @@ -135,15 +102,15 @@ Use /matrix [command] help to find out more.\n", |
| 135 | )); |
102 | )); |
| 136 | } |
103 | } |
| 137 | |
104 | |
| 138 | fn list_servers(servers: &Servers) { |
105 | fn list_servers(&self) { |
| 139 | if servers.borrow().is_empty() { |
106 | if self.servers.borrow().is_empty() { |
| 140 | return; |
107 | return; |
| 141 | } |
108 | } |
| 142 | |
109 | |
| 143 | Weechat::print("\nAll Matrix servers:"); |
110 | Weechat::print("\nAll Matrix servers:"); |
| 144 | |
111 | |
| 145 | // TODO print out some stats if the server is connected. |
112 | // TODO print out some stats if the server is connected. |
| 146 | for server in servers.borrow().keys() { |
113 | for server in self.servers.borrow().keys() { |
| 147 | Weechat::print(&format!( |
114 | Weechat::print(&format!( |
| 148 | " {}{}", |
115 | " {}{}", |
| 149 | Weechat::color("chat_server"), |
116 | Weechat::color("chat_server"), |
| @@ -152,24 +119,20 @@ Use /matrix [command] help to find out more.\n", |
| 152 | } |
119 | } |
| 153 | } |
120 | } |
| 154 | |
121 | |
| 155 | fn server_command( |
122 | fn server_command(&self, args: &ArgMatches) { |
| 156 | args: &ArgMatches, |
| |
| 157 | servers: &Servers, |
| |
| 158 | config: &ConfigHandle, |
| |
| 159 | ) { |
| |
| 160 | match args.subcommand() { |
123 | match args.subcommand() { |
| 161 | ("add", Some(subargs)) => { |
124 | ("add", Some(subargs)) => { |
| 162 | Commands::add_server(subargs, servers, config) |
125 | self.add_server(subargs) |
| 163 | } |
126 | } |
| 164 | ("delete", Some(subargs)) => { |
127 | ("delete", Some(subargs)) => { |
| 165 | Commands::delete_server(subargs, servers) |
128 | self.delete_server(subargs) |
| 166 | } |
129 | } |
| 167 | ("list", _) => Commands::list_servers(servers), |
130 | ("list", _) => self.list_servers(), |
| 168 | _ => Commands::list_servers(servers), |
131 | _ => self.list_servers(), |
| 169 | } |
132 | } |
| 170 | } |
133 | } |
| 171 | |
134 | |
| 172 | fn server_not_found(server_name: &str) { |
135 | fn server_not_found(&self, server_name: &str) { |
| 173 | Weechat::print(&format!( |
136 | Weechat::print(&format!( |
| 174 | "{}{}: Server \"{}{}{}\" not found.", |
137 | "{}{}: Server \"{}{}{}\" not found.", |
| 175 | Weechat::prefix("error"), |
138 | Weechat::prefix("error"), |
| @@ -180,12 +143,12 @@ Use /matrix [command] help to find out more.\n", |
| 180 | )); |
143 | )); |
| 181 | } |
144 | } |
| 182 | |
145 | |
| 183 | fn connect_command(args: &ArgMatches, servers: &Servers) { |
146 | fn connect_command(&self, args: &ArgMatches) { |
| 184 | let server_names = args |
147 | let server_names = args |
| 185 | .values_of("name") |
148 | .values_of("name") |
| 186 | .expect("Server names not set but were required"); |
149 | .expect("Server names not set but were required"); |
| 187 | |
150 | |
| 188 | let mut servers = servers.borrow_mut(); |
151 | let mut servers = self.servers.borrow_mut(); |
| 189 | |
152 | |
| 190 | for server_name in server_names { |
153 | for server_name in server_names { |
| 191 | let server = servers.get_mut(server_name); |
154 | let server = servers.get_mut(server_name); |
| @@ -195,13 +158,13 @@ Use /matrix [command] help to find out more.\n", |
| 195 | Err(e) => Weechat::print(&format!("{:?}", e)), |
158 | Err(e) => Weechat::print(&format!("{:?}", e)), |
| 196 | } |
159 | } |
| 197 | } else { |
160 | } else { |
| 198 | Commands::server_not_found(server_name) |
161 | self.server_not_found(server_name) |
| 199 | } |
162 | } |
| 200 | } |
163 | } |
| 201 | } |
164 | } |
| 202 | |
165 | |
| 203 | fn disconnect_command(args: &ArgMatches, servers: &Servers) { |
166 | fn disconnect_command(&self, args: &ArgMatches) { |
| 204 | let mut servers = servers.borrow_mut(); |
167 | let mut servers = self.servers.borrow_mut(); |
| 205 | |
168 | |
| 206 | let server_name = args |
169 | let server_name = args |
| 207 | .value_of("name") |
170 | .value_of("name") |
| @@ -212,15 +175,13 @@ Use /matrix [command] help to find out more.\n", |
| 212 | if let Some(s) = server { |
175 | if let Some(s) = server { |
| 213 | s.disconnect(); |
176 | s.disconnect(); |
| 214 | } else { |
177 | } else { |
| 215 | Commands::server_not_found(server_name) |
178 | self.server_not_found(server_name) |
| 216 | } |
179 | } |
| 217 | } |
180 | } |
| |
181 | } |
| 218 | |
182 | |
| 219 | fn matrix_command_cb( |
183 | impl CommandCallback for MatrixCommand { |
| 220 | data: &(ServersHandle, ConfigHandle), |
184 | fn callback(&mut self, _weechat: &Weechat, _buffer: &Buffer, arguments: ArgsWeechat) { |
| 221 | _buffer: Buffer, |
| |
| 222 | args: ArgsWeechat, |
| |
| 223 | ) { |
| |
| 224 | let server_command = SubCommand::with_name("server") |
185 | let server_command = SubCommand::with_name("server") |
| 225 | .about("List, add or delete Matrix servers.") |
186 | .about("List, add or delete Matrix servers.") |
| 226 | .subcommand( |
187 | .subcommand( |
| @@ -253,7 +214,7 @@ Use /matrix [command] help to find out more.\n", |
| 253 | |
214 | |
| 254 | let argparse = Argparse::new("matrix") |
215 | let argparse = Argparse::new("matrix") |
| 255 | .about("Matrix chat protocol command.") |
216 | .about("Matrix chat protocol command.") |
| 256 | .global_setting(ArgParseSettings::ColorNever) |
217 | // .global_setting(ArgParseSettings::ColorNever) |
| 257 | .global_setting(ArgParseSettings::DisableHelpFlags) |
218 | .global_setting(ArgParseSettings::DisableHelpFlags) |
| 258 | .global_setting(ArgParseSettings::DisableVersion) |
219 | .global_setting(ArgParseSettings::DisableVersion) |
| 259 | .global_setting(ArgParseSettings::VersionlessSubcommands) |
220 | .global_setting(ArgParseSettings::VersionlessSubcommands) |
| @@ -279,28 +240,68 @@ Use /matrix [command] help to find out more.\n", |
| 279 | ), |
240 | ), |
| 280 | ); |
241 | ); |
| 281 | |
242 | |
| 282 | let matches = match argparse.get_matches_from_safe(args) { |
243 | let matches = match argparse.get_matches_from_safe(arguments) { |
| 283 | Ok(m) => m, |
244 | Ok(m) => m, |
| 284 | Err(e) => { |
245 | Err(e) => { |
| 285 | Weechat::print(&e.to_string()); |
246 | Weechat::print( |
| |
247 | &Weechat::execute_modifier( |
| |
248 | "color_decode_ansi", |
| |
249 | "1", |
| |
250 | &e.to_string(), |
| |
251 | ) |
| |
252 | .unwrap(), |
| |
253 | ); |
| 286 | return; |
254 | return; |
| 287 | } |
255 | } |
| 288 | }; |
256 | }; |
| 289 | let (servers, config) = data; |
| |
| 290 | let servers_ref = servers.upgrade(); |
| |
| 291 | let servers = servers_ref; |
| |
| 292 | |
257 | |
| 293 | match matches.subcommand() { |
258 | match matches.subcommand() { |
| 294 | ("connect", Some(subargs)) => { |
259 | ("connect", Some(subargs)) => { |
| 295 | Commands::connect_command(subargs, &servers) |
260 | self.connect_command(subargs) |
| 296 | } |
261 | } |
| 297 | ("disconnect", Some(subargs)) => { |
262 | ("disconnect", Some(subargs)) => { |
| 298 | Commands::disconnect_command(subargs, &servers) |
263 | self.disconnect_command(subargs) |
| 299 | } |
264 | } |
| 300 | ("server", Some(subargs)) => { |
265 | ("server", Some(subargs)) => { |
| 301 | Commands::server_command(subargs, &servers, config) |
266 | self.server_command(subargs) |
| 302 | } |
267 | } |
| 303 | _ => unreachable!(), |
268 | _ => unreachable!(), |
| 304 | } |
269 | } |
| 305 | } |
270 | } |
| 306 | } |
271 | } |
| |
272 | |
| |
273 | impl Commands { |
| |
274 | pub fn hook_all( |
| |
275 | weechat: &Weechat, |
| |
276 | servers: &Servers, |
| |
277 | config: &Config, |
| |
278 | ) -> Commands { |
| |
279 | let matrix_settings = CommandSettings::new("matrix") |
| |
280 | .description("Matrix chat protocol command.") |
| |
281 | .add_argument("server add <server-name> <hostname>[:<port>]") |
| |
282 | .add_argument("server delete|list|listfull <server-name>") |
| |
283 | .add_argument("connect <server-name>") |
| |
284 | .add_argument("disconnect <server-name>") |
| |
285 | .add_argument("reconnect <server-name>") |
| |
286 | .add_argument("help <matrix-command> [<matrix-subcommand>]") |
| |
287 | .arguments_description( |
| |
288 | " server: List, add, or remove Matrix servers. |
| |
289 | connect: Connect to Matrix servers. |
| |
290 | disconnect: Disconnect from one or all Matrix servers. |
| |
291 | reconnect: Reconnect to server(s). |
| |
292 | help: Show detailed command help.\n |
| |
293 | Use /matrix [command] help to find out more.\n") |
| |
294 | .add_completion("server |add|delete|list|listfull") |
| |
295 | .add_completion("connect") |
| |
296 | .add_completion("disconnect") |
| |
297 | .add_completion("reconnect") |
| |
298 | .add_completion("help server|connect|disconnect|reconnect"); |
| |
299 | |
| |
300 | let matrix = weechat.hook_command( |
| |
301 | matrix_settings, |
| |
302 | MatrixCommand { servers: servers.clone(), config: config.clone() } |
| |
303 | ); |
| |
304 | |
| |
305 | Commands { _matrix: matrix } |
| |
306 | } |
| |
307 | } |