| @@ -13,6 +13,7 @@ use std::process; |
| 13 | use clap::Parser; |
13 | use clap::Parser; |
| 14 | use matrix_sdk::authentication::matrix::MatrixSession; |
14 | use matrix_sdk::authentication::matrix::MatrixSession; |
| 15 | use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId}; |
15 | use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId}; |
| |
16 | use matrix_sdk::EncryptionState; |
| 16 | use ratatui::style::{Color, Modifier as StyleModifier, Style}; |
17 | use ratatui::style::{Color, Modifier as StyleModifier, Style}; |
| 17 | use ratatui::text::Span; |
18 | use ratatui::text::Span; |
| 18 | use ratatui_image::picker::ProtocolType; |
19 | use ratatui_image::picker::ProtocolType; |
| @@ -53,6 +54,7 @@ const DEFAULT_ROOM_SORT: [SortColumn<SortFieldRoom>; 5] = [ |
| 53 | SortColumn(SortFieldRoom::Name, SortOrder::Ascending), |
54 | SortColumn(SortFieldRoom::Name, SortOrder::Ascending), |
| 54 | ]; |
55 | ]; |
| 55 | |
56 | |
| |
57 | const DEFAULT_ENC_INDICATOR_LOC: EncryptionIndicatorLocation = EncryptionIndicatorLocation::PROMPT; |
| 56 | const DEFAULT_REQ_TIMEOUT: u64 = 120; |
58 | const DEFAULT_REQ_TIMEOUT: u64 = 120; |
| 57 | |
59 | |
| 58 | const COLORS: [Color; 13] = [ |
60 | const COLORS: [Color; 13] = [ |
| @@ -315,6 +317,13 @@ pub struct UserDisplayTunables { |
| 315 | |
317 | |
| 316 | pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>; |
318 | pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>; |
| 317 | |
319 | |
| |
320 | fn merge_encryption(profile: Encryption, global: Encryption) -> Encryption { |
| |
321 | Encryption { |
| |
322 | indicator: profile.indicator.or(global.indicator), |
| |
323 | indicator_location: profile.indicator_location.or(global.indicator_location), |
| |
324 | } |
| |
325 | } |
| |
326 | |
| 318 | fn merge_sorts(profile: SortOverrides, global: SortOverrides) -> SortOverrides { |
327 | fn merge_sorts(profile: SortOverrides, global: SortOverrides) -> SortOverrides { |
| 319 | SortOverrides { |
328 | SortOverrides { |
| 320 | chats: profile.chats.or(global.chats), |
329 | chats: profile.chats.or(global.chats), |
| @@ -345,6 +354,69 @@ where |
| 345 | } |
354 | } |
| 346 | } |
355 | } |
| 347 | |
356 | |
| |
357 | #[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| |
358 | #[serde(rename_all = "kebab-case")] |
| |
359 | #[repr(u8)] |
| |
360 | pub enum EncryptionIndicator { |
| |
361 | /// Always indicate the room's encryption status. |
| |
362 | #[default] |
| |
363 | Enabled, |
| |
364 | /// Never indicate the room's encryption status. |
| |
365 | Disabled, |
| |
366 | /// Only indicate the room's encryption status when it is encrypted. |
| |
367 | OnlyEncrypted, |
| |
368 | /// Only indicate the room's encryption status when it is unencrypted. |
| |
369 | OnlyUnencrypted, |
| |
370 | } |
| |
371 | |
| |
372 | bitflags::bitflags! { |
| |
373 | /// Available options for where to show the encryption status indicator. |
| |
374 | #[derive(Clone, Debug, Eq, PartialEq)] |
| |
375 | pub struct EncryptionIndicatorLocation: u8 { |
| |
376 | const NONE = 0b00000000; |
| |
377 | const TITLE = 0b00000001; |
| |
378 | const PROMPT = 0b00000010; |
| |
379 | } |
| |
380 | } |
| |
381 | |
| |
382 | pub struct EncryptionIndicatorLocationVisitor; |
| |
383 | |
| |
384 | impl Visitor<'_> for EncryptionIndicatorLocationVisitor { |
| |
385 | type Value = EncryptionIndicatorLocation; |
| |
386 | |
| |
387 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| |
388 | formatter.write_str("a valid encryption indicator location (e.g. \"title\" or \"prompt\")") |
| |
389 | } |
| |
390 | |
| |
391 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| |
392 | where |
| |
393 | E: SerdeError, |
| |
394 | { |
| |
395 | let mut location = EncryptionIndicatorLocation::NONE; |
| |
396 | |
| |
397 | for value in value.split('|') { |
| |
398 | match value.to_ascii_lowercase().as_str() { |
| |
399 | "title" => location |= EncryptionIndicatorLocation::TITLE, |
| |
400 | "prompt" => location |= EncryptionIndicatorLocation::PROMPT, |
| |
401 | _ => { |
| |
402 | return Err(E::custom("could not parse into an encryption indicator location")) |
| |
403 | }, |
| |
404 | }; |
| |
405 | } |
| |
406 | |
| |
407 | Ok(location) |
| |
408 | } |
| |
409 | } |
| |
410 | |
| |
411 | impl<'de> Deserialize<'de> for EncryptionIndicatorLocation { |
| |
412 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| |
413 | where |
| |
414 | D: Deserializer<'de>, |
| |
415 | { |
| |
416 | deserializer.deserialize_str(EncryptionIndicatorLocationVisitor) |
| |
417 | } |
| |
418 | } |
| |
419 | |
| 348 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
420 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| 349 | #[serde(rename_all = "lowercase")] |
421 | #[serde(rename_all = "lowercase")] |
| 350 | pub enum UserDisplayStyle { |
422 | pub enum UserDisplayStyle { |
| @@ -430,6 +502,69 @@ impl<'de> Deserialize<'de> for NotifyVia { |
| 430 | } |
502 | } |
| 431 | } |
503 | } |
| 432 | |
504 | |
| |
505 | #[derive(Clone, Debug, Default, Deserialize)] |
| |
506 | pub struct Encryption { |
| |
507 | indicator: Option<EncryptionIndicator>, |
| |
508 | indicator_location: Option<EncryptionIndicatorLocation>, |
| |
509 | } |
| |
510 | |
| |
511 | impl Encryption { |
| |
512 | pub fn values(self) -> EncryptionValues { |
| |
513 | EncryptionValues { |
| |
514 | indicator: self.indicator.unwrap_or_default(), |
| |
515 | indicator_location: self.indicator_location.unwrap_or(DEFAULT_ENC_INDICATOR_LOC), |
| |
516 | } |
| |
517 | } |
| |
518 | } |
| |
519 | |
| |
520 | #[derive(Clone, Debug)] |
| |
521 | pub struct EncryptionValues { |
| |
522 | pub indicator: EncryptionIndicator, |
| |
523 | pub indicator_location: EncryptionIndicatorLocation, |
| |
524 | } |
| |
525 | |
| |
526 | impl EncryptionValues { |
| |
527 | pub fn get_indicator( |
| |
528 | &self, |
| |
529 | location: EncryptionIndicatorLocation, |
| |
530 | state: EncryptionState, |
| |
531 | ) -> Option<Span<'static>> { |
| |
532 | if !self.indicator_location.contains(location) { |
| |
533 | return None; |
| |
534 | } |
| |
535 | |
| |
536 | let indicator = match (self.indicator, state) { |
| |
537 | (EncryptionIndicator::Disabled, _) | |
| |
538 | (EncryptionIndicator::OnlyUnencrypted, EncryptionState::Encrypted) | |
| |
539 | (EncryptionIndicator::OnlyEncrypted, EncryptionState::NotEncrypted) => { |
| |
540 | // User doesn't want to see anything: |
| |
541 | return None; |
| |
542 | }, |
| |
543 | ( |
| |
544 | EncryptionIndicator::Enabled | EncryptionIndicator::OnlyEncrypted, |
| |
545 | EncryptionState::Encrypted, |
| |
546 | ) => { |
| |
547 | // Green lock: |
| |
548 | Span::styled("\u{1F512}\u{FE0E} ", Style::new().fg(Color::LightGreen)) |
| |
549 | }, |
| |
550 | ( |
| |
551 | EncryptionIndicator::Enabled | EncryptionIndicator::OnlyUnencrypted, |
| |
552 | EncryptionState::NotEncrypted, |
| |
553 | ) => { |
| |
554 | // Red unlocked lock: |
| |
555 | Span::styled("\u{1F513}\u{FE0E} ", Style::new().fg(Color::Red)) |
| |
556 | }, |
| |
557 | |
| |
558 | (_, EncryptionState::Unknown) => { |
| |
559 | // Yellow question mark: |
| |
560 | Span::styled("? ", Style::new().fg(Color::Yellow)) |
| |
561 | }, |
| |
562 | }; |
| |
563 | |
| |
564 | Some(indicator) |
| |
565 | } |
| |
566 | } |
| |
567 | |
| 433 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
568 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| 434 | pub struct Mouse { |
569 | pub struct Mouse { |
| 435 | #[serde(default)] |
570 | #[serde(default)] |
| @@ -519,6 +654,7 @@ impl SortOverrides { |
| 519 | |
654 | |
| 520 | #[derive(Clone)] |
655 | #[derive(Clone)] |
| 521 | pub struct TunableValues { |
656 | pub struct TunableValues { |
| |
657 | pub encryption: EncryptionValues, |
| 522 | pub log_level: String, |
658 | pub log_level: String, |
| 523 | pub max_log_files: usize, |
659 | pub max_log_files: usize, |
| 524 | pub message_shortcode_display: bool, |
660 | pub message_shortcode_display: bool, |
| @@ -548,6 +684,8 @@ pub struct TunableValues { |
| 548 | |
684 | |
| 549 | #[derive(Clone, Default, Deserialize)] |
685 | #[derive(Clone, Default, Deserialize)] |
| 550 | pub struct Tunables { |
686 | pub struct Tunables { |
| |
687 | #[serde(default)] |
| |
688 | pub encryption: Encryption, |
| 551 | pub log_level: Option<String>, |
689 | pub log_level: Option<String>, |
| 552 | pub max_log_files: Option<usize>, |
690 | pub max_log_files: Option<usize>, |
| 553 | pub message_shortcode_display: Option<bool>, |
691 | pub message_shortcode_display: Option<bool>, |
| @@ -579,6 +717,7 @@ pub struct Tunables { |
| 579 | impl Tunables { |
717 | impl Tunables { |
| 580 | fn merge(self, other: Self) -> Self { |
718 | fn merge(self, other: Self) -> Self { |
| 581 | Tunables { |
719 | Tunables { |
| |
720 | encryption: merge_encryption(self.encryption, other.encryption), |
| 582 | log_level: self.log_level.or(other.log_level), |
721 | log_level: self.log_level.or(other.log_level), |
| 583 | max_log_files: self.max_log_files.or(other.max_log_files), |
722 | max_log_files: self.max_log_files.or(other.max_log_files), |
| 584 | message_shortcode_display: self |
723 | message_shortcode_display: self |
| @@ -616,6 +755,7 @@ impl Tunables { |
| 616 | fn values(self) -> TunableValues { |
755 | fn values(self) -> TunableValues { |
| 617 | TunableValues { |
756 | TunableValues { |
| 618 | log_level: self.log_level.unwrap_or_else(|| "warn".to_string()), |
757 | log_level: self.log_level.unwrap_or_else(|| "warn".to_string()), |
| |
758 | encryption: self.encryption.values(), |
| 619 | max_log_files: self.max_log_files.unwrap_or(7), |
759 | max_log_files: self.max_log_files.unwrap_or(7), |
| 620 | message_shortcode_display: self.message_shortcode_display.unwrap_or(false), |
760 | message_shortcode_display: self.message_shortcode_display.unwrap_or(false), |
| 621 | normal_after_send: self.normal_after_send.unwrap_or(false), |
761 | normal_after_send: self.normal_after_send.unwrap_or(false), |
| @@ -1337,4 +1477,99 @@ mod tests { |
| 1337 | assert!(layout.is_some()); |
1477 | assert!(layout.is_some()); |
| 1338 | assert!(macros.is_some()); |
1478 | assert!(macros.is_some()); |
| 1339 | } |
1479 | } |
| |
1480 | |
| |
1481 | #[test] |
| |
1482 | fn test_encryption_indicator_enabled() { |
| |
1483 | use EncryptionState::*; |
| |
1484 | |
| |
1485 | let enc = EncryptionValues { |
| |
1486 | indicator: EncryptionIndicator::Enabled, |
| |
1487 | indicator_location: EncryptionIndicatorLocation::TITLE, |
| |
1488 | }; |
| |
1489 | |
| |
1490 | // Always shows in the title: |
| |
1491 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Encrypted).is_some()); |
| |
1492 | assert!(enc |
| |
1493 | .get_indicator(EncryptionIndicatorLocation::TITLE, NotEncrypted) |
| |
1494 | .is_some()); |
| |
1495 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Unknown).is_some()); |
| |
1496 | |
| |
1497 | // Doesn't show in the prompt: |
| |
1498 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Encrypted).is_none()); |
| |
1499 | assert!(enc |
| |
1500 | .get_indicator(EncryptionIndicatorLocation::PROMPT, NotEncrypted) |
| |
1501 | .is_none()); |
| |
1502 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Unknown).is_none()); |
| |
1503 | } |
| |
1504 | |
| |
1505 | #[test] |
| |
1506 | fn test_encryption_indicator_disabled() { |
| |
1507 | use EncryptionState::*; |
| |
1508 | |
| |
1509 | let enc = EncryptionValues { |
| |
1510 | indicator: EncryptionIndicator::Disabled, |
| |
1511 | indicator_location: EncryptionIndicatorLocation::TITLE, |
| |
1512 | }; |
| |
1513 | |
| |
1514 | // Never shows in the title or the prompt: |
| |
1515 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Encrypted).is_none()); |
| |
1516 | assert!(enc |
| |
1517 | .get_indicator(EncryptionIndicatorLocation::TITLE, NotEncrypted) |
| |
1518 | .is_none()); |
| |
1519 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Unknown).is_none()); |
| |
1520 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Encrypted).is_none()); |
| |
1521 | assert!(enc |
| |
1522 | .get_indicator(EncryptionIndicatorLocation::PROMPT, NotEncrypted) |
| |
1523 | .is_none()); |
| |
1524 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Unknown).is_none()); |
| |
1525 | } |
| |
1526 | |
| |
1527 | #[test] |
| |
1528 | fn test_encryption_indicator_only_encrypted() { |
| |
1529 | use EncryptionState::*; |
| |
1530 | |
| |
1531 | let enc = EncryptionValues { |
| |
1532 | indicator: EncryptionIndicator::OnlyEncrypted, |
| |
1533 | indicator_location: EncryptionIndicatorLocation::PROMPT, |
| |
1534 | }; |
| |
1535 | |
| |
1536 | // Shows in the prompt when encrypted or unknown: |
| |
1537 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Encrypted).is_some()); |
| |
1538 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Unknown).is_some()); |
| |
1539 | |
| |
1540 | // But is hidden when unencrypted: |
| |
1541 | assert!(enc |
| |
1542 | .get_indicator(EncryptionIndicatorLocation::PROMPT, NotEncrypted) |
| |
1543 | .is_none()); |
| |
1544 | |
| |
1545 | // Doesn't show in the title: |
| |
1546 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Encrypted).is_none()); |
| |
1547 | assert!(enc |
| |
1548 | .get_indicator(EncryptionIndicatorLocation::TITLE, NotEncrypted) |
| |
1549 | .is_none()); |
| |
1550 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Unknown).is_none()); |
| |
1551 | } |
| |
1552 | #[test] |
| |
1553 | fn test_encryption_indicator_only_unencrypted() { |
| |
1554 | use EncryptionState::*; |
| |
1555 | |
| |
1556 | let enc = EncryptionValues { |
| |
1557 | indicator: EncryptionIndicator::OnlyUnencrypted, |
| |
1558 | indicator_location: EncryptionIndicatorLocation::all(), |
| |
1559 | }; |
| |
1560 | |
| |
1561 | // Shows in both the prompt and title when unencrypted or unknown: |
| |
1562 | assert!(enc |
| |
1563 | .get_indicator(EncryptionIndicatorLocation::TITLE, NotEncrypted) |
| |
1564 | .is_some()); |
| |
1565 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Unknown).is_some()); |
| |
1566 | assert!(enc |
| |
1567 | .get_indicator(EncryptionIndicatorLocation::PROMPT, NotEncrypted) |
| |
1568 | .is_some()); |
| |
1569 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Unknown).is_some()); |
| |
1570 | |
| |
1571 | // But is hidden when encrypted: |
| |
1572 | assert!(enc.get_indicator(EncryptionIndicatorLocation::TITLE, Encrypted).is_none()); |
| |
1573 | assert!(enc.get_indicator(EncryptionIndicatorLocation::PROMPT, Encrypted).is_none()); |
| |
1574 | } |
| 1340 | } |
1575 | } |