E-MailRelay
gnetworkfilter.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 // gnetworkfilter.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gnetworkfilter.h"
24 #include "gstr.h"
25 #include "glog.h"
26 
28  bool server_side , const std::string & server ,
29  unsigned int connection_timeout , unsigned int response_timeout ) :
30  m_exception_handler(exception_handler) ,
31  m_server_side(server_side) ,
32  m_location(server) ,
33  m_connection_timeout(connection_timeout) ,
34  m_response_timeout(response_timeout) ,
35  m_lazy(true)
36 {
37  m_client.eventSignal().connect( G::Slot::slot(*this,&GSmtp::NetworkFilter::clientEvent) ) ;
38 }
39 
41 {
42  m_client.eventSignal().disconnect() ;
43 }
44 
45 std::string GSmtp::NetworkFilter::id() const
46 {
47  return m_location.displayString() ;
48 }
49 
51 {
52  return false ;
53 }
54 
55 void GSmtp::NetworkFilter::start( const std::string & path )
56 {
57  if( !m_lazy || m_client.get() == nullptr )
58  {
59  m_client.reset( new RequestClient("scanner","ok",m_location,m_connection_timeout,m_response_timeout) );
60  }
61  m_text.erase() ;
62  m_client->request( path ) ; // (no need to wait for connection)
63 }
64 
65 void GSmtp::NetworkFilter::clientEvent( std::string s1 , std::string s2 )
66 {
67  G_DEBUG( "GSmtp::NetworkFilter::clientEvent: [" << s1 << "] [" << s2 << "]" ) ;
68  if( s1 == "scanner" )
69  {
70  m_text = s2 ;
71  try
72  {
73  m_done_signal.emit( m_text.empty() ? 0 : 2 ) ;
74  }
75  catch( std::exception & e )
76  {
77  m_exception_handler.onException( e ) ;
78  }
79  }
80 }
81 
83 {
84  return false ;
85 }
86 
87 std::string GSmtp::NetworkFilter::response() const
88 {
89  // allow "<response><tab><reason>"
90  return G::Str::printable( G::Str::head( m_text , "\t" , false ) ) ;
91 }
92 
93 std::string GSmtp::NetworkFilter::reason() const
94 {
95  return G::Str::printable( G::Str::tail( m_text , "\t" , false ) ) ;
96 }
97 
99 {
100  return m_done_signal ;
101 }
102 
104 {
105  m_client.reset() ;
106  m_text.erase() ;
107 }
108 
110 {
111  return false ;
112 }
113 /// \file gnetworkfilter.cpp
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 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
virtual void start(const std::string &path) override
Override from from GSmtp::Filter.
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
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
virtual std::string response() const override
Override from from GSmtp::Filter.
virtual ~NetworkFilter()
Destructor.
virtual std::string reason() const override
Override from from GSmtp::Filter.
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
Definition: gslot.h:209
virtual bool simple() const override
Override from from GSmtp::Filter.
NetworkFilter(GNet::ExceptionHandler &, bool server_side, const std::string &server_location, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
virtual G::Slot::Signal1< int > & doneSignal() override
Override from from GSmtp::Filter.
virtual void cancel() override
Override from from GSmtp::Filter.
virtual bool special() const override
Override from from GSmtp::Filter.
A network client class that interacts with a remote server using a stateless line-based request/respo...
virtual bool abandoned() const override
Override from from GSmtp::Filter.
virtual std::string id() const override
Override from from GSmtp::Filter.