From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 61F1C1FF0B7 for ; Fri, 13 Nov 2026 16:10:55 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 61CFA21814; Fri, 13 Nov 2026 16:09:40 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox 14/55] notify: move Notification and sub-types to `notification` module Date: Fri, 13 Nov 2026 16:07:47 +0100 Message-ID: <20261113150828.756836-15-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261113150828.756836-1-l.wagner@proxmox.com> References: <20261113150828.756836-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794582524730 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.211 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: G4O6P5IMAGSYML6PKXCVGECCPCYZMRJK X-Message-ID-Hash: G4O6P5IMAGSYML6PKXCVGECCPCYZMRJK X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: No functional changes. Signed-off-by: Lukas Wagner --- proxmox-notify/src/lib.rs | 164 +---------------------------- proxmox-notify/src/notification.rs | 164 +++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 162 deletions(-) create mode 100644 proxmox-notify/src/notification.rs diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index 4407ae80d..f0d0f3866 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -1,14 +1,6 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![deny(unsafe_code)] -use std::{collections::HashMap, fmt::Display, str::FromStr}; - -use serde::{Deserialize, Serialize}; -use serde_json::Value; - -use proxmox_schema::api; -use proxmox_uuid::Uuid; - #[cfg(feature = "api-impl")] pub mod api; @@ -31,157 +23,5 @@ pub use config::Config; pub use context::Context; pub use context::ContextImpl; -#[api()] -#[derive(Clone, Debug, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd)] -#[serde(rename_all = "kebab-case")] -/// Severity of a notification -pub enum Severity { - /// General information - Info, - /// A noteworthy event - Notice, - /// Warning - Warning, - /// Error - Error, - /// Unknown severity (e.g. forwarded system mails) - Unknown, -} - -impl Display for Severity { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { - match self { - Severity::Info => f.write_str("info"), - Severity::Notice => f.write_str("notice"), - Severity::Warning => f.write_str("warning"), - Severity::Error => f.write_str("error"), - Severity::Unknown => f.write_str("unknown"), - } - } -} - -impl FromStr for Severity { - type Err = Error; - fn from_str(s: &str) -> Result { - match s { - "info" => Ok(Self::Info), - "notice" => Ok(Self::Notice), - "warning" => Ok(Self::Warning), - "error" => Ok(Self::Error), - "unknown" => Ok(Self::Unknown), - _ => Err(Error::invalid_config(format!("invalid severity {s}"))), - } - } -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum Content { - /// Title and body will be rendered as a template - #[serde(rename_all = "kebab-case")] - Template { - /// Name of the used template - template_name: String, - /// Data that can be used for template rendering. - data: Value, - }, - #[cfg(feature = "mail-forwarder")] - #[serde(rename_all = "kebab-case")] - ForwardedMail { - /// Raw mail contents - raw: Vec, - /// Fallback title - title: String, - /// Fallback body - body: String, - /// UID to use when calling sendmail - #[allow(dead_code)] // Unused in some feature flag permutations - uid: Option, - }, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub struct Metadata { - /// Notification severity - severity: Severity, - /// Timestamp of the notification as a UNIX epoch - timestamp: i64, - /// Additional fields for additional key-value metadata - additional_fields: HashMap, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -/// Notification which can be sent -pub struct Notification { - /// Notification content - content: Content, - /// Metadata - metadata: Metadata, - /// Unique ID - id: Uuid, -} - -impl Notification { - pub fn from_template>( - severity: Severity, - template_name: S, - template_data: Value, - fields: HashMap, - ) -> Self { - Self { - metadata: Metadata { - severity, - additional_fields: fields, - timestamp: proxmox_time::epoch_i64(), - }, - content: Content::Template { - template_name: template_name.as_ref().to_string(), - data: template_data, - }, - id: Uuid::generate(), - } - } - #[cfg(feature = "mail-forwarder")] - pub fn new_forwarded_mail(raw_mail: &[u8], uid: Option) -> Result { - let message = mail_parser::MessageParser::default() - .parse(raw_mail) - .ok_or_else(|| Error::invalid_forwarded_message("could not parse forwarded email"))?; - - let title = message.subject().unwrap_or_default().into(); - let body = message.body_text(0).unwrap_or_default().into(); - - let mut additional_fields = HashMap::new(); - additional_fields.insert("hostname".into(), proxmox_sys::nodename().into()); - additional_fields.insert("type".into(), "system-mail".into()); - - Ok(Self { - // Unfortunately we cannot reasonably infer the severity from the - // mail contents, so just set it to the highest for now so that - // it is not filtered out. - content: Content::ForwardedMail { - raw: raw_mail.into(), - title, - body, - uid, - }, - metadata: Metadata { - severity: Severity::Unknown, - additional_fields, - timestamp: proxmox_time::epoch_i64(), - }, - id: Uuid::generate(), - }) - } - - /// Return the unique ID of this notification. - pub fn id(&self) -> &Uuid { - &self.id - } - - /// Return the notification's timestamp - pub fn timestamp(&self) -> i64 { - self.metadata.timestamp - } -} +mod notification; +pub use notification::*; diff --git a/proxmox-notify/src/notification.rs b/proxmox-notify/src/notification.rs new file mode 100644 index 000000000..426a2259a --- /dev/null +++ b/proxmox-notify/src/notification.rs @@ -0,0 +1,164 @@ +use std::{collections::HashMap, fmt::Display, str::FromStr}; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use proxmox_schema::api; +use proxmox_uuid::Uuid; + +use crate::Error; + +#[api] +#[derive(Clone, Debug, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd)] +#[serde(rename_all = "kebab-case")] +/// Severity of a notification +pub enum Severity { + /// General information + Info, + /// A noteworthy event + Notice, + /// Warning + Warning, + /// Error + Error, + /// Unknown severity (e.g. forwarded system mails) + Unknown, +} + +impl Display for Severity { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { + match self { + Severity::Info => f.write_str("info"), + Severity::Notice => f.write_str("notice"), + Severity::Warning => f.write_str("warning"), + Severity::Error => f.write_str("error"), + Severity::Unknown => f.write_str("unknown"), + } + } +} + +impl FromStr for Severity { + type Err = Error; + fn from_str(s: &str) -> Result { + match s { + "info" => Ok(Self::Info), + "notice" => Ok(Self::Notice), + "warning" => Ok(Self::Warning), + "error" => Ok(Self::Error), + "unknown" => Ok(Self::Unknown), + _ => Err(Error::invalid_config(format!("invalid severity {s}"))), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Content { + /// Title and body will be rendered as a template + #[serde(rename_all = "kebab-case")] + Template { + /// Name of the used template + template_name: String, + /// Data that can be used for template rendering. + data: Value, + }, + #[cfg(feature = "mail-forwarder")] + #[serde(rename_all = "kebab-case")] + ForwardedMail { + /// Raw mail contents + raw: Vec, + /// Fallback title + title: String, + /// Fallback body + body: String, + /// UID to use when calling sendmail + #[allow(dead_code)] // Unused in some feature flag permutations + uid: Option, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct Metadata { + /// Notification severity + pub(crate) severity: Severity, + /// Timestamp of the notification as a UNIX epoch + pub(crate) timestamp: i64, + /// Additional fields for additional key-value metadata + pub(crate) additional_fields: HashMap, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +/// Notification which can be sent +pub struct Notification { + /// Notification content + pub(crate) content: Content, + /// Metadata + pub(crate) metadata: Metadata, + /// Unique ID + pub(crate) id: Uuid, +} + +impl Notification { + pub fn from_template>( + severity: Severity, + template_name: S, + template_data: Value, + fields: HashMap, + ) -> Self { + Self { + metadata: Metadata { + severity, + additional_fields: fields, + timestamp: proxmox_time::epoch_i64(), + }, + content: Content::Template { + template_name: template_name.as_ref().to_string(), + data: template_data, + }, + id: Uuid::generate(), + } + } + #[cfg(feature = "mail-forwarder")] + pub fn new_forwarded_mail(raw_mail: &[u8], uid: Option) -> Result { + let message = mail_parser::MessageParser::default() + .parse(raw_mail) + .ok_or_else(|| Error::invalid_forwarded_message("could not parse forwarded email"))?; + + let title = message.subject().unwrap_or_default().into(); + let body = message.body_text(0).unwrap_or_default().into(); + + let mut additional_fields = HashMap::new(); + additional_fields.insert("hostname".into(), proxmox_sys::nodename().into()); + additional_fields.insert("type".into(), "system-mail".into()); + + Ok(Self { + // Unfortunately we cannot reasonably infer the severity from the + // mail contents, so just set it to the highest for now so that + // it is not filtered out. + content: Content::ForwardedMail { + raw: raw_mail.into(), + title, + body, + uid, + }, + metadata: Metadata { + severity: Severity::Unknown, + additional_fields, + timestamp: proxmox_time::epoch_i64(), + }, + id: Uuid::generate(), + }) + } + + /// Return the unique ID of this notification. + pub fn id(&self) -> &Uuid { + &self.id + } + + /// Return the notification's timestamp + pub fn timestamp(&self) -> i64 { + self.metadata.timestamp + } +} -- 2.47.3