E-MailRelay

gfilestore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file gfilestore.h
19///
20
21#ifndef G_SMTP_FILE_STORE_H
22#define G_SMTP_FILE_STORE_H
23
24#include "gdef.h"
25#include "gmessagestore.h"
26#include "gdatetime.h"
27#include "gexception.h"
28#include "gprocess.h"
29#include "gslot.h"
30#include "groot.h"
31#include "gpath.h"
32#include <fstream>
33#include <memory>
34#include <string>
35
36 namespace GSmtp
37{
38 class FileStore ;
39 class FileReader ;
40 class FileWriter ;
41 class DirectoryReader ;
42}
43
44//| \class GSmtp::FileStore
45/// A concrete implementation of the MessageStore interface dealing
46/// in paired flat files.
47///
48/// The implementation puts separate envelope and content files
49/// in the spool directory. The content file is written first.
50/// The presence of a matching envelope file is used to indicate
51/// that the content file is valid and that it has been commited
52/// to the care of the SMTP system for delivery.
53///
54/// Passes out unique sequence numbers, filesystem paths and
55/// i/o streams to NewMessageImp.
56///
57 class GSmtp::FileStore : public MessageStore
58{
59 public:
60 G_EXCEPTION( InvalidDirectory , "invalid spool directory" ) ;
61 G_EXCEPTION( GetError , "error reading specific message" ) ;
62
63 FileStore( const G::Path & dir , unsigned long max_size , bool test_for_eight_bit ) ;
64 ///< Constructor. Throws an exception if the storage directory
65 ///< is invalid.
66
67 MessageId newId() ;
68 ///< Hands out a new message id.
69
70 std::unique_ptr<std::ofstream> stream( const G::Path & path ) ;
71 ///< Returns a stream to the given content.
72
73 G::Path contentPath( const MessageId & ) const ;
74 ///< Returns the path for a content file.
75
76 G::Path envelopePath( const MessageId & , const char * modifier = "" ) const ;
77 ///< Returns the path for an envelope file.
78
79 bool empty() const override ;
80 ///< Override from GSmtp::MessageStore.
81
82 std::string location( const MessageId & ) const override ;
83 ///< Override from GSmtp::MessageStore.
84
85 std::unique_ptr<StoredMessage> get( const MessageId & ) override ;
86 ///< Override from GSmtp::MessageStore.
87
88 std::shared_ptr<MessageStore::Iterator> iterator( bool lock ) override ;
89 ///< Override from GSmtp::MessageStore.
90
91 std::shared_ptr<MessageStore::Iterator> failures() override ;
92 ///< Override from GSmtp::MessageStore.
93
94 std::unique_ptr<NewMessage> newMessage( const std::string & from ,
95 const std::string & from_auth_in , const std::string & from_auth_out ) override ;
96 ///< Override from GSmtp::MessageStore.
97
98 static std::string x() ;
99 ///< Returns the prefix for envelope header lines.
100
101 static std::string format( int generation = 0 ) ;
102 ///< Returns an identifier for the storage format implemented
103 ///< by this class, or some older generation of it (eg. -1).
104
105 static bool knownFormat( const std::string & format ) ;
106 ///< Returns true if the storage format string is
107 ///< recognised and supported for reading.
108
109 void updated() override ;
110 ///< Override from GSmtp::MessageStore.
111
112 G::Slot::Signal<> & messageStoreUpdateSignal() override ;
113 ///< Override from GSmtp::MessageStore.
114
115 G::Slot::Signal<> & messageStoreRescanSignal() override ;
116 ///< Override from GSmtp::MessageStore.
117
118 private: // overrides
119 void rescan() override ; // Override from GSmtp::MessageStore.
120 void unfailAll() override ; // Override from GSmtp::MessageStore.
121
122 public:
123 ~FileStore() override = default ;
124 FileStore( const FileStore & ) = delete ;
125 FileStore( FileStore && ) = delete ;
126 void operator=( const FileStore & ) = delete ;
127 void operator=( FileStore && ) = delete ;
128
129 private:
130 static void checkPath( const G::Path & dir ) ;
131 G::Path fullPath( const std::string & filename ) const ;
132 std::string filePrefix( unsigned long seq ) const ;
133 std::string getline( std::istream & ) const ;
134 std::string value( const std::string & ) const ;
135 std::shared_ptr<MessageStore::Iterator> iteratorImp( bool ) ;
136 void unfailAllImp() ;
137 static const std::string & crlf() ;
138 bool emptyCore() const ;
139 void clearAll() ;
140
141 private:
142 unsigned long m_seq ;
143 G::Path m_dir ;
144 unsigned long m_max_size ;
145 bool m_test_for_eight_bit ;
146 unsigned long m_pid_modifier ;
147 G::Slot::Signal<> m_update_signal ;
148 G::Slot::Signal<> m_rescan_signal ;
149} ;
150
151//| \class GSmtp::FileReader
152/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
153/// claim read permissions for reading a file.
154/// \see G::Root
155///
156 class GSmtp::FileReader : private G::Root
157{
158 public:
159 FileReader() ;
160 ///< Default constructor. Switches identity for
161 ///< reading a file.
162
163 ~FileReader() ;
164 ///< Destructor. Switches identity back.
165
166 public:
167 FileReader( const FileReader & ) = delete ;
168 FileReader( FileReader && ) = delete ;
169 void operator=( const FileReader & ) = delete ;
170 void operator=( FileReader && ) = delete ;
171} ;
172
173//| \class GSmtp::DirectoryReader
174/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
175/// claim read permissions for reading a directory.
176/// \see G::Root
177///
178 class GSmtp::DirectoryReader : private G::Root
179{
180 public:
181 DirectoryReader() ;
182 ///< Default constructor. Switches identity for
183 ///< reading a directory.
184
185 ~DirectoryReader() ;
186 ///< Destructor. Switches identity back.
187
188 public:
189 DirectoryReader( const DirectoryReader & ) = delete ;
190 DirectoryReader( DirectoryReader && ) = delete ;
191 void operator=( const DirectoryReader & ) = delete ;
192 void operator=( DirectoryReader && ) = delete ;
193} ;
194
195//| \class GSmtp::FileWriter
196/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
197/// claim write permissions.
198/// \see G::Root
199///
200 class GSmtp::FileWriter : private G::Root , private G::Process::Umask
201{
202 public:
203 FileWriter() ;
204 ///< Default constructor. Switches identity for
205 ///< writing a file.
206
207 ~FileWriter() ;
208 ///< Destructor. Switches identity back.
209
210 public:
211 FileWriter( const FileWriter & ) = delete ;
212 FileWriter( FileWriter && ) = delete ;
213 void operator=( const FileWriter & ) = delete ;
214 void operator=( FileWriter && ) = delete ;
215} ;
216
217#endif
GSmtp::DirectoryReader
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
Definition: gfilestore.h:179
GSmtp::DirectoryReader::~DirectoryReader
~DirectoryReader()
Destructor. Switches identity back.
GSmtp::DirectoryReader::DirectoryReader
DirectoryReader()
Default constructor.
GSmtp::FileReader
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
Definition: gfilestore.h:157
GSmtp::FileReader::FileReader
FileReader()
Default constructor.
GSmtp::FileReader::~FileReader
~FileReader()
Destructor. Switches identity back.
GSmtp::FileStore
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:58
GSmtp::FileStore::messageStoreRescanSignal
G::Slot::Signal & messageStoreRescanSignal() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:285
GSmtp::FileStore::FileStore
FileStore(const G::Path &dir, unsigned long max_size, bool test_for_eight_bit)
Constructor.
Definition: gfilestore.cpp:107
GSmtp::FileStore::get
std::unique_ptr< StoredMessage > get(const MessageId &) override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:248
GSmtp::FileStore::failures
std::shared_ptr< MessageStore::Iterator > failures() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:243
GSmtp::FileStore::knownFormat
static bool knownFormat(const std::string &format)
Returns true if the storage format string is recognised and supported for reading.
Definition: gfilestore.cpp:145
GSmtp::FileStore::updated
void updated() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:274
GSmtp::FileStore::stream
std::unique_ptr< std::ofstream > stream(const G::Path &path)
Returns a stream to the given content.
Definition: gfilestore.cpp:190
GSmtp::FileStore::contentPath
G::Path contentPath(const MessageId &) const
Returns the path for a content file.
Definition: gfilestore.cpp:200
GSmtp::FileStore::iterator
std::shared_ptr< MessageStore::Iterator > iterator(bool lock) override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:233
GSmtp::FileStore::newMessage
std::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.
Definition: gfilestore.cpp:267
GSmtp::FileStore::x
static std::string x()
Returns the prefix for envelope header lines.
Definition: gfilestore.cpp:129
GSmtp::FileStore::envelopePath
G::Path envelopePath(const MessageId &, const char *modifier="") const
Returns the path for an envelope file.
Definition: gfilestore.cpp:205
GSmtp::FileStore::format
static std::string format(int generation=0)
Returns an identifier for the storage format implemented by this class, or some older generation of i...
Definition: gfilestore.cpp:134
GSmtp::FileStore::empty
bool empty() const override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:224
GSmtp::FileStore::location
std::string location(const MessageId &) const override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:185
GSmtp::FileStore::messageStoreUpdateSignal
G::Slot::Signal & messageStoreUpdateSignal() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:280
GSmtp::FileStore::newId
MessageId newId()
Hands out a new message id.
Definition: gfilestore.cpp:211
GSmtp::FileWriter
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim write permissions.
Definition: gfilestore.h:201
GSmtp::FileWriter::~FileWriter
~FileWriter()
Destructor. Switches identity back.
GSmtp::FileWriter::FileWriter
FileWriter()
Default constructor.
Definition: gfilestore.cpp:345
GSmtp::MessageId
A somewhat opaque identifer for a MessageStore message.
Definition: gmessagestore.h:43
GSmtp::MessageStore
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Process::Umask
Used to temporarily modify the process umask.
Definition: gprocess.h:158
G::Root
A class which acquires the process's special privileges on construction and releases them on destruct...
Definition: groot.h:52
gdatetime.h
gexception.h
gmessagestore.h
gpath.h
gprocess.h
groot.h
gslot.h
GSmtp
SMTP and message-store classes.
Definition: gadminserver.h:39
G::Slot::Signal
A slot holder, with connect() and emit() methods.
Definition: gslot.h:157