39 const std::string & from_auth_in ,
const std::string & from_auth_out ,
43 m_from_auth_in(from_auth_in) ,
44 m_from_auth_out(from_auth_out) ,
58 G_LOG(
"GSmtp::NewMessage: content file: " << m_content_path ) ;
59 unique_ptr<std::ostream> content_stream = m_store.
stream( m_content_path ) ;
60 m_content.reset( content_stream.release() ) ;
67 G_DEBUG(
"GSmtp::NewFile::dtor: " << m_content_path ) ;
76 void GSmtp::NewFile::cleanup()
87 const std::string & peer_certificate )
95 m_envelope_path_0 = m_store.envelopeWorkingPath( m_seq ) ;
96 m_envelope_path_1 = m_store.envelopePath( m_seq ) ;
97 if( ! saveEnvelope( session_auth_id , peer_socket_address , peer_certificate ) )
98 throw FileError(
"cannot write envelope file " + m_envelope_path_0.str() ) ;
102 if( m_to_local.size() != 0U )
104 deliver( m_to_local , m_content_path , m_envelope_path_0 , m_envelope_path_1 ) ;
107 return m_content_path.str() ;
113 bool ok = commitEnvelope() ;
115 throw FileError(
"cannot rename envelope file to " + m_envelope_path_1.str() ) ;
121 m_to_local.push_back( to ) ;
123 m_to_remote.push_back( to ) ;
128 m_size +=
static_cast<unsigned long>( line_size + 2U ) ;
141 m_eight_bit = m_eight_bit || isEightBit(line_data,line_size) ;
143 std::ostream & stream = *m_content.get() ;
144 stream.write( line_data , line_size ) ;
145 stream.write(
"\r\n" , 2U ) ;
147 return m_max_size == 0UL || m_size < m_max_size ;
150 void GSmtp::NewFile::flushContent()
152 G_ASSERT( m_content.get() != nullptr ) ;
154 if( ! m_content->good() )
155 throw FileError(
"cannot write content file " + m_content_path.str() ) ;
159 void GSmtp::NewFile::discardContent()
164 void GSmtp::NewFile::deleteContent()
166 FileWriter claim_writer ;
170 void GSmtp::NewFile::deleteEnvelope()
172 if( ! m_envelope_path_0.str().empty() )
174 FileWriter claim_writer ;
181 struct EightBit : std::unary_function<char,bool>
183 bool operator()(
char c ) {
return !! (
static_cast<unsigned char>(c) & 0x80U ) ; }
187 bool GSmtp::NewFile::isEightBit(
const char * line_data ,
size_t line_size )
189 const char * end = line_data + line_size ;
190 return std::find_if( line_data , end , EightBit() ) != end ;
193 bool GSmtp::NewFile::saveEnvelope(
const std::string & session_auth_id ,
const std::string & peer_socket_address ,
194 const std::string & peer_certificate )
const
196 unique_ptr<std::ostream> envelope_stream = m_store.stream( m_envelope_path_0 ) ;
197 writeEnvelope( *(envelope_stream.get()) , m_envelope_path_0.str() ,
198 session_auth_id , peer_socket_address , peer_certificate ) ;
199 bool ok = envelope_stream->good() ;
203 bool GSmtp::NewFile::commitEnvelope()
205 FileWriter claim_writer ;
211 const G::Path & content_path ,
const G::Path & envelope_path_now ,
212 const G::Path & envelope_path_later )
217 G_LOG_S(
"GSmtp::NewMessage: copying message for local recipient(s): "
218 << content_path.
basename() <<
".local" ) ;
220 FileWriter claim_writer ;
225 void GSmtp::NewFile::writeEnvelope( std::ostream & stream ,
const std::string & where ,
226 const std::string & session_auth_id ,
const std::string & peer_socket_address ,
227 const std::string & peer_certificate_in )
const
229 G_LOG(
"GSmtp::NewMessage: envelope file: " << where ) ;
231 std::string peer_certificate = peer_certificate_in ;
236 const std::string x( m_store.x() ) ;
238 stream << x <<
"Format: " << m_store.format() << crlf() ;
239 stream << x <<
"Content: " << (m_eight_bit?
"8":
"7") <<
"bit" << crlf() ;
240 stream << x <<
"From: " << m_from << crlf() ;
241 stream << x <<
"ToCount: " << (m_to_local.size()+m_to_remote.size()) << crlf() ;
243 G::StringArray::const_iterator to_p = m_to_local.begin() ;
244 for( ; to_p != m_to_local.end() ; ++to_p )
245 stream << x <<
"To-Local: " << *to_p << crlf() ;
248 G::StringArray::const_iterator to_p = m_to_remote.begin() ;
249 for( ; to_p != m_to_remote.end() ; ++to_p )
250 stream << x <<
"To-Remote: " << *to_p << crlf() ;
252 stream << x <<
"Authentication: " <<
G::Xtext::encode(session_auth_id) << crlf() ;
253 stream << x <<
"Client: " << peer_socket_address << crlf() ;
254 stream << x <<
"ClientCertificate: " << peer_certificate << crlf() ;
255 stream << x <<
"MailFromAuthIn: " << xnormalise(m_from_auth_in) << crlf() ;
256 stream << x <<
"MailFromAuthOut: " << xnormalise(m_from_auth_out) << crlf() ;
257 stream << x <<
"End: 1" << crlf() ;
261 std::string GSmtp::NewFile::xnormalise(
const std::string & s )
266 const char * GSmtp::NewFile::crlf()
278 return m_content_path ;
std::string str() const
Returns the path string.
G::Path contentPath(unsigned long seq) const
Returns the path for a content file.
virtual void commit(bool strict) override
Override from GSmtp::NewMessage.
G::Path contentPath() const
Returns the path of the content file.
static bool copy(const Path &from, const Path &to, const NoThrow &)
Copies a file. Returns false on error.
static std::string decode(const std::string &)
Decodes the given string.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
static std::string encode(const std::string &)
Encodes the given string.
std::vector< std::string > StringArray
A std::vector of std::strings.
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
virtual ~NewFile()
Destructor.
NewFile(FileStore &store, const std::string &from, const std::string &from_auth_in, const std::string &from_auth_out=std::string(), size_t max_size=0UL)
Constructor. The FileStore reference is kept.
unique_ptr< std::ostream > stream(const G::Path &path)
Returns a stream to the given content.
virtual unsigned long id() const override
Override from GSmtp::NewMessage.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
virtual void addTo(const std::string &to, bool local) override
Override from GSmtp::NewMessage.
virtual bool addText(const char *, size_t) override
Override from GSmtp::NewMessage.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
An overload discriminator class for File methods.
A concrete implementation of the MessageStore interface dealing in paired flat files.
virtual std::string prepare(const std::string &auth_id, const std::string &peer_socket_address, const std::string &peer_certificate) override
Override from GSmtp::NewMessage.
unsigned long newSeq()
Hands out a new non-zero sequence number.
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.