E-MailRelay
gnetworkfilter.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file gnetworkfilter.cpp
19///
20
21#include "gdef.h"
22#include "gnetworkfilter.h"
23#include "gstr.h"
24#include "glog.h"
25
27 const std::string & server , unsigned int connection_timeout , unsigned int response_timeout ) :
28 m_es(es) ,
29 m_file_store(file_store) ,
30 m_location(server) ,
31 m_connection_timeout(connection_timeout) ,
32 m_response_timeout(response_timeout)
33{
34 m_client_ptr.eventSignal().connect( G::Slot::slot(*this,&GSmtp::NetworkFilter::clientEvent) ) ;
35 m_client_ptr.deletedSignal().connect( G::Slot::slot(*this,&GSmtp::NetworkFilter::clientDeleted) ) ;
36}
37
39{
40 m_client_ptr.eventSignal().disconnect() ;
41 m_client_ptr.deletedSignal().disconnect() ;
42}
43
44std::string GSmtp::NetworkFilter::id() const
45{
46 return m_location.displayString() ;
47}
48
49bool GSmtp::NetworkFilter::simple() const
50{
51 return false ;
52}
53
54void GSmtp::NetworkFilter::start( const MessageId & message_id )
55{
56 m_text.erase() ;
57 if( m_client_ptr.get() == nullptr )
58 {
59 m_client_ptr.reset( std::make_unique<RequestClient>(
60 GNet::ExceptionSink(m_client_ptr,m_es.esrc()),
61 "scanner" , "ok" ,
62 m_location , m_connection_timeout , m_response_timeout ) ) ;
63 }
64 m_client_ptr->request( m_file_store.contentPath(message_id).str() ) ; // (no need to wait for connection)
65}
66
67void GSmtp::NetworkFilter::clientDeleted( const std::string & reason )
68{
69 if( !reason.empty() )
70 {
71 m_text = "failed" "\t" + reason ;
72 m_done_signal.emit( 2 ) ;
73 }
74}
75
76void GSmtp::NetworkFilter::clientEvent( const std::string & s1 , const std::string & s2 , const std::string & )
77{
78 if( s1 == "scanner" ) // ie. this is the response received by the RequestClient
79 {
80 m_text = s2 ;
81 m_done_signal.emit( m_text.empty() ? 0 : 2 ) ;
82 }
83}
84
85bool GSmtp::NetworkFilter::special() const
86{
87 return false ;
88}
89
90std::string GSmtp::NetworkFilter::response() const
91{
92 // allow "<response><tab><reason>"
93 return G::Str::printable( G::Str::head( m_text , "\t" , false ) ) ;
94}
95
96std::string GSmtp::NetworkFilter::reason() const
97{
98 return G::Str::printable( G::Str::tail( m_text , "\t" , false ) ) ;
99}
100
101G::Slot::Signal<int> & GSmtp::NetworkFilter::doneSignal()
102{
103 return m_done_signal ;
104}
105
106void GSmtp::NetworkFilter::cancel()
107{
108 m_client_ptr.reset() ;
109 m_text.erase() ;
110}
111
112bool GSmtp::NetworkFilter::abandoned() const
113{
114 return false ;
115}
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:58
~NetworkFilter() override
Destructor.
NetworkFilter(GNet::ExceptionSink, FileStore &, const std::string &server_location, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
static std::string tail(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:1287
static std::string printable(const std::string &in, char escape='\\')
Returns a printable representation of the given input string, using chacter code ranges 0x20 to 0x7e ...
Definition: gstr.cpp:885
static std::string head(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:1273
Slot< Args... > slot(TSink &sink, void(TSink::*method)(Args...))
A factory function for Slot objects.
Definition: gslot.h:201