Commit af3550d08d
Unsigned
Layout: unified · split
src/commands/devices.rs +24 −4
| @@ -1,8 +1,10 @@ | ||
| 1 | use std::convert::TryFrom; | |
| 2 | ||
| 1 | 3 | use clap::{ |
| 2 | 4 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, |
| 3 | 5 | SubCommand, |
| 4 | 6 | }; |
| 5 | use matrix_sdk::ruma::DeviceIdBox; | |
| 7 | use matrix_sdk::ruma::{DeviceIdBox, UserId}; | |
| 6 | 8 | |
| 7 | 9 | use weechat::{ |
| 8 | 10 | buffer::Buffer, |
| @@ -57,12 +59,12 @@ impl DevicesCommand { | ||
| 57 | 59 | } |
| 58 | 60 | } |
| 59 | 61 | |
| 60 | fn list(servers: &Servers, buffer: &Buffer) { | |
| 62 | fn list(servers: &Servers, buffer: &Buffer, user_id: Option<UserId>) { | |
| 61 | 63 | let server = servers.find_server(buffer); |
| 62 | 64 | |
| 63 | 65 | if let Some(s) = server { |
| 64 | 66 | let devices = || async move { |
| 65 | s.devices().await; | |
| 67 | s.devices(user_id).await; | |
| 66 | 68 | }; |
| 67 | 69 | Weechat::spawn(devices()).detach(); |
| 68 | 70 | } else { |
| @@ -72,7 +74,18 @@ impl DevicesCommand { | ||
| 72 | 74 | |
| 73 | 75 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { |
| 74 | 76 | match args.subcommand() { |
| 75 | ("list", _) => Self::list(servers, buffer), | |
| 77 | ("list", args) => { | |
| 78 | let user_id = args.and_then(|a| { | |
| 79 | a.args.get("user-id").and_then(|a| { | |
| 80 | a.vals.first().map(|u| { | |
| 81 | UserId::try_from(u.to_string_lossy().as_ref()) | |
| 82 | .expect("Argument wasn't a valid user id") | |
| 83 | }) | |
| 84 | }) | |
| 85 | }); | |
| 86 | ||
| 87 | Self::list(servers, buffer, user_id); | |
| 88 | } | |
| 76 | 89 | ("delete", args) => { |
| 77 | 90 | let devices = args |
| 78 | 91 | .and_then(|a| a.args.get("device-id")) |
| @@ -94,6 +107,13 @@ impl DevicesCommand { | ||
| 94 | 107 | pub fn subcommands() -> Vec<Argparse<'static, 'static>> { |
| 95 | 108 | vec![ |
| 96 | 109 | SubCommand::with_name("list") |
| 110 | .arg(Arg::with_name("user-id").required(false).validator(|u| { | |
| 111 | UserId::try_from(u) | |
| 112 | .map_err(|_| { | |
| 113 | "The given user isn't a valid user ID".to_owned() | |
| 114 | }) | |
| 115 | .map(|_| ()) | |
| 116 | })) | |
| 97 | 117 | .about("List your own Matrix devices on the server."), |
| 98 | 118 | SubCommand::with_name("delete") |
| 99 | 119 | .about("Delete the given device") |
src/server.rs +240 −149
| @@ -75,9 +75,10 @@ use matrix_sdk::{ | ||
| 75 | 75 | room::member::MemberEventContent, AnySyncRoomEvent, |
| 76 | 76 | AnySyncStateEvent, SyncStateEvent, |
| 77 | 77 | }, |
| 78 | DeviceIdBox, DeviceKeyAlgorithm, RoomId, UserId, | |
| 78 | DeviceId, DeviceIdBox, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, | |
| 79 | RoomId, UserId, | |
| 79 | 80 | }, |
| 80 | Client, ClientConfig, | |
| 81 | Client, ClientConfig, Error, | |
| 81 | 82 | }; |
| 82 | 83 | |
| 83 | 84 | use weechat::{ |
| @@ -99,6 +100,13 @@ pub enum ServerError { | ||
| 99 | 100 | IoError(String), |
| 100 | 101 | } |
| 101 | 102 | |
| 103 | #[derive(Debug, Clone, Copy)] | |
| 104 | enum DeviceTrust { | |
| 105 | Verified, | |
| 106 | Unverified, | |
| 107 | Unsupported, | |
| 108 | } | |
| 109 | ||
| 102 | 110 | #[derive(Clone, Debug, PartialEq)] |
| 103 | 111 | pub struct ServerSettings { |
| 104 | 112 | pub homeserver: Option<Url>, |
| @@ -873,177 +881,260 @@ impl InnerServer { | ||
| 873 | 881 | }; |
| 874 | 882 | } |
| 875 | 883 | |
| 876 | pub async fn devices(&self) { | |
| 877 | enum DeviceTrust { | |
| 878 | Verified, | |
| 879 | Unverified, | |
| 880 | Unsupported, | |
| 884 | async fn list_own_devices( | |
| 885 | &self, | |
| 886 | connection: Connection, | |
| 887 | ) -> Result<(), Error> { | |
| 888 | let mut response = connection.devices().await?; | |
| 889 | ||
| 890 | if response.devices.is_empty() { | |
| 891 | self.print_error("No devices were found for this server"); | |
| 892 | return Ok(()); | |
| 881 | 893 | } |
| 882 | 894 | |
| 883 | if let Some(c) = self.connection() { | |
| 884 | let mut response = match c.devices().await { | |
| 885 | Ok(r) => r, | |
| 886 | Err(e) => { | |
| 887 | self.print_error(&format!( | |
| 888 | "Error fetching devices {:?}", | |
| 889 | e | |
| 890 | )); | |
| 891 | return; | |
| 892 | } | |
| 893 | }; | |
| 895 | self.print_network(&format!( | |
| 896 | "Devices for server {}{}{}:", | |
| 897 | Weechat::color("chat_server"), | |
| 898 | self.name(), | |
| 899 | Weechat::color("reset") | |
| 900 | )); | |
| 894 | 901 | |
| 895 | if response.devices.is_empty() { | |
| 896 | self.print_error("No devices were found for this server"); | |
| 897 | return; | |
| 898 | } | |
| 902 | response.devices.sort_by_key(|d| Reverse(d.last_seen_ts)); | |
| 903 | let own_device_id = connection.client().device_id().await; | |
| 904 | let own_user_id = connection | |
| 905 | .client() | |
| 906 | .user_id() | |
| 907 | .await | |
| 908 | .expect("Getting our own devices while not being logged in"); | |
| 899 | 909 | |
| 900 | self.print_network(&format!( | |
| 901 | "Devices for server {}{}{}:", | |
| 902 | Weechat::color("chat_server"), | |
| 903 | self.name(), | |
| 904 | Weechat::color("reset") | |
| 905 | )); | |
| 910 | let mut lines: Vec<String> = Vec::new(); | |
| 906 | 911 | |
| 907 | response.devices.sort_by_key(|d| Reverse(d.last_seen_ts)); | |
| 908 | let own_device_id = c.client().device_id().await; | |
| 909 | let own_user_id = c.client().user_id().await.unwrap(); | |
| 912 | for device_info in response.devices { | |
| 913 | let device = connection | |
| 914 | .client() | |
| 915 | .get_device(&own_user_id, &device_info.device_id) | |
| 916 | .await?; | |
| 910 | 917 | |
| 911 | let mut lines: Vec<String> = Vec::new(); | |
| 918 | let own_device = | |
| 919 | own_device_id.as_ref() == Some(&device_info.device_id); | |
| 912 | 920 | |
| 913 | for device_info in response.devices { | |
| 914 | let device_color = Weechat::info_get( | |
| 915 | "nick_color_name", | |
| 916 | device_info.device_id.as_str(), | |
| 917 | ) | |
| 918 | .expect("Can't get device color"); | |
| 921 | let device_trust = if own_device { | |
| 922 | DeviceTrust::Verified | |
| 923 | } else { | |
| 924 | device | |
| 925 | .as_ref() | |
| 926 | .map(|d| { | |
| 927 | if d.verified() { | |
| 928 | DeviceTrust::Verified | |
| 929 | } else { | |
| 930 | DeviceTrust::Unverified | |
| 931 | } | |
| 932 | }) | |
| 933 | .unwrap_or(DeviceTrust::Unsupported) | |
| 934 | }; | |
| 919 | 935 | |
| 920 | let last_seen_date = device_info | |
| 921 | .last_seen_ts | |
| 936 | let info = Self::format_device( | |
| 937 | &device_info.device_id, | |
| 938 | device | |
| 922 | 939 | .and_then(|d| { |
| 923 | d.to_system_time().map(|d| { | |
| 924 | let date: DateTime<Utc> = d.into(); | |
| 925 | date.format("%Y/%m/%d %H:%M").to_string() | |
| 926 | }) | |
| 940 | d.get_key(DeviceKeyAlgorithm::Ed25519) | |
| 941 | .map(|f| f.to_string()) | |
| 927 | 942 | }) |
| 928 | .unwrap_or_else(|| "?".to_string()); | |
| 929 | ||
| 930 | let last_seen = format!( | |
| 931 | "{} @ {}", | |
| 932 | device_info.last_seen_ip.as_deref().unwrap_or("-"), | |
| 933 | last_seen_date | |
| 934 | ); | |
| 943 | .as_deref(), | |
| 944 | device_info.display_name.as_deref(), | |
| 945 | own_device, | |
| 946 | device_trust, | |
| 947 | device_info.last_seen_ip, | |
| 948 | device_info.last_seen_ts, | |
| 949 | ); | |
| 935 | 950 | |
| 936 | let is_own_device = own_device_id | |
| 937 | .as_ref() | |
| 938 | .map(|o| o == &device_info.device_id) | |
| 939 | .unwrap_or(false); | |
| 951 | lines.push(info); | |
| 952 | } | |
| 940 | 953 | |
| 941 | let (bold, color) = if is_own_device { | |
| 942 | (Weechat::color("bold"), format!("*{}", device_color)) | |
| 943 | } else { | |
| 944 | ("", device_color) | |
| 945 | }; | |
| 954 | let line = lines.join("\n"); | |
| 955 | self.print(&line); | |
| 946 | 956 | |
| 947 | let fingerprint = if is_own_device { | |
| 948 | c.client().ed25519_key().await | |
| 949 | } else { | |
| 950 | c.client() | |
| 951 | .get_device(&own_user_id, &device_info.device_id) | |
| 952 | .await | |
| 953 | .unwrap() | |
| 954 | .map(|d| { | |
| 955 | d.get_key(DeviceKeyAlgorithm::Ed25519).cloned() | |
| 956 | }) | |
| 957 | .flatten() | |
| 958 | }; | |
| 957 | Ok(()) | |
| 958 | } | |
| 959 | 959 | |
| 960 | let verified = if is_own_device { | |
| 960 | async fn list_other_devices( | |
| 961 | &self, | |
| 962 | connection: Connection, | |
| 963 | user_id: UserId, | |
| 964 | ) -> Result<(), Error> { | |
| 965 | let devices = connection.client().get_user_devices(&user_id).await?; | |
| 966 | ||
| 967 | let lines: Vec<_> = devices | |
| 968 | .devices() | |
| 969 | .map(|device| { | |
| 970 | let device_trust = if device.verified() { | |
| 961 | 971 | DeviceTrust::Verified |
| 962 | 972 | } else { |
| 963 | c.client() | |
| 964 | .get_device(&own_user_id, &device_info.device_id) | |
| 965 | .await | |
| 966 | .unwrap() | |
| 967 | .map(|d| { | |
| 968 | if d.is_trusted() { | |
| 969 | DeviceTrust::Verified | |
| 970 | } else { | |
| 971 | DeviceTrust::Unverified | |
| 972 | } | |
| 973 | }) | |
| 974 | .unwrap_or(DeviceTrust::Unsupported) | |
| 973 | DeviceTrust::Unverified | |
| 975 | 974 | }; |
| 976 | 975 | |
| 977 | let verified = match verified { | |
| 978 | DeviceTrust::Verified => { | |
| 979 | format!( | |
| 980 | "{}Trusted{}", | |
| 981 | Weechat::color("green"), | |
| 982 | Weechat::color("reset") | |
| 983 | ) | |
| 984 | } | |
| 985 | DeviceTrust::Unverified => { | |
| 986 | format!( | |
| 987 | "{}Not trusted{}", | |
| 988 | Weechat::color("red"), | |
| 989 | Weechat::color("reset") | |
| 990 | ) | |
| 991 | } | |
| 992 | DeviceTrust::Unsupported => { | |
| 993 | format!( | |
| 994 | "{}No encryption support{}", | |
| 995 | Weechat::color("darkgray"), | |
| 996 | Weechat::color("reset") | |
| 997 | ) | |
| 998 | } | |
| 999 | }; | |
| 976 | Self::format_device( | |
| 977 | device.device_id(), | |
| 978 | device | |
| 979 | .get_key(DeviceKeyAlgorithm::Ed25519) | |
| 980 | .map(|f| f.as_str()), | |
| 981 | device.display_name().as_deref(), | |
| 982 | false, | |
| 983 | device_trust, | |
| 984 | None, | |
| 985 | None, | |
| 986 | ) | |
| 987 | }) | |
| 988 | .collect(); | |
| 1000 | 989 | |
| 1001 | let fingerprint = if let Some(fingerprint) = fingerprint { | |
| 1002 | let fingerprint = fingerprint | |
| 1003 | .chars() | |
| 1004 | .collect::<Vec<char>>() | |
| 1005 | .chunks(4) | |
| 1006 | .map(|c| c.iter().collect::<String>()) | |
| 1007 | .collect::<Vec<String>>() | |
| 1008 | .join(" "); | |
| 1009 | ||
| 1010 | format!( | |
| 1011 | "{}{}{}", | |
| 1012 | Weechat::color("magenta"), | |
| 1013 | fingerprint, | |
| 1014 | Weechat::color("reset") | |
| 1015 | ) | |
| 1016 | } else { | |
| 1017 | format!( | |
| 1018 | "{}-{}", | |
| 1019 | Weechat::color("darkgray"), | |
| 1020 | Weechat::color("reset") | |
| 1021 | ) | |
| 1022 | }; | |
| 990 | let user_color = Weechat::info_get("nick_color_name", user_id.as_str()) | |
| 991 | .expect("Can't get user color"); | |
| 1023 | 992 | |
| 1024 | let info = format!( | |
| 1025 | " \ | |
| 1026 | Name: {}{}\n \ | |
| 1027 | Device ID: {}{}{}\n \ | |
| 1028 | Security: {}\n\ | |
| 1029 | Fingerprint: {}\n \ | |
| 1030 | Last seen: {}\n", | |
| 1031 | bold, | |
| 1032 | device_info.display_name.as_deref().unwrap_or(""), | |
| 1033 | Weechat::color(&color), | |
| 1034 | device_info.device_id.as_str(), | |
| 1035 | Weechat::color("reset"), | |
| 1036 | verified, | |
| 1037 | fingerprint, | |
| 1038 | last_seen, | |
| 1039 | ); | |
| 1040 | ||
| 1041 | lines.push(info); | |
| 1042 | } | |
| 993 | if lines.is_empty() { | |
| 994 | self.print_error(&format!( | |
| 995 | "No devices were found for user {}{}{} on this server", | |
| 996 | Weechat::color(&user_color), | |
| 997 | user_id.as_str(), | |
| 998 | Weechat::color("reset"), | |
| 999 | )); | |
| 1000 | } else { | |
| 1001 | self.print_network(&format!( | |
| 1002 | "Devices for user {}{}{} on server {}{}{}:", | |
| 1003 | Weechat::color(&user_color), | |
| 1004 | user_id.as_str(), | |
| 1005 | Weechat::color("reset"), | |
| 1006 | Weechat::color("chat_server"), | |
| 1007 | self.name(), | |
| 1008 | Weechat::color("reset") | |
| 1009 | )); | |
| 1043 | 1010 | |
| 1044 | 1011 | let line = lines.join("\n"); |
| 1045 | 1012 | self.print(&line); |
| 1013 | } | |
| 1014 | ||
| 1015 | Ok(()) | |
| 1016 | } | |
| 1017 | ||
| 1018 | fn format_device( | |
| 1019 | device_id: &DeviceId, | |
| 1020 | fingerprint: Option<&str>, | |
| 1021 | display_name: Option<&str>, | |
| 1022 | is_own_device: bool, | |
| 1023 | device_trust: DeviceTrust, | |
| 1024 | last_seen_ip: Option<String>, | |
| 1025 | last_seen_ts: Option<MilliSecondsSinceUnixEpoch>, | |
| 1026 | ) -> String { | |
| 1027 | let device_color = | |
| 1028 | Weechat::info_get("nick_color_name", device_id.as_str()) | |
| 1029 | .expect("Can't get device color"); | |
| 1030 | ||
| 1031 | let last_seen_date = last_seen_ts | |
| 1032 | .and_then(|d| { | |
| 1033 | d.to_system_time().map(|d| { | |
| 1034 | let date: DateTime<Utc> = d.into(); | |
| 1035 | date.format("%Y/%m/%d %H:%M").to_string() | |
| 1036 | }) | |
| 1037 | }) | |
| 1038 | .unwrap_or_else(|| "?".to_string()); | |
| 1039 | ||
| 1040 | let last_seen = format!( | |
| 1041 | "{} @ {}", | |
| 1042 | last_seen_ip.as_deref().unwrap_or("-"), | |
| 1043 | last_seen_date | |
| 1044 | ); | |
| 1045 | ||
| 1046 | let (bold, color) = if is_own_device { | |
| 1047 | (Weechat::color("bold"), format!("*{}", device_color)) | |
| 1048 | } else { | |
| 1049 | ("", device_color) | |
| 1050 | }; | |
| 1051 | ||
| 1052 | let verified = match device_trust { | |
| 1053 | DeviceTrust::Verified => { | |
| 1054 | format!( | |
| 1055 | "{}Trusted{}", | |
| 1056 | Weechat::color("green"), | |
| 1057 | Weechat::color("reset") | |
| 1058 | ) | |
| 1059 | } | |
| 1060 | DeviceTrust::Unverified => { | |
| 1061 | format!( | |
| 1062 | "{}Not trusted{}", | |
| 1063 | Weechat::color("red"), | |
| 1064 | Weechat::color("reset") | |
| 1065 | ) | |
| 1066 | } | |
| 1067 | DeviceTrust::Unsupported => { | |
| 1068 | format!( | |
| 1069 | "{}No encryption support{}", | |
| 1070 | Weechat::color("darkgray"), | |
| 1071 | Weechat::color("reset") | |
| 1072 | ) | |
| 1073 | } | |
| 1074 | }; | |
| 1075 | ||
| 1076 | let fingerprint = if let Some(fingerprint) = fingerprint { | |
| 1077 | let fingerprint = fingerprint | |
| 1078 | .chars() | |
| 1079 | .collect::<Vec<char>>() | |
| 1080 | .chunks(4) | |
| 1081 | .map(|c| c.iter().collect::<String>()) | |
| 1082 | .collect::<Vec<String>>() | |
| 1083 | .join(" "); | |
| 1084 | ||
| 1085 | format!( | |
| 1086 | "{}{}{}", | |
| 1087 | Weechat::color("magenta"), | |
| 1088 | fingerprint, | |
| 1089 | Weechat::color("reset") | |
| 1090 | ) | |
| 1091 | } else { | |
| 1092 | format!( | |
| 1093 | "{}-{}", | |
| 1094 | Weechat::color("darkgray"), | |
| 1095 | Weechat::color("reset") | |
| 1096 | ) | |
| 1097 | }; | |
| 1098 | ||
| 1099 | format!( | |
| 1100 | " \ | |
| 1101 | Name: {}{}\n \ | |
| 1102 | Device ID: {}{}{}\n \ | |
| 1103 | Security: {}\n\ | |
| 1104 | Fingerprint: {}\n \ | |
| 1105 | Last seen: {}\n", | |
| 1106 | bold, | |
| 1107 | display_name.as_deref().unwrap_or(""), | |
| 1108 | Weechat::color(&color), | |
| 1109 | device_id.as_str(), | |
| 1110 | Weechat::color("reset"), | |
| 1111 | verified, | |
| 1112 | fingerprint, | |
| 1113 | last_seen, | |
| 1114 | ) | |
| 1115 | } | |
| 1116 | ||
| 1117 | pub async fn devices(&self, user_id: Option<UserId>) { | |
| 1118 | let connection = if let Some(c) = self.connection() { | |
| 1119 | c | |
| 1120 | } else { | |
| 1121 | self.print_error("You must be connected to execute this command"); | |
| 1122 | return; | |
| 1046 | 1123 | }; |
| 1124 | ||
| 1125 | let ret = if let Some(user_id) = user_id { | |
| 1126 | if Some(&user_id) == connection.client().user_id().await.as_ref() { | |
| 1127 | self.list_own_devices(connection).await | |
| 1128 | } else { | |
| 1129 | self.list_other_devices(connection, user_id).await | |
| 1130 | } | |
| 1131 | } else { | |
| 1132 | self.list_own_devices(connection).await | |
| 1133 | }; | |
| 1134 | ||
| 1135 | if let Err(e) = ret { | |
| 1136 | self.print_error(&format!("Error fetching devices {:?}", e)); | |
| 1137 | } | |
| 1047 | 1138 | } |
| 1048 | 1139 | |
| 1049 | 1140 | pub fn autoconnect(&self) -> bool { |