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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! A milter client
//!
//!

mod codec;

#[cfg(feature = "_fuzzing")]
pub mod fuzzing;

use std::{ops::Deref, sync::Arc};

use asynchronous_codec::Framed;
use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
use paste::paste;
use thiserror::Error;

use miltr_common::{
    actions::{Abort, Action, Quit},
    commands::{
        Body, Command, Connect, Data, EndOfBody, EndOfHeader, Header, Helo, Mail, Recipient,
        Unknown,
    },
    decoding::ServerCommand,
    modifications::{ModificationAction, ModificationResponse},
    optneg::{CompatibilityError, OptNeg},
    ProtocolError,
};

use self::codec::MilterCodec;

/// A milter client using some options and a codec to talk to a milter server
pub struct Client {
    options: Arc<OptNeg>,
    codec: MilterCodec,
}

/// A single milter connection
///
/// This can be created by calling [`Client::connect_via`] to establish
/// a milter session.
///
/// A regular session could use these commands in order:
///
/// - [`Connection::connect`]
/// - [`Connection::helo`]
/// - [`Connection::mail`]
/// - [`Connection::recipient`]
/// - [`Connection::data`]
/// - [`Connection::header`] (multiple)
/// - [`Connection::end_of_header`]
/// - [`Connection::body`] (multiple)
/// - [`Connection::end_of_body`]
///
/// Be careful about the ordering of these commands, milter implementations
/// are designed to expect them in order they appear in the SMTP protocol.
///
/// # Protocol from `OptNeg`
///
/// Depending on what was set by client and server during option negotiation
/// when establishing the connection, commands might either not be sent at all
/// or no response is awaited.
///
/// Assuming [`Protocol::NO_HELO`](miltr_common::optneg::Protocol::NO_HELO) is
/// set during option negotiation, calling [`Connection::helo`] short-circuits
/// to `return Ok(())`.
///
/// If [`Protocol::NR_HELO`](miltr_common::optneg::Protocol::NR_HELO) is set,
/// calling [`Connection::helo`] does not wait for an answer from the milter
/// server, it immediately `return Ok(())` after sending the command.
///
/// Commands behave differently here, see the implementations for
/// [`Protocol::skip_send`](miltr_common::optneg::Protocol::should_skip_send) and
/// [`Protocol::skip_response`](miltr_common::optneg::Protocol::should_skip_response)
/// for details.
pub struct Connection<RW: AsyncRead + AsyncWrite + Unpin> {
    framed: Framed<RW, MilterCodec>,
    options: OptNeg,
}

impl Client {
    /// Create a client which is able to handle connections with the provided
    /// options.
    #[must_use]
    pub fn new(options: OptNeg) -> Self {
        let codec = MilterCodec::new(2_usize.pow(16));

        Self {
            options: Arc::new(options),
            codec,
        }
    }

    /// Option negotiate with the server
    ///
    /// The steps are:
    /// 1. Send our options to the server
    /// 2. Receive it's options back
    /// 3. Merge them into one
    async fn recv_option_negotiation<RW: AsyncRead + AsyncWrite + Unpin>(
        &self,
        framed: &mut Framed<RW, MilterCodec>,
    ) -> Result<OptNeg, ResponseError> {
        let client_options = &self.options;
        framed.send(&client_options.deref().clone().into()).await?;

        let resp = framed
            .next()
            .await
            .ok_or(ResponseError::MissingServerResponse)??;

        let server_options = match resp {
            ServerCommand::OptNeg(optneg) => Ok(optneg),
            command => Err(ResponseError::Unexpected(command)),
        }?;

        let options = server_options.merge_compatible(&self.options)?;

        Ok(options)
    }

    /// Handle a single milter connection via the provided RW connection
    ///
    /// # Errors
    /// This fails if an io-error is experienced or option negotiation fails
    pub async fn connect_via<RW: AsyncRead + AsyncWrite + Unpin>(
        &self,
        connection: RW,
    ) -> Result<Connection<RW>, ResponseError> {
        let codec = self.codec.clone();
        let mut framed = Framed::new(connection, codec);
        let options = self.recv_option_negotiation(&mut framed).await?;

        let connection = Connection { framed, options };

        Ok(connection)
    }
}

