E-MailRelay
gnetworkverifier.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 // gnetworkverifier.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gnetworkverifier.h"
24 #include "glocal.h"
25 #include "gstr.h"
26 #include "glog.h"
27 
29  const std::string & server , unsigned int connection_timeout , unsigned int response_timeout ,
30  bool compatible ) :
31  m_exception_handler(exception_handler) ,
32  m_location(server) ,
33  m_connection_timeout(connection_timeout) ,
34  m_response_timeout(response_timeout) ,
35  m_lazy(true) ,
36  m_compatible(compatible)
37 {
38  G_DEBUG( "GSmtp::NetworkVerifier::ctor: " << server ) ;
39  m_client.eventSignal().connect( G::Slot::slot(*this,&GSmtp::NetworkVerifier::clientEvent) ) ;
40 }
41 
43 {
44  m_client.eventSignal().disconnect() ;
45 }
46 
47 void GSmtp::NetworkVerifier::verify( const std::string & mail_to_address ,
48  const std::string & mail_from_address , const GNet::Address & client_ip ,
49  const std::string & auth_mechanism , const std::string & auth_extra )
50 {
51  if( !m_lazy || m_client.get() == nullptr )
52  {
53  m_client.reset( new RequestClient("verify","",m_location,m_connection_timeout,m_response_timeout) ) ;
54  }
55 
56  G::StringArray args ;
57  if( m_compatible )
58  {
59  std::string user = G::Str::head( mail_to_address , "@" , false ) ;
60  std::string host = G::Str::tail( mail_to_address , "@" , true ) ;
61  args.push_back( mail_to_address ) ;
62  args.push_back( G::Str::upper(user) ) ;
63  args.push_back( G::Str::upper(host) ) ;
64  args.push_back( G::Str::upper(GNet::Local::canonicalName()) ) ;
65  args.push_back( mail_from_address ) ;
66  args.push_back( client_ip.hostPartString() ) ;
67  args.push_back( auth_mechanism ) ;
68  args.push_back( auth_extra ) ;
69  }
70  else
71  {
72  args.push_back( mail_to_address ) ;
73  args.push_back( mail_from_address ) ;
74  args.push_back( client_ip.displayString() ) ;
75  args.push_back( GNet::Local::canonicalName() ) ;
76  args.push_back( G::Str::lower(auth_mechanism) ) ;
77  args.push_back( auth_extra ) ;
78  }
79 
80  m_to_address = mail_to_address ;
81  m_client->request( G::Str::join("|",args) ) ;
82 }
83 
84 void GSmtp::NetworkVerifier::clientEvent( std::string s1 , std::string s2 )
85 {
86  G_DEBUG( "GSmtp::NetworkVerifier::clientEvent: [" << s1 << "] [" << s2 << "]" ) ;
87  if( s1 == "verify" )
88  {
89  VerifierStatus status ;
90 
91  // parse the output from the remote verifier using pipe-delimited
92  // fields based on the script-based verifier interface but backwards
93  //
94  G::StringArray part ;
95  G::Str::splitIntoFields( s2 , part , "|" ) ;
96  if( part.size() >= 1U && part[0U] == "100" )
97  {
98  status.is_valid = false ;
99  status.abort = true ;
100  }
101  else if( part.size() >= 2U && part[0U] == "1" )
102  {
103  status.is_valid = true ;
104  status.is_local = false ;
105  status.address = part[1U] ;
106  }
107  else if( part.size() >= 3U && part[0U] == "0" )
108  {
109  status.is_valid = true ;
110  status.is_local = true ;
111  status.address = part[1U] ;
112  status.full_name = part[2U] ;
113  }
114  else if( part.size() >= 2U && ( part[0U] == "2" || part[0U] == "3" ) )
115  {
116  status.is_valid = false ;
117  status.response = part[1U] ;
118  status.temporary = part[0U] == "3" ;
119  }
120  else
121  {
122  status.is_valid = false ;
123  status.temporary = false ;
124  }
125 
126  try
127  {
128  doneSignal().emit( G::Str::printable(m_to_address) , status ) ;
129  }
130  catch( std::exception & e )
131  {
132  m_exception_handler.onException( e ) ;
133  }
134  }
135 }
136 
138 {
139  return m_done_signal ;
140 }
141 
143 {
144  m_client.reset() ;
145  m_to_address.erase() ;
146 }
147 
148 /// \file gnetworkverifier.cpp
virtual void verify(const std::string &rcpt_to_parameter, const std::string &mail_from_parameter, const GNet::Address &client_ip, const std::string &auth_mechanism, const std::string &auth_extra) override
Override from GSmtp::Verifier.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:701
static std::string canonicalName()
Returns the canonical network name assiciated with hostname().
Definition: glocal.cpp:54
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
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:1088
virtual G::Slot::Signal2< std::string, VerifierStatus > & doneSignal() override
Override from GSmtp::Verifier.
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
virtual ~NetworkVerifier()
Destructor.
virtual void cancel() override
Override from GSmtp::Verifier.
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 uppercase characters have been replaced by lowercase chara...
Definition: gstr.cpp:599
NetworkVerifier(GNet::ExceptionHandler &, const std::string &server, unsigned int connection_timeout, unsigned int response_timeout, bool compatible)
Constructor.
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
Definition: gslot.h:209
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lowercase characters have been replaced by uppercase chara...
Definition: gstr.cpp:619
A structure returned by GSmtp::Verifier to describe the status of a 'rcpt-to' recipient.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1023
static void splitIntoFields(const std::string &in, StringArray &out, const std::string &separators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:975
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
A network client class that interacts with a remote server using a stateless line-based request/respo...