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
use crate::smtp::*;
use std::net::SocketAddr;
use std::time::{Duration, Instant};

/// Represents the instructions for the client side of the stream.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum WriteControl {
    /// The stream should be shut down.
    Shutdown,
    /// Tell codec to start data
    StartData(SmtpReply),
    /// Tell stream to upgrade to TLS
    StartTls(SmtpReply),
    /// Send an SMTP reply
    Reply(SmtpReply),
}

/// Represents the instructions for the server side of the stream.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ReadControl {
    /** Peer connected */
    PeerConnected(Connection),
    /** Peer disconnected */
    PeerShutdown,
    /** SMTP command line */
    Command(SmtpCommand, Vec<u8>),
    /** raw input that could not be understood */
    Raw(Vec<u8>),
    /** Available mail data without signalling dots */
    MailDataChunk(Vec<u8>),
    /** The SMTP data terminating dot (. CR LF) is part of protocol signalling and not part of data  */
    EndOfMailData(Vec<u8>),
    /** The SMTP data escape dot (.) is part of protocol signalling and not part of data */
    EscapeDot(Vec<u8>),
    /// Empty line or white space
    Empty(Vec<u8>),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Connection {
    local_addr: Option<SocketAddr>,
    peer_addr: Option<SocketAddr>,
    established: Instant,
    extensions: ExtensionSet,
}

impl Connection {
    pub fn new<L, P>(local: L, peer: P) -> Connection
    where
        L: Into<Option<SocketAddr>>,
        P: Into<Option<SocketAddr>>,
    {
        Connection {
            local_addr: local.into(),
            peer_addr: peer.into(),
            established: Instant::now(),
            extensions: ExtensionSet::new(),
        }
    }
    pub fn local_addr(&self) -> Option<SocketAddr> {
        self.local_addr.clone()
    }
    pub fn peer_addr(&self) -> Option<SocketAddr> {
        self.peer_addr.clone()
    }
    pub fn age(&self) -> Duration {
        Instant::now() - self.established
    }
    pub fn extensions(&self) -> &ExtensionSet {
        &self.extensions
    }
    pub fn extensions_mut(&mut self) -> &mut ExtensionSet {
        &mut self.extensions
    }
}
impl Default for Connection {
    fn default() -> Connection {
        Connection::new(None, None)
    }
}

impl std::fmt::Display for Connection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(f, "Connection from peer ")?;
        if let Some(a) = self.peer_addr {
            write!(f, "{}", a)?;
        } else {
            write!(f, "Unknown")?;
        }
        write!(f, " to local ")?;
        if let Some(a) = self.local_addr {
            write!(f, "{}", a)?;
        } else {
            write!(f, "Unknown")?;
        }
        write!(f, " established {:?} ago", self.age())?;
        Ok(())
    }
}