34 m_envelope_path(envelope_path) ,
40 m_name = m_envelope_path.
basename() ;
41 size_t pos = m_name.rfind(
".envelope") ;
42 if( pos != std::string::npos ) m_name.erase( pos ) ;
43 G_DEBUG(
"GSmtp::StoredFile::ctor: name=[" << m_name <<
"]" ) ;
68 return contentPath().str() ;
78 readEnvelopeCore(
true ) ;
85 readEnvelopeCore( check_recipients ) ;
88 catch( std::exception & e )
95 void GSmtp::StoredFile::readEnvelopeCore(
bool check_recipients )
98 std::ifstream stream( m_envelope_path.str().c_str() , std::ios_base::binary | std::ios_base::in ) ;
100 throw ReadError( m_envelope_path.str() ) ;
103 readFormat( stream ) ;
106 readToList( stream ) ;
107 readAuthentication( stream ) ;
108 readClientSocketAddress( stream ) ;
111 readClientCertificate( stream ) ;
112 readFromAuthIn( stream ) ;
113 readFromAuthOut( stream ) ;
117 readClientSocketName( stream ) ;
118 readClientCertificate( stream ) ;
122 if( check_recipients && m_to_remote.size() == 0U )
123 throw FormatError(
"no recipients" ) ;
125 if( ! stream.good() )
126 throw ReadError( m_envelope_path.str() ) ;
128 readReasons( stream ) ;
131 void GSmtp::StoredFile::readFormat( std::istream & stream )
133 m_format = readValue( stream ,
"Format" ) ;
135 throw FormatError(
"unknown format id" , m_format ) ;
138 void GSmtp::StoredFile::readFlag( std::istream & stream )
140 m_eight_bit = readValue(stream,
"Content") ==
"8bit" ;
143 void GSmtp::StoredFile::readFrom( std::istream & stream )
145 m_from = readValue( stream ,
"From" ) ;
146 G_DEBUG(
"GSmtp::StoredFile::readFrom: from \"" << m_from <<
"\"" ) ;
149 void GSmtp::StoredFile::readFromAuthIn( std::istream & stream )
151 m_from_auth_in = readValue( stream ,
"MailFromAuthIn" ) ;
152 if( !m_from_auth_in.empty() && m_from_auth_in !=
"+" && !
G::Xtext::valid(m_from_auth_in) )
153 throw FormatError(
"invalid mail-from-auth-in encoding" ) ;
156 void GSmtp::StoredFile::readFromAuthOut( std::istream & stream )
158 m_from_auth_out = readValue( stream ,
"MailFromAuthOut" ) ;
159 if( !m_from_auth_out.empty() && m_from_auth_out !=
"+" && !
G::Xtext::valid(m_from_auth_out) )
160 throw FormatError(
"invalid mail-from-auth-out encoding" ) ;
163 void GSmtp::StoredFile::readToList( std::istream & stream )
166 m_to_remote.clear() ;
168 unsigned int to_count =
G::Str::toUInt( readValue(stream,
"ToCount") ) ;
170 for(
unsigned int i = 0U ; i < to_count ; i++ )
172 std::string to_line = readLine( stream ) ;
173 bool is_local = to_line.find(
FileStore::x()+
"To-Local: ") == 0U ;
174 bool is_remote = to_line.find(
FileStore::x()+
"To-Remote: ") == 0U ;
175 if( ! is_local && ! is_remote )
176 throw FormatError(
"bad 'to' line" ) ;
179 m_to_local.push_back( value(to_line) ) ;
181 m_to_remote.push_back( value(to_line) ) ;
185 void GSmtp::StoredFile::readAuthentication( std::istream & stream )
190 void GSmtp::StoredFile::readClientSocketAddress( std::istream & stream )
192 m_client_socket_address = readValue( stream ,
"Client" ) ;
195 void GSmtp::StoredFile::readClientSocketName( std::istream & stream )
200 void GSmtp::StoredFile::readClientCertificate( std::istream & stream )
202 m_client_certificate = readValue( stream ,
"ClientCertificate" ) ;
205 void GSmtp::StoredFile::readEnd( std::istream & stream )
207 std::string end = readLine( stream ) ;
209 throw FormatError(
"no end line" ) ;
212 void GSmtp::StoredFile::readReasons( std::istream & stream )
215 while( stream.good() )
217 std::string reason = readLine( stream ) ;
228 G::Path content_path = contentPath() ;
229 G_DEBUG(
"GSmtp::FileStore::openContent: \"" << content_path <<
"\"" ) ;
230 unique_ptr<std::istream> stream(
new std::ifstream(
231 content_path.
str().c_str() , std::ios_base::in | std::ios_base::binary ) ) ;
232 if( !stream->good() )
234 reason =
"cannot open content file" ;
238 G_DEBUG(
"GSmtp::MessageStore: processing envelope \"" << m_envelope_path.basename() <<
"\"" ) ;
239 G_DEBUG(
"GSmtp::MessageStore: processing content \"" << content_path.
basename() <<
"\"" ) ;
241 m_content.reset( stream.release() ) ;
244 catch( std::exception & e )
246 G_DEBUG(
"GSmtp::FileStore: exception: " << e.what() ) ;
252 std::string GSmtp::StoredFile::readLine( std::istream & stream )
257 if( !line.empty() && line.at(line.size()-1U) ==
'\r' )
264 std::string GSmtp::StoredFile::readValue( std::istream & stream ,
const std::string & expected_key )
266 std::string line = readLine( stream ) ;
268 std::string prefix =
FileStore::x() + expected_key +
":" ;
270 return std::string() ;
272 prefix.append( 1U ,
' ' ) ;
273 size_t pos = line.find( prefix ) ;
275 throw FormatError(
"expected \"" +
FileStore::x() + expected_key +
":\"" ) ;
280 char c = stream.peek() ;
281 if( c ==
' ' || c ==
'\t' )
282 line.append( readLine(stream) ) ;
287 return value( line ) ;
290 std::string GSmtp::StoredFile::value(
const std::string & line )
const
295 const std::string & GSmtp::StoredFile::eol()
const
297 static const std::string crlf(
"\r\n" ) ;
298 static const std::string lf(
"\n" ) ;
299 return m_crlf ? crlf : lf ;
304 const G::Path src = m_envelope_path ;
313 G_LOG(
"GSmtp::StoredMessage: locking file \"" << src.
basename() <<
"\"" ) ;
314 m_envelope_path = dst ;
315 m_old_envelope_path = src ;
322 void GSmtp::StoredFile::unlock()
330 G_LOG(
"GSmtp::StoredMessage: unlocking file \"" << m_envelope_path.basename() <<
"\"" ) ;
331 m_envelope_path = m_old_envelope_path ;
341 addReason( m_envelope_path , reason , reason_code ) ;
343 G::Path bad_path = badPath( m_envelope_path ) ;
344 G_LOG_S(
"GSmtp::StoredMessage: failing file: "
345 <<
"\"" << m_envelope_path.basename() <<
"\" -> "
346 <<
"\"" << bad_path.
basename() <<
"\"" ) ;
355 G_DEBUG(
"GSmtp::StoredMessage: unfailing file: " << m_envelope_path ) ;
356 if( m_envelope_path.extension() ==
"bad" )
366 G_LOG(
"GSmtp::StoredMessage: unfailed file: "
367 <<
"\"" << m_envelope_path.basename() <<
"\" -> "
368 <<
"\"" << dst.
basename() <<
"\"" ) ;
369 m_envelope_path = dst ;
373 G_WARNING(
"GSmtp::StoredMessage: failed to unfail file: \"" << m_envelope_path <<
"\"" ) ;
378 void GSmtp::StoredFile::addReason(
const G::Path & path ,
const std::string & reason ,
int reason_code )
const
381 std::ofstream file( path.
str().c_str() ,
382 std::ios_base::binary | std::ios_base::app ) ;
383 file <<
FileStore::x() <<
"Reason: " << reason << eol() ;
384 file <<
FileStore::x() <<
"ReasonCode:" ;
if( reason_code ) file <<
" " << reason_code ; file << eol() ;
394 G_LOG(
"GSmtp::StoredMessage: deleting file: \"" << m_envelope_path.basename() <<
"\"" ) ;
400 G::Path content_path = contentPath() ;
401 G_LOG(
"GSmtp::StoredMessage: deleting file: \"" << content_path.
basename() <<
"\"" ) ;
421 G_ASSERT( m_content.get() != nullptr ) ;
422 return unique_ptr<std::istream>( m_content.release() ) ;
425 G::Path GSmtp::StoredFile::contentPath()
const
427 std::string filename = m_envelope_path.
str() ;
429 size_t pos = filename.rfind(
".envelope" ) ;
430 if( pos == std::string::npos )
431 throw FilenameError( filename ) ;
438 return m_to_remote.size() ;
448 return m_authentication ;
453 return m_from_auth_in ;
458 return m_from_auth_out ;
bool readEnvelope(std::string &reason, bool check_for_no_remote_recipients)
Reads the envelope.
std::string str() const
Returns the path string.
virtual void unfail() override
Override from GSmtp::StoredMessage.
static std::string format(int generation=0)
Returns an identifier for the storage format implemented by this class, or some older generation of i...
static std::string x()
Returns the prefix for envelope header lines.
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Path withoutExtension() const
Returns a path without the basename extension, if any.
static std::string decode(const std::string &)
Decodes the given string.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
virtual const std::string & from() const override
Override from GSmtp::StoredMessage.
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
std::vector< std::string > StringArray
A std::vector of std::strings.
virtual unique_ptr< std::istream > extractContentStream() override
Override from GSmtp::StoredMessage.
virtual std::string fromAuthIn() const override
Override from GSmtp::StoredMessage.
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim write permissions.
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
virtual void destroy() override
Override from GSmtp::StoredMessage.
virtual void fail(const std::string &reason, int reason_code) override
Override from GSmtp::StoredMessage.
virtual std::string location() const override
Override from GSmtp::StoredMessage.
virtual std::string authentication() const override
Override from GSmtp::StoredMessage.
virtual size_t errorCount() const override
Override from GSmtp::StoredMessage.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
virtual ~StoredFile()
Destructor.
static bool valid(const std::string &)
Returns true if a valid encoding.
static void trimRight(std::string &s, const std::string &ws, size_type limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
bool openContent(std::string &reason)
Opens the content file.
virtual std::string fromAuthOut() const override
Override from GSmtp::StoredMessage.
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
StoredFile(FileStore &store, const G::Path &envelope_path)
Constructor.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
virtual size_t remoteRecipientCount() const override
Override from GSmtp::StoredMessage.
An overload discriminator class for File methods.
A concrete implementation of the MessageStore interface dealing in paired flat files.
static bool knownFormat(const std::string &format)
Returns true if the storage format string is recognised and supported for reading.
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
bool lock()
Locks the file by renaming the envelope file.
virtual const G::StringArray & to() const override
Override from GSmtp::StoredMessage.
virtual std::string name() const override
Override from GSmtp::StoredMessage.
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.
virtual void sync(bool) override
Override from GSmtp::StoredMessage.
virtual bool eightBit() const override
Override from GSmtp::StoredMessage.
Path withExtension(const std::string &ext) const
Returns the path with the new basename extension.