Skip to main content

rmilter/
message_handler.rs

1use crate::accept_reject_action::AcceptRejectAction;
2use crate::milter_message::{MilterMacro, ProtocolFamily};
3
4/// Implement this trait to define the behavior of your milter application.
5///
6/// All methods have a default implementation which returns AcceptRejectAction::Continue. Overwrite
7/// any of these methods to implement the desired behavior.
8pub trait MessageHandler {
9    /// Milter checks for the current message have been aborted (SMFIC_ABORT).
10    ///
11    /// # Example:
12    /// ```
13    /// use rmilter::message_handler::MessageHandler;
14    /// use rmilter::milter_message::MilterMacro;
15    ///
16    /// struct MyMessageHandler {}
17    ///
18    /// impl MessageHandler for MyMessageHandler {
19    ///     fn abort_filter_checks(&mut self) {
20    ///         println!("Aborted filter checks");
21    ///     }
22    /// }
23    /// ```
24    fn abort_filter_checks(&mut self) {}
25
26    /// A body chunk of the incoming email (SMFIC_BODY).
27    ///
28    /// - `value` contains the value of the body chunk.
29    ///
30    /// # Example:
31    /// ```
32    /// use rmilter::accept_reject_action::AcceptRejectAction;
33    /// use rmilter::message_handler::MessageHandler;
34    ///
35    /// struct MyMessageHandler {}
36    ///
37    /// impl MessageHandler for MyMessageHandler {
38    ///     fn body_chunk(&mut self, value: &str) -> AcceptRejectAction {
39    ///         println!("value: {}", value);
40    ///         AcceptRejectAction::Continue
41    ///     }
42    /// }
43    /// ```
44    #[allow(unused_variables)]
45    fn body_chunk(&mut self, value: &str) -> AcceptRejectAction {
46        AcceptRejectAction::Continue
47    }
48
49    /// Provides information about the connection to the MTA (SMFIC_CONNECT).
50    ///
51    /// - `hostname` The hostname of the machine running the MTA.
52    /// - `family` The protocol family used.
53    /// - `port` The used port (Inet4 and Inet6 only).
54    /// - `address` The IP address or socket path used.
55    ///
56    /// # Example:
57    /// ```
58    /// use rmilter::accept_reject_action::AcceptRejectAction;
59    /// use rmilter::message_handler::MessageHandler;
60    /// use rmilter::milter_message::ProtocolFamily;
61    ///
62    /// struct MyMessageHandler {}
63    ///
64    /// impl MessageHandler for MyMessageHandler {
65    ///     fn connection(
66    ///     &mut self,
67    ///     hostname: &str,
68    ///     family: &ProtocolFamily,
69    ///     port: &u16,
70    ///     address: &str,
71    ///     ) -> AcceptRejectAction {
72    ///         println!(
73    ///             "hostname: {}, family: {:?}, port: {}, address: {}",
74    ///             hostname, family, port, address
75    ///         );
76    ///         AcceptRejectAction::Continue
77    ///     }
78    /// }
79    /// ```
80    #[allow(unused_variables)]
81    fn connection(
82        &mut self,
83        hostname: &str,
84        family: &ProtocolFamily,
85        port: &u16,
86        address: &str,
87    ) -> AcceptRejectAction {
88        AcceptRejectAction::Continue
89    }
90
91    /// A set of macros defined by the MTA (SMFIC_MACRO).
92    ///
93    /// - `cmdcode` represents the command for which the macros are defined.
94    /// - `macros` contains the defined macros.
95    ///
96    /// # Example:
97    /// ```
98    /// use rmilter::message_handler::MessageHandler;
99    /// use rmilter::milter_message::MilterMacro;
100    ///
101    /// struct MyMessageHandler {}
102    ///
103    /// impl MessageHandler for MyMessageHandler {
104    ///     fn define_macros(&mut self, cmdcode: &char, macros: Vec<MilterMacro>) {
105    ///         println!("cmdcode: {}", cmdcode);
106    ///     }
107    /// }
108    /// ```
109    #[allow(unused_variables)]
110    fn define_macros(&mut self, cmdcode: &char, macros: Vec<MilterMacro>) {}
111
112    /// The MTA informs that all body chunks of the message are sent (SMFIC_BODYEOB).
113    ///
114    /// # Example:
115    /// ```
116    /// use rmilter::accept_reject_action::AcceptRejectAction;
117    /// use rmilter::message_handler::MessageHandler;
118    ///
119    /// struct MyMessageHandler {}
120    ///
121    /// impl MessageHandler for MyMessageHandler {
122    ///     fn end_of_body(&mut self) -> AcceptRejectAction {
123    ///         println!("End of body");
124    ///         AcceptRejectAction::Continue
125    ///     }
126    /// }
127    /// ```
128    fn end_of_body(&mut self) -> AcceptRejectAction {
129        // TODO: Add support for modifying here (header, body, recipients, etc.)
130        AcceptRejectAction::Continue
131    }
132
133    /// The MTA informs that all header chunks of the message are sent (SMFIC_EOH).
134    ///
135    /// # Example:
136    /// ```
137    /// use rmilter::accept_reject_action::AcceptRejectAction;
138    /// use rmilter::message_handler::MessageHandler;
139    ///
140    /// struct MyMessageHandler {}
141    ///
142    /// impl MessageHandler for MyMessageHandler {
143    ///     fn end_of_header(&mut self) -> AcceptRejectAction {
144    ///         println!("End of header");
145    ///         AcceptRejectAction::Continue
146    ///     }
147    /// }
148    /// ```
149    fn end_of_header(&mut self) -> AcceptRejectAction {
150        AcceptRejectAction::Continue
151    }
152
153    /// A header chunk (SMFIC_HEADER).
154    ///
155    /// - `name` defines the name of the provided value.
156    /// - `value` contains the actual value.
157    ///
158    /// # Example:
159    /// ```
160    /// use rmilter::accept_reject_action::AcceptRejectAction;
161    /// use rmilter::message_handler::MessageHandler;
162    ///
163    /// struct MyMessageHandler {}
164    ///
165    /// impl MessageHandler for MyMessageHandler {
166    ///     fn header(&mut self, name: &str, value: &str) -> AcceptRejectAction {
167    ///         println!("name: {}, value: {}", name, value);
168    ///         AcceptRejectAction::Continue
169    ///     }
170    /// }
171    /// ```
172    #[allow(unused_variables)]
173    fn header(&mut self, name: &str, value: &str) -> AcceptRejectAction {
174        AcceptRejectAction::Continue
175    }
176
177    /// A helo message (SMFIC_HELO).
178    ///
179    /// - `msg` contains the sent helo message.
180    ///
181    /// # Example:
182    /// ```
183    /// use rmilter::accept_reject_action::AcceptRejectAction;
184    /// use rmilter::message_handler::MessageHandler;
185    ///
186    /// struct MyMessageHandler {}
187    ///
188    /// impl MessageHandler for MyMessageHandler {
189    ///     fn helo(&mut self, msg: &str) -> AcceptRejectAction {
190    ///         println!("msg: {}", msg);
191    ///         AcceptRejectAction::Continue
192    ///     }
193    /// }
194    /// ```
195    #[allow(unused_variables)]
196    fn helo(&mut self, msg: &str) -> AcceptRejectAction {
197        AcceptRejectAction::Continue
198    }
199
200    /// A mail from message (SMFIC_MAIL).
201    ///
202    /// - `address` contains the address of the sender.
203    /// - `args` contains optional arguments.
204    ///
205    /// # Example:
206    /// ```
207    /// use rmilter::accept_reject_action::AcceptRejectAction;
208    /// use rmilter::message_handler::MessageHandler;
209    ///
210    /// struct MyMessageHandler {}
211    ///
212    /// impl MessageHandler for MyMessageHandler {
213    ///     fn mail_from(&mut self, address: &str, args: &[String]) -> AcceptRejectAction {
214    ///         println!("address: {}, args: {:?}", address, args);
215    ///         AcceptRejectAction::Continue
216    ///     }
217    /// }
218    /// ```
219    #[allow(unused_variables)]
220    fn mail_from(&mut self, address: &str, args: &[String]) -> AcceptRejectAction {
221        AcceptRejectAction::Continue
222    }
223
224    /// Recipient information (SMFIC_RCPT).
225    ///
226    /// - `recipient` contains the recipient of the message.
227    /// - `args` contains optional arguments.
228    ///
229    /// # Example:
230    /// ```
231    /// use rmilter::accept_reject_action::AcceptRejectAction;
232    /// use rmilter::message_handler::MessageHandler;
233    ///
234    /// struct MyMessageHandler {}
235    ///
236    /// impl MessageHandler for MyMessageHandler {
237    ///     fn recipient(&mut self, recipient: &str, args: &[String]) -> AcceptRejectAction {
238    ///         println!("recipient: {}, args: {:?}", recipient, args);
239    ///         AcceptRejectAction::Continue
240    ///     }
241    /// }
242    /// ```
243    #[allow(unused_variables)]
244    fn recipient(&mut self, recipient: &str, args: &[String]) -> AcceptRejectAction {
245        AcceptRejectAction::Continue
246    }
247}