E-MailRelay
gsmtpclient.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 gsmtpclient.h
19///
20
21#ifndef G_SMTP_CLIENT_H
22#define G_SMTP_CLIENT_H
23
24#include "gdef.h"
25#include "glocation.h"
26#include "gsecrets.h"
27#include "glinebuffer.h"
28#include "gclient.h"
29#include "gsmtpclientprotocol.h"
30#include "gmessagestore.h"
31#include "gstoredmessage.h"
32#include "gfilter.h"
33#include "gfilterfactory.h"
34#include "gcall.h"
35#include "gsocket.h"
36#include "gslot.h"
37#include "gtimer.h"
38#include "gstrings.h"
39#include "gexception.h"
40#include <memory>
41#include <iostream>
42
43namespace GSmtp
44{
45 class Client ;
46 class ClientProtocol ;
47}
48
49//| \class GSmtp::Client
50/// A class which acts as an SMTP client, extracting messages from a
51/// message store and forwarding them to a remote SMTP server.
52///
54{
55public:
56 struct Config /// A structure containing GSmtp::Client configuration parameters.
57 {
58 std::string filter_address ;
59 unsigned int filter_timeout{0U} ;
60 bool bind_local_address{false} ;
61 GNet::Address local_address ;
62 ClientProtocol::Config client_protocol_config ;
63 unsigned int connection_timeout{0U} ;
64 unsigned int secure_connection_timeout{0U} ;
65 bool secure_tunnel{false} ;
66 std::string sasl_client_config ;
67
68 Config() ;
69 Config( const std::string & filter_address , unsigned int filter_timeout ,
70 bool bind_local_address , const GNet::Address & local_address ,
71 const ClientProtocol::Config & protocol_config ,
72 unsigned int connection_timeout , unsigned int secure_connection_timeout ,
73 bool secure_tunnel , const std::string & sasl_client_config ) ;
74
75 Config & set_filter_address( const std::string & ) ;
76 Config & set_filter_timeout( unsigned int ) ;
77 Config & set_bind_local_address( bool = true ) ;
78 Config & set_local_address( const GNet::Address & ) ;
79 Config & set_client_protocol_config( const ClientProtocol::Config & ) ;
80 Config & set_connection_timeout( unsigned int ) ;
81 Config & set_secure_connection_timeout( unsigned int ) ;
82 Config & set_secure_tunnel( bool = true ) ;
83 Config & set_sasl_client_config( const std::string & ) ;
84 } ;
85
87 const GAuth::SaslClientSecrets & client_secrets , const Config & config ) ;
88 ///< Constructor. Starts connecting immediately.
89 ///<
90 ///< Use sendMessagesFrom() once, or use sendMessage()
91 ///< repeatedly. Wait for a messageDoneSignal() between
92 ///< each sendMessage().
93
94 ~Client() override ;
95 ///< Destructor.
96
97 void sendMessagesFrom( MessageStore & store ) ;
98 ///< Sends all messages from the given message store once
99 ///< connected. This must be used immediately after
100 ///< construction with a non-empty message store.
101 ///<
102 ///< Once all messages have been sent the client will throw
103 ///< GNet::Done. See GNet::ClientPtr.
104 ///<
105 ///< The messageDoneSignal() is not used when sending
106 ///< messages using this method.
107
108 void sendMessage( std::unique_ptr<StoredMessage> message ) ;
109 ///< Starts sending the given message. Cannot be called
110 ///< if there is a message already in the pipeline.
111 ///<
112 ///< The messageDoneSignal() is used to indicate that the
113 ///< message filtering has finished or failed.
114 ///<
115 ///< The message is fail()ed if it cannot be sent. If this
116 ///< Client object is deleted before the message is sent
117 ///< the message is neither fail()ed or destroy()ed.
118 ///<
119 ///< Does nothing if there are no message recipients.
120
122 ///< Returns a signal that indicates that sendMessage()
123 ///< has completed or failed.
124
125private: // overrides
126 void onConnect() override ; // Override from GNet::SimpleClient.
127 bool onReceive( const char * , std::size_t , std::size_t , std::size_t , char ) override ; // Override from GNet::Client.
128 void onDelete( const std::string & ) override ; // Override from GNet::HeapClient.
129 void onSendComplete() override ; // Override from GNet::BufferedClient.
130 void onSecure( const std::string & , const std::string & , const std::string & ) override ; // Override from GNet::SocketProtocol.
131 bool protocolSend( const std::string & , std::size_t , bool ) override ; // Override from ClientProtocol::Sender.
132
133public:
134 Client( const Client & ) = delete ;
135 Client( Client && ) = delete ;
136 void operator=( const Client & ) = delete ;
137 void operator=( Client && ) = delete ;
138
139private:
140 std::shared_ptr<StoredMessage> message() ;
141 void protocolDone( int , const std::string & , const std::string & , const G::StringArray & ) ; // see ClientProtocol::doneSignal()
142 void filterStart() ;
143 void filterDone( int ) ;
144 bool sendNext() ;
145 void start() ;
146 void messageFail( int = 0 , const std::string & = std::string() ) ;
147 void messageDestroy() ;
148 void startSending() ;
149 void quitAndFinish() ;
150 static GNet::Client::Config netConfig( const Config & smtp_config ) ;
151
152private:
153 MessageStore * m_store ;
154 G::CallStack m_stack ;
155 std::unique_ptr<Filter> m_filter ;
156 std::shared_ptr<StoredMessage> m_message ;
157 std::shared_ptr<MessageStore::Iterator> m_iter ;
158 ClientProtocol m_protocol ;
159 bool m_secure_tunnel ;
160 G::Slot::Signal<const std::string&> m_message_done_signal ;
161 unsigned int m_message_count ;
162} ;
163
164inline GSmtp::Client::Config & GSmtp::Client::Config::set_filter_address( const std::string & s ) { filter_address = s ; return *this ; }
165inline GSmtp::Client::Config & GSmtp::Client::Config::set_filter_timeout( unsigned int t ) { filter_timeout = t ; return *this ; }
166inline GSmtp::Client::Config & GSmtp::Client::Config::set_bind_local_address( bool b ) { bind_local_address = b ; return *this ; }
167inline GSmtp::Client::Config & GSmtp::Client::Config::set_local_address( const GNet::Address & a ) { local_address = a ; return *this ; }
168inline GSmtp::Client::Config & GSmtp::Client::Config::set_client_protocol_config( const ClientProtocol::Config & c ) { client_protocol_config = c ; return *this ; }
169inline GSmtp::Client::Config & GSmtp::Client::Config::set_connection_timeout( unsigned int t ) { connection_timeout = t ; return *this ; }
170inline GSmtp::Client::Config & GSmtp::Client::Config::set_secure_connection_timeout( unsigned int t ) { secure_connection_timeout = t ; return *this ; }
171inline GSmtp::Client::Config & GSmtp::Client::Config::set_secure_tunnel( bool b ) { secure_tunnel = b ; return *this ; }
172inline GSmtp::Client::Config & GSmtp::Client::Config::set_sasl_client_config( const std::string & s ) { sasl_client_config = s ; return *this ; }
173
174#endif
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret.
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:53
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gclient.h:75
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A class that represents the remote target for out-going client connections.
Definition: glocation.h:71
An interface used by ClientProtocol to send protocol messages.
Implements the client-side SMTP protocol.
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
Definition: gsmtpclient.h:54
void sendMessage(std::unique_ptr< StoredMessage > message)
Starts sending the given message.
Definition: gsmtpclient.cpp:79
void sendMessagesFrom(MessageStore &store)
Sends all messages from the given message store once connected.
Definition: gsmtpclient.cpp:72
~Client() override
Destructor.
Definition: gsmtpclient.cpp:48
Client(GNet::ExceptionSink, FilterFactory &, const GNet::Location &remote, const GAuth::SaslClientSecrets &client_secrets, const Config &config)
Constructor.
Definition: gsmtpclient.cpp:34
G::Slot::Signal< const std::string & > & messageDoneSignal()
Returns a signal that indicates that sendMessage() has completed or failed.
Definition: gsmtpclient.cpp:67
A factory interface for GSmtp::Filter message processors.
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
A linked list of CallFrame pointers.
Definition: gcall.h:58
SMTP and message-store classes.
Definition: gadminserver.h:39
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31
A structure containing GNet::Client configuration parameters.
Definition: gclient.h:84
A structure containing GSmtp::ClientProtocol configuration parameters.
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:57