1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
1415use matrix_sdk_crypto::OlmMachine;
16use ruma::{
17 events::{
18 room::message::MessageType, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
19 SyncMessageLikeEvent,
20 },
21 RoomId,
22};
2324use super::{e2ee::E2EE, Context};
25use crate::Result;
2627/// Process the given event as a verification event if it is a candidate. The
28/// event must be decrypted.
29pub async fn process_if_relevant(
30 context: &mut Context,
31 event: &AnySyncTimelineEvent,
32 e2ee: E2EE<'_>,
33 room_id: &RoomId,
34) -> Result<()> {
35if let AnySyncTimelineEvent::MessageLike(event) = event {
36// That's it, we are good, the event has been decrypted successfully.
3738 // However, let's run an additional action. Check if this is a verification
39 // event (`m.key.verification.*`), and call `verification` accordingly.
40if match &event {
41// This is an original (i.e. non-redacted) `m.room.message` event and its
42 // content is a verification request…
43AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(
44 original_event,
45 )) => {
46matches!(&original_event.content.msgtype, MessageType::VerificationRequest(_))
47 }
4849// … or this is verification request event
50AnySyncMessageLikeEvent::KeyVerificationReady(_)
51 | AnySyncMessageLikeEvent::KeyVerificationStart(_)
52 | AnySyncMessageLikeEvent::KeyVerificationCancel(_)
53 | AnySyncMessageLikeEvent::KeyVerificationAccept(_)
54 | AnySyncMessageLikeEvent::KeyVerificationKey(_)
55 | AnySyncMessageLikeEvent::KeyVerificationMac(_)
56 | AnySyncMessageLikeEvent::KeyVerificationDone(_) => true,
5758_ => false,
59 } {
60 verification(context, e2ee.verification_is_allowed, e2ee.olm_machine, event, room_id)
61 .await?;
62 }
63 }
6465Ok(())
66}
6768async fn verification(
69 _context: &mut Context,
70 verification_is_allowed: bool,
71 olm_machine: Option<&OlmMachine>,
72 event: &AnySyncMessageLikeEvent,
73 room_id: &RoomId,
74) -> Result<()> {
75if !verification_is_allowed {
76return Ok(());
77 }
7879if let Some(olm) = olm_machine {
80 olm.receive_verification_event(&event.clone().into_full_event(room_id.to_owned())).await?;
81 }
8283Ok(())
84}