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
use std::collections::HashMap;

use crate::{
    common::EhloData,
    data_types::{EsmtpKeyword, EsmtpValue, ForwardPath, ReversePath},
    error::MissingCapabilities,
    Cmd, ExecFuture, Io,
};

/// Quit command, but as it makes the connection unusable we do
/// not publicly provide it for usage with `Connection::send`,
/// instead using `Connection::quit` is recommended.
#[doc(hidden)]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Quit;

impl Cmd for Quit {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, io: Io) -> ExecFuture {
        io.exec_simple_cmd(&["QUIT"])
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Noop;

impl Cmd for Noop {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, io: Io) -> ExecFuture {
        io.exec_simple_cmd(&["NOOP"])
    }
}

pub type Params = HashMap<EsmtpKeyword, Option<EsmtpValue>>;

pub fn params_with_smtputf8(mut p: Params) -> Params {
    p.insert(EsmtpKeyword::from_unchecked("SMTPUTF8"), None);
    p
}

#[derive(Debug, Clone)]
pub struct Mail {
    pub reverse_path: ReversePath,
    pub params: Params,
}

impl Mail {
    pub fn new(reverse_path: ReversePath) -> Self {
        Mail {
            reverse_path,
            params: Params::new(),
        }
    }
}

impl Cmd for Mail {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, con: Io) -> ExecFuture {
        handle_pathy_cmd(con, "MAIL FROM:", self.reverse_path.as_str(), &self.params)
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Recipient {
    //Grammar: "<Postmaster@" Domain ">" / "<Postmaster>" / forward-path
    //Note: that Postmaster is case-sensitive
    pub forward_path: ForwardPath,
    pub params: Params,
}

impl Recipient {
    pub fn new(forward_path: ForwardPath) -> Self {
        Recipient {
            forward_path,
            params: Params::new(),
        }
    }
}

impl Cmd for Recipient {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, con: Io) -> ExecFuture {
        handle_pathy_cmd(con, "RCPT TO:", self.forward_path.as_str(), &self.params)
    }
}

fn handle_pathy_cmd(io: Io, cmd: &str, path: &str, params: &Params) -> ExecFuture {
    //no additional heap alloc
    if params.is_empty() {
        io.exec_simple_cmd(&[cmd, "<", path, ">"])
    } else {
        let mut parts = vec![cmd, "<", path, ">"];
        for (k, v) in params.iter() {
            parts.push(" ");
            parts.push(k.as_str());
            if let Some(v) = v.as_ref() {
                parts.push("=");
                parts.push(v.as_str());
            }
        }
        io.exec_simple_cmd(parts.as_slice())
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Verify {
    pub query: String,
}

impl Cmd for Verify {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, io: Io) -> ExecFuture {
        io.exec_simple_cmd(&["VRFY ", self.query.as_str()])
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Help {
    pub topic: Option<String>,
}

impl Cmd for Help {
    fn check_cmd_availability(&self, _caps: Option<&EhloData>) -> Result<(), MissingCapabilities> {
        Ok(())
    }

    fn exec(self, io: Io) -> ExecFuture {
        if let Some(topic) = self.topic.as_ref() {
            io.exec_simple_cmd(&["HELP ", topic.as_str()])
        } else {
            io.exec_simple_cmd(&["HELP"])
        }
    }
}