E-MailRelay
gspamfilter.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 // gspamfilter.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gspamfilter.h"
24 #include "gstr.h"
25 #include "glog.h"
26 
28  bool server_side , const std::string & server , bool read_only , bool always_pass ,
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_read_only(read_only) ,
34  m_always_pass(always_pass) ,
35  m_connection_timeout(connection_timeout) ,
36  m_response_timeout(response_timeout)
37 {
38  m_client.eventSignal().connect( G::Slot::slot(*this,&GSmtp::SpamFilter::clientEvent) ) ;
39 }
40 
42 {
43  m_client.eventSignal().disconnect() ;
44 }
45 
46 std::string GSmtp::SpamFilter::id() const
47 {
48  return m_location.displayString() ;
49 }
50 
52 {
53  return false ;
54 }
55 
56 void GSmtp::SpamFilter::start( const std::string & path )
57 {
58  // the spam client can do more than one request, but it is simpler to start fresh
59  m_client.reset( new SpamClient(m_location,m_read_only,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::SpamFilter::clientEvent( std::string s1 , std::string s2 )
66 {
67  G_DEBUG( "GSmtp::SpamFilter::clientEvent: [" << s1 << "] [" << s2 << "]" ) ;
68  if( s1 == "spam" )
69  {
70  m_text = ( s2.empty() || m_always_pass ) ? std::string() : ("spam: "+G::Str::printable(s2)) ;
71  emit( m_text.empty() ) ;
72  }
73  else if( s1 == "failed" )
74  {
75  m_text = G::Str::printable(s2) ;
76  emit( m_text.empty() ) ;
77  }
78 }
79 
80 void GSmtp::SpamFilter::emit( bool ok )
81 {
82  try
83  {
84  m_done_signal.emit( ok ? 0 : 2 ) ;
85  }
86  catch( std::exception & e )
87  {
88  m_exception_handler.onException( e ) ;
89  }
90 }
91 
93 {
94  return false ;
95 }
96 
97 std::string GSmtp::SpamFilter::response() const
98 {
99  return m_text.empty() ? std::string() : std::string("rejected") ;
100 }
101 
102 std::string GSmtp::SpamFilter::reason() const
103 {
104  return m_text ;
105 }
106 
108 {
109  return m_done_signal ;
110 }
111 
113 {
114  G_DEBUG( "GSmtp::SpamFilter::cancel: cancelled" ) ;
115  m_text.erase() ;
116  if( m_client.get() != nullptr && m_client->busy() )
117  m_client.reset() ;
118 }
119 
121 {
122  return false ;
123 }
124 
125 /// \file gspamfilter.cpp
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:701
virtual ~SpamFilter()
Destructor.
Definition: gspamfilter.cpp:41
virtual void start(const std::string &path) override
Override from from GSmtp::Filter.
Definition: gspamfilter.cpp:56
virtual std::string response() const override
Override from from GSmtp::Filter.
Definition: gspamfilter.cpp:97
virtual std::string reason() const override
Override from from GSmtp::Filter.
virtual bool abandoned() const override
Override from from GSmtp::Filter.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
Definition: gslot.h:209
A client class that interacts with a remote process using a protocol somewhat similar to the spamassa...
Definition: gspamclient.h:46
virtual bool special() const override
Override from from GSmtp::Filter.
Definition: gspamfilter.cpp:92
virtual bool simple() const override
Override from from GSmtp::Filter.
Definition: gspamfilter.cpp:51
SpamFilter(GNet::ExceptionHandler &, bool server_side, const std::string &server_location, bool read_only, bool always_pass, unsigned int connection_timeout, unsigned int response_timeout)
Constructor.
Definition: gspamfilter.cpp:27
virtual G::Slot::Signal1< int > & doneSignal() override
Override from from GSmtp::Filter.
virtual void cancel() override
Override from from GSmtp::Filter.
virtual std::string id() const override
Override from from GSmtp::Filter.
Definition: gspamfilter.cpp:46