E-MailRelay

gverifierstatus.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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
31 GSmtp::VerifierStatus GSmtp::VerifierStatus::invalid( const std::string & recipient ,
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
43 GSmtp::VerifierStatus GSmtp::VerifierStatus::remote( const std::string & recipient ,
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
54 GSmtp::VerifierStatus GSmtp::VerifierStatus::local( const std::string & recipient ,
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
66 GSmtp::VerifierStatus GSmtp::VerifierStatus::parse( const std::string & line )
67{
68 try
69 {
70 G::StringArray part ;
71 G::Str::splitIntoFields( line , part , {"|",1U} , '\\' ) ;
72 if( part.size() != 9U )
73 throw InvalidStatus() ;
74
75 std::size_t i = 0U ;
76 VerifierStatus s ;
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 , '\\' , "\\|" , "\\|" ) ; } ;
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
GSmtp::VerifierStatus
A structure returned by GSmtp::Verifier to describe the status of a 'rcpt-to' or 'vrfy' recipient.
Definition: gverifierstatus.h:52
GSmtp::VerifierStatus::local
static VerifierStatus local(const std::string &recipient, const std::string &full_name, const std::string &mbox)
Constructor for a valid local mailbox.
Definition: gverifierstatus.cpp:54
GSmtp::VerifierStatus::str
std::string str() const
Returns a string representation of the structure.
Definition: gverifierstatus.cpp:95
GSmtp::VerifierStatus::parse
static VerifierStatus parse(const std::string &str)
Parses a str() string into a structure.
Definition: gverifierstatus.cpp:66
GSmtp::VerifierStatus::invalid
static VerifierStatus invalid(const std::string &recipient, bool temporary=false, const std::string &response={}, const std::string &reason={})
Factory for an invalid address.
Definition: gverifierstatus.cpp:31
GSmtp::VerifierStatus::remote
static VerifierStatus remote(const std::string &recipient, const std::string &address={})
Constructor for a valid remote mailbox.
Definition: gverifierstatus.cpp:43
G::Str::splitIntoFields
static void splitIntoFields(const std::string &in, StringArray &out, string_view ws, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:1146
G::Str::escaped
static std::string escaped(const std::string &, char c_escape, const std::string &specials_in, const std::string &specials_out)
Returns the escape()d string.
Definition: gstr.cpp:101
glog.h
gstr.h
gstringview.h
gverifier.h
gverifierstatus.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31