E-MailRelay
gsmtpservertext.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gsmtpservertext.cpp
19///
20
21#include "gdef.h"
22#include "gsmtpservertext.h"
23#include "gdate.h"
24#include "gtime.h"
25#include "gdatetime.h"
26#include "gstr.h"
27#include "glog.h"
28#include "gassert.h"
29#include <string>
30
31GSmtp::ServerText::ServerText( const std::string & code_ident , bool anonymous , bool with_received_line ,
32 const std::string & domain , const GNet::Address & peer_address ) :
33 m_code_ident(code_ident) ,
34 m_anonymous(anonymous) ,
35 m_with_received_line(with_received_line) ,
36 m_domain(domain) ,
37 m_peer_address(peer_address)
38{
39 G_ASSERT( !domain.empty() ) ;
40 if( domain.empty() )
41 m_domain = "localhost" ; // never gets here
42}
43
44std::string GSmtp::ServerText::greeting() const
45{
46 std::string greeting_domain = m_domain ;
47 return
48 m_anonymous ?
49 std::string("greeting") :
50 greeting_domain.append(" -- ",4U).append(m_code_ident).append(" -- Service ready") ;
51}
52
53std::string GSmtp::ServerText::hello( const std::string & ) const
54{
55 std::string greeting_domain = m_domain ;
56 return
57 m_anonymous ?
58 std::string("smtp says hello") :
59 greeting_domain.append(" says hello") ;
60}
61
62std::string GSmtp::ServerText::received( const std::string & smtp_peer_name ,
63 bool authenticated , bool secure , const std::string & protocol , const std::string & cipher ) const
64{
65 return m_with_received_line ? receivedLine( smtp_peer_name , m_peer_address.hostPartString() , m_domain ,
66 authenticated , secure , protocol , cipher ) : std::string() ;
67}
68
69std::string GSmtp::ServerText::receivedLine( const std::string & smtp_peer_name ,
70 const std::string & peer_address , const std::string & receivedline_domain ,
71 bool authenticated , bool secure , const std::string & , const std::string & cipher_in )
72{
74 const G::BrokenDownTime tm = t.local() ;
75 const std::string zone = G::DateTime::offsetString(G::DateTime::offset(t)) ;
76 const G::Date date( tm ) ;
77 const G::Time time( tm ) ;
78 const std::string esmtp = std::string("ESMTP") + (secure?"S":"") + (authenticated?"A":"") ; // RFC-3848
79 const std::string peer_name = G::Str::toPrintableAscii(
80 G::Str::replaced(smtp_peer_name,' ','-') ) ; // typically alphanumeric with ".-:[]_"
81 std::string cipher = secure ?
82 G::Str::only(G::Str::alnum_(),G::Str::replaced(cipher_in,'-','_')) :
83 std::string() ;
84
85 // RFC-5321 4.4
86 std::ostringstream ss ;
87 ss
88 << "Received: from " << peer_name
89 << " ("
90 << "[" << peer_address << "]"
91 << ") by " << receivedline_domain << " with " << esmtp
92 << (cipher.empty()?"":" tls ") << cipher // RFC-8314 4.3 7.4
93 << " ; "
94 << date.weekdayName(true) << ", "
95 << date.monthday() << " "
96 << date.monthName(true) << " "
97 << date.yyyy() << " "
98 << time.hhmmss(":") << " "
99 << zone ;
100 return ss.str() ;
101}
102
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:63
ServerText(const std::string &code_ident, bool anonymous, bool with_received_line, const std::string &greeting_and_receivedline_domain, const GNet::Address &peer_address)
Constructor.
An encapsulation of 'struct std::tm'.
Definition: gdatetime.h:45
static Offset offset(SystemTime)
Returns the offset in seconds between UTC and localtime as at the given system time.
Definition: gdatetime.cpp:772
static std::string offsetString(Offset offset)
Converts the given utc/localtime offset into a five-character "+/-hhmm" string.
Definition: gdatetime.cpp:792
A day-month-year date class.
Definition: gdate.h:42
static std::string replaced(const std::string &s, char from, char to)
Returns the string 's' with all occurrences of 'from' replaced by 'to'.
Definition: gstr.cpp:255
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
Definition: gstr.cpp:931
static std::string_view alnum_() noexcept
Returns alnum() with an additional trailing underscore character.
Definition: gstr.cpp:1279
static std::string only(std::string_view allow_chars, std::string_view s)
Returns the 's' with all occurrences of the characters not appearing in the first string deleted.
Definition: gstr.cpp:276
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:140
static SystemTime now()
Factory function for the current time.
Definition: gdatetime.cpp:328
BrokenDownTime local() const
Returns the locale-dependent local broken-down time.
Definition: gdatetime.cpp:356
A simple time-of-day (hh/mm/ss) class.
Definition: gtime.h:39
STL namespace.