E-MailRelay
gsmtpservertext.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 "glocal.h"
27#include "gstr.h"
28#include "glog.h"
29#include "gassert.h"
30#include <string>
31
32GSmtp::ServerText::ServerText( const std::string & code_ident , bool anonymous , bool with_received_line ,
33 const std::string & domain , const GNet::Address & peer_address ) :
34 m_code_ident(code_ident) ,
35 m_anonymous(anonymous) ,
36 m_with_received_line(with_received_line) ,
37 m_domain(domain) ,
38 m_peer_address(peer_address)
39{
40 G_ASSERT( !domain.empty() ) ;
41 if( domain.empty() )
42 m_domain = GNet::Local::canonicalName() ;
43}
44
45std::string GSmtp::ServerText::greeting() const
46{
47 std::string greeting_domain = m_domain ;
48 return
49 m_anonymous ?
50 std::string("greeting") :
51 greeting_domain.append(" -- ",4U).append(m_code_ident).append(" -- Service ready") ;
52}
53
54std::string GSmtp::ServerText::hello( const std::string & ) const
55{
56 std::string greeting_domain = m_domain ;
57 return
58 m_anonymous ?
59 std::string("smtp says hello") :
60 greeting_domain.append(" says hello") ;
61}
62
63std::string GSmtp::ServerText::received( const std::string & smtp_peer_name ,
64 bool authenticated , bool secure , const std::string & protocol , const std::string & cipher ) const
65{
66 return m_with_received_line ? receivedLine( smtp_peer_name , m_peer_address.hostPartString() , m_domain ,
67 authenticated , secure , protocol , cipher ) : std::string() ;
68}
69
70std::string GSmtp::ServerText::receivedLine( const std::string & smtp_peer_name ,
71 const std::string & peer_address , const std::string & receivedline_domain ,
72 bool authenticated , bool secure , const std::string & , const std::string & cipher_in )
73{
75 const G::BrokenDownTime tm = t.local() ;
76 const std::string zone = G::DateTime::offsetString(G::DateTime::offset(t)) ;
77 const G::Date date( tm ) ;
78 const G::Time time( tm ) ;
79 const std::string esmtp = std::string("ESMTP") + (secure?"S":"") + (authenticated?"A":"") ; // RFC-3848
80 const std::string peer_name = G::Str::toPrintableAscii(
81 G::Str::replaced(smtp_peer_name,' ','-') ) ; // typically alphanumeric with ".-:[]_"
82 std::string cipher = secure ?
83 G::Str::only(G::Str::alnum_(),G::Str::replaced(cipher_in,'-','_')) :
84 std::string() ;
85
86 // RFC-5321 4.4
87 std::ostringstream ss ;
88 ss
89 << "Received: from " << peer_name
90 << " ("
91 << "[" << peer_address << "]"
92 << ") by " << receivedline_domain << " with " << esmtp
93 << (cipher.empty()?"":" tls ") << cipher // RFC-8314 4.3 7.4
94 << " ; "
95 << date.weekdayName(true) << ", "
96 << date.monthday() << " "
97 << date.monthName(true) << " "
98 << date.yyyy() << " "
99 << time.hhmmss(":") << " "
100 << zone ;
101 return ss.str() ;
102}
103
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:62
static std::string canonicalName()
Returns the canonical network name assiciated with hostname().
Definition: glocal.cpp:34
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:934
static string_view alnum_() noexcept
Returns alnum() with an additional trailing underscore character.
Definition: gstr.cpp:1282
static std::string only(string_view allow_chars, 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:134
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.