postfix_log_parser/events/
smtp.rs

1//! SMTP客户端投递模块
2//!
3//! 处理Postfix smtp客户端的事件,包括外部邮件投递、SMTP会话和投递状态报告
4
5use serde::{Deserialize, Serialize};
6
7/// SMTP投递组件事件
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum SmtpEvent {
10    /// 投递成功
11    Sent {
12        queue_id: String,
13        to: String,
14        relay_hostname: String,
15        relay_ip: Option<String>,
16        relay_port: Option<u16>,
17        delay: f64,
18        delay_before_queue: Option<f64>,
19        delay_in_queue: Option<f64>,
20        delay_connection: Option<f64>,
21        delay_transmission: Option<f64>,
22        dsn: Option<String>,
23        status: String,
24        message_size: Option<u64>,
25    },
26    /// 投递延迟
27    Deferred {
28        queue_id: String,
29        to: Option<String>,
30        relay_hostname: Option<String>,
31        relay_ip: Option<String>,
32        relay_port: Option<u16>,
33        delay: Option<f64>,
34        delay_before_queue: Option<f64>,
35        delay_in_queue: Option<f64>,
36        delay_connection: Option<f64>,
37        delay_transmission: Option<f64>,
38        dsn: Option<String>,
39        status: String,
40        defer_reason: String,
41    },
42    /// 投递失败(退信)
43    Bounced {
44        queue_id: String,
45        to: String,
46        relay_hostname: Option<String>,
47        relay_ip: Option<String>,
48        relay_port: Option<u16>,
49        delay: Option<f64>,
50        delay_before_queue: Option<f64>,
51        delay_in_queue: Option<f64>,
52        delay_connection: Option<f64>,
53        delay_transmission: Option<f64>,
54        dsn: Option<String>,
55        status: String,
56        bounce_reason: String,
57    },
58    /// 连接超时
59    ConnectionTimeout {
60        queue_id: String,
61        target_hostname: String,
62        target_ip: String,
63        target_port: u16,
64        timeout_duration: Option<u32>,
65    },
66    /// 连接拒绝
67    ConnectionRefused {
68        queue_id: String,
69        target_hostname: String,
70        target_ip: String,
71        target_port: u16,
72    },
73    /// 连接丢失
74    ConnectionLost {
75        queue_id: String,
76        target_hostname: String,
77        target_ip: String,
78        lost_stage: String,
79    },
80    /// 协议交互
81    ProtocolInteraction {
82        queue_id: String,
83        interaction_type: String,
84        details: String,
85    },
86    /// 其他SMTP事件
87    Other {
88        queue_id: Option<String>,
89        event_type: String,
90        message: String,
91    },
92}
93
94impl SmtpEvent {
95    pub fn event_type(&self) -> &'static str {
96        match self {
97            SmtpEvent::Sent { .. } => "sent",
98            SmtpEvent::Deferred { .. } => "deferred",
99            SmtpEvent::Bounced { .. } => "bounced",
100            SmtpEvent::ConnectionTimeout { .. } => "connection_timeout",
101            SmtpEvent::ConnectionRefused { .. } => "connection_refused",
102            SmtpEvent::ConnectionLost { .. } => "connection_lost",
103            SmtpEvent::ProtocolInteraction { .. } => "protocol_interaction",
104            SmtpEvent::Other { .. } => "other",
105        }
106    }
107
108    pub fn queue_id(&self) -> Option<&str> {
109        match self {
110            SmtpEvent::Sent { queue_id, .. } => Some(queue_id),
111            SmtpEvent::Deferred { queue_id, .. } => Some(queue_id),
112            SmtpEvent::Bounced { queue_id, .. } => Some(queue_id),
113            SmtpEvent::ConnectionTimeout { queue_id, .. } => Some(queue_id),
114            SmtpEvent::ConnectionRefused { queue_id, .. } => Some(queue_id),
115            SmtpEvent::ConnectionLost { queue_id, .. } => Some(queue_id),
116            SmtpEvent::ProtocolInteraction { queue_id, .. } => Some(queue_id),
117            SmtpEvent::Other { queue_id, .. } => queue_id.as_deref(),
118        }
119    }
120}