1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// indymilter – asynchronous milter library
// Copyright © 2021–2024 David Bürgin <dbuergin@gluet.ch>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//! A library for writing asynchronous milter applications.
//!
//! This library provides an API for creating milters that can be integrated
//! with MTAs using the sendmail mail filter protocol, also known as
//! *libmilter*.
//!
//! # Usage
//!
//! The function [`run`] is the main entry point to the indymilter API. This
//! function returns a future that can be awaited in order to execute a milter
//! application.
//!
//! The API presented for implementing a milter is modeled after the sendmail
//! milter (libmilter) API. Refer to the sendmail milter documentation for
//! details and background. The documentation included in indymilter is less
//! complete.
//!
//! # Trace logging
//!
//! This library uses the [tracing] crate for internal trace logging. For
//! insight into library operation, use a [tracing
//! subscriber][tracing-subscriber] and enable logging at `trace` level.
//!
//! A word of warning: Trace logging is very noisy, and can be misleading. Even
//! during normal operation it is common to see ‘errors’ such as inability to
//! parse empty macro definitions from the MTA. However, this is expected,
//! historically grown behaviour (no different from libmilter) and does not
//! impact operation.
//!
//! [tracing]: https://crates.io/crates/tracing
//! [tracing-subscriber]: https://crates.io/crates/tracing-subscriber
pub use crate::;
use ;
use ;
use ;
// Logging policy: When this library encounters an unanticipated failure
// condition (programming error) it panics. No error logging is done in such a
// case.
//
// For all other error conditions, the general principle is not to log about
// library operation above `trace` level. The only exceptions are:
// - `error`: The milter library fails to provide service, eg when no new
// connections can be accepted due to an I/O problem.
// - `warn`: The user-provided milter implementation misbehaves (user error), eg
// when `Noreply` status is not used even though it was negotiated beforehand.
/// Runs a milter that handles MTA connections until it is shut down.
///
/// While the future returned by `run` is awaited, it perpetually accepts new
/// MTA connections and spawns a session task for each connection. This
/// procedure continues and the future will not complete until the supplied
/// `shutdown` future completes.
///
/// # Termination
///
/// For graceful termination, the milter task should be shut down by letting the
/// `shutdown` future complete. If instead the future returned by `run` is
/// simply dropped, currently active, spawned sessions may continue to execute.
///
/// When the `shutdown` future completes, all sessions exit cleanly as soon as
/// possible: That is, any milter command in the act of being processed will be
/// processed to completion (including the callback call), but commands waiting
/// in the queue are dropped.
///
/// # Errors
///
/// When the listener fails to accept any new connections an error is returned.
/// Else, the task runs for ever until it is shut down.
///
/// # Examples
///
/// The following example shows the simplest possible, no-op milter.
///
/// ```
/// # async fn f() -> std::io::Result<()> {
/// use indymilter::Callbacks;
/// use std::future;
/// use tokio::net::TcpListener;
///
/// let listener = TcpListener::bind("127.0.0.1:3000").await?;
/// let callbacks = Callbacks::<()>::new();
/// let config = Default::default();
/// let shutdown = future::pending::<()>();
///
/// indymilter::run(listener, callbacks, config, shutdown).await
/// # }
/// ```
pub async
// Main loop spawning session tasks that handle commands coming in on a
// connection. Normally runs for ever.
//
// However, there is an error condition that should result in loop exit and
// `Err` result: If the listener somehow breaks and cannot accept any new
// connections, the fault is not of some individual connection, but ours, and is
// propagated.
//
// Arguments are moved into the `run_milter` future, so that when it is dropped,
// associated resources (except the one only borrowed) are dropped at the same
// time, too.
async