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
26 GSmtp::NetworkFilter::NetworkFilter( GNet::ExceptionSink es , FileStore & file_store ,
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
38 GSmtp::NetworkFilter::~NetworkFilter()
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
49 bool GSmtp::NetworkFilter::simple() const
50{
51 return false ;
52}
53
54 void 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
67 void 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
76 void 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
85 bool 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
101 G::Slot::Signal<int> & GSmtp::NetworkFilter::doneSignal()
102{
103 return m_done_signal ;
104}
105
106 void GSmtp::NetworkFilter::cancel()
107{
108 m_client_ptr.reset() ;
109 m_text.erase() ;
110}
111
112 bool GSmtp::NetworkFilter::abandoned() const
113{
114 return false ;
115}
GNet::ExceptionSink
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Definition: gexceptionsink.h:100
GSmtp::FileStore
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:58
GSmtp::NetworkFilter::~NetworkFilter
~NetworkFilter() override
Destructor.
Definition: gnetworkfilter.cpp:38
GSmtp::NetworkFilter::NetworkFilter
NetworkFilter(GNet::ExceptionSink, FileStore &, const std::string &server_location, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
Definition: gnetworkfilter.cpp:26
G::Str::tail
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
G::Str::printable
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
G::Str::head
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
glog.h
gnetworkfilter.h
gstr.h
G::Slot::slot
Slot< Args... > slot(TSink &sink, void(TSink::*method)(Args...))
A factory function for Slot objects.
Definition: gslot.h:201
G::Slot::Signal< int >