E-MailRelay
gverifierstatus.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 gverifierstatus.cpp
19///
20
21#include "gdef.h"
22#include "gverifier.h"
23#include "gverifierstatus.h"
24#include "gstringview.h"
25#include "gstr.h"
26#include "glog.h"
27
28GSmtp::VerifierStatus::VerifierStatus()
29= default;
30
32 bool temporary , const std::string & response , const std::string & reason )
33{
34 VerifierStatus status ;
35 status.is_valid = false ;
36 status.temporary = temporary ;
37 status.recipient = recipient ;
38 status.response = response ;
39 status.reason = reason ;
40 return status ;
41}
42
44 const std::string & address )
45{
46 VerifierStatus status ;
47 status.is_valid = true ;
48 status.is_local = false ;
49 status.recipient = recipient ;
50 status.address = address.empty() ? recipient : address ;
51 return status ;
52}
53
55 const std::string & full_name , const std::string & mbox )
56{
57 VerifierStatus status ;
58 status.is_valid = true ;
59 status.is_local = true ;
60 status.recipient = recipient ;
61 status.full_name = full_name ;
62 status.address = mbox ;
63 return status ;
64}
65
67{
68 try
69 {
70 G::StringArray part ;
71 G::Str::splitIntoFields( line , part , '|' , '\\' ) ;
72 if( part.size() != 9U )
73 throw InvalidStatus() ;
74
75 std::size_t i = 0U ;
77 s.recipient = part.at(i++) ;
78 s.is_valid = part.at(i++) == "1" ;
79 s.is_local = part.at(i++) == "1" ;
80 s.temporary = part.at(i++) == "1" ;
81 s.abort = part.at(i++) == "1" ;
82 s.full_name = part.at(i++) ;
83 s.address = part.at(i++) ;
84 s.response = part.at(i++) ;
85 s.reason = part.at(i++) ;
86 return s ;
87 }
88 catch( std::exception & )
89 {
90 G_ERROR( "GSmtp::VerifierStatus::parse: invalid verifier status: [" << line << "]" ) ;
91 throw ;
92 }
93}
94
95std::string GSmtp::VerifierStatus::str() const
96{
97 auto escape = [](const std::string &s){ return G::Str::escaped( s , '\\' , "\\|"_sv , "\\|"_sv ) ; } ;
98 const char sep = '|' ;
99 const char t = '1' ;
100 const char f = '0' ;
101 return escape(recipient)
102 .append(1U,sep)
103 .append(1U,is_valid?t:f).append(1U,sep)
104 .append(1U,is_local?t:f).append(1U,sep)
105 .append(1U,temporary?t:f).append(1U,sep)
106 .append(1U,abort?t:f).append(1U,sep)
107 .append(escape(full_name)).append(1U,sep)
108 .append(escape(address)).append(1U,sep)
109 .append(escape(response)).append(1U,sep)
110 .append(escape(reason)) ;
111}
112
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.
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(std::string_view in, StringArray &out, char sep, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:1191
static std::string escaped(std::string_view, char c_escape, std::string_view specials_in, std::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