| @@ -1,6 +1,8 @@ |
| 1 | 1 | use clap::{ |
| 2 | | App as Argparse, AppSettings as ArgParseSettings, ArgMatches, SubCommand, |
| 2 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, |
| 3 | SubCommand, |
| 3 | 4 | }; |
| 5 | use matrix_sdk::ruma::{OwnedUserId, UserId}; |
| 4 | 6 | |
| 5 | 7 | use weechat::{ |
| 6 | 8 | buffer::Buffer, |
| @@ -25,7 +27,8 @@ impl VerificationCommand { |
| 25 | 27 | pub const DESCRIPTION: &'static str = |
| 26 | 28 | "Control interactive verification flows"; |
| 27 | 29 | |
| 28 | | pub const COMPLETION: &'static str = "accept|confirm|cancel"; |
| 30 | pub const COMPLETION: &'static str = |
| 31 | "start %(matrix-users)|accept|confirm|cancel"; |
| 29 | 32 | pub const SETTINGS: &'static [ArgParseSettings] = &[ |
| 30 | 33 | ArgParseSettings::DisableHelpFlags, |
| 31 | 34 | ArgParseSettings::DisableVersion, |
| @@ -36,9 +39,11 @@ impl VerificationCommand { |
| 36 | 39 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 37 | 40 | let settings = CommandSettings::new("verification") |
| 38 | 41 | .description(Self::DESCRIPTION) |
| 42 | .add_argument("verification start <contact>") |
| 39 | 43 | .add_argument("verification accept|confirm|cancel") |
| 40 | 44 | .arguments_description( |
| 41 | | "accept: accept the verification request |
| 45 | " start: start an interactive verification with a contact |
| 46 | accept: accept the verification request |
| 42 | 47 | confirm: confirm that the emojis match on both sides or \ |
| 43 | 48 | confirm that the other side has scanned our QR code |
| 44 | 49 | cancel: cancel the verification flow or request", |
| @@ -77,8 +82,27 @@ impl VerificationCommand { |
| 77 | 82 | } |
| 78 | 83 | } |
| 79 | 84 | |
| 85 | fn start(servers: &Servers, buffer: &Buffer, user_id: OwnedUserId) { |
| 86 | if let Some(server) = servers.find_server(buffer) { |
| 87 | Weechat::spawn(async move { |
| 88 | server.start_verification(user_id).await; |
| 89 | }) |
| 90 | .detach(); |
| 91 | } else { |
| 92 | Weechat::print("Must be executed on Matrix buffer") |
| 93 | } |
| 94 | } |
| 95 | |
| 80 | 96 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { |
| 81 | 97 | match args.subcommand() { |
| 98 | ("start", Some(args)) => { |
| 99 | let user_id = |
| 100 | UserId::parse(args.value_of("contact").expect( |
| 101 | "Contact wasn't provided despite being required", |
| 102 | )) |
| 103 | .expect("Contact wasn't a valid Matrix user ID"); |
| 104 | Self::start(servers, buffer, user_id); |
| 105 | } |
| 82 | 106 | ("accept", _) => { |
| 83 | 107 | Self::verification(servers, buffer, CommandType::Accept) |
| 84 | 108 | } |
| @@ -94,6 +118,16 @@ impl VerificationCommand { |
| 94 | 118 | |
| 95 | 119 | pub fn subcommands() -> Vec<Argparse<'static, 'static>> { |
| 96 | 120 | vec![ |
| 121 | SubCommand::with_name("start") |
| 122 | .about("Start an interactive verification with a contact") |
| 123 | .arg(Arg::with_name("contact").required(true).validator( |
| 124 | |contact| { |
| 125 | UserId::parse(contact).map(|_| ()).map_err(|_| { |
| 126 | "The contact isn't a valid Matrix user ID" |
| 127 | .to_owned() |
| 128 | }) |
| 129 | }, |
| 130 | )), |
| 97 | 131 | SubCommand::with_name("accept") |
| 98 | 132 | .about("Accept a verification request"), |
| 99 | 133 | SubCommand::with_name("confirm").about( |
| @@ -106,6 +140,38 @@ impl VerificationCommand { |
| 106 | 140 | } |
| 107 | 141 | } |
| 108 | 142 | |
| 143 | #[cfg(test)] |
| 144 | mod tests { |
| 145 | use super::*; |
| 146 | |
| 147 | fn parser() -> Argparse<'static, 'static> { |
| 148 | Argparse::new("verification") |
| 149 | .settings(VerificationCommand::SETTINGS) |
| 150 | .subcommands(VerificationCommand::subcommands()) |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn parses_verification_start_contact() { |
| 155 | let matches = parser() |
| 156 | .get_matches_from_safe(vec![ |
| 157 | "verification", |
| 158 | "start", |
| 159 | "@alice:example.org", |
| 160 | ]) |
| 161 | .expect("valid verification start command"); |
| 162 | let args = matches.subcommand_matches("start").expect("start args"); |
| 163 | |
| 164 | assert_eq!(args.value_of("contact"), Some("@alice:example.org")); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn rejects_invalid_verification_start_contact() { |
| 169 | assert!(parser() |
| 170 | .get_matches_from_safe(vec!["verification", "start", "alice"]) |
| 171 | .is_err()); |
| 172 | } |
| 173 | } |
| 174 | |
| 109 | 175 | impl CommandCallback for VerificationCommand { |
| 110 | 176 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { |
| 111 | 177 | let argparse = Argparse::new("verification") |