Commit c142db3205

c142db3205658e013d03044eaf5ca7cde3cbd7c3

parent: c3176fac0f

Unsigned

Sandro Santilli (AI assisted) <strk@kbt.io> · 2026-09-16 10:01 UTC

fix: surface actionable guidance for undecryptable messages

Show the sender and a hint with the room key recovery steps when a
message can't be decrypted, and surface m.room_key.withheld
notifications with the reason the sender did not share the key.

Closes: #308
Assisted-By: big-pickle via opencode 1.18.18

Layout: unified · split

src/render.rs +33 −9
@@ -629,25 +629,49 @@ impl<C: HasUrlOrFile> Render for C {
629 629
630impl Render for RoomEncryptedEventContent { 630impl Render for RoomEncryptedEventContent {
631 const TAGS: &'static [&'static str] = &["matrix_encrypted"]; 631 const TAGS: &'static [&'static str] = &["matrix_encrypted"];
632 type RenderContext = (); 632 type RenderContext = WeechatRoomMember;
633 633
634 fn render(&self, _: &Self::RenderContext) -> RenderedContent { 634 fn render(&self, sender: &Self::RenderContext) -> RenderedContent {
635 // The prefix already shows the sender, but naming them in the
636 // placeholder makes the guidance below unambiguous.
635 let message = format!( 637 let message = format!(
636 "{}<{}Unable to decrypt message{}>{}", 638 "{}<{}Unable to decrypt message from {}{}>{}",
637 Weechat::color("chat_delimiters"), 639 Weechat::color("chat_delimiters"),
638 Weechat::color("logger.color.backlog_line"), 640 Weechat::color("logger.color.backlog_line"),
641 sender.nick(),
639 Weechat::color("chat_delimiters"), 642 Weechat::color("chat_delimiters"),
640 Weechat::color("reset"), 643 Weechat::color("reset"),
641 ); 644 );
642 645
643 let line = RenderedLine { 646 let tags = self.tags();
647 let mut lines = vec![RenderedLine {
644 message, 648 message,
645 // TODO: add tags that allow us decrypt the event at a later point in 649 // TODO: add tags that allow us decrypt the event at a later point
646 // time, sender key, algorithm, session id. 650 // in time, sender key, algorithm, session id.
647 tags: self.tags(), 651 tags: tags.clone(),
648 }; 652 }];
653
654 // The room key is missing, which usually means the sender's client is
655 // not sharing keys with us. Print actionable guidance: only the sender
656 // can help (re-send the message or forward the room key from another
657 // device). The event is retried and re-rendered automatically once the
658 // key arrives (see #274).
659 lines.push(RenderedLine {
660 message: format!(
661 "{}<{}Waiting for the room key from {}. Ask them to re-send \
662 their message or forward the room key from one of their \
663 other devices. This message will be decrypted automatically \
664 once the key arrives.{}>{}",
665 Weechat::color("chat_delimiters"),
666 Weechat::color("logger.color.backlog_line"),
667 sender.nick(),
668 Weechat::color("chat_delimiters"),
669 Weechat::color("reset"),
670 ),
671 tags,
672 });
649 673
650 RenderedContent::new(vec![line]) 674 RenderedContent::new(lines)
651 } 675 }
652} 676}
653 677
src/room/mod.rs +1 −1
@@ -1377,7 +1377,7 @@ impl MatrixRoom {
1377 1377
1378 let rendered = match content { 1378 let rendered = match content {
1379 RoomEncrypted(c) => { 1379 RoomEncrypted(c) => {
1380 c.render_with_prefix(send_time, event_id, sender, &()) 1380 c.render_with_prefix(send_time, event_id, sender, sender)
1381 } 1381 }
1382 RoomMessage(c) => { 1382 RoomMessage(c) => {
1383 let reply_to = match c.relates_to.as_ref() { 1383 let reply_to = match c.relates_to.as_ref() {
src/server.rs +89
@@ -1668,6 +1668,95 @@ impl InnerServer {
1668 } 1668 }
1669 } 1669 }
1670 AnyToDeviceEvent::RoomKeyRequest(_) => {} 1670 AnyToDeviceEvent::RoomKeyRequest(_) => {}
1671 AnyToDeviceEvent::RoomKeyWithheld(e) => {
1672 use matrix_sdk::ruma::events::room_key::withheld::{
1673 RoomKeyWithheldCodeInfo, RoomKeyWithheldSessionData,
1674 };
1675
1676 // The sender's client proactively told us it will not share
1677 // the room key. Surface an actionable hint, routed to the
1678 // room buffer when we know which room the key belongs to.
1679 // Each recognized code gets its own remediation; the raw
1680 // reason is only shown when the code is unknown, as ruma
1681 // documents `reason` as a fallback for unrecognized codes.
1682 let code = &e.content.code;
1683 let session_data: Option<&RoomKeyWithheldSessionData> =
1684 match code {
1685 RoomKeyWithheldCodeInfo::Blacklisted(data) => {
1686 Some(data)
1687 }
1688 RoomKeyWithheldCodeInfo::Unverified(data) => Some(data),
1689 RoomKeyWithheldCodeInfo::Unauthorized(data) => {
1690 Some(data)
1691 }
1692 RoomKeyWithheldCodeInfo::Unavailable(data) => {
1693 Some(data)
1694 }
1695 RoomKeyWithheldCodeInfo::NoOlm => None,
1696 _ => None,
1697 };
1698
1699 let message = match code {
1700 RoomKeyWithheldCodeInfo::Unavailable(_) => format!(
1701 "{} withheld the room key: the room key is not \
1702 available on their device. Ask them to re-send \
1703 their message or forward the room key from another \
1704 of their devices.",
1705 e.sender
1706 ),
1707 RoomKeyWithheldCodeInfo::Unverified(_) => format!(
1708 "{} withheld the room key: your device is not \
1709 verified and they only share keys with verified \
1710 devices. Verify your device with them so the room \
1711 key gets shared.",
1712 e.sender
1713 ),
1714 RoomKeyWithheldCodeInfo::Blacklisted(_) => format!(
1715 "{} withheld the room key: your device is \
1716 blacklisted. Ask them to unblacklist your device to \
1717 receive the room key.",
1718 e.sender
1719 ),
1720 RoomKeyWithheldCodeInfo::Unauthorized(_) => format!(
1721 "{} withheld the room key: your device is not \
1722 allowed to receive it. Ask them to share the room \
1723 key with you.",
1724 e.sender
1725 ),
1726 RoomKeyWithheldCodeInfo::NoOlm => format!(
1727 "{} withheld the room key: no Olm session could be \
1728 established with your device. Make sure both \
1729 devices are online so an Olm session can be \
1730 established, then ask them to share the room key \
1731 again.",
1732 e.sender
1733 ),
1734 other => {
1735 let mut message = format!(
1736 "{} withheld the room key with code: {}.",
1737 e.sender,
1738 other.code()
1739 );
1740 if let Some(reason) = &e.content.reason {
1741 message
1742 .push_str(&format!(" Reason given: {reason}"));
1743 }
1744 message
1745 }
1746 };
1747
1748 let buffer = session_data.and_then(|data| {
1749 self.rooms
1750 .borrow()
1751 .get(&data.room_id)
1752 .map(|room| room.buffer_handle())
1753 });
1754 self.print_with_prefix_to(
1755 buffer.as_ref(),
1756 &Weechat::prefix(Prefix::Network),
1757 &message,
1758 );
1759 }
1671 AnyToDeviceEvent::KeyVerificationRequest(e) => { 1760 AnyToDeviceEvent::KeyVerificationRequest(e) => {
1672 refresh_status_bar = true; 1761 refresh_status_bar = true;
1673 if let Some(client) = self.get_client() { 1762 if let Some(client) = self.get_client() {