E-MailRelay

gspamclient.h
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 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 "gclient.h"
26#include "gtimer.h"
27#include "gpath.h"
28#include "gslot.h"
29#include "gexception.h"
30#include "gexceptionsink.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///
46 class GSmtp::SpamClient : public GNet::Client
47{
48 public:
49 G_EXCEPTION( Error , "spam client error" ) ;
50
51 SpamClient( GNet::ExceptionSink , const GNet::Location & host_and_service ,
52 bool read_only , 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 private: // overrides
75 void onConnect() override ; // Override from GNet::SimpleClient.
76 bool onReceive( const char * , std::size_t , std::size_t , std::size_t , char ) override ; // Override from GNet::Client.
77 void onSendComplete() override ; // Override from GNet::SimpleClient.
78 void onSecure( const std::string & , const std::string & , const std::string & ) override ; // Override from GNet::SocketProtocolSink.
79 void onDelete( const std::string & ) override ; // Override from GNet::HeapClient.
80
81 public:
82 ~SpamClient() override = default ;
83 SpamClient( const SpamClient & ) = delete ;
84 SpamClient( SpamClient && ) = delete ;
85 void operator=( const SpamClient & ) = delete ;
86 void operator=( SpamClient && ) = delete ;
87
88 private:
89 void onTimeout() ;
90 void start() ;
91 static GNet::Client::Config netConfig( unsigned int connect_timeout , unsigned int response_timeout ) ;
92
93 private:
94 struct Request
95 {
96 explicit Request( Client & ) ;
97 void send( const std::string & path , const std::string & username ) ;
98 bool sendMore() ;
99 Client * m_client ;
100 std::ifstream m_stream ;
101 std::string m_size ;
102 std::vector<char> m_buffer ;
103 } ;
104 struct Response
105 {
106 explicit Response( bool read_only ) ;
107 ~Response() ;
108 Response( const Response & ) = delete ;
109 Response( Response && ) = delete ;
110 void operator=( const Response & ) = delete ;
111 void operator=( Response && ) = delete ;
112 void add( const std::string & , const std::string & ) ;
113 bool ok( const std::string & ) const ;
114 bool complete() const ;
115 std::string result() const ;
116 bool m_read_only ;
117 int m_state ;
118 std::string m_path_tmp ;
119 std::string m_path_final ;
120 std::ofstream m_stream ;
121 std::size_t m_content_length ;
122 std::size_t m_size ;
123 std::string m_result ;
124 } ;
125
126 private:
127 std::string m_path ;
128 bool m_busy ;
129 GNet::Timer<SpamClient> m_timer ;
130 Request m_request ;
131 Response m_response ;
132 static std::string m_username ;
133} ;
134
135#endif
GNet::Client
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gclient.h:75
GNet::Client::send
bool send(const std::string &data, std::size_t offset=0)
Sends data to the peer and starts the response timer (if configured).
Definition: gclient.cpp:474
GNet::ExceptionSink
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Definition: gexceptionsink.h:100
GNet::Location
A class that represents the remote target for out-going client connections.
Definition: glocation.h:71
GNet::Timer
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:129
GSmtp::Client
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
Definition: gsmtpclient.h:54
GSmtp::SpamClient
A client class that interacts with a remote process using a protocol somewhat similar to the spamassa...
Definition: gspamclient.h:47
GSmtp::SpamClient::username
static void username(const std::string &)
Sets the username used in the network protocol.
Definition: gspamclient.cpp:52
GSmtp::SpamClient::SpamClient
SpamClient(GNet::ExceptionSink, const GNet::Location &host_and_service, bool read_only, unsigned int connect_timeout, unsigned int response_timeout)
Constructor.
Definition: gspamclient.cpp:30
GSmtp::SpamClient::request
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:62
GSmtp::SpamClient::busy
bool busy() const
Returns true after request() and before the subsequent event signal.
Definition: gspamclient.cpp:57
gclient.h
gexception.h
gexceptionsink.h
gpath.h
gslot.h
gtimer.h
GSmtp
SMTP and message-store classes.
Definition: gadminserver.h:39
GNet::Client::Config
A structure containing GNet::Client configuration parameters.
Definition: gclient.h:84