| @@ -0,0 +1,259 @@ |
| |
1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg}; |
| |
2 | use serde::Deserialize; |
| |
3 | |
| |
4 | use weechat::{ |
| |
5 | buffer::Buffer, |
| |
6 | hooks::{Command, CommandCallback, CommandSettings}, |
| |
7 | Args, Prefix, Weechat, |
| |
8 | }; |
| |
9 | |
| |
10 | use super::parse_and_run; |
| |
11 | |
| |
12 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] |
| |
13 | pub(crate) struct MatrixMemberProfile { |
| |
14 | pub(crate) user_id: String, |
| |
15 | pub(crate) display_name: String, |
| |
16 | pub(crate) nick: String, |
| |
17 | pub(crate) membership: String, |
| |
18 | pub(crate) role: String, |
| |
19 | pub(crate) power_level: Option<i64>, |
| |
20 | pub(crate) avatar_mxc: Option<String>, |
| |
21 | } |
| |
22 | |
| |
23 | pub struct WhoisCommand; |
| |
24 | |
| |
25 | impl WhoisCommand { |
| |
26 | pub const DESCRIPTION: &'static str = |
| |
27 | "Show Matrix room member identity information."; |
| |
28 | |
| |
29 | pub fn create() -> Result<Command, ()> { |
| |
30 | let settings = CommandSettings::new("whois") |
| |
31 | .description(Self::DESCRIPTION) |
| |
32 | .add_argument("<user>") |
| |
33 | .arguments_description( |
| |
34 | "user: Matrix user ID, display name, or current room nick.", |
| |
35 | ) |
| |
36 | .add_completion("%(matrix-room-members)"); |
| |
37 | |
| |
38 | Command::new(settings, WhoisCommand) |
| |
39 | } |
| |
40 | |
| |
41 | fn parser() -> Argparse<'static, 'static> { |
| |
42 | Argparse::new("whois") |
| |
43 | .settings(&[ |
| |
44 | ArgParseSettings::DisableHelpFlags, |
| |
45 | ArgParseSettings::DisableVersion, |
| |
46 | ]) |
| |
47 | .arg(Arg::with_name("user").required(true)) |
| |
48 | } |
| |
49 | |
| |
50 | fn print_whois(buffer: &Buffer, query: &str) { |
| |
51 | match whois_reply(&matrix_member_profiles(buffer), query) { |
| |
52 | Ok(reply) => buffer.print(&reply), |
| |
53 | Err(error) => buffer.print(&format!( |
| |
54 | "{}matrix: {}", |
| |
55 | Weechat::prefix(Prefix::Error), |
| |
56 | error, |
| |
57 | )), |
| |
58 | } |
| |
59 | } |
| |
60 | } |
| |
61 | |
| |
62 | impl CommandCallback for WhoisCommand { |
| |
63 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { |
| |
64 | parse_and_run(Self::parser(), arguments, |args| { |
| |
65 | let query = args |
| |
66 | .value_of("user") |
| |
67 | .expect("required whois user argument missing"); |
| |
68 | Self::print_whois(buffer, query); |
| |
69 | }); |
| |
70 | } |
| |
71 | } |
| |
72 | |
| |
73 | pub(crate) fn matrix_member_profiles( |
| |
74 | buffer: &Buffer, |
| |
75 | ) -> Vec<MatrixMemberProfile> { |
| |
76 | buffer |
| |
77 | .get_localvar("matrix_members_v1") |
| |
78 | .and_then(|json| serde_json::from_str(json.as_ref()).ok()) |
| |
79 | .unwrap_or_default() |
| |
80 | } |
| |
81 | |
| |
82 | pub(crate) fn matrix_member_completion_candidates( |
| |
83 | profiles: &[MatrixMemberProfile], |
| |
84 | ) -> Vec<String> { |
| |
85 | let mut candidates = profiles |
| |
86 | .iter() |
| |
87 | .flat_map(|profile| { |
| |
88 | [ |
| |
89 | profile.nick.as_str(), |
| |
90 | profile.display_name.as_str(), |
| |
91 | profile.user_id.as_str(), |
| |
92 | ] |
| |
93 | }) |
| |
94 | .filter(|candidate| !candidate.is_empty()) |
| |
95 | .map(str::to_owned) |
| |
96 | .collect::<Vec<_>>(); |
| |
97 | candidates.sort_unstable_by_key(|candidate| candidate.to_lowercase()); |
| |
98 | candidates.dedup(); |
| |
99 | candidates |
| |
100 | } |
| |
101 | |
| |
102 | fn whois_reply( |
| |
103 | profiles: &[MatrixMemberProfile], |
| |
104 | query: &str, |
| |
105 | ) -> Result<String, String> { |
| |
106 | let query = query.trim(); |
| |
107 | |
| |
108 | if query.is_empty() { |
| |
109 | return Err("Usage: /whois <user>".to_owned()); |
| |
110 | } |
| |
111 | |
| |
112 | if profiles.is_empty() { |
| |
113 | return Err("No Matrix room members are known yet.".to_owned()); |
| |
114 | } |
| |
115 | |
| |
116 | let matches = matching_profiles(profiles, query); |
| |
117 | |
| |
118 | match matches.as_slice() { |
| |
119 | [] => Err(format!("No Matrix room member matches '{}'.", query)), |
| |
120 | [profile] => Ok(format_profile(profile)), |
| |
121 | matches => { |
| |
122 | let names = matches |
| |
123 | .iter() |
| |
124 | .take(8) |
| |
125 | .map(|profile| { |
| |
126 | format!("{} ({})", profile.nick, profile.user_id) |
| |
127 | }) |
| |
128 | .collect::<Vec<_>>() |
| |
129 | .join(", "); |
| |
130 | Err(format!("'{}' is ambiguous: {}.", query, names)) |
| |
131 | } |
| |
132 | } |
| |
133 | } |
| |
134 | |
| |
135 | fn matching_profiles<'a>( |
| |
136 | profiles: &'a [MatrixMemberProfile], |
| |
137 | query: &str, |
| |
138 | ) -> Vec<&'a MatrixMemberProfile> { |
| |
139 | let query_folded = query.to_lowercase(); |
| |
140 | |
| |
141 | let mut matches = profiles |
| |
142 | .iter() |
| |
143 | .filter(|profile| { |
| |
144 | [profile.user_id.as_str(), profile.nick.as_str()] |
| |
145 | .iter() |
| |
146 | .any(|candidate| *candidate == query) |
| |
147 | || profile.display_name.eq_ignore_ascii_case(query) |
| |
148 | }) |
| |
149 | .collect::<Vec<_>>(); |
| |
150 | |
| |
151 | if matches.is_empty() { |
| |
152 | matches = profiles |
| |
153 | .iter() |
| |
154 | .filter(|profile| { |
| |
155 | [ |
| |
156 | profile.user_id.as_str(), |
| |
157 | profile.nick.as_str(), |
| |
158 | profile.display_name.as_str(), |
| |
159 | ] |
| |
160 | .iter() |
| |
161 | .any(|candidate| candidate.to_lowercase() == query_folded) |
| |
162 | }) |
| |
163 | .collect(); |
| |
164 | } |
| |
165 | |
| |
166 | matches |
| |
167 | } |
| |
168 | |
| |
169 | fn format_profile(profile: &MatrixMemberProfile) -> String { |
| |
170 | let power_level = profile |
| |
171 | .power_level |
| |
172 | .map(|level| level.to_string()) |
| |
173 | .unwrap_or_else(|| "unknown".to_owned()); |
| |
174 | let avatar = profile.avatar_mxc.as_deref().unwrap_or("none"); |
| |
175 | |
| |
176 | format!( |
| |
177 | "Matrix user: {nick}\n user_id: {user_id}\n display name: {display_name}\n membership: {membership}\n role: {role}\n power level: {power_level}\n avatar: {avatar}", |
| |
178 | nick = profile.nick, |
| |
179 | user_id = profile.user_id, |
| |
180 | display_name = profile.display_name, |
| |
181 | membership = profile.membership, |
| |
182 | role = profile.role, |
| |
183 | ) |
| |
184 | } |
| |
185 | |
| |
186 | #[cfg(test)] |
| |
187 | mod tests { |
| |
188 | use super::{ |
| |
189 | matrix_member_completion_candidates, whois_reply, MatrixMemberProfile, |
| |
190 | }; |
| |
191 | |
| |
192 | fn profile( |
| |
193 | user_id: &str, |
| |
194 | display_name: &str, |
| |
195 | nick: &str, |
| |
196 | ) -> MatrixMemberProfile { |
| |
197 | MatrixMemberProfile { |
| |
198 | user_id: user_id.to_owned(), |
| |
199 | display_name: display_name.to_owned(), |
| |
200 | nick: nick.to_owned(), |
| |
201 | membership: "join".to_owned(), |
| |
202 | role: "member".to_owned(), |
| |
203 | power_level: Some(0), |
| |
204 | avatar_mxc: Some("mxc://example.org/avatar".to_owned()), |
| |
205 | } |
| |
206 | } |
| |
207 | |
| |
208 | #[test] |
| |
209 | fn whois_finds_member_by_mxid() { |
| |
210 | let profiles = vec![profile("@ada:example.org", "Ada", "Ada")]; |
| |
211 | |
| |
212 | let reply = whois_reply(&profiles, "@ada:example.org").unwrap(); |
| |
213 | |
| |
214 | assert!(reply.contains("user_id: @ada:example.org")); |
| |
215 | assert!(reply.contains("display name: Ada")); |
| |
216 | assert!(reply.contains("membership: join")); |
| |
217 | } |
| |
218 | |
| |
219 | #[test] |
| |
220 | fn whois_finds_member_by_display_name_case_insensitively() { |
| |
221 | let profiles = vec![profile("@ada:example.org", "Ada Lovelace", "Ada")]; |
| |
222 | |
| |
223 | assert!(whois_reply(&profiles, "ada lovelace") |
| |
224 | .unwrap() |
| |
225 | .contains("@ada:example.org")); |
| |
226 | } |
| |
227 | |
| |
228 | #[test] |
| |
229 | fn whois_reports_ambiguous_display_names() { |
| |
230 | let profiles = vec![ |
| |
231 | profile("@one:example.org", "Sam", "Sam"), |
| |
232 | profile("@two:example.org", "Sam", "Sam (@two:example.org)"), |
| |
233 | ]; |
| |
234 | |
| |
235 | let error = whois_reply(&profiles, "Sam").unwrap_err(); |
| |
236 | |
| |
237 | assert!(error.contains("ambiguous")); |
| |
238 | assert!(error.contains("@one:example.org")); |
| |
239 | assert!(error.contains("@two:example.org")); |
| |
240 | } |
| |
241 | |
| |
242 | #[test] |
| |
243 | fn completion_uses_nicks_display_names_and_mxids() { |
| |
244 | let profiles = vec![profile( |
| |
245 | "@ada:example.org", |
| |
246 | "Ada Lovelace", |
| |
247 | "Ada (@ada:example.org)", |
| |
248 | )]; |
| |
249 | |
| |
250 | assert_eq!( |
| |
251 | matrix_member_completion_candidates(&profiles), |
| |
252 | vec![ |
| |
253 | "@ada:example.org".to_owned(), |
| |
254 | "Ada (@ada:example.org)".to_owned(), |
| |
255 | "Ada Lovelace".to_owned(), |
| |
256 | ], |
| |
257 | ); |
| |
258 | } |
| |
259 | } |