E-MailRelay
gverifierstatus.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 gverifierstatus.cpp
19///
20
21#include "gdef.h"
22#include "gverifier.h"
23#include "gverifierstatus.h"
24#include "gsmtpserverparser.h"
25#include "gstringview.h"
26#include "gstr.h"
27#include "glog.h"
28
29GSmtp::VerifierStatus::VerifierStatus()
30= default;
31
33 bool temporary , const std::string & response , const std::string & reason )
34{
35 VerifierStatus status ;
36 status.is_valid = false ;
37 status.temporary = temporary ;
38 status.recipient = recipient ;
39 status.response = response ;
40 status.reason = reason ;
41 return status ;
42}
43
45 const std::string & address )
46{
47 VerifierStatus status ;
48 status.is_valid = true ;
49 status.is_local = false ;
50 status.recipient = recipient ;
51 status.address = address.empty() ? recipient : address ;
52 return status ;
53}
54
56 const std::string & full_name , const std::string & mbox )
57{
58 VerifierStatus status ;
59 status.is_valid = true ;
60 status.is_local = true ;
61 status.recipient = recipient ;
62 status.full_name = full_name ;
63 status.address = mbox ;
64 return status ;
65}
66
68{
69 try
70 {
71 G::StringArray part ;
72 G::Str::splitIntoFields( line , part , '|' , '\\' ) ;
73 if( part.size() != 9U )
74 throw InvalidStatus() ;
75
76 std::size_t i = 0U ;
78 s.recipient = part.at(i++) ;
79 s.is_valid = part.at(i++) == "1" ;
80 s.is_local = part.at(i++) == "1" ;
81 s.temporary = part.at(i++) == "1" ;
82 s.abort = part.at(i++) == "1" ;
83 s.full_name = part.at(i++) ;
84 s.address = part.at(i++) ;
85 s.response = part.at(i++) ;
86 s.reason = part.at(i++) ;
87 return s ;
88 }
89 catch( std::exception & )
90 {
91 G_ERROR( "GSmtp::VerifierStatus::parse: invalid verifier status: [" << line << "]" ) ;
92 throw ;
93 }
94}
95
96std::string GSmtp::VerifierStatus::str() const
97{
98 auto escape = [](const std::string &s){ return G::Str::escaped( s , '\\' , "\\|"_sv , "\\|"_sv ) ; } ;
99 const char sep = '|' ;
100 const char t = '1' ;
101 const char f = '0' ;
102 return escape(recipient)
103 .append(1U,sep)
104 .append(1U,is_valid?t:f).append(1U,sep)
105 .append(1U,is_local?t:f).append(1U,sep)
106 .append(1U,temporary?t:f).append(1U,sep)
107 .append(1U,abort?t:f).append(1U,sep)
108 .append(escape(full_name)).append(1U,sep)
109 .append(escape(address)).append(1U,sep)
110 .append(escape(response)).append(1U,sep)
111 .append(escape(reason)) ;
112}
113
115{
116 return ServerParser::mailboxStyle( address ) == ServerParser::MailboxStyle::Utf8 ;
117}
118
static MailboxStyle mailboxStyle(const std::string &mailbox)
Classifies the given mailbox name.
A structure returned by GSmtp::Verifier to describe the status of a 'rcpt-to' or 'vrfy' recipient.
static VerifierStatus local(const std::string &recipient, const std::string &full_name, const std::string &mbox)
Constructor for a valid local mailbox.
std::string str() const
Returns a string representation of the structure.
bool utf8address() const
Returns true if 'address' is utf8 according to GSmtp::ServerParser::mailboxStyle().
static VerifierStatus parse(const std::string &str)
Parses a str() string into a structure.
static VerifierStatus invalid(const std::string &recipient, bool temporary=false, const std::string &response={}, const std::string &reason={})
Factory function for an invalid address.
static VerifierStatus remote(const std::string &recipient, const std::string &address={})
Constructor for a valid remote mailbox.
static void splitIntoFields(string_view in, StringArray &out, char sep, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:1194
static std::string escaped(string_view, char c_escape, string_view specials_in, string_view specials_out)
Returns the escape()d string.
Definition: gstr.cpp:92
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30