Commit 9880f35b9a

9880f35b9a086de857b7b0d2f1d5d04adf728ab0

parent: d8ac3c0ba8

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-29 10:49 UTC
committer: <strk@kbt.io>

feat: start contact verification

Implements the contact-level interactive verification request from #251 using the matrix-sdk identity API and existing verification flow buffers.

Layout: unified · split

src/commands/matrix.rs +1 −1
@@ -79,7 +79,7 @@ Use /matrix [command] help to find out more.\n",
7979 .add_completion("reconnect %(matrix_servers)")
8080 .add_completion("sso-complete %(matrix_servers)")
8181 .add_completion(
82 "help server|connect|disconnect|reconnect|join|sso-complete|read|version|keys|devices|media",
82 "help server|connect|disconnect|reconnect|join|sso-complete|read|version|keys|devices|media|verification",
8383 );
8484
8585 Command::new(
src/commands/verification.rs +69 −3
@@ -1,6 +1,8 @@
11use clap::{
2 App as Argparse, AppSettings as ArgParseSettings, ArgMatches, SubCommand,
2 App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches,
3 SubCommand,
34};
5use matrix_sdk::ruma::{OwnedUserId, UserId};
46
57use weechat::{
68 buffer::Buffer,
@@ -25,7 +27,8 @@ impl VerificationCommand {
2527 pub const DESCRIPTION: &'static str =
2628 "Control interactive verification flows";
2729
28 pub const COMPLETION: &'static str = "accept|confirm|cancel";
30 pub const COMPLETION: &'static str =
31 "start %(matrix-users)|accept|confirm|cancel";
2932 pub const SETTINGS: &'static [ArgParseSettings] = &[
3033 ArgParseSettings::DisableHelpFlags,
3134 ArgParseSettings::DisableVersion,
@@ -36,9 +39,11 @@ impl VerificationCommand {
3639 pub fn create(servers: &Servers) -> Result<Command, ()> {
3740 let settings = CommandSettings::new("verification")
3841 .description(Self::DESCRIPTION)
42 .add_argument("verification start <contact>")
3943 .add_argument("verification accept|confirm|cancel")
4044 .arguments_description(
41 "accept: accept the verification request
45 " start: start an interactive verification with a contact
46 accept: accept the verification request
4247 confirm: confirm that the emojis match on both sides or \
4348 confirm that the other side has scanned our QR code
4449 cancel: cancel the verification flow or request",
@@ -77,8 +82,27 @@ impl VerificationCommand {
7782 }
7883 }
7984
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
8096 pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) {
8197 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 }
82106 ("accept", _) => {
83107 Self::verification(servers, buffer, CommandType::Accept)
84108 }
@@ -94,6 +118,16 @@ impl VerificationCommand {
94118
95119 pub fn subcommands() -> Vec<Argparse<'static, 'static>> {
96120 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 )),
97131 SubCommand::with_name("accept")
98132 .about("Accept a verification request"),
99133 SubCommand::with_name("confirm").about(
@@ -106,6 +140,38 @@ impl VerificationCommand {
106140 }
107141}
108142
143#[cfg(test)]
144mod 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
109175impl CommandCallback for VerificationCommand {
110176 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
111177 let argparse = Argparse::new("verification")
src/server.rs +58
@@ -1860,6 +1860,64 @@ impl InnerServer {
18601860 }
18611861 }
18621862
1863 pub async fn start_verification(&self, user_id: OwnedUserId) {
1864 let Some(connection) = self.connection() else {
1865 self.print_error("You must be connected to execute this command");
1866 return;
1867 };
1868
1869 let client = connection.client().clone();
1870 let requested_user_id = user_id.clone();
1871 let result = connection
1872 .spawn(async move {
1873 let Some(identity) = client
1874 .encryption()
1875 .request_user_identity(&requested_user_id)
1876 .await
1877 .map_err(|error| error.to_string())?
1878 else {
1879 return Ok(None);
1880 };
1881
1882 identity
1883 .request_verification()
1884 .await
1885 .map(Some)
1886 .map_err(|error| error.to_string())
1887 })
1888 .await;
1889
1890 match result {
1891 Ok(Some(request)) => {
1892 self.print_network(&format!(
1893 "Sent a verification request to {}",
1894 user_id
1895 ));
1896
1897 if request.room_id().is_none() {
1898 let flow_id = request.flow_id().to_owned();
1899 let buffer = VerificationBuffer::new(
1900 &self.server_name,
1901 &user_id,
1902 request,
1903 self.connection.clone(),
1904 );
1905 self.verification_buffers
1906 .borrow_mut()
1907 .insert(flow_id, buffer);
1908 }
1909 }
1910 Ok(None) => self.print_error(&format!(
1911 "No cross-signing identity was found for {}",
1912 user_id
1913 )),
1914 Err(error) => self.print_error(&format!(
1915 "Error starting verification with {}: {}",
1916 user_id, error
1917 )),
1918 }
1919 }
1920
18631921 pub fn autoconnect(&self) -> bool {
18641922 self.settings.borrow().autoconnect
18651923 }