Migration implementation (part 1)

This commit is contained in:
mdecimus
2025-12-10 19:13:04 +01:00
parent efd1d86ff3
commit f1fe0345ef
86 changed files with 2553 additions and 356 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "http"
version = "0.14.1"
version = "0.15.0"
edition = "2024"
[dependencies]

View File

@@ -7,8 +7,8 @@
use common::{Server, auth::AccessToken};
use directory::backend::internal::manage;
use email::message::crypto::{
EncryptMessage, EncryptMessageError, EncryptionMethod, EncryptionParams, EncryptionType,
try_parse_certs,
ENCRYPT_TRAIN_SPAM_FILTER, EncryptMessage, EncryptMessageError, EncryptionMethod,
EncryptionParams, EncryptionType, try_parse_certs,
};
use http_proto::*;
use mail_builder::encoders::base64::base64_encode_mime;
@@ -52,6 +52,7 @@ impl CryptoHandler for Server {
.caused_by(trc::location!())?;
let algo = params.algo();
let method = params.method();
let allow_spam_training = params.can_train_spam_filter();
let mut certs = Vec::new();
certs.extend_from_slice(b"-----STALWART CERTIFICATE-----\r\n");
let _ = base64_encode_mime(&params_.into_inner(), &mut certs, false);
@@ -59,8 +60,16 @@ impl CryptoHandler for Server {
let certs = String::from_utf8(certs).unwrap_or_default();
match method {
EncryptionMethod::PGP => EncryptionType::PGP { algo, certs },
EncryptionMethod::SMIME => EncryptionType::SMIME { algo, certs },
EncryptionMethod::PGP => EncryptionType::PGP {
algo,
certs,
allow_spam_training,
},
EncryptionMethod::SMIME => EncryptionType::SMIME {
algo,
certs,
allow_spam_training,
},
}
} else {
EncryptionType::Disabled
@@ -80,9 +89,17 @@ impl CryptoHandler for Server {
let request = serde_json::from_slice::<EncryptionType>(body.as_deref().unwrap_or_default())
.map_err(|err| trc::ResourceEvent::BadParameters.into_err().reason(err))?;
let (method, algo, mut certs) = match request {
EncryptionType::PGP { algo, certs } => (EncryptionMethod::PGP, algo, certs),
EncryptionType::SMIME { algo, certs } => (EncryptionMethod::SMIME, algo, certs),
let (method, algo, mut certs, allow_spam_training) = match request {
EncryptionType::PGP {
algo,
certs,
allow_spam_training,
} => (EncryptionMethod::PGP, algo, certs, allow_spam_training),
EncryptionType::SMIME {
algo,
certs,
allow_spam_training,
} => (EncryptionMethod::SMIME, algo, certs, allow_spam_training),
EncryptionType::Disabled => {
// Disable encryption at rest
let mut batch = BatchBuilder::new();
@@ -110,12 +127,17 @@ impl CryptoHandler for Server {
}
// Parse certificates
let todo = "fetch privacy spam train";
let certs = try_parse_certs(method, certs.into_bytes())
.map_err(|err| manage::error(err, None::<u32>))?;
let num_certs = certs.len();
let params = Archiver::new(EncryptionParams {
flags: method.flags() | algo.flags(),
flags: method.flags()
| algo.flags()
| if allow_spam_training {
ENCRYPT_TRAIN_SPAM_FILTER
} else {
0
},
certs,
})
.serialize()

View File

@@ -21,6 +21,7 @@ use serde_json::json;
use spam_filter::{
SpamFilterInput,
analysis::{init::SpamFilterInit, score::SpamFilterAnalyzeScore},
modules::classifier::SpamClassifier,
};
use std::future::Future;
use std::net::IpAddr;
@@ -137,14 +138,42 @@ impl ManageSpamHandler for Server {
}))
.into_http_response())
}
(Some("train"), _, &Method::GET) => {
(Some("train"), request, &Method::GET) => {
// Validate the access token
access_token.assert_has_permission(Permission::SpamFilterTrain)?;
let todo = "implement";
let result = match request {
Some("start") | Some("reset") => {
if !self.inner.ipc.train_task_controller.is_running() {
let reset = matches!(request, Some("reset"));
let server = self.clone();
tokio::spawn(async move {
if let Err(err) = server.spam_train(reset).await {
trc::error!(err.caused_by(trc::location!()));
}
});
true
} else {
false
}
}
Some("stop") => {
if self.inner.ipc.train_task_controller.is_running() {
self.inner.ipc.train_task_controller.stop();
true
} else {
false
}
}
Some("status") => self.inner.ipc.train_task_controller.is_running(),
_ => {
return Err(trc::ResourceEvent::NotFound.into_err());
}
};
Ok(JsonResponse::new(json!({
"data": (),
"data": result,
}))
.into_http_response())
}