E-MailRelay

gstoredfile.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 gstoredfile.h
19///
20
21#ifndef G_SMTP_STORED_FILE_H
22#define G_SMTP_STORED_FILE_H
23
24#include "gdef.h"
25#include "gfilestore.h"
26#include "gstoredmessage.h"
27#include "genvelope.h"
28#include "gexception.h"
29#include "gfbuf.h"
30#include "gpath.h"
31#include "gstringarray.h"
32#include <iostream>
33#include <memory>
34#include <new>
35#include <cstdio>
36
37 namespace GStore
38{
39 class StoredFile ;
40 class FileIterator ;
41}
42
43//| \class GStore::StoredFile
44/// A concete class implementing the GStore::StoredMessage interface
45/// for separate envelope and content files in a spool directory.
46/// The GStore::MessageStore::Iterator interface is normally used to
47/// retrieve StoredFile instances.
48///
49/// \see GStore::FileStore, GStore::MessageStore::Iterator, GPop::Store
50///
51 class GStore::StoredFile : public StoredMessage
52{
53 public:
54 G_EXCEPTION( FormatError , tx("invalid envelope file") )
55 G_EXCEPTION( FilenameError , tx("invalid envelope filename") )
56 G_EXCEPTION( EditError , tx("cannot update envelope file") )
57 G_EXCEPTION( SizeError , tx("cannot get content file size") )
58 using State = FileStore::State ;
59
60 StoredFile( FileStore & store , const MessageId & , State = State::Normal ) ;
61 ///< Constructor.
62
63 ~StoredFile() override ;
64 ///< Destructor. Unlocks the file if it has been lock()ed
65 ///< but not destroy()ed or fail()ed or noUnlock()ed.
66
67 bool lock() ;
68 ///< Locks the file by renaming the envelope file.
69
70 bool readEnvelope( std::string & reason ) ;
71 ///< Reads the envelope. Returns false on error with a
72 ///< reason; does not throw.
73
74 bool openContent( std::string & reason ) ;
75 ///< Opens the content file. Returns false on error.
76
77 MessageId id() const override ;
78 ///< Override from GStore::StoredMessage.
79
80 void noUnlock() ;
81 ///< Disable unlocking in the destructor.
82
83 void editEnvelope( std::function<void(Envelope&)> , std::istream * headers = nullptr ) ;
84 ///< Edits the envelope and updates it in the file store.
85 ///< Optionally adds more trailing headers.
86
87 private: // overrides
88 void fail( const std::string & reason , int reason_code ) override ; // GStore::StoredMessage
89 std::string location() const override ; // GStore::StoredMessage
90 MessageStore::BodyType bodyType() const override ; // GStore::StoredMessage
91 std::string from() const override ; // GStore::StoredMessage
92 std::string to( std::size_t ) const override ; // GStore::StoredMessage
93 std::size_t toCount() const override ; // GStore::StoredMessage
94 std::string authentication() const override ; // GStore::StoredMessage
95 std::string fromAuthIn() const override ; // GStore::StoredMessage
96 std::string fromAuthOut() const override ; // GStore::StoredMessage
97 std::string forwardTo() const override ; // GStore::StoredMessage
98 std::string forwardToAddress() const override ; // GStore::StoredMessage
99 std::string clientAccountSelector() const override ; // GStore::StoredMessage
100 bool utf8Mailboxes() const override ; // GStore::StoredMessage
101 void close() override ; // GStore::StoredMessage
102 std::string reopen() override ; // GStore::StoredMessage
103 void destroy() override ; // GStore::StoredMessage
104 std::size_t contentSize() const override ; // GStore::StoredMessage
105 std::istream & contentStream() override ; // GStore::StoredMessage
106 void editRecipients( const G::StringArray & ) override ; // GStore::StoredMessage
107
108 public:
109 StoredFile( const StoredFile & ) = delete ;
110 StoredFile( StoredFile && ) = delete ;
111 StoredFile & operator=( const StoredFile & ) = delete ;
112 StoredFile & operator=( StoredFile && ) = delete ;
113
114 private:
115 using FileOp = FileStore::FileOp ;
116 using StreamBuf = G::fbuf<int,BUFSIZ> ;
117 struct Stream : StreamBuf , std::istream
118 {
119 Stream() ;
120 explicit Stream( const G::Path & ) ;
121 void open( const G::Path & ) ;
122 std::streamoff size() const ;
123 } ;
124
125 private:
126 G::Path cpath() const ;
127 G::Path epath( State ) const ;
128 void replaceEnvelope( const G::Path & , const G::Path & ) ;
129 const std::string & eol() const ;
130 void addReason( const G::Path & path , const std::string & , int ) const ;
131 static std::size_t writeEnvelopeImp( const Envelope & , const G::Path & , std::ofstream & ) ;
132
133 private:
134 FileStore & m_store ;
135 std::unique_ptr<Stream> m_content ;
136 MessageId m_id ;
137 Envelope m_env ;
138 State m_state ;
139 bool m_unlock {false} ;
140} ;
141
142#endif
GStore::Envelope
A structure containing the contents of an envelope file, with support for file reading,...
Definition: genvelope.h:41
GStore::FileStore
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:56
GStore::MessageId
A somewhat opaque identifer for a GStore::MessageStore message id.
Definition: gmessagestore.h:43
GStore::StoredFile
A concete class implementing the GStore::StoredMessage interface for separate envelope and content fi...
Definition: gstoredfile.h:52
GStore::StoredFile::openContent
bool openContent(std::string &reason)
Opens the content file. Returns false on error.
Definition: gstoredfile.cpp:116
GStore::StoredFile::readEnvelope
bool readEnvelope(std::string &reason)
Reads the envelope.
Definition: gstoredfile.cpp:102
GStore::StoredFile::StoredFile
StoredFile(FileStore &store, const MessageId &, State=State::Normal)
Constructor.
Definition: gstoredfile.cpp:35
GStore::StoredFile::lock
bool lock()
Locks the file by renaming the envelope file.
Definition: gstoredfile.cpp:146
GStore::StoredFile::editEnvelope
void editEnvelope(std::function< void(Envelope &)>, std::istream *headers=nullptr)
Edits the envelope and updates it in the file store.
Definition: gstoredfile.cpp:171
GStore::StoredFile::noUnlock
void noUnlock()
Disable unlocking in the destructor.
Definition: gstoredfile.cpp:58
GStore::StoredFile::~StoredFile
~StoredFile() override
Destructor.
Definition: gstoredfile.cpp:42
GStore::StoredFile::id
MessageId id() const override
Override from GStore::StoredMessage.
Definition: gstoredfile.cpp:63
GStore::StoredMessage
An abstract interface for messages which have come from the store.
Definition: gstoredmessage.h:43
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
G::fbuf
A simple file streambuf using a "file descriptor" and three function pointers for read,...
Definition: gfbuf.h:56
genvelope.h
gexception.h
gfbuf.h
gfilestore.h
gpath.h
gstoredmessage.h
gstringarray.h
GStore
Message store classes.
Definition: genvelope.cpp:30
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
GStore::FileStore::FileOp
Low-level file-system operations for GStore::FileStore.
Definition: gfilestore.h:76