E-MailRelay
gspamclient.h
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 /// \file gspamclient.h
19 ///
20 
21 #ifndef G_SMTP_SPAM_CLIENT_H
22 #define G_SMTP_SPAM_CLIENT_H
23 
24 #include "gdef.h"
25 #include "gsmtp.h"
26 #include "gclient.h"
27 #include "gtimer.h"
28 #include "gpath.h"
29 #include "gslot.h"
30 #include "gexception.h"
31 #include <fstream>
32 #include <vector>
33 
34 namespace GSmtp
35 {
36  class SpamClient ;
37 }
38 
39 /// \class GSmtp::SpamClient
40 /// A client class that interacts with a remote process using a
41 /// protocol somewhat similar to the spamassassin spamc/spamd
42 /// protocol. The interface is similar to GSmtp::RequestClient
43 /// but it is single-use: only one request() can be made per
44 /// object.
45 ///
47 {
48 public:
49  G_EXCEPTION( Error , "spam client error" ) ;
50 
51  SpamClient( const GNet::Location & host_and_service , bool read_only ,
52  unsigned int connect_timeout , unsigned int response_timeout ) ;
53  ///< Constructor.
54 
55  void request( const std::string & file_path ) ;
56  ///< Starts sending a request that comprises a few http-like header
57  ///< lines followed by the contents of the given file. The response
58  ///< is spooled into a temporary file and then committed back to the
59  ///< same file.
60  ///<
61  ///< The base class's "event" signal will be emitted when processing
62  ///< is complete. In this case the first signal parameter will "spam"
63  ///< and the second will be the parsed response.
64  ///<
65  ///< See also GNet::Client::eventSignal().
66 
67  bool busy() const ;
68  ///< Returns true after request() and before the subsequent
69  ///< event signal.
70 
71  static void username( const std::string & ) ;
72  ///< Sets the username used in the network protocol.
73 
74 protected:
75  virtual ~SpamClient() ;
76  ///< Destructor.
77 
78  virtual void onConnect() override ;
79  ///< Override from GNet::SimpleClient.
80 
81  virtual bool onReceive( const char * , size_t , size_t ) override ;
82  ///< Override from GNet::Client.
83 
84  virtual void onSendComplete() override ;
85  ///< Override from GNet::SimpleClient.
86 
87  virtual void onSecure( const std::string & ) override ;
88  ///< Override from GNet::SocketProtocolSink.
89 
90  virtual void onDelete( const std::string & ) override ;
91  ///< Override from GNet::HeapClient.
92 
93 private:
94  SpamClient( const SpamClient & ) ; // not implemented
95  void operator=( const SpamClient & ) ; // not implemented
96  void onTimeout() ;
97  void start() ;
98 
99 private:
100  struct Request
101  {
102  explicit Request( Client & ) ;
103  void send( const std::string & path , const std::string & username ) ;
104  bool sendMore() ;
105  Client * m_client ;
106  std::ifstream m_stream ;
107  std::string m_size ;
108  std::vector<char> m_buffer ;
109  } ;
110  struct Response
111  {
112  explicit Response( bool read_only ) ;
113  ~Response() ;
114  void add( const std::string & , const std::string & ) ;
115  bool ok( const std::string & ) const ;
116  bool complete() const ;
117  std::string result() const ;
118  bool m_read_only ;
119  int m_state ;
120  std::string m_path_tmp ;
121  std::string m_path_final ;
122  std::ofstream m_stream ;
123  size_t m_content_length ;
124  size_t m_size ;
125  std::string m_result ;
126  } ;
127 
128 private:
129  std::string m_path ;
130  bool m_busy ;
131  GNet::Timer<SpamClient> m_timer ;
132  Request m_request ;
133  Response m_response ;
134  static std::string m_username ;
135 } ;
136 
137 #endif
bool busy() const
Returns true after request() and before the subsequent event signal.
Definition: gspamclient.cpp:56
SMTP and message-store classes.
Definition: gadminserver.h:39
SpamClient(const GNet::Location &host_and_service, bool read_only, unsigned int connect_timeout, unsigned int response_timeout)
Constructor.
Definition: gspamclient.cpp:32
bool send(const std::string &data, std::string::size_type offset=0)
Sends data to the peer.
virtual bool onReceive(const char *, size_t, size_t) override
Override from GNet::Client.
static void username(const std::string &)
Sets the username used in the network protocol.
Definition: gspamclient.cpp:51
virtual void onSecure(const std::string &) override
Override from GNet::SocketProtocolSink.
Definition: gspamclient.cpp:82
virtual void onDelete(const std::string &) override
Override from GNet::HeapClient.
Definition: gspamclient.cpp:78
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
Definition: glocation.h:50
virtual ~SpamClient()
Destructor.
Definition: gspamclient.cpp:46
A client class that interacts with a remote process using a protocol somewhat similar to the spamassa...
Definition: gspamclient.h:46
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
Definition: gsmtpclient.h:52
virtual void onSendComplete() override
Override from GNet::SimpleClient.
Definition: gspamclient.cpp:97
virtual void onConnect() override
Override from GNet::SimpleClient.
Definition: gspamclient.cpp:86
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:115
A HeapClient class that adds slot/signal signalling, connection/response timeouts, and input line buffering.
Definition: gclient.h:48
void request(const std::string &file_path)
Starts sending a request that comprises a few http-like header lines followed by the contents of the ...
Definition: gspamclient.cpp:61