E-MailRelay

gsmtpforward.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 gsmtpforward.h
19///
20
21#ifndef G_SMTP_FORWARD_H
22#define G_SMTP_FORWARD_H
23
24#include "gdef.h"
25#include "glocation.h"
26#include "gsaslclientsecrets.h"
27#include "glinebuffer.h"
28#include "gclient.h"
29#include "gclientptr.h"
30#include "gsmtpclientprotocol.h"
31#include "gmessagestore.h"
32#include "gstoredmessage.h"
33#include "gfilterfactorybase.h"
34#include "gsmtpclient.h"
35#include "gfilter.h"
36#include "gsocket.h"
37#include "gslot.h"
38#include "gtimer.h"
39#include "gstringarray.h"
40#include "gexception.h"
41#include <memory>
42#include <iostream>
43
44 namespace GSmtp
45{
46 class Forward ;
47}
48
49//| \class GSmtp::Forward
50/// A class for forwarding messages from a message store that manages
51/// a GSmtp::Client instance, connecting and disconnecting as necessary
52/// to do routing and re-authentication.
53///
54 class GSmtp::Forward
55{
56 public:
57 using Config = GSmtp::Client::Config ;
58
59 Forward( GNet::EventState , GStore::MessageStore & store ,
60 FilterFactoryBase & , const GNet::Location & forward_to_default ,
61 const GAuth::SaslClientSecrets & , const Config & config ) ;
62 ///< Constructor. Starts sending the first message from the
63 ///< message store.
64 ///<
65 ///< Once all messages have been sent the client will
66 ///< throw GNet::Done. See GNet::ClientPtr.
67 ///<
68 ///< Do not use sendMessage(). The messageDoneSignal()
69 ///< is not emitted.
70
71 Forward( GNet::EventState ,
72 FilterFactoryBase & , const GNet::Location & forward_to_default ,
73 const GAuth::SaslClientSecrets & , const Config & config ) ;
74 ///< Constructor. Use sendMessage() immediately after
75 ///< construction.
76 ///<
77 ///< A messageDoneSignal() is emitted when the message
78 ///< has been sent, allowing the next sendMessage().
79 ///<
80 ///< Use quitAndFinish() at the end.
81
82 virtual ~Forward() ;
83 ///< Destructor.
84
85 void sendMessage( std::unique_ptr<GStore::StoredMessage> message ) ;
86 ///< Starts sending the given message. Cannot be called
87 ///< if there is a message already in the pipeline.
88 ///<
89 ///< The messageDoneSignal() is used to indicate that the
90 ///< message filtering has finished or failed.
91 ///<
92 ///< The message is fail()ed if it cannot be sent. If this
93 ///< object is deleted before the message is sent the message
94 ///< is neither fail()ed or destroy()ed.
95 ///<
96 ///< Does nothing if there are no message recipients.
97
98 void quitAndFinish() ;
99 ///< Finishes a sendMessage() sequence.
100
101 G::Slot::Signal<const Client::MessageDoneInfo&> & messageDoneSignal() noexcept ;
102 ///< Returns a signal that indicates that sendMessage()
103 ///< has completed or failed.
104
105 G::Slot::Signal<const std::string&,const std::string&,const std::string&> & eventSignal() noexcept ;
106 ///< See GNet::Client::eventSignal()
107
108 void doOnDelete( const std::string & reason , bool done ) ;
109 ///< Used by owning ClientPtr when handling an exception.
110
111 bool finished() const ;
112 ///< Returns true after quitAndFinish().
113
114 std::string peerAddressString() const ;
115 ///< Returns the Client's peerAddressString() if currently connected.
116
117 public:
118 Forward( const Forward & ) = delete ;
119 Forward( Forward && ) = delete ;
120 Forward & operator=( const Forward & ) = delete ;
121 Forward & operator=( Forward && ) = delete ;
122
123 private:
124 void onErrorTimeout() ;
125 void onContinueTimeout() ;
126 bool sendNext() ;
127 void start( std::unique_ptr<GStore::StoredMessage> ) ;
128 void onMessageDoneSignal( const Client::MessageDoneInfo & ) ;
129 void onEventSignal( const std::string & , const std::string & , const std::string & ) ;
130 void onDelete( const std::string & reason ) ;
131 void onDeleteSignal( const std::string & ) ;
132 void onDeletedSignal( const std::string & ) ;
133 bool updateClient( const GStore::StoredMessage & ) ;
134 void newClient( const GStore::StoredMessage & ) ;
135 void routingFilterDone( int ) ;
136 bool unconnectable( const std::string & ) const ;
137 static void insert( G::StringArray & , const std::string & ) ;
138 static bool contains( const G::StringArray & , const std::string & ) ;
139 static std::string messageInfo( const GStore::StoredMessage & ) ;
140
141 private:
142 GNet::EventState m_es ;
143 GStore::MessageStore * m_store ;
144 FilterFactoryBase & m_ff ;
145 GNet::Location m_forward_to_default ;
146 GNet::Location m_forward_to_location ;
147 std::string m_forward_to_address ;
148 G::StringArray m_unconnectable ;
149 GNet::ClientPtr<GSmtp::Client> m_client_ptr ;
150 const GAuth::SaslClientSecrets & m_secrets ;
151 Config m_config ;
152 GNet::Timer<Forward> m_error_timer ;
153 GNet::Timer<Forward> m_continue_timer ;
154 std::string m_error ;
155 std::shared_ptr<GStore::MessageStore::Iterator> m_iter ;
156 std::unique_ptr<GStore::StoredMessage> m_message ;
157 unsigned int m_message_count ;
158 std::unique_ptr<Filter> m_routing_filter ;
159 std::string m_selector ;
160 bool m_has_connected ;
161 bool m_finished ;
162 G::Slot::Signal<const Client::MessageDoneInfo&> m_message_done_signal ;
163 G::Slot::Signal<const std::string&,const std::string&,const std::string&> m_event_signal ;
164} ;
165
166#endif
GAuth::SaslClientSecrets
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret.
Definition: gsaslclientsecrets.h:39
GNet::ClientPtr< GSmtp::Client >
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::Location
A class that represents the remote target for out-going client connections.
Definition: glocation.h:70
GNet::Timer
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:141
GSmtp::FilterFactoryBase
A factory interface for making GSmtp::Filter message processors.
Definition: gfilterfactorybase.h:43
GSmtp::Forward
A class for forwarding messages from a message store that manages a GSmtp::Client instance,...
Definition: gsmtpforward.h:55
GSmtp::Forward::eventSignal
G::Slot::Signal< const std::string &, const std::string &, const std::string & > & eventSignal() noexcept
See GNet::Client::eventSignal()
Definition: gsmtpforward.cpp:370
GSmtp::Forward::Forward
Forward(GNet::EventState, GStore::MessageStore &store, FilterFactoryBase &, const GNet::Location &forward_to_default, const GAuth::SaslClientSecrets &, const Config &config)
Constructor.
Definition: gsmtpforward.cpp:29
GSmtp::Forward::peerAddressString
std::string peerAddressString() const
Returns the Client's peerAddressString() if currently connected.
Definition: gsmtpforward.cpp:324
GSmtp::Forward::doOnDelete
void doOnDelete(const std::string &reason, bool done)
Used by owning ClientPtr when handling an exception.
Definition: gsmtpforward.cpp:307
GSmtp::Forward::finished
bool finished() const
Returns true after quitAndFinish().
Definition: gsmtpforward.cpp:330
GSmtp::Forward::~Forward
virtual ~Forward()
Destructor.
Definition: gsmtpforward.cpp:60
GSmtp::Forward::messageDoneSignal
G::Slot::Signal< const Client::MessageDoneInfo & > & messageDoneSignal() noexcept
Returns a signal that indicates that sendMessage() has completed or failed.
Definition: gsmtpforward.cpp:365
GSmtp::Forward::quitAndFinish
void quitAndFinish()
Finishes a sendMessage() sequence.
Definition: gsmtpforward.cpp:239
GSmtp::Forward::sendMessage
void sendMessage(std::unique_ptr< GStore::StoredMessage > message)
Starts sending the given message.
Definition: gsmtpforward.cpp:114
GStore::MessageStore
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
GStore::StoredMessage
An abstract interface for messages which have come from the store.
Definition: gstoredmessage.h:43
gclient.h
gclientptr.h
gexception.h
gfilter.h
gfilterfactorybase.h
glinebuffer.h
glocation.h
gmessagestore.h
gsaslclientsecrets.h
gslot.h
gsmtpclient.h
gsmtpclientprotocol.h
gsocket.h
gstoredmessage.h
gstringarray.h
gtimer.h
GSmtp
SMTP classes.
Definition: gadminserver.h:42
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
GSmtp::Client::Config
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:67
GSmtp::Client::MessageDoneInfo
Signal parameters for GNet::Client::messageDoneSignal()
Definition: gsmtpclient.h:87
G::Slot::Signal
A slot holder, with connect() and emit() methods.
Definition: gslot.h:184