Commit af3550d08d
Unsigned
Layout: unified · split
src/commands/devices.rs +24 −4
| @@ -1,8 +1,10 @@ | |||
| 1 | use std::convert::TryFrom; | ||
| 2 | |||
| 1 | use clap::{ | 3 | use clap::{ |
| 2 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, | 4 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, |
| 3 | SubCommand, | 5 | SubCommand, |
| 4 | }; | 6 | }; |
| 5 | use matrix_sdk::ruma::DeviceIdBox; | 7 | use matrix_sdk::ruma::{DeviceIdBox, UserId}; |
| 6 | 8 | ||
| 7 | use weechat::{ | 9 | use weechat::{ |
| 8 | buffer::Buffer, | 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 | let server = servers.find_server(buffer); | 63 | let server = servers.find_server(buffer); |
| 62 | 64 | ||
| 63 | if let Some(s) = server { | 65 | if let Some(s) = server { |
| 64 | let devices = || async move { | 66 | let devices = || async move { |
| 65 | s.devices().await; | 67 | s.devices(user_id).await; |
| 66 | }; | 68 | }; |
| 67 | Weechat::spawn(devices()).detach(); | 69 | Weechat::spawn(devices()).detach(); |
| 68 | } else { | 70 | } else { |
| @@ -72,7 +74,18 @@ impl DevicesCommand { | |||
| 72 | 74 | ||
| 73 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { | 75 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { |
| 74 | match args.subcommand() { | 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 | ("delete", args) => { | 89 | ("delete", args) => { |
| 77 | let devices = args | 90 | let devices = args |
| 78 | .and_then(|a| a.args.get("device-id")) | 91 | .and_then(|a| a.args.get("device-id")) |
| @@ -94,6 +107,13 @@ impl DevicesCommand { | |||
| 94 | pub fn subcommands() -> Vec<Argparse<'static, 'static>> { | 107 | pub fn subcommands() -> Vec<Argparse<'static, 'static>> { |
| 95 | vec![ | 108 | vec![ |
| 96 | SubCommand::with_name("list") | 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 | .about("List your own Matrix devices on the server."), | 117 | .about("List your own Matrix devices on the server."), |
| 98 | SubCommand::with_name("delete") | 118 | SubCommand::with_name("delete") |
| 99 | .about("Delete the given device") | 119 | .about("Delete the given device") |
src/server.rs +240 −149
| @@ -75,9 +75,10 @@ use matrix_sdk::{ | |||
| 75 | room::member::MemberEventContent, AnySyncRoomEvent, | 75 | room::member::MemberEventContent, AnySyncRoomEvent, |
| 76 | AnySyncStateEvent, SyncStateEvent, | 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 | use weechat::{ | 84 | use weechat::{ |
| @@ -99,6 +100,13 @@ pub enum ServerError { | |||
| 99 | IoError(String), | 100 | IoError(String), |
| 100 | } | 101 | } |
| 101 | 102 | ||
| 103 | #[derive(Debug, Clone, Copy)] | ||
| 104 | enum DeviceTrust { | ||
| 105 | Verified, | ||
| 106 | Unverified, | ||
| 107 | Unsupported, | ||
| 108 | } | ||
| 109 | |||
| 102 | #[derive(Clone, Debug, PartialEq)] | 110 | #[derive(Clone, Debug, PartialEq)] |
| 103 | pub struct ServerSettings { | 111 | pub struct ServerSettings { |
| 104 | pub homeserver: Option<Url>, | 112 | pub homeserver: Option<Url>, |
| @@ -873,177 +881,260 @@ impl InnerServer { | |||
| 873 | }; | 881 | }; |
| 874 | } | 882 | } |
| 875 | 883 | ||
| 876 | pub async fn devices(&self) { | 884 | async fn list_own_devices( |
| 877 | enum DeviceTrust { | 885 | &self, |
| 878 | Verified, | 886 | connection: Connection, |
| 879 | Unverified, | 887 | ) -> Result<(), Error> { |
| 880 | Unsupported, | 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() { | 895 | self.print_network(&format!( |
| 884 | let mut response = match c.devices().await { | 896 | "Devices for server {}{}{}:", |
| 885 | Ok(r) => r, | 897 | Weechat::color("chat_server"), |
| 886 | Err(e) => { | 898 | self.name(), |
| 887 | self.print_error(&format!( | 899 | Weechat::color("reset") |
| 888 | "Error fetching devices {:?}", | 900 | )); |
| 889 | e | ||
| 890 | )); | ||
| 891 | return; | ||
| 892 | } | ||
| 893 | }; | ||
| 894 | 901 | ||
| 895 | if response.devices.is_empty() { | 902 | response.devices.sort_by_key(|d| Reverse(d.last_seen_ts)); |
| 896 | self.print_error("No devices were found for this server"); | 903 | let own_device_id = connection.client().device_id().await; |
| 897 | return; | 904 | let own_user_id = connection |
| 898 | } | 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!( | 910 | let mut lines: Vec<String> = Vec::new(); |
| 901 | "Devices for server {}{}{}:", | ||
| 902 | Weechat::color("chat_server"), | ||
| 903 | self.name(), | ||
| 904 | Weechat::color("reset") | ||
| 905 | )); | ||
| 906 | 911 | ||
| 907 | response.devices.sort_by_key(|d| Reverse(d.last_seen_ts)); | 912 | for device_info in response.devices { |
| 908 | let own_device_id = c.client().device_id().await; | 913 | let device = connection |
| 909 | let own_user_id = c.client().user_id().await.unwrap(); | 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 { | 921 | let device_trust = if own_device { |
| 914 | let device_color = Weechat::info_get( | 922 | DeviceTrust::Verified |
| 915 | "nick_color_name", | 923 | } else { |
| 916 | device_info.device_id.as_str(), | 924 | device |
| 917 | ) | 925 | .as_ref() |
| 918 | .expect("Can't get device color"); | 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 | 936 | let info = Self::format_device( |
| 921 | .last_seen_ts | 937 | &device_info.device_id, |
| 938 | device | ||
| 922 | .and_then(|d| { | 939 | .and_then(|d| { |
| 923 | d.to_system_time().map(|d| { | 940 | d.get_key(DeviceKeyAlgorithm::Ed25519) |
| 924 | let date: DateTime<Utc> = d.into(); | 941 | .map(|f| f.to_string()) |
| 925 | date.format("%Y/%m/%d %H:%M").to_string() | ||
| 926 | }) | ||
| 927 | }) | 942 | }) |
| 928 | .unwrap_or_else(|| "?".to_string()); | 943 | .as_deref(), |
| 929 | 944 | device_info.display_name.as_deref(), | |
| 930 | let last_seen = format!( | 945 | own_device, |
| 931 | "{} @ {}", | 946 | device_trust, |
| 932 | device_info.last_seen_ip.as_deref().unwrap_or("-"), | 947 | device_info.last_seen_ip, |
| 933 | last_seen_date | 948 | device_info.last_seen_ts, |
| 934 | ); | 949 | ); |
| 935 | 950 | ||
| 936 | let is_own_device = own_device_id | 951 | lines.push(info); |
| 937 | .as_ref() | 952 | } |
| 938 | .map(|o| o == &device_info.device_id) | ||
| 939 | .unwrap_or(false); | ||
| 940 | 953 | ||
| 941 | let (bold, color) = if is_own_device { | 954 | let line = lines.join("\n"); |
| 942 | (Weechat::color("bold"), format!("*{}", device_color)) | 955 | self.print(&line); |
| 943 | } else { | ||
| 944 | ("", device_color) | ||
| 945 | }; | ||
| 946 | 956 | ||
| 947 | let fingerprint = if is_own_device { | 957 | Ok(()) |
| 948 | c.client().ed25519_key().await | 958 | } |
| 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 | }; | ||
| 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 | DeviceTrust::Verified | 971 | DeviceTrust::Verified |
| 962 | } else { | 972 | } else { |
| 963 | c.client() | 973 | DeviceTrust::Unverified |
| 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) | ||
| 975 | }; | 974 | }; |
| 976 | 975 | ||
| 977 | let verified = match verified { | 976 | Self::format_device( |
| 978 | DeviceTrust::Verified => { | 977 | device.device_id(), |
| 979 | format!( | 978 | device |
| 980 | "{}Trusted{}", | 979 | .get_key(DeviceKeyAlgorithm::Ed25519) |
| 981 | Weechat::color("green"), | 980 | .map(|f| f.as_str()), |
| 982 | Weechat::color("reset") | 981 | device.display_name().as_deref(), |
| 983 | ) | 982 | false, |
| 984 | } | 983 | device_trust, |
| 985 | DeviceTrust::Unverified => { | 984 | None, |
| 986 | format!( | 985 | None, |
| 987 | "{}Not trusted{}", | 986 | ) |
| 988 | Weechat::color("red"), | 987 | }) |
| 989 | Weechat::color("reset") | 988 | .collect(); |
| 990 | ) | ||
| 991 | } | ||
| 992 | DeviceTrust::Unsupported => { | ||
| 993 | format!( | ||
| 994 | "{}No encryption support{}", | ||
| 995 | Weechat::color("darkgray"), | ||
| 996 | Weechat::color("reset") | ||
| 997 | ) | ||
| 998 | } | ||
| 999 | }; | ||
| 1000 | 989 | ||
| 1001 | let fingerprint = if let Some(fingerprint) = fingerprint { | 990 | let user_color = Weechat::info_get("nick_color_name", user_id.as_str()) |
| 1002 | let fingerprint = fingerprint | 991 | .expect("Can't get user color"); |
| 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 | }; | ||
| 1023 | 992 | ||
| 1024 | let info = format!( | 993 | if lines.is_empty() { |
| 1025 | " \ | 994 | self.print_error(&format!( |
| 1026 | Name: {}{}\n \ | 995 | "No devices were found for user {}{}{} on this server", |
| 1027 | Device ID: {}{}{}\n \ | 996 | Weechat::color(&user_color), |
| 1028 | Security: {}\n\ | 997 | user_id.as_str(), |
| 1029 | Fingerprint: {}\n \ | 998 | Weechat::color("reset"), |
| 1030 | Last seen: {}\n", | 999 | )); |
| 1031 | bold, | 1000 | } else { |
| 1032 | device_info.display_name.as_deref().unwrap_or(""), | 1001 | self.print_network(&format!( |
| 1033 | Weechat::color(&color), | 1002 | "Devices for user {}{}{} on server {}{}{}:", |
| 1034 | device_info.device_id.as_str(), | 1003 | Weechat::color(&user_color), |
| 1035 | Weechat::color("reset"), | 1004 | user_id.as_str(), |
| 1036 | verified, | 1005 | Weechat::color("reset"), |
| 1037 | fingerprint, | 1006 | Weechat::color("chat_server"), |
| 1038 | last_seen, | 1007 | self.name(), |
| 1039 | ); | 1008 | Weechat::color("reset") |
| 1040 | 1009 | )); | |
| 1041 | lines.push(info); | ||
| 1042 | } | ||
| 1043 | 1010 | ||
| 1044 | let line = lines.join("\n"); | 1011 | let line = lines.join("\n"); |
| 1045 | self.print(&line); | 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 | pub fn autoconnect(&self) -> bool { | 1140 | pub fn autoconnect(&self) -> bool { |