1 //
2 // Copyright (C) 2001-2013 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 // gprotocolmessagestore.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gprotocolmessagestore.h"
24 #include "gmessagestore.h"
25 #include "gmemory.h"
26 #include "gstr.h"
27 #include "gassert.h"
28 #include "glog.h"
29 
ProtocolMessageStore(MessageStore & store,std::auto_ptr<Processor> processor)30 GSmtp::ProtocolMessageStore::ProtocolMessageStore( MessageStore & store , std::auto_ptr<Processor> processor ) :
31 	m_store(store) ,
32 	m_processor(processor)
33 {
34 	m_processor->doneSignal().connect( G::slot(*this,&ProtocolMessageStore::preprocessorDone) ) ;
35 }
36 
~ProtocolMessageStore()37 GSmtp::ProtocolMessageStore::~ProtocolMessageStore()
38 {
39 	m_processor->doneSignal().disconnect() ;
40 }
41 
reset()42 void GSmtp::ProtocolMessageStore::reset()
43 {
44 	G_DEBUG( "GSmtp::ProtocolMessageStore::reset" ) ;
45 	clear() ;
46 }
47 
clear()48 void GSmtp::ProtocolMessageStore::clear()
49 {
50 	G_DEBUG( "GSmtp::ProtocolMessageStore::clear" ) ;
51 	m_msg <<= 0 ;
52 	m_from.erase() ;
53 	m_processor->abort() ;
54 }
55 
setFrom(const std::string & from)56 bool GSmtp::ProtocolMessageStore::setFrom( const std::string & from )
57 {
58 	G_DEBUG( "GSmtp::ProtocolMessageStore::setFrom: " << from ) ;
59 
60 	if( from.length() == 0U ) // => probably a failure notification message
61 		G_WARNING( "GSmtp::ProtocolMessageStore: empty MAIL-FROM return path" ) ;
62 
63 	G_ASSERT( m_msg.get() == NULL ) ;
64 	clear() ; // just in case
65 
66 	std::auto_ptr<NewMessage> new_message( m_store.newMessage(from) ) ;
67 	m_msg <<= new_message.release() ;
68 
69 	m_from = from ;
70 	return true ; // accept any name
71 }
72 
addTo(const std::string & to,VerifierStatus to_status)73 bool GSmtp::ProtocolMessageStore::addTo( const std::string & to , VerifierStatus to_status )
74 {
75 	G_DEBUG( "GSmtp::ProtocolMessageStore::addTo: " << to ) ;
76 
77 	G_ASSERT( m_msg.get() != NULL ) ;
78 	if( to.length() > 0U && m_msg.get() != NULL )
79 	{
80 		if( !to_status.is_valid )
81 		{
82 			G_WARNING( "GSmtp::ProtocolMessage: rejecting recipient \"" << to << "\": "
83 				<< to_status.reason << ": " << to_status.help );
84 			return false ;
85 		}
86 		else
87 		{
88 			m_msg->addTo( to_status.address , to_status.is_local ) ;
89 			return true ;
90 		}
91 	}
92 	else
93 	{
94 		return false ;
95 	}
96 }
97 
addReceived(const std::string & received_line)98 void GSmtp::ProtocolMessageStore::addReceived( const std::string & received_line )
99 {
100 	G_DEBUG( "GSmtp::ProtocolMessageStore::addReceived" ) ;
101 	addText( received_line ) ;
102 }
103 
addText(const std::string & line)104 bool GSmtp::ProtocolMessageStore::addText( const std::string & line )
105 {
106 	G_ASSERT( m_msg.get() != NULL ) ;
107 	if( m_msg.get() == NULL )
108 		return true ;
109 	return m_msg->addText( line ) ;
110 }
111 
from() const112 std::string GSmtp::ProtocolMessageStore::from() const
113 {
114 	return m_msg.get() ? m_from : std::string() ;
115 }
116 
process(const std::string & auth_id,const std::string & peer_socket_address,const std::string & peer_socket_name,const std::string & peer_certificate)117 void GSmtp::ProtocolMessageStore::process( const std::string & auth_id , const std::string & peer_socket_address ,
118 	const std::string & peer_socket_name , const std::string & peer_certificate )
119 {
120 	try
121 	{
122 		G_DEBUG( "ProtocolMessageStore::process: \"" << auth_id << "\", \"" << peer_socket_address << "\"" ) ;
123 		G_ASSERT( m_msg.get() != NULL ) ;
124 		if( m_msg.get() == NULL )
125 			throw G::Exception( "internal error" ) ; // never gets here
126 
127 		// write ".new" envelope
128 		std::string message_location = m_msg->prepare( auth_id , peer_socket_address , peer_socket_name , peer_certificate ) ;
129 
130 		// start preprocessing
131 		m_processor->start( message_location ) ;
132 	}
133 	catch( std::exception & e ) // catch preprocessing errors
134 	{
135 		G_DEBUG( "GSmtp::ProtocolMessage::process: exception: " << e.what() ) ;
136 		clear() ;
137 		m_done_signal.emit( false , 0UL , e.what() ) ;
138 	}
139 }
140 
preprocessorDone(bool ok)141 void GSmtp::ProtocolMessageStore::preprocessorDone( bool ok )
142 {
143 	try
144 	{
145 		G_DEBUG( "ProtocolMessageStore::preprocessorDone: " << (ok?1:0) ) ;
146 		G_ASSERT( m_msg.get() != NULL ) ;
147 		if( m_msg.get() == NULL )
148 			throw G::Exception( "internal error" ) ; // never gets here
149 
150 		unsigned long id = 0UL ;
151 		std::string reason ;
152 		if( ok )
153 		{
154 			m_msg->commit() ;
155 			id = m_msg->id() ;
156 		}
157 		else if( m_processor->cancelled() )
158 		{
159 			try { m_msg->commit() ; } catch( std::exception & ) {}
160 		}
161 		else
162 		{
163 			reason = m_processor->text() ;
164 			reason = reason.empty() ? "error" : reason ;
165 			G_LOG_S( "GSmtp::ProtocolMessageStore::preprocessorDone: error storing message: " << reason ) ;
166 		}
167 		if( m_processor->repoll() )
168 		{
169 			m_store.repoll() ;
170 		}
171 		clear() ;
172 		G_DEBUG( "ProtocolMessageStore::preprocessorDone: emiting done signal" ) ;
173 		m_done_signal.emit( reason.empty() , id , reason ) ;
174 	}
175 	catch( std::exception & e ) // catch preprocessing errors
176 	{
177 		G_DEBUG( "GSmtp::ProtocolMessage::preprocessorDone: exception: " << e.what() ) ;
178 		clear() ;
179 		m_done_signal.emit( false , 0UL , e.what() ) ;
180 	}
181 }
182 
doneSignal()183 G::Signal3<bool,unsigned long,std::string> & GSmtp::ProtocolMessageStore::doneSignal()
184 {
185 	return m_done_signal ;
186 }
187 
188 /// \file gprotocolmessagestore.cpp
189