gnetworkverifier.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 
28 GSmtp::NetworkVerifier::NetworkVerifier( const std::string & server ,
29  unsigned int connection_timeout , unsigned int response_timeout ) :
30  m_resolver_info(server) ,
31  m_connection_timeout(connection_timeout) ,
32  m_response_timeout(response_timeout) ,
33  m_lazy(true)
34 {
35  G_DEBUG( "GSmtp::NetworkVerifier::ctor: " << server ) ;
36  m_client.eventSignal().connect( G::slot(*this,&GSmtp::NetworkVerifier::clientEvent) ) ;
37 }
38 
40 {
41  m_client.eventSignal().disconnect() ;
42 }
43 
44 void GSmtp::NetworkVerifier::verify( const std::string & to ,
45  const std::string & mail_from_parameter , const GNet::Address & client_ip ,
46  const std::string & auth_mechanism , const std::string & auth_extra )
47 {
48  if( !m_lazy || m_client.get() == NULL )
49  {
50  m_client.reset( new RequestClient("verify","","\n",m_resolver_info,m_connection_timeout,m_response_timeout) ) ;
51  }
52 
53  G::Strings args ;
54  args.push_back( to ) ;
55  args.push_back( G::Str::upper(G::Str::head(to,to.find('@'),to)) ) ;
56  args.push_back( G::Str::upper(G::Str::tail(to,to.find('@'),std::string())) ) ;
57  args.push_back( G::Str::upper(GNet::Local::fqdn()) ) ;
58  args.push_back( mail_from_parameter ) ;
59  args.push_back( client_ip.displayString(false) ) ;
60  args.push_back( auth_mechanism ) ;
61  args.push_back( auth_extra ) ;
62 
63  m_to = to ;
64  m_client->request( G::Str::join(args,"|") ) ;
65 }
66 
67 void GSmtp::NetworkVerifier::clientEvent( std::string s1 , std::string s2 )
68 {
69  G_DEBUG( "GSmtp::NetworkVerifier::clientEvent: [" << s1 << "] [" << s2 << "]" ) ;
70  if( s1 == "verify" )
71  {
72  VerifierStatus status ;
73 
74  // parse the output from the remote verifier using pipe-delimited
75  // fields based on the script-based verifier interface but backwards
76  //
77  G::StringArray part ;
78  G::Str::splitIntoFields( s2 , part , "|" ) ;
79  if( part.size() >= 1U && part[0U] == "100" )
80  {
81  status.is_valid = false ;
82  status.reason = "abort request" ; // TODO -- make this drop the connection
83  status.temporary = false ;
84  }
85  else if( part.size() >= 2U && part[0U] == "1" )
86  {
87  status.is_valid = true ;
88  status.is_local = false ;
89  status.address = part[1U] ;
90  }
91  else if( part.size() >= 3U && part[0U] == "0" )
92  {
93  status.is_valid = true ;
94  status.is_local = true ;
95  status.address = part[1U] ;
96  status.full_name = part[2U] ;
97  }
98  else if( part.size() >= 2U && ( part[0U] == "2" || part[0U] == "3" ) )
99  {
100  status.is_valid = false ;
101  status.reason = part[1U] ;
102  status.temporary = part[0U] == "3" ;
103  }
104  else
105  {
106  status.is_valid = false ;
107  status.reason = "external verifier protocol error" ;
108  status.temporary = false ;
109  }
110 
111  doneSignal().emit( G::Str::printable(m_to) , status ) ;
112  }
113 }
114 
116 {
117  return m_done_signal ;
118 }
119 
121 {
122  m_client.reset() ; // kiss
123  m_to.erase() ;
124 }
125 
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:507
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
static void splitIntoFields(const std::string &in, Strings &out, const std::string &seperators, char escape= '\0', bool discard_bogus_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:765
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
static std::string fqdn()
Returns the fully-qualified-domain-name.
Definition: glocal.cpp:67
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:44
Slot0 slot(T &object, void(T::*fn)())
Part of the slot/signal system.
Definition: gslot.h:156
virtual void reset()
Final override from GSmtp::Verifier.
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)
Final override from GSmtp::Verifier.
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:842
virtual G::Signal2< std::string, VerifierStatus > & doneSignal()
Final override from GSmtp::Verifier.
virtual ~NetworkVerifier()
Destructor.
std::string displayString(bool with_port=true, bool with_scope_id=false) const
Returns a string which represents the address for debugging and diagnostics purposes.
static std::string head(const std::string &in, std::string::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:834
static std::string upper(const std::string &s)
Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters...
Definition: gstr.cpp:426
#define G_DEBUG(expr)
Definition: glog.h:95
A structure returned by GSmtp::Verifier to describe the status of a rcpt-to recipient.
NetworkVerifier(const std::string &, unsigned int, unsigned int)
Constructor.
static std::string join(const Strings &strings, const std::string &sep)
Concatenates a set of strings.
Definition: gstr.cpp:799
A client class that interacts with a remote process with a stateless line-based request/response prot...