macro_rules! command {
    (
        $(#[$outer:meta])*
        (into) $variant:ident
    ) => {
        paste! {
            $(#[$outer])*
            pub async fn [<$variant:snake>]<C: Into<[<$variant:camel>]>>(&mut self, command: C) -> Result<(), ResponseError> {
                let command_intoed: [<$variant:camel>] = command.into();
                let command: Command = command_intoed.into();

                self.send_command(command).await
            }
        }
    };
    (
        $(#[$outer:meta])*
        (new) $variant:ident
    ) => {
        paste! {
            $(#[$outer])*
            pub async fn [<$variant:snake>](&mut self) -> Result<(), ResponseError> {
                let command: Command = [<$variant:camel>].into();

                self.send_command(command).await
            }
        }
    };
}

impl<RW: AsyncRead + AsyncWrite + Unpin> Connection<RW> {
    command!(
        /// Send connect information.
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Connect
    );

    command!(
        /// Handle a client helo
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Helo
    );

    command!(
        /// Send the sender info
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Mail
    );

    command!(
        /// Send the recipient info
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Recipient
    );

    command!(
        /// Indicate that data follows
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (new) Data
    );

    command!(
        /// Send headers
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Header
    );

    command!(
        /// Indicate all headers have been sent
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (new) EndOfHeader
    );

    command!(
        /// Send a body part
        ///
        ///
        /// # Errors
        /// Errors on any response from the milter server that is not Continue
        (into) Body
    );

    // command!(
    //     /// Indicate all body parts have been sent
    //     ///
    //     /// # Errors
    //     /// Errors on any response from the milter server that is not Continue
    //     (new) EndOfBody
    // );

    /// Indicate all body parts have been sent
    ///
    /// # Errors
    /// Errors on any response from the milter server that is not Continue
    pub async fn end_of_body(&mut self) -> Result<ModificationResponse, ResponseError> {
        // First, send the eob command
        let command: Command = EndOfBody.into();
        self.framed.send(&command.into()).await?;

        let mut modification_response_builder = ModificationResponse::builder();
        loop {
            // Receive a response from the server
            let answer = self.receive_answer().await?;

            // Convert it to a command type
            let command: CommandType = answer.try_into()?;

            match command {
                CommandType::Action(action) => {
                    return Ok(modification_response_builder.build(action));
                }
                CommandType::ModificationAction(action) => {
                    modification_response_builder.push(action);
                }
            };
        }
    }

    /// Receive all modification requests from the server
    ///
    /// # Errors
    /// Errors on error regarding server communication
    pub async fn modification(&mut self) -> Result<CommandType, ResponseError> {
        let resp = self.receive_answer().await?;

        CommandType::try_from(resp)
    }

    /// Ask for a graceful connection shutdown
    ///
    /// # Errors
    /// Errors on io or codec Errors
    pub async fn quit(mut self) -> Result<(), ProtocolError> {
        self.framed.send(&Action::Quit(Quit).into()).await?;

        Ok(())
    }

    /// Ask to re-use this connection for a new mail
    ///
    /// # Errors
    /// Errors on any response from the milter server that is not Continue
    pub fn quit_nc(self) -> Result<(), ProtocolError> {
        todo!("Quit_NC Not yet implemented")
    }

    /// Abort processing for the current mail
    ///
    /// # Errors
    /// Errors on io or codec Errors
    pub async fn abort(mut self) -> Result<(), ProtocolError> {
        self.framed.send(&Action::from(Abort).into()).await?;

        Ok(())
    }

    command!(
        /// Send an unknown command to the server.
        ///
        ///
        /// # Errors
        /// Errors on io or codec Errors
        (into) Unknown
    );

    /// Send a command to the server respecting protocol settings
    async fn send_command(&mut self, command: Command) -> Result<(), ResponseError> {
        // Eval skips
        if self.options.protocol.should_skip_send(&command) {
            return Ok(());
        }
        let skip_response = self.options.protocol.should_skip_response(&command);

        // Send it
        self.framed.send(&command.into()).await?;

        // Check response
        if skip_response {
            return Ok(());
        }
        self.expect_continue().await
    }

    /// Shortcut to fetch an answer from the server
    async fn receive_answer(&mut self) -> Result<ServerCommand, ResponseError> {
        let resp = self
            .framed
            .next()
            .await
            .ok_or(ResponseError::MissingServerResponse)??;

        Ok(resp)
    }
    /// Shortcut expect a Continue answer from the server
    async fn expect_continue(&mut self) -> Result<(), ResponseError> {
        // Receive back answer
        let resp = self.receive_answer().await?;

        // If continue, just continue. Otherwise return an error
        match resp {
            ServerCommand::Continue(_c) => Ok(()),
            command => Err(ResponseError::Unexpected(command)),
        }
    }
}

/// An error for all problems the client could experience
#[derive(Debug, Error)]
pub enum ResponseError {
    /// Anything protocol related
    #[error(transparent)]
    ProtocolError(#[from] ProtocolError),
    /// If there should have been a response
    #[error("Server did not respond to a query")]
    MissingServerResponse,
    /// If there was a response but it was the wrong one
    #[error("Server respond with an unexpected answer")]
    Unexpected(ServerCommand),
    /// If we have a protocol compatibility issue
    #[error(transparent)]
    CompatibilityError(#[from] CompatibilityError),
}

/// The types of commands the server may respond with
pub enum CommandType {
    /// A regular control flow action
    Action(Action),
    /// A data modification action
    ModificationAction(ModificationAction),
}

impl TryFrom<ServerCommand> for CommandType {
    type Error = ResponseError;

    fn try_from(value: ServerCommand) -> Result<Self, Self::Error> {
        match value {
            ServerCommand::OptNeg(value) => Err(ResponseError::Unexpected(value.into())),
            ServerCommand::Abort(value) => Ok(Self::Action(value.into())),
            ServerCommand::Continue(value) => Ok(Self::Action(value.into())),
            ServerCommand::Discard(value) => Ok(Self::Action(value.into())),
            ServerCommand::Reject(value) => Ok(Self::Action(value.into())),
            ServerCommand::Tempfail(value) => Ok(Self::Action(value.into())),
            ServerCommand::Skip(value) => Ok(Self::Action(value.into())),
            ServerCommand::Replycode(value) => Ok(Self::Action(value.into())),
            ServerCommand::AddRecipient(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::DeleteRecipient(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::ReplaceBody(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::AddHeader(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::InsertHeader(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::ChangeHeader(value) => Ok(Self::ModificationAction(value.into())),
            ServerCommand::Quarantine(value) => Ok(Self::ModificationAction(value.into())),
        }
    }
}