35 GNet::
Client(remote,config.connection_timeout,0U,
36 config.secure_connection_timeout,
GNet::LineBufferConfig::smtp(),config.bind_local_address,
37 config.local_address) ,
39 m_filter(
FilterFactory::newFilter(*this,false,config.filter_address,config.filter_timeout)) ,
40 m_protocol(*this,*this,secrets,config.client_protocol_config,config.secure_tunnel) ,
41 m_secure_tunnel(config.secure_tunnel) ,
46 m_filter->doneSignal().connect(
G::Slot::slot(*
this,&Client::filterDone) ) ;
51 m_protocol.doneSignal().disconnect() ;
52 m_protocol.filterSignal().disconnect() ;
53 m_filter->doneSignal().disconnect() ;
58 return m_message_done_signal ;
63 G_ASSERT( !connected() ) ;
69 G_ASSERT( m_message.get() == nullptr ) ;
70 m_message.reset( message.release() ) ;
73 start( *m_message.get() ) ;
77 bool GSmtp::Client::protocolSend(
const std::string & line ,
size_t offset ,
bool go_secure )
79 bool rc = send( line , offset ) ;
85 void GSmtp::Client::filterStart()
87 G_ASSERT( m_message.get() != nullptr ) ;
90 if( m_filter->id() !=
"none" )
92 G_LOG(
"GSmtp::Client::filterStart: client filter: [" << m_filter->id() <<
"]" ) ;
94 m_filter->start( m_message->location() ) ;
98 void GSmtp::Client::filterDone(
int filter_result )
100 G_ASSERT( m_message.get() != nullptr ) ;
102 const bool ok = filter_result == 0 ;
103 const bool abandon = filter_result == 1 ;
104 const bool stop_scanning = m_filter->special() ;
105 G_ASSERT( m_filter->reason().empty() == (ok || abandon) ) ;
110 m_message->sync( !m_filter->simple() ) ;
115 G_DEBUG(
"GSmtp::Client::filterDone: making this the last message" ) ;
119 if( m_filter->id() !=
"none" )
121 G_LOG(
"GSmtp::Client::filterDone: client filter done: " << m_filter->str(
false) ) ;
127 m_protocol.filterDone(
true , std::string() , std::string() ) ;
131 m_protocol.filterDone(
false , std::string() , std::string() ) ;
135 m_protocol.filterDone(
false , m_filter->response() , m_filter->reason() ) ;
141 if( m_secure_tunnel )
147 m_protocol.secure() ;
151 void GSmtp::Client::logCertificate(
const std::string & certificate )
153 if( !certificate.empty() )
155 static std::string previous ;
156 if( certificate != previous )
158 previous = certificate ;
160 lines.reserve( 30U ) ;
162 for( G::StringArray::iterator p = lines.begin() ; p != lines.end() ; ++p )
165 G_LOG(
"GSmtp::Client: certificate: " << (*p) ) ;
173 if( m_secure_tunnel )
183 void GSmtp::Client::doOnConnect()
185 G_LOG_S(
"GSmtp::Client::doOnConnect: smtp connection to " << peerAddress().second.displayString() ) ;
186 if( m_store !=
nullptr )
189 m_iter = m_store->iterator(
true ) ;
192 bool started = sendNext() ;
195 G_DEBUG(
"GSmtp::Client::doOnConnect: deleting" ) ;
196 doDelete( std::string() ) ;
201 G_ASSERT( m_message.get() != nullptr ) ;
202 start( *m_message.get() ) ;
206 bool GSmtp::Client::sendNext()
212 unique_ptr<StoredMessage> message( m_iter.next() ) ;
213 if( message.get() == nullptr )
215 if( m_message_count != 0U )
216 G_LOG(
"GSmtp::Client: no more messages to send" ) ;
217 m_message_count = 0U ;
220 m_message.reset( message.release() ) ;
223 start( *m_message.get() ) ;
227 void GSmtp::Client::start( StoredMessage & message )
230 eventSignal().emit(
"sending" , message.name() ) ;
232 m_protocol.start( message.from() , message.to() ,
233 message.eightBit() , message.fromAuthOut() ,
234 unique_ptr<std::istream>(message.extractContentStream()) ) ;
237 void GSmtp::Client::protocolDone(
int response_code , std::string response , std::string reason )
239 G_DEBUG(
"GSmtp::Client::protocolDone: \"" << response <<
"\"" ) ;
240 if( ! response.empty() )
241 response =
"smtp client failure: " + response ;
243 if( response_code == -1 )
248 else if( response_code == -2 )
252 messageFail( 550 , reason.empty() ? response : reason ) ;
254 else if( response.empty() )
263 messageFail( response_code , reason.empty() ? response : reason ) ;
266 if( m_store !=
nullptr )
270 m_protocol.finish() ;
271 socket().shutdown() ;
277 messageDoneSignal().emit( response ) ;
281 void GSmtp::Client::messageDestroy()
283 if( m_message.get() != nullptr )
285 m_message->destroy() ;
290 void GSmtp::Client::messageFail(
int response_code ,
const std::string & reason )
292 if( m_message.get() != nullptr )
294 m_message->fail( reason , response_code ) ;
301 std::string line( line_data , line_size ) ;
303 bool done = m_protocol.apply( line ) ;
305 doDelete( std::string() ) ;
311 G_DEBUG(
"GSmtp::Client::onDelete: error [" << error <<
"]" ) ;
312 if( ! error.empty() )
314 G_LOG(
"GSmtp::Client: smtp client error: " << error ) ;
315 messageFail( 0 , error ) ;
322 m_protocol.sendDone() ;
327 GSmtp::Client::Config::Config( std::string filter_address_ ,
unsigned int filter_timeout_ ,
328 bool bind_local_address ,
const GNet::Address & local_address ,
330 unsigned int secure_connection_timeout_ ,
bool secure_tunnel_ ) :
331 filter_address(filter_address_) ,
332 filter_timeout(filter_timeout_) ,
333 bind_local_address(bind_local_address) ,
334 local_address(local_address) ,
335 client_protocol_config(protocol_config) ,
336 connection_timeout(connection_timeout_) ,
337 secure_connection_timeout(secure_connection_timeout_) ,
338 secure_tunnel(secure_tunnel_)
virtual void onDelete(const std::string &) override
Override from GNet::HeapClient.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
virtual void onSecure(const std::string &) override
Override from GNet::SocketProtocol.
G::Slot::Signal0 & filterSignal()
Returns a signal that is raised when the protocol needs to do message filtering.
void sendMessage(unique_ptr< StoredMessage > message)
Starts sending the given message.
The GNet::Address class encapsulates a TCP/UDP transport address.
G::Slot::Signal1< std::string > & messageDoneSignal()
Returns a signal that indicates that sendMessage() has completed or failed.
std::vector< std::string > StringArray
A std::vector of std::strings.
G::Slot::Signal3< int, std::string, std::string > & doneSignal()
Returns a signal that is raised once the protocol has finished with a given message.
A structure containing GSmtp::ClientProtocol configuration parameters.
virtual void onConnect() override
Override from GNet::SimpleClient.
virtual ~Client()
Destructor.
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.
Client(const GNet::Location &remote, const GAuth::Secrets &secrets, Config config)
Constructor.
A factory for message processors.
A class which allows SMTP messages to be stored and retrieved.
A simple interface for a store of secrets used in authentication.
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
virtual bool onReceive(const char *, size_t, size_t) override
Override from GNet::Client.
virtual void onSendComplete() override
Override from GNet::BufferedClient.
A structure containing GSmtp::Client configuration parameters.
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
void sendMessagesFrom(MessageStore &store)
Sends all messages from the given message store once connected.
static void splitIntoFields(const std::string &in, StringArray &out, const std::string &separators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.