src/commands/verification.rs
120 lines · 3984 bytes
1use clap::{
2 App as Argparse, AppSettings as ArgParseSettings, ArgMatches, SubCommand,
3};
4
5use weechat::{
6 buffer::Buffer,
7 hooks::{Command, CommandCallback, CommandSettings},
8 Args, Weechat,
9};
10
11use super::parse_and_run;
12use crate::{BufferOwner, Servers};
13
14pub struct VerificationCommand {
15 servers: Servers,
16}
17
18enum CommandType {
19 Accept,
20 Confirm,
21 Cancel,
22}
23
24impl VerificationCommand {
25 pub const DESCRIPTION: &'static str =
26 "Control interactive verification flows";
27
28 pub const COMPLETION: &'static str = "accept|confirm|cancel";
29 pub const SETTINGS: &'static [ArgParseSettings] = &[
30 ArgParseSettings::DisableHelpFlags,
31 ArgParseSettings::DisableVersion,
32 ArgParseSettings::VersionlessSubcommands,
33 ArgParseSettings::SubcommandRequiredElseHelp,
34 ];
35
36 pub fn create(servers: &Servers) -> Result<Command, ()> {
37 let settings = CommandSettings::new("verification")
38 .description(Self::DESCRIPTION)
39 .add_argument("verification accept|confirm|cancel")
40 .arguments_description(
41 "accept: accept the verification request
42 confirm: confirm that the emojis match on both sides or \
43 confirm that the other side has scanned our QR code
44 cancel: cancel the verification flow or request",
45 )
46 .add_completion(Self::COMPLETION)
47 .add_completion("help accept|confirm|cancel");
48
49 Command::new(
50 settings,
51 VerificationCommand {
52 servers: servers.clone(),
53 },
54 )
55 }
56
57 fn verification(servers: &Servers, buffer: &Buffer, command: CommandType) {
58 let buffer_owner = servers.buffer_owner(buffer);
59
60 match buffer_owner {
61 BufferOwner::Room(_, b) => match command {
62 CommandType::Accept => b.accept_verification(),
63 CommandType::Confirm => b.confirm_verification(),
64 CommandType::Cancel => b.cancel_verification(),
65 },
66 BufferOwner::Verification(_, b) => match command {
67 CommandType::Accept => b.accept(),
68 CommandType::Confirm => b.confirm(),
69 CommandType::Cancel => b.cancel(),
70 },
71 BufferOwner::Server(_) | BufferOwner::None => {
72 Weechat::print(
73 "The verification command needs to be executed in a room or \
74 verification buffer",
75 );
76 }
77 }
78 }
79
80 pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) {
81 match args.subcommand() {
82 ("accept", _) => {
83 Self::verification(servers, buffer, CommandType::Accept)
84 }
85 ("confirm", _) => {
86 Self::verification(servers, buffer, CommandType::Confirm)
87 }
88 ("cancel", _) => {
89 Self::verification(servers, buffer, CommandType::Cancel)
90 }
91 _ => unreachable!(),
92 }
93 }
94
95 pub fn subcommands() -> Vec<Argparse<'static, 'static>> {
96 vec![
97 SubCommand::with_name("accept")
98 .about("Accept a verification request"),
99 SubCommand::with_name("confirm").about(
100 "Confirm that the emoji matches or that the other side has \
101 scanned our QR code",
102 ),
103 SubCommand::with_name("cancel")
104 .about("Cancel the verification flow"),
105 ]
106 }
107}
108
109impl CommandCallback for VerificationCommand {
110 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
111 let argparse = Argparse::new("verification")
112 .about(Self::DESCRIPTION)
113 .settings(Self::SETTINGS)
114 .subcommands(Self::subcommands());
115
116 parse_and_run(argparse, arguments, |matches| {
117 Self::run(buffer, &self.servers, &matches)
118 });
119 }
120}