| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | use crate::diagnostics; |
| 4 | use crate::version; |
| 5 | |
| 6 | pub const SUPPORT_BUNDLES_DIR: &str = "support-bundles"; |
| 7 | pub const CALL_REPORTS_DIR: &str = "call-reports"; |
| 8 | pub const CALL_DUMPS_DIR: &str = "call-dumps"; |
| 9 | pub const CRASH_LOG_FILE: &str = "crash.log"; |
| 10 | |
| 11 | /// Upstream tracker — fork mirror has Issues disabled. |
| 12 | pub const GITHUB_ISSUES_NEW: &str = "https://github.com/danieldaeschle/rusty-teams/issues/new"; |
| 13 | |
| 14 | /// Runtime facts for Help, `client.md`, clipboard, and issue drafts. English keys only. |
| 15 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | pub struct ClientInfo { |
| 17 | pub version: String, |
| 18 | pub build: String, |
| 19 | pub os: String, |
| 20 | pub arch: String, |
| 21 | pub session: String, |
| 22 | pub ui_locale: String, |
| 23 | pub timezone: String, |
| 24 | pub signed_in: bool, |
| 25 | pub call_reports: bool, |
| 26 | pub debug_logging: bool, |
| 27 | pub audio_input: String, |
| 28 | pub audio_output: String, |
| 29 | pub camera: String, |
| 30 | pub logs_root: String, |
| 31 | pub support_bundles_root: String, |
| 32 | } |
| 33 | |
| 34 | impl ClientInfo { |
| 35 | pub fn capture( |
| 36 | package_version: &str, |
| 37 | ui_locale: &str, |
| 38 | signed_in: bool, |
| 39 | call_reports: bool, |
| 40 | audio_input: impl Into<String>, |
| 41 | audio_output: impl Into<String>, |
| 42 | camera: impl Into<String>, |
| 43 | ) -> Self { |
| 44 | let logs_root = local_data_dir() |
| 45 | .map(|path| path.display().to_string()) |
| 46 | .unwrap_or_else(|| "unknown".to_owned()); |
| 47 | let support_bundles_root = support_bundles_dir() |
| 48 | .map(|path| path.display().to_string()) |
| 49 | .unwrap_or_else(|| "unknown".to_owned()); |
| 50 | ClientInfo { |
| 51 | version: package_version.to_owned(), |
| 52 | build: version::build_version().to_owned(), |
| 53 | os: std::env::consts::OS.to_owned(), |
| 54 | arch: std::env::consts::ARCH.to_owned(), |
| 55 | session: desktop_session(), |
| 56 | ui_locale: ui_locale.to_owned(), |
| 57 | timezone: timezone_name(), |
| 58 | signed_in, |
| 59 | call_reports, |
| 60 | debug_logging: diagnostics::is_enabled(), |
| 61 | audio_input: audio_input.into(), |
| 62 | audio_output: audio_output.into(), |
| 63 | camera: camera.into(), |
| 64 | logs_root, |
| 65 | support_bundles_root, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn markdown(&self) -> String { |
| 70 | format!( |
| 71 | "### Client\n\ |
| 72 | - Version: {}\n\ |
| 73 | - Build: {}\n\ |
| 74 | - OS: {} ({})\n\ |
| 75 | - Session: {}\n\ |
| 76 | - UI language: {}\n\ |
| 77 | - Timezone: {}\n\ |
| 78 | - Signed in: {}\n\ |
| 79 | - Call reports: {}\n\ |
| 80 | - Debug logging: {}\n\ |
| 81 | - Audio input: {}\n\ |
| 82 | - Audio output: {}\n\ |
| 83 | - Camera: {}\n\ |
| 84 | - Logs root: {}\n\ |
| 85 | - Support bundles: {}\n", |
| 86 | self.version, |
| 87 | self.build, |
| 88 | self.os, |
| 89 | self.arch, |
| 90 | self.session, |
| 91 | self.ui_locale, |
| 92 | self.timezone, |
| 93 | yes_no(self.signed_in), |
| 94 | yes_no(self.call_reports), |
| 95 | yes_no(self.debug_logging), |
| 96 | self.audio_input, |
| 97 | self.audio_output, |
| 98 | self.camera, |
| 99 | self.logs_root, |
| 100 | self.support_bundles_root, |
| 101 | ) |
| 102 | } |
| 103 | |
| 104 | pub fn one_line(&self) -> String { |
| 105 | format!( |
| 106 | "client version={} build={} os={}/{} session={} locale={} tz={} signed_in={} call_reports={} debug_logging={} logs={}", |
| 107 | self.version, |
| 108 | self.build, |
| 109 | self.os, |
| 110 | self.arch, |
| 111 | self.session, |
| 112 | self.ui_locale, |
| 113 | self.timezone, |
| 114 | yes_no(self.signed_in), |
| 115 | yes_no(self.call_reports), |
| 116 | yes_no(self.debug_logging), |
| 117 | self.logs_root, |
| 118 | ) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn yes_no(value: bool) -> &'static str { |
| 123 | if value { "yes" } else { "no" } |
| 124 | } |
| 125 | |
| 126 | pub fn desktop_session() -> String { |
| 127 | #[cfg(target_os = "linux")] |
| 128 | { |
| 129 | std::env::var("XDG_SESSION_TYPE") |
| 130 | .ok() |
| 131 | .filter(|value| !value.is_empty()) |
| 132 | .unwrap_or_else(|| "unknown".to_owned()) |
| 133 | } |
| 134 | #[cfg(target_os = "macos")] |
| 135 | { |
| 136 | "macos".to_owned() |
| 137 | } |
| 138 | #[cfg(target_os = "windows")] |
| 139 | { |
| 140 | "windows".to_owned() |
| 141 | } |
| 142 | #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] |
| 143 | { |
| 144 | "unknown".to_owned() |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn timezone_name() -> String { |
| 149 | std::env::var("TZ") |
| 150 | .ok() |
| 151 | .filter(|value| !value.is_empty()) |
| 152 | .unwrap_or_else(|| format!("{}", chrono::Local::now().offset())) |
| 153 | } |
| 154 | |
| 155 | /// OS local application data root for `ms-teams-linux` (legacy product directory name). |
| 156 | pub fn local_data_dir() -> Option<PathBuf> { |
| 157 | directories::ProjectDirs::from("", "", store::DATA_DIR_NAME) |
| 158 | .map(|directories| directories.data_local_dir().to_path_buf()) |
| 159 | } |
| 160 | |
| 161 | pub fn diagnostics_log_path() -> Option<PathBuf> { |
| 162 | local_data_dir().map(|root| root.join(diagnostics::DIAGNOSTICS_FILE)) |
| 163 | } |
| 164 | |
| 165 | pub fn crash_log_path() -> Option<PathBuf> { |
| 166 | local_data_dir().map(|root| root.join(CRASH_LOG_FILE)) |
| 167 | } |
| 168 | |
| 169 | pub fn call_dumps_dir() -> Option<PathBuf> { |
| 170 | local_data_dir().map(|root| root.join(CALL_DUMPS_DIR)) |
| 171 | } |
| 172 | |
| 173 | pub fn call_reports_dir() -> Option<PathBuf> { |
| 174 | local_data_dir().map(|root| root.join(CALL_REPORTS_DIR)) |
| 175 | } |
| 176 | |
| 177 | pub fn support_bundles_dir() -> Option<PathBuf> { |
| 178 | local_data_dir().map(|root| root.join(SUPPORT_BUNDLES_DIR)) |
| 179 | } |
| 180 | |
| 181 | pub fn support_bundles_directory(data_dir: &Path) -> PathBuf { |
| 182 | data_dir.join(SUPPORT_BUNDLES_DIR) |
| 183 | } |
| 184 | |
| 185 | pub fn bundle_folder_name(utc: chrono::DateTime<chrono::Utc>) -> String { |
| 186 | format!("support-{}", utc.format("%Y%m%dT%H%M%SZ")) |
| 187 | } |
| 188 | |
| 189 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 190 | pub struct SupportBundleEntry { |
| 191 | pub directory: PathBuf, |
| 192 | pub name: String, |
| 193 | } |
| 194 | |
| 195 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 196 | pub struct BundleFileEntry { |
| 197 | pub path: PathBuf, |
| 198 | pub name: String, |
| 199 | pub size_bytes: u64, |
| 200 | } |
| 201 | |
| 202 | /// Newest-first list of package folders under `support-bundles/`. |
| 203 | pub fn list_support_bundles(root: &Path) -> std::io::Result<Vec<SupportBundleEntry>> { |
| 204 | if !root.is_dir() { |
| 205 | return Ok(Vec::new()); |
| 206 | } |
| 207 | let mut entries: Vec<SupportBundleEntry> = std::fs::read_dir(root)? |
| 208 | .filter_map(Result::ok) |
| 209 | .map(|entry| entry.path()) |
| 210 | .filter(|path| path.is_dir()) |
| 211 | .filter_map(|directory| { |
| 212 | let name = directory.file_name()?.to_string_lossy().into_owned(); |
| 213 | Some(SupportBundleEntry { directory, name }) |
| 214 | }) |
| 215 | .collect(); |
| 216 | entries.sort_by(|left, right| right.name.cmp(&left.name)); |
| 217 | Ok(entries) |
| 218 | } |
| 219 | |
| 220 | pub fn list_bundle_files(bundle: &Path) -> std::io::Result<Vec<BundleFileEntry>> { |
| 221 | if !bundle.is_dir() { |
| 222 | return Ok(Vec::new()); |
| 223 | } |
| 224 | let mut files: Vec<BundleFileEntry> = std::fs::read_dir(bundle)? |
| 225 | .filter_map(Result::ok) |
| 226 | .map(|entry| entry.path()) |
| 227 | .filter(|path| path.is_file()) |
| 228 | .filter_map(|path| { |
| 229 | let name = path.file_name()?.to_string_lossy().into_owned(); |
| 230 | let size_bytes = std::fs::metadata(&path).ok()?.len(); |
| 231 | Some(BundleFileEntry { |
| 232 | path, |
| 233 | name, |
| 234 | size_bytes, |
| 235 | }) |
| 236 | }) |
| 237 | .collect(); |
| 238 | files.sort_by(|left, right| left.name.cmp(&right.name)); |
| 239 | Ok(files) |
| 240 | } |
| 241 | |
| 242 | /// Remove one support package directory. Refuses paths outside `parent` when provided. |
| 243 | pub fn delete_support_bundle(bundle: &Path, parent: Option<&Path>) -> std::io::Result<()> { |
| 244 | if let Some(parent) = parent { |
| 245 | let canonical_parent = parent.canonicalize().unwrap_or_else(|_| parent.to_path_buf()); |
| 246 | let canonical_bundle = bundle.canonicalize().unwrap_or_else(|_| bundle.to_path_buf()); |
| 247 | if !canonical_bundle.starts_with(&canonical_parent) { |
| 248 | return Err(std::io::Error::new( |
| 249 | std::io::ErrorKind::InvalidInput, |
| 250 | "bundle path is outside the support-bundles directory", |
| 251 | )); |
| 252 | } |
| 253 | } |
| 254 | if bundle.is_dir() { |
| 255 | std::fs::remove_dir_all(bundle)?; |
| 256 | } |
| 257 | Ok(()) |
| 258 | } |
| 259 | |
| 260 | pub fn ensure_dir(path: &Path) -> std::io::Result<()> { |
| 261 | std::fs::create_dir_all(path) |
| 262 | } |
| 263 | |
| 264 | pub fn format_bytes(size: u64) -> String { |
| 265 | const KB: u64 = 1024; |
| 266 | const MB: u64 = KB * 1024; |
| 267 | if size >= MB { |
| 268 | format!("{:.1} MB", size as f64 / MB as f64) |
| 269 | } else if size >= KB { |
| 270 | format!("{:.1} KB", size as f64 / KB as f64) |
| 271 | } else { |
| 272 | format!("{size} B") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | pub fn issue_title() -> &'static str { |
| 277 | "Bug report" |
| 278 | } |
| 279 | |
| 280 | pub fn issue_body(client: &ClientInfo, bundle: &SupportBundlePaths) -> String { |
| 281 | format!( |
| 282 | "## What happened\n\n\ |
| 283 | <!-- Describe the bug -->\n\n\ |
| 284 | ## Expected\n\n\ |
| 285 | <!-- What should happen -->\n\n\ |
| 286 | {}\n\ |
| 287 | Support package folder: `{}`\n\ |
| 288 | Support package path: `{}`\n\n\ |
| 289 | Attach the files from that folder (do not paste secrets).\n", |
| 290 | client.markdown(), |
| 291 | bundle.folder_name, |
| 292 | bundle.directory.display(), |
| 293 | ) |
| 294 | } |
| 295 | |
| 296 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 297 | pub struct SupportBundlePaths { |
| 298 | pub directory: PathBuf, |
| 299 | pub folder_name: String, |
| 300 | } |
| 301 | |
| 302 | pub fn issue_url(client: &ClientInfo, bundle: &SupportBundlePaths) -> String { |
| 303 | format!( |
| 304 | "{GITHUB_ISSUES_NEW}?title={}&body={}", |
| 305 | encode_query(issue_title()), |
| 306 | encode_query(&issue_body(client, bundle)) |
| 307 | ) |
| 308 | } |
| 309 | |
| 310 | pub fn encode_query(value: &str) -> String { |
| 311 | let mut out = String::with_capacity(value.len() * 3); |
| 312 | for byte in value.bytes() { |
| 313 | match byte { |
| 314 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { |
| 315 | out.push(byte as char); |
| 316 | } |
| 317 | b' ' => out.push_str("%20"), |
| 318 | _ => out.push_str(&format!("%{byte:02X}")), |
| 319 | } |
| 320 | } |
| 321 | out |
| 322 | } |
| 323 | |
| 324 | /// Append a client snapshot when debug logging is on (after store/locale when possible). |
| 325 | pub fn record_startup_client_line( |
| 326 | package_version: &str, |
| 327 | ui_locale: &str, |
| 328 | signed_in: bool, |
| 329 | call_reports: bool, |
| 330 | ) { |
| 331 | if !diagnostics::is_enabled() { |
| 332 | return; |
| 333 | } |
| 334 | let info = ClientInfo::capture( |
| 335 | package_version, |
| 336 | ui_locale, |
| 337 | signed_in, |
| 338 | call_reports, |
| 339 | "n/a", |
| 340 | "n/a", |
| 341 | "n/a", |
| 342 | ); |
| 343 | diagnostics::record(info.one_line()); |
| 344 | } |
| 345 | |
| 346 | /// Chain a panic hook that also writes a short line into the diagnostics log. |
| 347 | pub fn install_panic_diagnostics_hook() { |
| 348 | let previous = std::panic::take_hook(); |
| 349 | std::panic::set_hook(Box::new(move |info| { |
| 350 | let payload = info.payload(); |
| 351 | let message = payload |
| 352 | .downcast_ref::<&str>() |
| 353 | .copied() |
| 354 | .map(str::to_owned) |
| 355 | .or_else(|| payload.downcast_ref::<String>().cloned()) |
| 356 | .unwrap_or_else(|| "non-text panic payload".to_owned()); |
| 357 | let location = info |
| 358 | .location() |
| 359 | .map(|location| { |
| 360 | format!( |
| 361 | "{}:{}:{}", |
| 362 | location.file(), |
| 363 | location.line(), |
| 364 | location.column() |
| 365 | ) |
| 366 | }) |
| 367 | .unwrap_or_else(|| "unknown".to_owned()); |
| 368 | diagnostics::record(format!("panic at {location}: {message}")); |
| 369 | previous(info); |
| 370 | })); |
| 371 | } |
| 372 | |
| 373 | pub fn readme_text() -> &'static str { |
| 374 | "Attach the files in this folder to a GitHub issue on danieldaeschle/rusty-teams.\n\ |
| 375 | The app store, cookies, and tokens are not included.\n" |
| 376 | } |
| 377 | |
| 378 | #[cfg(test)] |
| 379 | mod tests { |
| 380 | use super::*; |
| 381 | |
| 382 | fn sample_client() -> ClientInfo { |
| 383 | ClientInfo { |
| 384 | version: "0.3.0".into(), |
| 385 | build: "abc".into(), |
| 386 | os: "linux".into(), |
| 387 | arch: "x86_64".into(), |
| 388 | session: "wayland".into(), |
| 389 | ui_locale: "de".into(), |
| 390 | timezone: "Europe/Zurich".into(), |
| 391 | signed_in: true, |
| 392 | call_reports: false, |
| 393 | debug_logging: true, |
| 394 | audio_input: "Default device".into(), |
| 395 | audio_output: "Default device".into(), |
| 396 | camera: "none".into(), |
| 397 | logs_root: "/tmp/ms-teams-linux".into(), |
| 398 | support_bundles_root: "/tmp/ms-teams-linux/support-bundles".into(), |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn markdown_block_lists_stable_keys_and_paths() { |
| 404 | let text = sample_client().markdown(); |
| 405 | assert!(text.contains("- Version: 0.3.0")); |
| 406 | assert!(text.contains("- Build: abc")); |
| 407 | assert!(text.contains("- OS: linux (x86_64)")); |
| 408 | assert!(text.contains("- Session: wayland")); |
| 409 | assert!(text.contains("- Signed in: yes")); |
| 410 | assert!(text.contains("- Call reports: no")); |
| 411 | assert!(text.contains("- Logs root: /tmp/ms-teams-linux")); |
| 412 | assert!(text.contains("- Support bundles: /tmp/ms-teams-linux/support-bundles")); |
| 413 | assert!(!text.contains('@')); |
| 414 | } |
| 415 | |
| 416 | #[test] |
| 417 | fn layout_relative_names_are_stable() { |
| 418 | assert_eq!(SUPPORT_BUNDLES_DIR, "support-bundles"); |
| 419 | assert_eq!(CALL_REPORTS_DIR, "call-reports"); |
| 420 | assert_eq!(CALL_DUMPS_DIR, "call-dumps"); |
| 421 | assert_eq!(CRASH_LOG_FILE, "crash.log"); |
| 422 | assert_eq!(diagnostics::DIAGNOSTICS_FILE, "diagnostics.log"); |
| 423 | let root = PathBuf::from("/data/ms-teams-linux"); |
| 424 | assert_eq!( |
| 425 | support_bundles_directory(&root), |
| 426 | root.join("support-bundles") |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | #[test] |
| 431 | fn issue_url_encodes_spaces_and_newlines() { |
| 432 | let info = sample_client(); |
| 433 | let bundle = SupportBundlePaths { |
| 434 | directory: PathBuf::from("/tmp/ms-teams-linux/support-bundles/support-20260101T000000Z"), |
| 435 | folder_name: "support-20260101T000000Z".into(), |
| 436 | }; |
| 437 | let url = issue_url(&info, &bundle); |
| 438 | assert!(url.starts_with(GITHUB_ISSUES_NEW)); |
| 439 | assert!(url.contains("title=Bug%20report")); |
| 440 | assert!(url.contains("body=")); |
| 441 | assert!(url.contains("support-20260101T000000Z")); |
| 442 | assert!(!url.contains(' ')); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn encode_query_percent_encodes_specials() { |
| 447 | assert_eq!(encode_query("a b"), "a%20b"); |
| 448 | assert_eq!(encode_query("a&b"), "a%26b"); |
| 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn list_and_delete_support_bundles_in_temp_dir() { |
| 453 | let root = tempfile::tempdir().unwrap(); |
| 454 | let bundles = root.path().join(SUPPORT_BUNDLES_DIR); |
| 455 | let older = bundles.join("support-20260101T000000Z"); |
| 456 | let newer = bundles.join("support-20260102T000000Z"); |
| 457 | std::fs::create_dir_all(&older).unwrap(); |
| 458 | std::fs::create_dir_all(&newer).unwrap(); |
| 459 | std::fs::write(older.join("client.md"), "old").unwrap(); |
| 460 | std::fs::write(newer.join("client.md"), "new").unwrap(); |
| 461 | std::fs::write(newer.join("diagnostics.log"), "log").unwrap(); |
| 462 | |
| 463 | let listed = list_support_bundles(&bundles).unwrap(); |
| 464 | assert_eq!(listed.len(), 2); |
| 465 | assert_eq!(listed[0].name, "support-20260102T000000Z"); |
| 466 | assert_eq!(listed[1].name, "support-20260101T000000Z"); |
| 467 | |
| 468 | let files = list_bundle_files(&newer).unwrap(); |
| 469 | assert_eq!(files.len(), 2); |
| 470 | assert_eq!(files[0].name, "client.md"); |
| 471 | assert_eq!(files[1].name, "diagnostics.log"); |
| 472 | |
| 473 | delete_support_bundle(&newer, Some(&bundles)).unwrap(); |
| 474 | let listed = list_support_bundles(&bundles).unwrap(); |
| 475 | assert_eq!(listed.len(), 1); |
| 476 | assert_eq!(listed[0].name, "support-20260101T000000Z"); |
| 477 | assert!(!newer.exists()); |
| 478 | } |
| 479 | |
| 480 | #[test] |
| 481 | fn delete_refuses_paths_outside_parent() { |
| 482 | let root = tempfile::tempdir().unwrap(); |
| 483 | let bundles = root.path().join(SUPPORT_BUNDLES_DIR); |
| 484 | let outside = root.path().join("other"); |
| 485 | std::fs::create_dir_all(&bundles).unwrap(); |
| 486 | std::fs::create_dir_all(&outside).unwrap(); |
| 487 | assert!(delete_support_bundle(&outside, Some(&bundles)).is_err()); |
| 488 | assert!(outside.exists()); |
| 489 | } |
| 490 | } |