E-MailRelay

gfactoryparser.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 gfactoryparser.cpp
19///
20
21#include "gdef.h"
22#include "gfactoryparser.h"
23#include "gaddress.h"
24#include "gresolver.h"
25#include "gexecutablecommand.h"
26#include "gstr.h"
27#include "gfile.h"
28#include "glog.h"
29
30 GSmtp::FactoryParser::Result GSmtp::FactoryParser::parse( const std::string & identifier , bool allow_spam )
31{
32 G_DEBUG( "GSmtp::FactoryParser::parse: [" << identifier << "]" ) ;
33 if( identifier.find("net:") == 0U )
34 {
35 return Result( "net" , G::Str::tail(identifier,":") ) ;
36 }
37 else if( allow_spam && identifier.find("spam:") == 0U )
38 {
39 return Result( "spam" , G::Str::tail(identifier,":") , 0 ) ;
40 }
41 else if( allow_spam && identifier.find("spam-edit:") == 0U )
42 {
43 return Result( "spam" , G::Str::tail(identifier,":") , 1 ) ;
44 }
45 else if( identifier.find("file:") == 0U )
46 {
47 return Result( "file" , G::Str::tail(identifier,":") ) ;
48 }
49 else if( identifier.find("exit:") == 0U )
50 {
51 return Result( "exit" , G::Str::tail(identifier,":") ) ;
52 }
53 else if( !identifier.empty() )
54 {
55 return Result( "file" , identifier ) ;
56 }
57 else
58 {
59 return Result() ;
60 }
61}
62
63std::string GSmtp::FactoryParser::check( const std::string & identifier , bool allow_spam )
64{
65 Result p = parse( identifier , allow_spam ) ;
66 if( p.first == "net" || ( allow_spam && p.first == "spam" ) )
67 {
68 return std::string() ;
69 }
70 else if( p.first == "file" )
71 {
72 G::Path exe = p.second ;
73 if( !G::File::exists(exe,std::nothrow) )
74 return "no such file" ;
75 else if( !G::is_windows() && !G::File::isExecutable(exe,std::nothrow) )
76 return "probably not executable" ;
77 else if( !exe.isAbsolute() )
78 return "not an absolute path" ;
79 else
80 return std::string() ;
81 }
82 else if( p.first == "exit" )
83 {
84 if( !G::Str::isUInt(p.second) )
85 return "not a numeric exit code" ;
86 else
87 return std::string() ;
88 }
89 else
90 {
91 return std::string() ;
92 }
93}
94
95GSmtp::FactoryParser::Result::Result()
96= default ;
97
98GSmtp::FactoryParser::Result::Result( const std::string & first_ , const std::string & second_ ) :
99 first(first_) ,
100 second(second_)
101{
102}
103
104GSmtp::FactoryParser::Result::Result( const std::string & first_ , const std::string & second_ , int third_ ) :
105 first(first_) ,
106 second(second_) ,
107 third(third_)
108{
109}
110
GSmtp::FactoryParser::parse
static Result parse(const std::string &identifier, bool allow_spam)
Parses an identifier like "/usr/bin/foo" or "net:127.0.0.1:99" or "net:/run/spamd....
Definition: gfactoryparser.cpp:30
GSmtp::FactoryParser::check
static std::string check(const std::string &identifier, bool allow_spam)
Parses and checks an identifier.
Definition: gfactoryparser.cpp:63
G::File::isExecutable
static bool isExecutable(const Path &, std::nothrow_t)
Returns true if the path is probably executable by the calling process.
Definition: gfile.cpp:193
G::File::exists
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
Definition: gfile.cpp:151
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Path::isAbsolute
bool isAbsolute() const
Returns !isRelative().
Definition: gpath.cpp:318
G::Str::tail
static std::string tail(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:1287
G::Str::isUInt
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception.
Definition: gstr.cpp:444
gaddress.h
gexecutablecommand.h
gfactoryparser.h
gfile.h
glog.h
gresolver.h
gstr.h
GSmtp::FactoryParser::Result
Result tuple for GSmtp::FactoryParser::parse().
Definition: gfactoryparser.h:42