Check for encryption
- collect body of incoming mail. - check if incoming mail is already encrypted - dont touch it if it is. - check if recipients of that mail can encrypt.
This commit is contained in:
@@ -5,16 +5,25 @@
|
||||
//
|
||||
|
||||
use std::sync::Arc;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use sequoia_cert_store::LazyCert;
|
||||
|
||||
use crate::types::recipient::Recipient;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum ProtectionPossibility {
|
||||
NoProtection,
|
||||
Partial,
|
||||
Full
|
||||
}
|
||||
|
||||
|
||||
pub struct MailContext<'mc> {
|
||||
pub sender: Option<String>,
|
||||
pub recipients: Vec<Recipient<'mc>>,
|
||||
// header
|
||||
// body
|
||||
// body size
|
||||
body: Vec<Bytes>,
|
||||
pub body_size: usize,
|
||||
}
|
||||
|
||||
impl<'mc> MailContext<'mc> {
|
||||
@@ -23,6 +32,8 @@ impl<'mc> MailContext<'mc> {
|
||||
MailContext {
|
||||
sender: None,
|
||||
recipients: Vec::new(),
|
||||
body: Vec::new(),
|
||||
body_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +50,39 @@ impl<'mc> MailContext<'mc> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_chunk(&mut self, chunk: Bytes) {
|
||||
self.body_size += chunk.len();
|
||||
self.body.push(chunk);
|
||||
}
|
||||
|
||||
pub fn collect_body(&self) -> Bytes {
|
||||
let mut buffer = BytesMut::with_capacity(self.body_size);
|
||||
for chunk in &self.body {
|
||||
buffer.extend_from_slice(&chunk[..]);
|
||||
}
|
||||
buffer.into()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl From<&MailContext<'_>> for ProtectionPossibility {
|
||||
fn from(mail_context: &MailContext<'_>) -> ProtectionPossibility {
|
||||
let mut can_encrypt = false;
|
||||
let mut cannot_encrypt = false;
|
||||
|
||||
for rcpt in mail_context.recipients.iter() {
|
||||
if rcpt.can_encrypt() {
|
||||
can_encrypt = true;
|
||||
} else {
|
||||
cannot_encrypt = true;
|
||||
}
|
||||
}
|
||||
|
||||
match (can_encrypt, cannot_encrypt) {
|
||||
(true, false) => ProtectionPossibility::Full,
|
||||
(true, true) => ProtectionPossibility::Partial,
|
||||
(false, true) => ProtectionPossibility::NoProtection,
|
||||
(false, false) => ProtectionPossibility::NoProtection, // this should not occure
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,4 +36,13 @@ impl TryFrom<CString> for Recipient<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Recipient<'_> {
|
||||
pub fn can_encrypt(&self) -> bool {
|
||||
if self.certs.len() == 0 {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user