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
44namespace 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///
55{
56public:
58
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
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
102 ///< Returns a signal that indicates that sendMessage()
103 ///< has completed or failed.
104
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
117public:
118 Forward( const Forward & ) = delete ;
119 Forward( Forward && ) = delete ;
120 Forward & operator=( const Forward & ) = delete ;
121 Forward & operator=( Forward && ) = delete ;
122
123private:
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
141private:
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 ;
164} ;
165
166#endif
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret.
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
A class that represents the remote target for out-going client connections.
Definition: glocation.h:70
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:141
A factory interface for making GSmtp::Filter message processors.
A class for forwarding messages from a message store that manages a GSmtp::Client instance,...
Definition: gsmtpforward.h:55
G::Slot::Signal< const std::string &, const std::string &, const std::string & > & eventSignal() noexcept
See GNet::Client::eventSignal()
Forward(GNet::EventState, GStore::MessageStore &store, FilterFactoryBase &, const GNet::Location &forward_to_default, const GAuth::SaslClientSecrets &, const Config &config)
Constructor.
std::string peerAddressString() const
Returns the Client's peerAddressString() if currently connected.
void doOnDelete(const std::string &reason, bool done)
Used by owning ClientPtr when handling an exception.
bool finished() const
Returns true after quitAndFinish().
virtual ~Forward()
Destructor.
G::Slot::Signal< const Client::MessageDoneInfo & > & messageDoneSignal() noexcept
Returns a signal that indicates that sendMessage() has completed or failed.
void quitAndFinish()
Finishes a sendMessage() sequence.
void sendMessage(std::unique_ptr< GStore::StoredMessage > message)
Starts sending the given message.
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
An abstract interface for messages which have come from the store.
SMTP classes.
Definition: gadminserver.h:42
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
A structure containing GSmtp::Client configuration parameters.
Definition: gsmtpclient.h:67
Signal parameters for GNet::Client::messageDoneSignal()
Definition: gsmtpclient.h:87
A slot holder, with connect() and emit() methods.
Definition: gslot.h:184