E-MailRelay

gspamfilter.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 gspamfilter.cpp
19///
20
21#include "gdef.h"
22#include "gspamfilter.h"
23#include "gstr.h"
24#include "glog.h"
25
26 GSmtp::SpamFilter::SpamFilter( GNet::ExceptionSink es , FileStore & file_store ,
27 const std::string & server , bool read_only , bool always_pass ,
28 unsigned int connection_timeout , unsigned int response_timeout ) :
29 m_es(es) ,
30 m_file_store(file_store) ,
31 m_location(server) ,
32 m_read_only(read_only) ,
33 m_always_pass(always_pass) ,
34 m_connection_timeout(connection_timeout) ,
35 m_response_timeout(response_timeout)
36{
37 m_client_ptr.eventSignal().connect( G::Slot::slot(*this,&GSmtp::SpamFilter::clientEvent) ) ;
38 m_client_ptr.deletedSignal().connect( G::Slot::slot(*this,&GSmtp::SpamFilter::clientDeleted) ) ;
39}
40
41 GSmtp::SpamFilter::~SpamFilter()
42{
43 m_client_ptr.eventSignal().disconnect() ;
44 m_client_ptr.deletedSignal().disconnect() ;
45}
46
47std::string GSmtp::SpamFilter::id() const
48{
49 return m_location.displayString() ;
50}
51
52 bool GSmtp::SpamFilter::simple() const
53{
54 return false ;
55}
56
57 void GSmtp::SpamFilter::start( const MessageId & message_id )
58{
59 // the spam client can do more than one request, but it is simpler to start fresh
60 m_client_ptr.reset( std::make_unique<SpamClient>( GNet::ExceptionSink(m_client_ptr,m_es.esrc()) ,
61 m_location , m_read_only , m_connection_timeout , m_response_timeout ) ) ;
62
63 m_text.erase() ;
64 m_client_ptr->request( m_file_store.contentPath(message_id).str() ) ; // (no need to wait for connection)
65}
66
67 void GSmtp::SpamFilter::clientDeleted( const std::string & reason )
68{
69 if( !reason.empty() )
70 {
71 G_WARNING( "GSmtp::SpamFilter::clientDeleted: spamd interaction failed: " << reason ) ;
72 m_text = reason ;
73 emit( false ) ;
74 }
75}
76
77 void GSmtp::SpamFilter::clientEvent( const std::string & s1 , const std::string & s2 , const std::string & )
78{
79 G_DEBUG( "GSmtp::SpamFilter::clientEvent: [" << s1 << "] [" << s2 << "]" ) ;
80 if( s1 == "spam" )
81 {
82 m_text = ( s2.empty() || m_always_pass ) ? std::string() : ("spam: "+G::Str::printable(s2)) ;
83 emit( m_text.empty() ) ;
84 }
85 else if( s1 == "failed" )
86 {
87 m_text = G::Str::printable(s2) ;
88 emit( m_text.empty() ) ;
89 }
90}
91
92 void GSmtp::SpamFilter::emit( bool ok )
93{
94 m_done_signal.emit( ok ? 0 : 2 ) ;
95}
96
97 bool GSmtp::SpamFilter::special() const
98{
99 return false ;
100}
101
102std::string GSmtp::SpamFilter::response() const
103{
104 return m_text.empty() ? std::string() : std::string("rejected") ;
105}
106
107std::string GSmtp::SpamFilter::reason() const
108{
109 return m_text ;
110}
111
112 G::Slot::Signal<int> & GSmtp::SpamFilter::doneSignal()
113{
114 return m_done_signal ;
115}
116
117 void GSmtp::SpamFilter::cancel()
118{
119 G_DEBUG( "GSmtp::SpamFilter::cancel: cancelled" ) ;
120 m_text.erase() ;
121 if( m_client_ptr.get() != nullptr && m_client_ptr->busy() )
122 m_client_ptr.reset() ;
123}
124
125 bool GSmtp::SpamFilter::abandoned() const
126{
127 return false ;
128}
129
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::SpamFilter::~SpamFilter
~SpamFilter() override
Destructor.
Definition: gspamfilter.cpp:41
GSmtp::SpamFilter::SpamFilter
SpamFilter(GNet::ExceptionSink, FileStore &, const std::string &server_location, bool read_only, bool always_pass, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
Definition: gspamfilter.cpp:26
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
glog.h
gspamfilter.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 >