E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
27 GSmtp::NetworkFilter::NetworkFilter( GNet::ExceptionHandler & exception_handler ,
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
40 GSmtp::NetworkFilter::~NetworkFilter()
41 {
42 m_client.eventSignal().disconnect() ;
43 }
44
45 std::string GSmtp::NetworkFilter::id() const
46 {
47 return m_location.displayString() ;
48 }
49
50 bool GSmtp::NetworkFilter::simple() const
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
82 bool GSmtp::NetworkFilter::special() const
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
98 G::Slot::Signal1<int> & GSmtp::NetworkFilter::doneSignal()
99 {
100 return m_done_signal ;
101 }
102
103 void GSmtp::NetworkFilter::cancel()
104 {
105 m_client.reset() ;
106 m_text.erase() ;
107 }
108
109 bool GSmtp::NetworkFilter::abandoned() const
110 {
111 return false ;
112 }
113 /// \file gnetworkfilter.cpp
G::Str::printable
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:701
G::Str::tail
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
GSmtp::NetworkFilter::start
virtual void start(const std::string &path) override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:55
glog.h
G::Str::head
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
GNet::ExceptionHandler
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
Definition: gexceptionhandler.h:44
GSmtp::NetworkFilter::response
virtual std::string response() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:87
gstr.h
GSmtp::NetworkFilter::~NetworkFilter
virtual ~NetworkFilter()
Destructor.
Definition: gnetworkfilter.cpp:40
GSmtp::NetworkFilter::reason
virtual std::string reason() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:93
G::Slot::slot
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
Definition: gslot.h:209
GSmtp::NetworkFilter::simple
virtual bool simple() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:50
GSmtp::NetworkFilter::NetworkFilter
NetworkFilter(GNet::ExceptionHandler &, bool server_side, const std::string &server_location, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
Definition: gnetworkfilter.cpp:27
GSmtp::NetworkFilter::doneSignal
virtual G::Slot::Signal1< int > & doneSignal() override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:98
gsmtp.h
GSmtp::NetworkFilter::cancel
virtual void cancel() override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:103
GSmtp::NetworkFilter::special
virtual bool special() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:82
G::Slot::Signal1< int >
gnetworkfilter.h
GSmtp::RequestClient
A network client class that interacts with a remote server using a stateless line-based request/respo...
Definition: grequestclient.h:44
GSmtp::NetworkFilter::abandoned
virtual bool abandoned() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:109
GSmtp::NetworkFilter::id
virtual std::string id() const override
Override from from GSmtp::Filter.
Definition: gnetworkfilter.cpp:45