postfix_log_parser/events/
smtp.rs

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