Skip to content

Commit 4cc5777

Browse files
poljarpanekj
authored andcommitted
weechat-matrix: verification support
1 parent a7f9297 commit 4cc5777

17 files changed

Lines changed: 1604 additions & 289 deletions

Cargo.lock

Lines changed: 63 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
2626
uuid = { version = "1.1.2", features = ["v4"] }
2727
unicode-segmentation = "1.10.0"
2828

29+
# Auto-select version from matrix-sdk
30+
qrcode = { version = "*", default-features = false }
31+
image = { version = "*", default-features = false, features = ["jpeg"] }
32+
2933
[dependencies.weechat]
3034
git = "https://github.com/poljar/rust-weechat"
3135
features = ["async", "config_macro"]
@@ -34,7 +38,7 @@ features = ["async", "config_macro"]
3438
version = "0.14.0"
3539
# git = "https://github.com/matrix-org/matrix-rust-sdk.git"
3640
# rev = "92192c549b3f53889c23954386743867a73b31b1"
37-
features = ["markdown", "socks"]
41+
features = ["markdown", "socks", "qrcode"]
3842

3943
[profile.dev.package]
4044
sha2 = { opt-level = 2 }

src/bar_items/buffer_name.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ impl BarItemCallback for BufferName {
4545
format!("{}{}", Weechat::color(color), buffer.short_name())
4646
}
4747

48+
BufferOwner::Verification(_, _) => {
49+
// TODO special format this
50+
format!("{}{}", Weechat::color("status_name"), buffer.name())
51+
}
52+
4853
BufferOwner::None => {
4954
format!("{}{}", Weechat::color("status_name"), buffer.name())
5055
}

src/bar_items/buffer_plugin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ impl BufferPlugin {
2020
impl BarItemCallback for BufferPlugin {
2121
fn callback(&mut self, _: &Weechat, buffer: &Buffer) -> String {
2222
match self.servers.buffer_owner(buffer) {
23-
BufferOwner::Server(s) | BufferOwner::Room(s, _) => {
23+
BufferOwner::Server(s)
24+
| BufferOwner::Room(s, _)
25+
| BufferOwner::Verification(s, _) => {
2426
format!(
2527
"{plugin_name}{del_color}/{color}{name}",
2628
plugin_name = PLUGIN_NAME,

src/commands/matrix.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use weechat::{
1010
Args, Prefix, Weechat,
1111
};
1212

13-
use super::parse_and_run;
13+
use super::{parse_and_run, verification::VerificationCommand};
1414
use crate::{
1515
commands::{DevicesCommand, KeysCommand},
1616
config::ConfigHandle,
@@ -44,14 +44,20 @@ impl MatrixCommand {
4444
reconnect: Reconnect to server(s).
4545
devices: {}
4646
keys: {}
47+
verification: {}
4748
help: Show detailed command help.\n
4849
Use /matrix [command] help to find out more.\n",
4950
DevicesCommand::DESCRIPTION,
5051
KeysCommand::DESCRIPTION,
52+
VerificationCommand::DESCRIPTION,
5153
))
5254
.add_completion("server add|delete|list|listfull")
5355
.add_completion("devices list|delete|set-name %(matrix-users)")
5456
.add_completion(format!("keys {}", KeysCommand::COMPLETION))
57+
.add_completion(format!(
58+
"verification {}",
59+
VerificationCommand::COMPLETION
60+
))
5561
.add_completion("connect %(matrix_servers)")
5662
.add_completion("disconnect %(matrix_servers)")
5763
.add_completion("reconnect %(matrix_servers)")
@@ -224,6 +230,9 @@ Use /matrix [command] help to find out more.\n",
224230
("keys", Some(subargs)) => {
225231
KeysCommand::run(buffer, &self.servers, subargs)
226232
}
233+
("verification", Some(subargs)) => {
234+
VerificationCommand::run(buffer, &self.servers, subargs)
235+
}
227236
_ => unreachable!(),
228237
}
229238
}
@@ -291,6 +300,11 @@ impl CommandCallback for MatrixCommand {
291300
.settings(KeysCommand::SETTINGS)
292301
.subcommands(KeysCommand::subcommands()),
293302
)
303+
.subcommand(
304+
SubCommand::with_name("verification")
305+
.about(VerificationCommand::DESCRIPTION)
306+
.subcommands(VerificationCommand::subcommands()),
307+
)
294308
.subcommand(
295309
SubCommand::with_name("connect")
296310
.about("Connect to Matrix servers.")

src/commands/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clap::{App, ArgMatches};
2+
use verification::VerificationCommand;
23
use weechat::{
34
hooks::{Command, CommandRun},
45
Args, Weechat,
@@ -12,6 +13,7 @@ mod keys;
1213
mod matrix;
1314
mod me;
1415
mod page_up;
16+
mod verification;
1517

1618
use buffer_clear::BufferClearCommand;
1719
use devices::DevicesCommand;
@@ -25,6 +27,7 @@ pub struct Commands {
2527
_keys: Command,
2628
_devices: Command,
2729
_page_up: CommandRun,
30+
_verification: Command,
2831
_buffer_clear: CommandRun,
2932
_me: CommandRun,
3033
}
@@ -39,6 +42,7 @@ impl Commands {
3942
_devices: DevicesCommand::create(servers)?,
4043
_keys: KeysCommand::create(servers)?,
4144
_page_up: PageUpCommand::create(servers)?,
45+
_verification: VerificationCommand::create(servers)?,
4246
_buffer_clear: BufferClearCommand::create(servers)?,
4347
_me: MeCommand::create(servers)?,
4448
})

src/commands/verification.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use clap::{
2+
App as Argparse, AppSettings as ArgParseSettings, ArgMatches, SubCommand,
3+
};
4+
5+
use weechat::{
6+
buffer::Buffer,
7+
hooks::{Command, CommandCallback, CommandSettings},
8+
Args, Weechat,
9+
};
10+
11+
use super::parse_and_run;
12+
use crate::{BufferOwner, Servers};
13+
14+
pub struct VerificationCommand {
15+
servers: Servers,
16+
}
17+
18+
enum CommandType {
19+
Accept,
20+
Confirm,
21+
Cancel,
22+
}
23+
24+
impl 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+
103+
impl 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+
}

0 commit comments

Comments
 (0)