54 virtual unique_ptr<GSmtp::StoredMessage> next() ;
65 GSmtp::FileIterator::FileIterator(
FileStore & store ,
const G::Path & dir ,
bool lock ,
bool failures ) :
71 m_iter.
readType( dir , std::string(failures?
".envelope.bad":
".envelope") ) ;
74 GSmtp::FileIterator::~FileIterator()
78 unique_ptr<GSmtp::StoredMessage> GSmtp::FileIterator::next()
80 while( m_iter.more() )
82 unique_ptr<StoredFile> m(
new StoredFile(m_store,m_iter.filePath()) ) ;
83 if( m_lock && !m->lock() )
85 G_WARNING(
"GSmtp::MessageStore: cannot lock file: \"" << m_iter.filePath() <<
"\"" ) ;
90 const bool check_recipients = m_lock ;
91 bool ok = m->readEnvelope(reason,check_recipients) && m->openContent(reason) ;
93 return unique_ptr<StoredMessage>(m.release()) ;
96 m->fail( reason , 0 ) ;
98 G_WARNING(
"GSmtp::MessageStore: ignoring \"" << m_iter.filePath() <<
"\": " << reason ) ;
100 return unique_ptr<StoredMessage>() ;
108 m_optimise(optimise) ,
112 m_pid_modifier =
static_cast<unsigned long>(
G::DateTime::now().s) % 1000000UL ;
122 createTestMessage() ;
127 return "X-MailRelay-" ;
132 if( generation == -2 )
134 else if( generation == -1 )
143 format_in == format(0) ||
144 format_in == format(-1) ||
145 format_in == format(-2) ;
148 void GSmtp::FileStore::checkPath(
const G::Path & directory_path )
156 ok = dir_test.valid() ;
160 throw InvalidDirectory( directory_path.
str() ) ;
166 FileWriter claim_writer ;
167 ok = dir_test.writeable(tmp) ;
171 G_WARNING(
"GSmtp::MessageStore: directory not writable: \"" << directory_path <<
"\"" ) ;
178 return unique_ptr<std::ostream>(
new std::ofstream( path.
str().c_str() ,
179 std::ios_base::binary | std::ios_base::out | std::ios_base::trunc ) ) ;
184 return fullPath( filePrefix(seq) +
".content" ) ;
189 return fullPath( filePrefix(seq) +
".envelope" ) ;
194 return fullPath( filePrefix(seq) +
".envelope.new" ) ;
197 std::string GSmtp::FileStore::filePrefix(
unsigned long seq )
const
199 std::ostringstream ss ;
201 ss <<
"emailrelay." << pid.str() <<
"." << m_pid_modifier <<
"." << seq ;
205 G::Path GSmtp::FileStore::fullPath(
const std::string & filename )
const
208 p.pathAppend( filename ) ;
225 const_cast<FileStore*
>(
this)->m_empty = emptyCore() ;
234 bool GSmtp::FileStore::emptyCore()
const
238 list.
readType( m_dir ,
".envelope" , 1U ) ;
239 const bool no_more = !list.
more() ;
255 G::Path path = envelopePath(
id ) ;
257 unique_ptr<StoredFile> message(
new StoredFile(*
this,path) ) ;
259 if( ! message->
lock() )
260 throw GetError( path.
str() +
": cannot lock the file" ) ;
263 const bool check_recipients = false ;
265 throw GetError( path.
str() +
": cannot read the envelope: " + reason ) ;
268 throw GetError( path.
str() +
": cannot read the content: " + reason ) ;
270 G_LOG(
"GSmtp::FileStore::get: processing message \"" << message->
name() <<
"\"" ) ;
272 return unique_ptr<StoredMessage>( message.release() ) ;
276 const std::string & from_auth_in ,
const std::string & from_auth_out )
279 return unique_ptr<NewMessage>(
new NewFile(*
this,from,from_auth_in,from_auth_out,m_max_size) ) ;
284 G_DEBUG(
"GSmtp::FileStore::updated" ) ;
285 m_update_signal.emit() ;
290 return m_update_signal ;
295 return m_rescan_signal ;
300 messageStoreRescanSignal().emit() ;
308 unique_ptr<StoredMessage> message = iter.next() ;
309 if( message.get() == nullptr )
311 G_DEBUG(
"GSmtp::FileStore::unfailAll: " << message->
name() ) ;
316 void GSmtp::FileStore::createTestMessage()
319 NewFile f( *
this ,
"test@local.net" ,
"" ) ;
320 f.addTo(
"test@local.net" ,
false ) ;
321 for(
int i = 0 ; i < 1000 ; i++ )
323 std::string line( 998U ,
'a'+(i%26) ) ;
324 f.addText( line.data() , line.size() ) ;
326 f.prepare(
"test" ,
"127.0.0.1:999" , std::string() ) ;
330 void GSmtp::FileStore::clearAll()
333 MessageStore::Iterator iter( iterator(
true) ) ;
336 unique_ptr<StoredMessage> message = iter.next() ;
368 G::Process::Umask(
G::Process::Umask::Tighter)
bool readEnvelope(std::string &reason, bool check_for_no_remote_recipients)
Reads the envelope.
std::string str() const
Returns the path string.
A noncopyable base class (a la boost).
G::Path contentPath(unsigned long seq) const
Returns the path for a content file.
static std::string format(int generation=0)
Returns an identifier for the storage format implemented by this class, or some older generation of i...
SMTP and message-store classes.
static std::string x()
Returns the prefix for envelope header lines.
FileWriter()
Default constructor.
virtual G::Slot::Signal0 & messageStoreUpdateSignal() override
Override from GSmtp::MessageStore.
virtual void unfail()=0
Marks the message as unfailed within the store.
virtual std::string name() const =0
Returns some sort of unique identifier for the message.
bool more()
Returns true if more and advances by one.
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
virtual G::Slot::Signal0 & messageStoreRescanSignal() override
Override from GSmtp::MessageStore.
virtual void destroy()=0
Deletes the message within the store.
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim write permissions.
~DirectoryReader()
Destructor. Switches identity back.
A base class for MessageStore::Iterator implementations.
static EpochTime now()
Returns the current epoch time.
void readType(const Path &dir, const std::string &suffix, unsigned int limit=0U)
An initialiser that is to be used after default construction.
An encapsulation of a file system directory that allows for iterating through the set of contained fi...
virtual MessageStore::Iterator failures() override
Override from GSmtp::MessageStore.
unique_ptr< std::ostream > stream(const G::Path &path)
Returns a stream to the given content.
virtual MessageStore::Iterator iterator(bool lock) override
Override from GSmtp::MessageStore.
FileStore(const G::Path &dir, bool optimise=false, unsigned long max_size=0UL)
Constructor.
A 'body' class for the MessageStore::Iterator 'handle'.
static bool enabled()
Returns true if test features are enabled.
bool openContent(std::string &reason)
Opens the content file.
A concrete derived class implementing the NewMessage interface.
A concete derived class implementing the StoredMessage interface.
A Directory iterator with the same kind of interface as G::DirectoryIterator, but doing all file i/o ...
~FileReader()
Destructor. Switches identity back.
virtual void updated() override
Override from GSmtp::MessageStore.
virtual unique_ptr< NewMessage > newMessage(const std::string &from, const std::string &from_auth_in, const std::string &from_auth_out) override
Override from GSmtp::MessageStore.
static std::string tmp()
A convenience function for constructing a filename for writeable().
A signal class for zero-parameter callbacks.
~FileWriter()
Destructor. Switches identity back.
A concrete implementation of the MessageStore interface dealing in paired flat files.
G::Path envelopePath(unsigned long seq) const
Returns the path for an envelope file.
virtual unique_ptr< StoredMessage > get(unsigned long id) override
Override from GSmtp::MessageStore.
DirectoryReader()
Default constructor.
virtual void unfailAll() override
Override from GSmtp::MessageStore.
static bool knownFormat(const std::string &format)
Returns true if the storage format string is recognised and supported for reading.
unsigned long newSeq()
Hands out a new non-zero sequence number.
bool lock()
Locks the file by renaming the envelope file.
virtual std::string name() const override
Override from GSmtp::StoredMessage.
A Path object represents a file system path.
FileReader()
Default constructor.
G::Path envelopeWorkingPath(unsigned long seq) const
Returns the path for an envelope file which is in the process of being written.
virtual void rescan() override
Override from GSmtp::MessageStore.
virtual bool empty() const override
Override from GSmtp::MessageStore.
An iterator class for GSmtp::MessageStore.