E-MailRelay

gfiledelivery.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 gfiledelivery.h
19///
20
21#ifndef G_FILE_DELIVERY_H
22#define G_FILE_DELIVERY_H
23
24#include "gdef.h"
25#include "gmessagedelivery.h"
26#include "gfilestore.h"
27#include "genvelope.h"
28#include "gexception.h"
29#include "gstringview.h"
30#include "gpath.h"
31#include <fstream>
32#include <utility>
33
34 namespace GStore
35{
36 class FileDelivery ;
37}
38
39//| \class GStore::FileDelivery
40/// An implementation of the MessageDelivery interface that delivers
41/// message files to mailboxes. Also provides a low-level delivery
42/// function deliverTo().
43///
44/// The deliver() override takes a ".new" or ".busy" message from the
45/// file store and delivers it to its local recipient mailbox
46/// sub-directories and then deletes the original message files
47/// (unless configured with 'no_delete').
48///
49 class GStore::FileDelivery : public MessageDelivery
50{
51 public:
52 G_EXCEPTION( EnvelopeWriteError , tx("delivery: cannot write envelope file") )
53 G_EXCEPTION( ContentWriteError , tx("delivery: cannot write content file") )
54 G_EXCEPTION( MkdirError , tx("delivery: cannot create delivery directory") )
55 G_EXCEPTION( MaildirCopyError , tx("delivery: cannot write maildir tmp file") )
56 G_EXCEPTION( MaildirMoveError , tx("delivery: cannot move maildir file") )
57 struct Config /// A configuration structure for GStore::FileDelivery.
58 {
59 bool hardlink {false} ; // copy the content by hard-linking
60 bool no_delete {false} ; // don't delete the original message
61 bool pop_by_name {false} ; // copy only the envelope file
62 } ;
63
64 FileDelivery( FileStore & , const Config & ) ;
65 ///< Constructor. The delivery base directory is an attribute of
66 ///< the FileStore.
67
68 static void deliverTo( FileStore & , std::string_view prefix ,
69 const G::Path & dst_dir , const G::Path & envelope_path , const G::Path & content_path ,
70 bool hardlink = false , bool pop_by_name = false ) ;
71 ///< Low-level function to copy a single message into a mailbox
72 ///< sub-directory or a pop-by-name sub-directory. Throws
73 ///< on error (incorporating the given prefix).
74 ///<
75 ///< If pop-by-name then only the envelope is copied and the
76 ///< given destination directory is expected to be an immediate
77 ///< sub-directory of the content file's directory.
78 ///<
79 ///< Does "maildir" delivery if the mailbox directory contains
80 ///< tmp/new/cur sub-directories (if not pop-by-name).
81 ///<
82 ///< The content file is optionally hard-linked.
83 ///<
84 ///< The process umask is modified when creating files so that
85 ///< the new files have full group access. The destination
86 ///< directory should normally have sticky group ownership.
87
88 public:
89 ~FileDelivery() override = default ;
90 FileDelivery( const FileDelivery & ) = delete ;
91 FileDelivery( FileDelivery && ) = delete ;
92 FileDelivery & operator=( const FileDelivery & ) = delete ;
93 FileDelivery & operator=( FileDelivery && ) = delete ;
94
95 private: // overrides
96 bool deliver( const MessageId & , bool ) override ; // GStore::MessageDelivery
97
98 private:
99 using FileOp = FileStore::FileOp ;
100 bool deliverToMailboxes( const G::Path & , const Envelope & , const G::Path & , const G::Path & ) ;
101 static G::StringArray mailboxes( const Config & , const GStore::Envelope & ) ;
102 static std::string mailbox( const Config & , const std::string & ) ;
103 static std::string id( const G::Path & ) ;
104 static std::string hostname() ;
105 G::Path cpath( const MessageId & ) const ;
106 G::Path epath( const MessageId & , FileStore::State ) const ;
107
108 private:
109 FileStore & m_store ;
110 Config m_config ;
111} ;
112
113#endif
GStore::Envelope
A structure containing the contents of an envelope file, with support for file reading,...
Definition: genvelope.h:41
GStore::FileDelivery
An implementation of the MessageDelivery interface that delivers message files to mailboxes.
Definition: gfiledelivery.h:50
GStore::FileDelivery::deliverTo
static void deliverTo(FileStore &, std::string_view prefix, const G::Path &dst_dir, const G::Path &envelope_path, const G::Path &content_path, bool hardlink=false, bool pop_by_name=false)
Low-level function to copy a single message into a mailbox sub-directory or a pop-by-name sub-directo...
Definition: gfiledelivery.cpp:102
GStore::FileDelivery::FileDelivery
FileDelivery(FileStore &, const Config &)
Constructor.
Definition: gfiledelivery.cpp:36
GStore::FileStore
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:56
GStore::MessageDelivery
An interface to deliver a message to its local recipients' mailboxes.
Definition: gmessagedelivery.h:52
GStore::MessageId
A somewhat opaque identifer for a GStore::MessageStore message id.
Definition: gmessagestore.h:43
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
genvelope.h
gexception.h
gfilestore.h
gmessagedelivery.h
gpath.h
gstringview.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::FileDelivery::Config
A configuration structure for GStore::FileDelivery.
Definition: gfiledelivery.h:58
GStore::FileStore::FileOp
Low-level file-system operations for GStore::FileStore.
Definition: gfilestore.h:76