Skip to main content

smtp_proto/response/
mod.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use std::fmt::Display;
8
9use crate::tokens::define_tokens_128;
10use crate::{EhloResponse, Response};
11
12pub mod generate;
13pub mod parser;
14
15define_tokens_128! {
16    _8BITMIME = "8BITMIME",
17    ATRN,
18    AUTH,
19    BINARYMIME,
20    BURL,
21    CHECKPOINT,
22    CHUNKING,
23    CONNEG,
24    CONPERM,
25    DELIVERBY,
26    DSN,
27    ENHANCEDSTATUSCO,
28    ETRN,
29    EXPN,
30    VRFY,
31    FUTURERELEASE,
32    HELP,
33    MT_PRIORITY = "MT-PRIORITY",
34    MTRK,
35    NO_SOLICITING = "NO-SOLICITING",
36    ONEX,
37    PIPELINING,
38    REQUIRETLS,
39    RRVS,
40    SIZE,
41    SMTPUTF8,
42    STARTTLS,
43    VERB,
44
45    // Priorities
46    MIXER,
47    STANAG4406,
48    NSEP,
49}
50
51impl<T: Display> EhloResponse<T> {
52    /// Returns the hostname of the SMTP server.
53    pub fn hostname(&self) -> &T {
54        &self.hostname
55    }
56
57    /// Returns the capabilities of the SMTP server.
58    pub fn capabilities(&self) -> u32 {
59        self.capabilities
60    }
61
62    /// Returns `true` if the SMTP server supports a given extension.
63    pub fn has_capability(&self, capability: u32) -> bool {
64        (self.capabilities & capability) != 0
65    }
66
67    /// Returns all supported authentication mechanisms.
68    pub fn auth(&self) -> u64 {
69        self.auth_mechanisms
70    }
71}
72
73impl<T: Display> Display for Response<T> {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        write!(
76            f,
77            "Code: {}, Enhanced code: {}.{}.{}, Message: {}",
78            self.code, self.esc[0], self.esc[1], self.esc[2], self.message,
79        )
80    }
81}
82
83#[cfg(feature = "rkyv")]
84impl<T: Display + rkyv::Archive> Display for crate::ArchivedResponse<T>
85where
86    <T as rkyv::Archive>::Archived: std::fmt::Display,
87{
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        write!(
90            f,
91            "Code: {}, Enhanced code: {}.{}.{}, Message: {}",
92            self.code, self.esc[0], self.esc[1], self.esc[2], self.message,
93        )
94    }
95}