src/commands/verification.rs
117 lines · 3955 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
30 pub fn create(servers: &Servers) -> Result<Command, ()> {
31 let settings = CommandSettings::new("verification")
32 .description(Self::DESCRIPTION)
33 .add_argument("verification accept|confirm|cancel")
34 .arguments_description(
35 "accept: accept the verification request
36 confirm: confirm that the emojis match on both sides or \
37 confirm that the other side has scanned our QR code
38 cancel: cancel the verification flow or request",
39 )
40 .add_completion(Self::COMPLETION)
41 .add_completion("help accept|confirm|cancel");
42
43 Command::new(
44 settings,
45 VerificationCommand {
46 servers: servers.clone(),
47 },
48 )
49 }
50
51 fn verification(servers: &Servers, buffer: &Buffer, command: CommandType) {
52 let buffer_owner = servers.buffer_owner(buffer);
53
54 match buffer_owner {
55 BufferOwner::Room(_, b) => match command {
56 CommandType::Accept => b.accept_verification(),
57 CommandType::Confirm => b.confirm_verification(),
58 CommandType::Cancel => b.cancel_verification(),
59 },
60 BufferOwner::Verification(_, b) => match command {
61 CommandType::Accept => b.accept(),
62 CommandType::Confirm => b.confirm(),
63 CommandType::Cancel => b.cancel(),
64 },
65 BufferOwner::Server(_) | BufferOwner::None => {
66 Weechat::print(
67 "The verification command needs to be executed in a room or \
68 verification buffer",
69 );
70 }
71 }
72 }
73
74 pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) {
75 match args.subcommand() {
76 ("accept", _) => {
77 Self::verification(servers, buffer, CommandType::Accept)
78 }
79 ("confirm", _) => {
80 Self::verification(servers, buffer, CommandType::Confirm)
81 }
82 ("cancel", _) => {
83 Self::verification(servers, buffer, CommandType::Cancel)
84 }
85 _ => unreachable!(),
86 }
87 }
88
89 pub fn subcommands() -> Vec<Argparse<'static, 'static>> {
90 vec![
91 SubCommand::with_name("accept")
92 .about("Accept a verification request"),
93 SubCommand::with_name("confirm").about(
94 "Confirm that the emoji matches or that the other side has \
95 scanned our QR code",
96 ),
97 SubCommand::with_name("cancel")
98 .about("Cancel the verification flow"),
99 ]
100 }
101}
102
103impl CommandCallback for VerificationCommand {
104 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
105 let argparse = Argparse::new("verification")
106 .about(Self::DESCRIPTION)
107 .global_setting(ArgParseSettings::DisableHelpFlags)
108 .global_setting(ArgParseSettings::DisableVersion)
109 .global_setting(ArgParseSettings::VersionlessSubcommands)
110 .setting(ArgParseSettings::SubcommandRequiredElseHelp)
111 .subcommands(Self::subcommands());
112
113 parse_and_run(argparse, arguments, |matches| {
114 Self::run(buffer, &self.servers, &matches)
115 });
116 }
117}