| @@ -3,56 +3,24 @@ use clap::AppSettings as ArgParseSettings; |
| 3 | 3 | use clap::{Arg, ArgMatches, SubCommand}; |
| 4 | 4 | use url::Url; |
| 5 | 5 | |
| 6 | | use crate::config::{Config, ConfigHandle}; |
| 6 | use crate::config::Config; |
| 7 | 7 | use crate::PLUGIN_NAME; |
| 8 | | use crate::{MatrixServer, Servers, ServersHandle}; |
| 8 | use crate::{MatrixServer, Servers}; |
| 9 | 9 | use weechat::buffer::Buffer; |
| 10 | | use weechat::hooks::{CommandDescription, CommandHook}; |
| 10 | use weechat::hooks::{CommandSettings, Command, CommandCallback}; |
| 11 | 11 | use weechat::{ArgsWeechat, Weechat}; |
| 12 | 12 | |
| 13 | 13 | pub struct Commands { |
| 14 | | _matrix: CommandHook<(ServersHandle, ConfigHandle)>, |
| 14 | _matrix: Command, |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | | impl Commands { |
| 18 | | pub fn hook_all( |
| 19 | | weechat: &Weechat, |
| 20 | | servers: &Servers, |
| 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 | | } |
| 17 | struct MatrixCommand { |
| 18 | servers: Servers, |
| 19 | config: Config, |
| 20 | } |
| 54 | 21 | |
| 55 | | fn add_server(args: &ArgMatches, servers: &Servers, config: &ConfigHandle) { |
| 22 | impl MatrixCommand { |
| 23 | fn add_server(&self, args: &ArgMatches) { |
| 56 | 24 | let server_name = args |
| 57 | 25 | .value_of("name") |
| 58 | 26 | .expect("Server name not set but was required"); |
| @@ -62,15 +30,14 @@ Use /matrix [command] help to find out more.\n", |
| 62 | 30 | let homeserver = Url::parse(homeserver) |
| 63 | 31 | .expect("Can't parse Homeserver even if validation passed"); |
| 64 | 32 | |
| 65 | | let config = config.upgrade(); |
| 66 | | let mut config_borrow = config.borrow_mut(); |
| 33 | let mut config_borrow = self.config.borrow_mut(); |
| 67 | 34 | let mut section = config_borrow |
| 68 | 35 | .search_section_mut("server") |
| 69 | 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 | 41 | servers.insert(server_name.to_owned(), server); |
| 75 | 42 | |
| 76 | 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 | 58 | let server_name = args |
| 92 | 59 | .value_of("name") |
| 93 | 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 | 64 | let connected = { |
| 98 | 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) { |
| 139 | | if servers.borrow().is_empty() { |
| 105 | fn list_servers(&self) { |
| 106 | if self.servers.borrow().is_empty() { |
| 140 | 107 | return; |
| 141 | 108 | } |
| 142 | 109 | |
| 143 | 110 | Weechat::print("\nAll Matrix servers:"); |
| 144 | 111 | |
| 145 | 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 | 114 | Weechat::print(&format!( |
| 148 | 115 | " {}{}", |
| 149 | 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( |
| 156 | | args: &ArgMatches, |
| 157 | | servers: &Servers, |
| 158 | | config: &ConfigHandle, |
| 159 | | ) { |
| 122 | fn server_command(&self, args: &ArgMatches) { |
| 160 | 123 | match args.subcommand() { |
| 161 | 124 | ("add", Some(subargs)) => { |
| 162 | | Commands::add_server(subargs, servers, config) |
| 125 | self.add_server(subargs) |
| 163 | 126 | } |
| 164 | 127 | ("delete", Some(subargs)) => { |
| 165 | | Commands::delete_server(subargs, servers) |
| 128 | self.delete_server(subargs) |
| 166 | 129 | } |
| 167 | | ("list", _) => Commands::list_servers(servers), |
| 168 | | _ => Commands::list_servers(servers), |
| 130 | ("list", _) => self.list_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 | 136 | Weechat::print(&format!( |
| 174 | 137 | "{}{}: Server \"{}{}{}\" not found.", |
| 175 | 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 | 147 | let server_names = args |
| 185 | 148 | .values_of("name") |
| 186 | 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 | 153 | for server_name in server_names { |
| 191 | 154 | let server = servers.get_mut(server_name); |
| @@ -195,13 +158,13 @@ Use /matrix [command] help to find out more.\n", |
| 195 | 158 | Err(e) => Weechat::print(&format!("{:?}", e)), |
| 196 | 159 | } |
| 197 | 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) { |
| 204 | | let mut servers = servers.borrow_mut(); |
| 166 | fn disconnect_command(&self, args: &ArgMatches) { |
| 167 | let mut servers = self.servers.borrow_mut(); |
| 205 | 168 | |
| 206 | 169 | let server_name = args |
| 207 | 170 | .value_of("name") |
| @@ -212,15 +175,13 @@ Use /matrix [command] help to find out more.\n", |
| 212 | 175 | if let Some(s) = server { |
| 213 | 176 | s.disconnect(); |
| 214 | 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( |
| 220 | | data: &(ServersHandle, ConfigHandle), |
| 221 | | _buffer: Buffer, |
| 222 | | args: ArgsWeechat, |
| 223 | | ) { |
| 183 | impl CommandCallback for MatrixCommand { |
| 184 | fn callback(&mut self, _weechat: &Weechat, _buffer: &Buffer, arguments: ArgsWeechat) { |
| 224 | 185 | let server_command = SubCommand::with_name("server") |
| 225 | 186 | .about("List, add or delete Matrix servers.") |
| 226 | 187 | .subcommand( |
| @@ -253,7 +214,7 @@ Use /matrix [command] help to find out more.\n", |
| 253 | 214 | |
| 254 | 215 | let argparse = Argparse::new("matrix") |
| 255 | 216 | .about("Matrix chat protocol command.") |
| 256 | | .global_setting(ArgParseSettings::ColorNever) |
| 217 | // .global_setting(ArgParseSettings::ColorNever) |
| 257 | 218 | .global_setting(ArgParseSettings::DisableHelpFlags) |
| 258 | 219 | .global_setting(ArgParseSettings::DisableVersion) |
| 259 | 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 | 244 | Ok(m) => m, |
| 284 | 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 | 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 | 258 | match matches.subcommand() { |
| 294 | 259 | ("connect", Some(subargs)) => { |
| 295 | | Commands::connect_command(subargs, &servers) |
| 260 | self.connect_command(subargs) |
| 296 | 261 | } |
| 297 | 262 | ("disconnect", Some(subargs)) => { |
| 298 | | Commands::disconnect_command(subargs, &servers) |
| 263 | self.disconnect_command(subargs) |
| 299 | 264 | } |
| 300 | 265 | ("server", Some(subargs)) => { |
| 301 | | Commands::server_command(subargs, &servers, config) |
| 266 | self.server_command(subargs) |
| 302 | 267 | } |
| 303 | 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 | } |