E-MailRelay

gsmtpserverparser.h
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 gsmtpserverparser.h
19///
20
21#ifndef G_SMTP_SERVER_PARSER_H
22#define G_SMTP_SERVER_PARSER_H
23
24#include "gdef.h"
25#include "gstringview.h"
26#include "gmessagestore.h"
27#include <string>
28
29 namespace GSmtp
30{
31 class ServerParser ;
32}
33
34//| \class GSmtp::ServerParser
35/// A static class for SMTP command parsing, used by GSmtp::ServerProtocol
36/// as a mix-in base.
37///
38/// See also RFC-5321 4.1.2.
39///
40 class GSmtp::ServerParser
41{
42 public:
43 struct Config /// A configuration structure for GSmtp::ServerParser.
44 {
45 bool allow_spaces {false} ; // sr #89
46 bool allow_nobrackets {false} ; // sr #97
47 bool alabels {false} ; // normalise domain names using A-labels
48 std::string allow_spaces_help ;
49 std::string allow_nobrackets_help ;
50 Config & set_allow_spaces( bool b = true ) noexcept ;
51 Config & set_allow_nobrackets( bool b = true ) noexcept ;
52 Config & set_alabels( bool b = true ) noexcept ;
53 Config & set_allow_spaces_help( const std::string & ) ;
54 Config & set_allow_nobrackets_help( const std::string & ) ;
55 } ;
56 using AddressStyle = GStore::MessageStore::AddressStyle ;
57 struct AddressCommand /// mail-from or rcpt-to
58 {
59 AddressCommand() = default ;
60 AddressCommand( const std::string & e ) : error(e) {}
61 std::string error ;
62 std::string raw_address ; // raw address, possibly UTF-8 and/or with local-part quoted and escaped
63 std::string address ; // address with domain part using A-labels (if requested by Config::alabels)
64 AddressStyle address_style {AddressStyle::Ascii} ; // see GStore::MessageStore::addressStyle()
65 bool utf8_mailbox_part {false} ; // see address_style
66 bool utf8_domain_part {false} ; // see address_style
67 std::size_t tailpos {std::string::npos} ;
68 std::size_t size {0U} ;
69 std::string auth ;
70 std::string body ; // 7BIT, 8BITMIME, BINARYMIME
71 bool smtputf8 {false} ; // SMTPUTF8 option
72 bool invalid_spaces {false} ;
73 bool invalid_nobrackets {false} ;
74 } ;
75
76 static AddressCommand parseMailFrom( std::string_view , const Config & ) ;
77 ///< Parses a MAIL-FROM command.
78
79 static AddressCommand parseRcptTo( std::string_view , const Config & ) ;
80 ///< Parses a RCPT-TO command.
81
82 static std::pair<std::size_t,bool> parseBdatSize( std::string_view ) ;
83 ///< Parses a BDAT command.
84
85 static std::pair<bool,bool> parseBdatLast( std::string_view ) ;
86 ///< Parses a BDAT LAST command.
87
88 static std::string parseHeloPeerName( const std::string & ) ;
89 ///< Parses the peer name from an HELO/EHLO command.
90
91 static std::string parseVrfy( const std::string & ) ;
92 ///< Parses a VRFY command.
93
94 private:
95 enum class Conversion
96 {
97 None ,
98 ValidXtext ,
99 Upper
100 } ;
101 static AddressCommand parseAddressPart( std::string_view , const Config & ) ;
102 static std::size_t parseMailNumericValue( std::string_view , std::string_view , AddressCommand & ) ;
103 static std::string parseMailStringValue( std::string_view , std::string_view , AddressCommand & , Conversion = Conversion::None ) ;
104 static bool parseMailBoolean( std::string_view , std::string_view , AddressCommand & ) ;
105 static std::string encodeDomain( std::string_view ) ;
106} ;
107
108 inline GSmtp::ServerParser::Config & GSmtp::ServerParser::Config::set_allow_spaces( bool b ) noexcept { allow_spaces = b ; return *this ; }
109 inline GSmtp::ServerParser::Config & GSmtp::ServerParser::Config::set_allow_nobrackets( bool b ) noexcept { allow_nobrackets = b ; return *this ; }
110 inline GSmtp::ServerParser::Config & GSmtp::ServerParser::Config::set_alabels( bool b ) noexcept { alabels = b ; return *this ; }
111 inline GSmtp::ServerParser::Config & GSmtp::ServerParser::Config::set_allow_spaces_help( const std::string & s ) { allow_spaces_help = s ; return *this ; }
112 inline GSmtp::ServerParser::Config & GSmtp::ServerParser::Config::set_allow_nobrackets_help( const std::string & s ) { allow_nobrackets_help = s ; return *this ; }
113
114#endif
GSmtp::ServerParser
A static class for SMTP command parsing, used by GSmtp::ServerProtocol as a mix-in base.
Definition: gsmtpserverparser.h:41
GSmtp::ServerParser::parseRcptTo
static AddressCommand parseRcptTo(std::string_view, const Config &)
Parses a RCPT-TO command.
Definition: gsmtpserverparser.cpp:87
GSmtp::ServerParser::parseVrfy
static std::string parseVrfy(const std::string &)
Parses a VRFY command.
Definition: gsmtpserverparser.cpp:270
GSmtp::ServerParser::parseMailFrom
static AddressCommand parseMailFrom(std::string_view, const Config &)
Parses a MAIL-FROM command.
Definition: gsmtpserverparser.cpp:62
GSmtp::ServerParser::parseBdatLast
static std::pair< bool, bool > parseBdatLast(std::string_view)
Parses a BDAT LAST command.
Definition: gsmtpserverparser.cpp:48
GSmtp::ServerParser::parseBdatSize
static std::pair< std::size_t, bool > parseBdatSize(std::string_view)
Parses a BDAT command.
Definition: gsmtpserverparser.cpp:32
GSmtp::ServerParser::parseHeloPeerName
static std::string parseHeloPeerName(const std::string &)
Parses the peer name from an HELO/EHLO command.
Definition: gsmtpserverparser.cpp:292
gmessagestore.h
gstringview.h
GSmtp
SMTP classes.
Definition: gadminserver.h:42
GSmtp::ServerParser::AddressCommand
mail-from or rcpt-to
Definition: gsmtpserverparser.h:58
GSmtp::ServerParser::Config
A configuration structure for GSmtp::ServerParser.
Definition: gsmtpserverparser.h:44