E-MailRelay
gfactoryparser.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 // gfactoryparser.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gfactoryparser.h"
24 #include "gaddress.h"
25 #include "gresolver.h"
26 #include "gexecutablecommand.h"
27 #include "gstr.h"
28 #include "gfile.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 
63 std::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,G::File::NoThrow()) )
74  return "no such file" ;
75  else if( ! G::File::executable(exe) )
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 
95 GSmtp::FactoryParser::Result::Result() :
96  third(0)
97 {
98 }
99 
100 GSmtp::FactoryParser::Result::Result( const std::string & first_ , const std::string & second_ ) :
101  first(first_) ,
102  second(second_) ,
103  third(0)
104 {
105 }
106 
107 GSmtp::FactoryParser::Result::Result( const std::string & first_ , const std::string & second_ , int third_ ) :
108  first(first_) ,
109  second(second_) ,
110  third(third_)
111 {
112 }
113 
114 /// \file gfactoryparser.cpp
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:1102
Result tuple for GSmtp::FactoryParser::parse().
static std::string check(const std::string &identifier, bool allow_spam)
Parses and checks an identifier.
static bool executable(const Path &)
Returns true if the path is probably executable.
Definition: gfile_unix.cpp:79
bool isAbsolute() const
Returns !isRelative().
Definition: gpath.cpp:300
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
Definition: gfile.cpp:141
An overload discriminator class for File methods.
Definition: gfile.h:53
static Result parse(const std::string &identifier, bool allow_spam)
Parses an identifier like "/usr/bin/foo" or "net:127.0.0.1:99" returning the type and the specificati...
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:304
A Path object represents a file system path.
Definition: gpath.h:72