mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
Refactor handle_degrade_event
This commit is contained in:
committed by
holger krekel
parent
1c2b4fa7fc
commit
55389c4190
@@ -15,7 +15,6 @@ use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey};
|
|||||||
use crate::keyring::*;
|
use crate::keyring::*;
|
||||||
use crate::peerstate::*;
|
use crate::peerstate::*;
|
||||||
use crate::pgp;
|
use crate::pgp;
|
||||||
use crate::securejoin::handle_degrade_event;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EncryptHelper {
|
pub struct EncryptHelper {
|
||||||
@@ -166,9 +165,7 @@ pub async fn try_decrypt(
|
|||||||
peerstate = Peerstate::from_addr(&context, &from).await?;
|
peerstate = Peerstate::from_addr(&context, &from).await?;
|
||||||
}
|
}
|
||||||
if let Some(peerstate) = peerstate {
|
if let Some(peerstate) = peerstate {
|
||||||
if peerstate.degrade_event.is_some() {
|
peerstate.handle_degrade_event(context).await?;
|
||||||
handle_degrade_event(context, &peerstate).await?;
|
|
||||||
}
|
|
||||||
if let Some(key) = peerstate.gossip_key {
|
if let Some(key) = peerstate.gossip_key {
|
||||||
public_keyring_for_validate.add(key);
|
public_keyring_for_validate.add(key);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ use crate::location;
|
|||||||
use crate::message;
|
use crate::message;
|
||||||
use crate::param::*;
|
use crate::param::*;
|
||||||
use crate::peerstate::Peerstate;
|
use crate::peerstate::Peerstate;
|
||||||
use crate::securejoin::handle_degrade_event;
|
|
||||||
use crate::simplify::*;
|
use crate::simplify::*;
|
||||||
use crate::stock::StockMessage;
|
use crate::stock::StockMessage;
|
||||||
|
|
||||||
@@ -1047,9 +1046,7 @@ async fn update_gossip_peerstates(
|
|||||||
peerstate = Some(p);
|
peerstate = Some(p);
|
||||||
}
|
}
|
||||||
if let Some(peerstate) = peerstate {
|
if let Some(peerstate) = peerstate {
|
||||||
if peerstate.degrade_event.is_some() {
|
peerstate.handle_degrade_event(context).await?;
|
||||||
handle_degrade_event(context, &peerstate).await?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gossipped_addr.insert(header.addr.clone());
|
gossipped_addr.insert(header.addr.clone());
|
||||||
|
|||||||
@@ -5,10 +5,14 @@ use std::fmt;
|
|||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
|
|
||||||
use crate::aheader::*;
|
use crate::aheader::*;
|
||||||
|
use crate::chat;
|
||||||
|
use crate::constants::Blocked;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::error::Result;
|
use crate::error::{bail, Result};
|
||||||
|
use crate::events::EventType;
|
||||||
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
|
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
|
||||||
use crate::sql::Sql;
|
use crate::sql::Sql;
|
||||||
|
use crate::stock::StockMessage;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PeerstateKeyType {
|
pub enum PeerstateKeyType {
|
||||||
@@ -264,6 +268,43 @@ impl<'a> Peerstate<'a> {
|
|||||||
self.to_save = Some(ToSave::All);
|
self.to_save = Some(ToSave::All);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a warning to the chat corresponding to peerstate if fingerprint has changed.
|
||||||
|
pub(crate) async fn handle_degrade_event(&self, context: &Context) -> Result<()> {
|
||||||
|
match self.degrade_event {
|
||||||
|
Some(DegradeEvent::FingerprintChanged) => {
|
||||||
|
if let Some(contact_id) = context
|
||||||
|
.sql
|
||||||
|
.query_get_value_result(
|
||||||
|
"SELECT id FROM contacts WHERE addr=?;",
|
||||||
|
paramsv![self.addr],
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
let (contact_chat_id, _) = chat::create_or_lookup_by_contact_id(
|
||||||
|
context,
|
||||||
|
contact_id,
|
||||||
|
Blocked::Deaddrop,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let msg = context
|
||||||
|
.stock_string_repl_str(StockMessage::ContactSetupChanged, self.addr.clone())
|
||||||
|
.await;
|
||||||
|
|
||||||
|
chat::add_info_msg(context, contact_chat_id, msg).await;
|
||||||
|
emit_event!(context, EventType::ChatModified(contact_chat_id));
|
||||||
|
} else {
|
||||||
|
bail!("contact with peerstate.addr {:?} not found", &self.addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A warning for this is not issued, as it is quite normal.
|
||||||
|
Some(DegradeEvent::EncryptionPaused) => {}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn apply_header(&mut self, header: &Aheader, message_time: i64) {
|
pub fn apply_header(&mut self, header: &Aheader, message_time: i64) {
|
||||||
if self.addr.to_lowercase() != header.addr.to_lowercase() {
|
if self.addr.to_lowercase() != header.addr.to_lowercase() {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1036,45 +1036,3 @@ fn encrypted_and_signed(
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_degrade_event(
|
|
||||||
context: &Context,
|
|
||||||
peerstate: &Peerstate<'_>,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
// - we do not issue an warning for DC_DE_ENCRYPTION_PAUSED as this is quite normal
|
|
||||||
// - currently, we do not issue an extra warning for DC_DE_VERIFICATION_LOST - this always comes
|
|
||||||
// together with DC_DE_FINGERPRINT_CHANGED which is logged, the idea is not to bother
|
|
||||||
// with things they cannot fix, so the user is just kicked from the verified group
|
|
||||||
// (and he will know this and can fix this)
|
|
||||||
if Some(DegradeEvent::FingerprintChanged) == peerstate.degrade_event {
|
|
||||||
let contact_id: i32 = match context
|
|
||||||
.sql
|
|
||||||
.query_get_value(
|
|
||||||
context,
|
|
||||||
"SELECT id FROM contacts WHERE addr=?;",
|
|
||||||
paramsv![peerstate.addr],
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
None => bail!(
|
|
||||||
"contact with peerstate.addr {:?} not found",
|
|
||||||
&peerstate.addr
|
|
||||||
),
|
|
||||||
Some(contact_id) => contact_id,
|
|
||||||
};
|
|
||||||
if contact_id > 0 {
|
|
||||||
let (contact_chat_id, _) =
|
|
||||||
chat::create_or_lookup_by_contact_id(context, contact_id as u32, Blocked::Deaddrop)
|
|
||||||
.await
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let msg = context
|
|
||||||
.stock_string_repl_str(StockMessage::ContactSetupChanged, peerstate.addr.clone())
|
|
||||||
.await;
|
|
||||||
|
|
||||||
chat::add_info_msg(context, contact_chat_id, msg).await;
|
|
||||||
emit_event!(context, EventType::ChatModified(contact_chat_id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user