Commit 4a612b75e6
Unsigned
Layout: unified · split
src/commands/matrix.rs added +299
| @@ -0,0 +1,299 @@ | |||
| 1 | use clap::App as Argparse; | ||
| 2 | use clap::AppSettings as ArgParseSettings; | ||
| 3 | use clap::{Arg, ArgMatches, SubCommand}; | ||
| 4 | use url::Url; | ||
| 5 | |||
| 6 | use crate::PLUGIN_NAME; | ||
| 7 | |||
| 8 | use crate::config::ConfigHandle; | ||
| 9 | use crate::{MatrixServer, Servers}; | ||
| 10 | use weechat::buffer::Buffer; | ||
| 11 | use weechat::hooks::{Command, CommandCallback, CommandSettings}; | ||
| 12 | use weechat::{Args, Weechat}; | ||
| 13 | |||
| 14 | pub struct MatrixCommand { | ||
| 15 | servers: Servers, | ||
| 16 | config: ConfigHandle, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl MatrixCommand { | ||
| 20 | pub fn new( | ||
| 21 | servers: &Servers, | ||
| 22 | config: &ConfigHandle, | ||
| 23 | ) -> Result<Command, ()> { | ||
| 24 | let matrix_settings = CommandSettings::new("matrix") | ||
| 25 | .description("Matrix chat protocol command.") | ||
| 26 | .add_argument("server add <server-name> <hostname>[:<port>]") | ||
| 27 | .add_argument("server delete|list|listfull <server-name>") | ||
| 28 | .add_argument("connect <server-name>") | ||
| 29 | .add_argument("disconnect <server-name>") | ||
| 30 | .add_argument("reconnect <server-name>") | ||
| 31 | .add_argument("help <matrix-command> [<matrix-subcommand>]") | ||
| 32 | .arguments_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 | ) | ||
| 40 | .add_completion("server |add|delete|list|listfull") | ||
| 41 | .add_completion("connect") | ||
| 42 | .add_completion("disconnect") | ||
| 43 | .add_completion("reconnect") | ||
| 44 | .add_completion("help server|connect|disconnect|reconnect"); | ||
| 45 | |||
| 46 | Command::new( | ||
| 47 | matrix_settings, | ||
| 48 | MatrixCommand { | ||
| 49 | servers: servers.clone(), | ||
| 50 | config: config.clone(), | ||
| 51 | }, | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | |||
| 55 | fn add_server(&self, args: &ArgMatches) { | ||
| 56 | let server_name = args | ||
| 57 | .value_of("name") | ||
| 58 | .expect("Server name not set but was required"); | ||
| 59 | let homeserver = args | ||
| 60 | .value_of("homeserver") | ||
| 61 | .expect("Homeserver not set but was required"); | ||
| 62 | let homeserver = Url::parse(homeserver) | ||
| 63 | .expect("Can't parse Homeserver even if validation passed"); | ||
| 64 | |||
| 65 | let mut config_borrow = self.config.borrow_mut(); | ||
| 66 | let mut section = config_borrow | ||
| 67 | .search_section_mut("server") | ||
| 68 | .expect("Can't get server section"); | ||
| 69 | |||
| 70 | let server = MatrixServer::new(server_name, &self.config, &mut section); | ||
| 71 | |||
| 72 | let mut servers = self.servers.borrow_mut(); | ||
| 73 | servers.insert(server_name.to_owned(), server); | ||
| 74 | |||
| 75 | let homeserver_option = section | ||
| 76 | .search_option(&format!("{}.homeserver", server_name)) | ||
| 77 | .expect("Homeserver option wasn't created"); | ||
| 78 | homeserver_option.set(homeserver.as_str(), true); | ||
| 79 | |||
| 80 | Weechat::print(&format!( | ||
| 81 | "{}: Server {}{}{} has been added.", | ||
| 82 | PLUGIN_NAME, | ||
| 83 | Weechat::color("chat_server"), | ||
| 84 | server_name, | ||
| 85 | Weechat::color("reset") | ||
| 86 | )); | ||
| 87 | } | ||
| 88 | |||
| 89 | fn delete_server(&self, args: &ArgMatches) { | ||
| 90 | let server_name = args | ||
| 91 | .value_of("name") | ||
| 92 | .expect("Server name not set but was required"); | ||
| 93 | |||
| 94 | let mut servers = self.servers.borrow_mut(); | ||
| 95 | |||
| 96 | let connected = { | ||
| 97 | let server = servers.get(server_name); | ||
| 98 | |||
| 99 | if let Some(s) = server { | ||
| 100 | s.connected() | ||
| 101 | } else { | ||
| 102 | Weechat::print(&format!( | ||
| 103 | "{}: No such server {}{}{} found.", | ||
| 104 | PLUGIN_NAME, | ||
| 105 | Weechat::color("chat_server"), | ||
| 106 | server_name, | ||
| 107 | Weechat::color("reset") | ||
| 108 | )); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | if connected { | ||
| 114 | Weechat::print(&format!( | ||
| 115 | "{}: Server {}{}{} is still connected.", | ||
| 116 | PLUGIN_NAME, | ||
| 117 | Weechat::color("chat_server"), | ||
| 118 | server_name, | ||
| 119 | Weechat::color("reset") | ||
| 120 | )); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | let server = servers.remove(server_name).unwrap(); | ||
| 125 | |||
| 126 | drop(server); | ||
| 127 | |||
| 128 | Weechat::print(&format!( | ||
| 129 | "{}: Server {}{}{} has been deleted.", | ||
| 130 | PLUGIN_NAME, | ||
| 131 | Weechat::color("chat_server"), | ||
| 132 | server_name, | ||
| 133 | Weechat::color("reset") | ||
| 134 | )); | ||
| 135 | } | ||
| 136 | |||
| 137 | fn list_servers(&self, details: bool) { | ||
| 138 | if self.servers.borrow().is_empty() { | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | Weechat::print("\nAll Matrix servers:"); | ||
| 143 | |||
| 144 | // TODO print out some stats if the server is connected. | ||
| 145 | for server in self.servers.borrow().values() { | ||
| 146 | Weechat::print(&format!(" {}", server.get_info_str(details))); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | fn server_command(&self, args: &ArgMatches) { | ||
| 151 | match args.subcommand() { | ||
| 152 | ("add", Some(subargs)) => self.add_server(subargs), | ||
| 153 | ("delete", Some(subargs)) => self.delete_server(subargs), | ||
| 154 | ("list", _) => self.list_servers(false), | ||
| 155 | ("listfull", _) => self.list_servers(true), | ||
| 156 | _ => self.list_servers(false), | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | fn server_not_found(&self, server_name: &str) { | ||
| 161 | Weechat::print(&format!( | ||
| 162 | "{}{}: Server \"{}{}{}\" not found.", | ||
| 163 | Weechat::prefix("error"), | ||
| 164 | PLUGIN_NAME, | ||
| 165 | Weechat::color("chat_server"), | ||
| 166 | server_name, | ||
| 167 | Weechat::color("reset") | ||
| 168 | )); | ||
| 169 | } | ||
| 170 | |||
| 171 | fn connect_command(&self, args: &ArgMatches) { | ||
| 172 | let server_names = args | ||
| 173 | .values_of("name") | ||
| 174 | .expect("Server names not set but were required"); | ||
| 175 | |||
| 176 | let mut servers = self.servers.borrow_mut(); | ||
| 177 | |||
| 178 | for server_name in server_names { | ||
| 179 | let server = servers.get_mut(server_name); | ||
| 180 | if let Some(s) = server { | ||
| 181 | match s.connect() { | ||
| 182 | Ok(_) => (), | ||
| 183 | Err(e) => Weechat::print(&format!("{:?}", e)), | ||
| 184 | } | ||
| 185 | } else { | ||
| 186 | self.server_not_found(server_name) | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | fn disconnect_command(&self, args: &ArgMatches) { | ||
| 192 | let mut servers = self.servers.borrow_mut(); | ||
| 193 | |||
| 194 | let server_name = args | ||
| 195 | .value_of("name") | ||
| 196 | .expect("Server name not set but was required"); | ||
| 197 | |||
| 198 | let server = servers.get_mut(server_name); | ||
| 199 | |||
| 200 | if let Some(s) = server { | ||
| 201 | s.disconnect(); | ||
| 202 | } else { | ||
| 203 | self.server_not_found(server_name) | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | impl CommandCallback for MatrixCommand { | ||
| 209 | fn callback( | ||
| 210 | &mut self, | ||
| 211 | _weechat: &Weechat, | ||
| 212 | _buffer: &Buffer, | ||
| 213 | arguments: Args, | ||
| 214 | ) { | ||
| 215 | let server_command = SubCommand::with_name("server") | ||
| 216 | .about("List, add or delete Matrix servers.") | ||
| 217 | .subcommand( | ||
| 218 | SubCommand::with_name("add") | ||
| 219 | .about("Add a new Matrix server.") | ||
| 220 | .arg( | ||
| 221 | Arg::with_name("name") | ||
| 222 | .value_name("server-name") | ||
| 223 | .required(true), | ||
| 224 | ) | ||
| 225 | .arg( | ||
| 226 | Arg::with_name("homeserver") | ||
| 227 | .required(true) | ||
| 228 | .validator(MatrixServer::parse_homeserver_url), | ||
| 229 | ), | ||
| 230 | ) | ||
| 231 | .subcommand( | ||
| 232 | SubCommand::with_name("delete") | ||
| 233 | .about("Delete an existing Matrix server.") | ||
| 234 | .arg( | ||
| 235 | Arg::with_name("name") | ||
| 236 | .value_name("server-name") | ||
| 237 | .required(true), | ||
| 238 | ), | ||
| 239 | ) | ||
| 240 | .subcommand( | ||
| 241 | SubCommand::with_name("list") | ||
| 242 | .about("List the configured Matrix servers."), | ||
| 243 | ) | ||
| 244 | .subcommand( | ||
| 245 | SubCommand::with_name("listfull") | ||
| 246 | .about("List detailed information about the configured Matrix servers."), | ||
| 247 | ); | ||
| 248 | |||
| 249 | let argparse = Argparse::new("matrix") | ||
| 250 | .about("Matrix chat protocol command.") | ||
| 251 | // .global_setting(ArgParseSettings::ColorNever) | ||
| 252 | .global_setting(ArgParseSettings::DisableHelpFlags) | ||
| 253 | .global_setting(ArgParseSettings::DisableVersion) | ||
| 254 | .global_setting(ArgParseSettings::VersionlessSubcommands) | ||
| 255 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) | ||
| 256 | .subcommand(server_command) | ||
| 257 | .subcommand( | ||
| 258 | SubCommand::with_name("connect") | ||
| 259 | .about("Connect to Matrix servers.") | ||
| 260 | .arg( | ||
| 261 | Arg::with_name("name") | ||
| 262 | .value_name("server-name") | ||
| 263 | .required(true) | ||
| 264 | .multiple(true), | ||
| 265 | ), | ||
| 266 | ) | ||
| 267 | .subcommand( | ||
| 268 | SubCommand::with_name("disconnect") | ||
| 269 | .about("Disconnect from one or all Matrix servers") | ||
| 270 | .arg( | ||
| 271 | Arg::with_name("name") | ||
| 272 | .value_name("server-name") | ||
| 273 | .required(true), | ||
| 274 | ), | ||
| 275 | ); | ||
| 276 | |||
| 277 | let matches = match argparse.get_matches_from_safe(arguments) { | ||
| 278 | Ok(m) => m, | ||
| 279 | Err(e) => { | ||
| 280 | Weechat::print( | ||
| 281 | &Weechat::execute_modifier( | ||
| 282 | "color_decode_ansi", | ||
| 283 | "1", | ||
| 284 | &e.to_string(), | ||
| 285 | ) | ||
| 286 | .unwrap(), | ||
| 287 | ); | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | }; | ||
| 291 | |||
| 292 | match matches.subcommand() { | ||
| 293 | ("connect", Some(subargs)) => self.connect_command(subargs), | ||
| 294 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), | ||
| 295 | ("server", Some(subargs)) => self.server_command(subargs), | ||
| 296 | _ => unreachable!(), | ||
| 297 | } | ||
| 298 | } | ||
| 299 | } | ||
src/commands/mod.rs +6 −294
| @@ -1,301 +1,13 @@ | |||
| 1 | use clap::App as Argparse; | ||
| 2 | use clap::AppSettings as ArgParseSettings; | ||
| 3 | use clap::{Arg, ArgMatches, SubCommand}; | ||
| 4 | use url::Url; | ||
| 5 | |||
| 6 | use crate::config::ConfigHandle; | 1 | use crate::config::ConfigHandle; |
| 7 | use crate::PLUGIN_NAME; | 2 | use crate::Servers; |
| 8 | use crate::{MatrixServer, Servers}; | 3 | use weechat::hooks::Command; |
| 9 | use weechat::buffer::Buffer; | ||
| 10 | use weechat::hooks::{Command, CommandCallback, CommandSettings}; | ||
| 11 | use weechat::{Args, Weechat}; | ||
| 12 | |||
| 13 | pub struct Commands { | ||
| 14 | _matrix: Command, | ||
| 15 | } | ||
| 16 | |||
| 17 | struct MatrixCommand { | ||
| 18 | servers: Servers, | ||
| 19 | config: ConfigHandle, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl MatrixCommand { | ||
| 23 | fn new(servers: &Servers, config: &ConfigHandle) -> Result<Command, ()> { | ||
| 24 | let matrix_settings = CommandSettings::new("matrix") | ||
| 25 | .description("Matrix chat protocol command.") | ||
| 26 | .add_argument("server add <server-name> <hostname>[:<port>]") | ||
| 27 | .add_argument("server delete|list|listfull <server-name>") | ||
| 28 | .add_argument("connect <server-name>") | ||
| 29 | .add_argument("disconnect <server-name>") | ||
| 30 | .add_argument("reconnect <server-name>") | ||
| 31 | .add_argument("help <matrix-command> [<matrix-subcommand>]") | ||
| 32 | .arguments_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 | ) | ||
| 40 | .add_completion("server |add|delete|list|listfull") | ||
| 41 | .add_completion("connect") | ||
| 42 | .add_completion("disconnect") | ||
| 43 | .add_completion("reconnect") | ||
| 44 | .add_completion("help server|connect|disconnect|reconnect"); | ||
| 45 | |||
| 46 | Command::new( | ||
| 47 | matrix_settings, | ||
| 48 | MatrixCommand { | ||
| 49 | servers: servers.clone(), | ||
| 50 | config: config.clone(), | ||
| 51 | }, | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | |||
| 55 | fn add_server(&self, args: &ArgMatches) { | ||
| 56 | let server_name = args | ||
| 57 | .value_of("name") | ||
| 58 | .expect("Server name not set but was required"); | ||
| 59 | let homeserver = args | ||
| 60 | .value_of("homeserver") | ||
| 61 | .expect("Homeserver not set but was required"); | ||
| 62 | let homeserver = Url::parse(homeserver) | ||
| 63 | .expect("Can't parse Homeserver even if validation passed"); | ||
| 64 | |||
| 65 | let mut config_borrow = self.config.borrow_mut(); | ||
| 66 | let mut section = config_borrow | ||
| 67 | .search_section_mut("server") | ||
| 68 | .expect("Can't get server section"); | ||
| 69 | |||
| 70 | let server = MatrixServer::new(server_name, &self.config, &mut section); | ||
| 71 | |||
| 72 | let mut servers = self.servers.borrow_mut(); | ||
| 73 | servers.insert(server_name.to_owned(), server); | ||
| 74 | |||
| 75 | let homeserver_option = section | ||
| 76 | .search_option(&format!("{}.homeserver", server_name)) | ||
| 77 | .expect("Homeserver option wasn't created"); | ||
| 78 | homeserver_option.set(homeserver.as_str(), true); | ||
| 79 | |||
| 80 | Weechat::print(&format!( | ||
| 81 | "{}: Server {}{}{} has been added.", | ||
| 82 | PLUGIN_NAME, | ||
| 83 | Weechat::color("chat_server"), | ||
| 84 | server_name, | ||
| 85 | Weechat::color("reset") | ||
| 86 | )); | ||
| 87 | } | ||
| 88 | |||
| 89 | fn delete_server(&self, args: &ArgMatches) { | ||
| 90 | let server_name = args | ||
| 91 | .value_of("name") | ||
| 92 | .expect("Server name not set but was required"); | ||
| 93 | |||
| 94 | let mut servers = self.servers.borrow_mut(); | ||
| 95 | |||
| 96 | let connected = { | ||
| 97 | let server = servers.get(server_name); | ||
| 98 | |||
| 99 | if let Some(s) = server { | ||
| 100 | s.connected() | ||
| 101 | } else { | ||
| 102 | Weechat::print(&format!( | ||
| 103 | "{}: No such server {}{}{} found.", | ||
| 104 | PLUGIN_NAME, | ||
| 105 | Weechat::color("chat_server"), | ||
| 106 | server_name, | ||
| 107 | Weechat::color("reset") | ||
| 108 | )); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | if connected { | ||
| 114 | Weechat::print(&format!( | ||
| 115 | "{}: Server {}{}{} is still connected.", | ||
| 116 | PLUGIN_NAME, | ||
| 117 | Weechat::color("chat_server"), | ||
| 118 | server_name, | ||
| 119 | Weechat::color("reset") | ||
| 120 | )); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | let server = servers.remove(server_name).unwrap(); | ||
| 125 | |||
| 126 | drop(server); | ||
| 127 | 4 | ||
| 128 | Weechat::print(&format!( | 5 | mod matrix; |
| 129 | "{}: Server {}{}{} has been deleted.", | ||
| 130 | PLUGIN_NAME, | ||
| 131 | Weechat::color("chat_server"), | ||
| 132 | server_name, | ||
| 133 | Weechat::color("reset") | ||
| 134 | )); | ||
| 135 | } | ||
| 136 | |||
| 137 | fn list_servers(&self, details: bool) { | ||
| 138 | if self.servers.borrow().is_empty() { | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | Weechat::print("\nAll Matrix servers:"); | ||
| 143 | |||
| 144 | // TODO print out some stats if the server is connected. | ||
| 145 | for server in self.servers.borrow().values() { | ||
| 146 | Weechat::print(&format!(" {}", server.get_info_str(details))); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | fn server_command(&self, args: &ArgMatches) { | ||
| 151 | match args.subcommand() { | ||
| 152 | ("add", Some(subargs)) => self.add_server(subargs), | ||
| 153 | ("delete", Some(subargs)) => self.delete_server(subargs), | ||
| 154 | ("list", _) => self.list_servers(false), | ||
| 155 | ("listfull", _) => self.list_servers(true), | ||
| 156 | _ => self.list_servers(false), | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | fn server_not_found(&self, server_name: &str) { | ||
| 161 | Weechat::print(&format!( | ||
| 162 | "{}{}: Server \"{}{}{}\" not found.", | ||
| 163 | Weechat::prefix("error"), | ||
| 164 | PLUGIN_NAME, | ||
| 165 | Weechat::color("chat_server"), | ||
| 166 | server_name, | ||
| 167 | Weechat::color("reset") | ||
| 168 | )); | ||
| 169 | } | ||
| 170 | |||
| 171 | fn connect_command(&self, args: &ArgMatches) { | ||
| 172 | let server_names = args | ||
| 173 | .values_of("name") | ||
| 174 | .expect("Server names not set but were required"); | ||
| 175 | |||
| 176 | let mut servers = self.servers.borrow_mut(); | ||
| 177 | |||
| 178 | for server_name in server_names { | ||
| 179 | let server = servers.get_mut(server_name); | ||
| 180 | if let Some(s) = server { | ||
| 181 | match s.connect() { | ||
| 182 | Ok(_) => (), | ||
| 183 | Err(e) => Weechat::print(&format!("{:?}", e)), | ||
| 184 | } | ||
| 185 | } else { | ||
| 186 | self.server_not_found(server_name) | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | fn disconnect_command(&self, args: &ArgMatches) { | ||
| 192 | let mut servers = self.servers.borrow_mut(); | ||
| 193 | |||
| 194 | let server_name = args | ||
| 195 | .value_of("name") | ||
| 196 | .expect("Server name not set but was required"); | ||
| 197 | |||
| 198 | let server = servers.get_mut(server_name); | ||
| 199 | |||
| 200 | if let Some(s) = server { | ||
| 201 | s.disconnect(); | ||
| 202 | } else { | ||
| 203 | self.server_not_found(server_name) | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | impl CommandCallback for MatrixCommand { | ||
| 209 | fn callback( | ||
| 210 | &mut self, | ||
| 211 | _weechat: &Weechat, | ||
| 212 | _buffer: &Buffer, | ||
| 213 | arguments: Args, | ||
| 214 | ) { | ||
| 215 | let server_command = SubCommand::with_name("server") | ||
| 216 | .about("List, add or delete Matrix servers.") | ||
| 217 | .subcommand( | ||
| 218 | SubCommand::with_name("add") | ||
| 219 | .about("Add a new Matrix server.") | ||
| 220 | .arg( | ||
| 221 | Arg::with_name("name") | ||
| 222 | .value_name("server-name") | ||
| 223 | .required(true), | ||
| 224 | ) | ||
| 225 | .arg( | ||
| 226 | Arg::with_name("homeserver") | ||
| 227 | .required(true) | ||
| 228 | .validator(MatrixServer::parse_homeserver_url), | ||
| 229 | ), | ||
| 230 | ) | ||
| 231 | .subcommand( | ||
| 232 | SubCommand::with_name("delete") | ||
| 233 | .about("Delete an existing Matrix server.") | ||
| 234 | .arg( | ||
| 235 | Arg::with_name("name") | ||
| 236 | .value_name("server-name") | ||
| 237 | .required(true), | ||
| 238 | ), | ||
| 239 | ) | ||
| 240 | .subcommand( | ||
| 241 | SubCommand::with_name("list") | ||
| 242 | .about("List the configured Matrix servers."), | ||
| 243 | ) | ||
| 244 | .subcommand( | ||
| 245 | SubCommand::with_name("listfull") | ||
| 246 | .about("List detailed information about the configured Matrix servers."), | ||
| 247 | ); | ||
| 248 | |||
| 249 | let argparse = Argparse::new("matrix") | ||
| 250 | .about("Matrix chat protocol command.") | ||
| 251 | // .global_setting(ArgParseSettings::ColorNever) | ||
| 252 | .global_setting(ArgParseSettings::DisableHelpFlags) | ||
| 253 | .global_setting(ArgParseSettings::DisableVersion) | ||
| 254 | .global_setting(ArgParseSettings::VersionlessSubcommands) | ||
| 255 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) | ||
| 256 | .subcommand(server_command) | ||
| 257 | .subcommand( | ||
| 258 | SubCommand::with_name("connect") | ||
| 259 | .about("Connect to Matrix servers.") | ||
| 260 | .arg( | ||
| 261 | Arg::with_name("name") | ||
| 262 | .value_name("server-name") | ||
| 263 | .required(true) | ||
| 264 | .multiple(true), | ||
| 265 | ), | ||
| 266 | ) | ||
| 267 | .subcommand( | ||
| 268 | SubCommand::with_name("disconnect") | ||
| 269 | .about("Disconnect from one or all Matrix servers") | ||
| 270 | .arg( | ||
| 271 | Arg::with_name("name") | ||
| 272 | .value_name("server-name") | ||
| 273 | .required(true), | ||
| 274 | ), | ||
| 275 | ); | ||
| 276 | 6 | ||
| 277 | let matches = match argparse.get_matches_from_safe(arguments) { | 7 | use matrix::MatrixCommand; |
| 278 | Ok(m) => m, | ||
| 279 | Err(e) => { | ||
| 280 | Weechat::print( | ||
| 281 | &Weechat::execute_modifier( | ||
| 282 | "color_decode_ansi", | ||
| 283 | "1", | ||
| 284 | &e.to_string(), | ||
| 285 | ) | ||
| 286 | .unwrap(), | ||
| 287 | ); | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | }; | ||
| 291 | 8 | ||
| 292 | match matches.subcommand() { | 9 | pub struct Commands { |
| 293 | ("connect", Some(subargs)) => self.connect_command(subargs), | 10 | _matrix: Command, |
| 294 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), | ||
| 295 | ("server", Some(subargs)) => self.server_command(subargs), | ||
| 296 | _ => unreachable!(), | ||
| 297 | } | ||
| 298 | } | ||
| 299 | } | 11 | } |
| 300 | 12 | ||
| 301 | impl Commands { | 13 | impl Commands { |