E-MailRelay
gpopstore.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 gpopstore.h
19///
20
21#ifndef G_POP_STORE_H
22#define G_POP_STORE_H
23
24#include "gdef.h"
25#include "gpath.h"
26#include "gexception.h"
27#include <string>
28#include <memory>
29#include <iostream>
30#include <set>
31#include <vector>
32
33namespace GPop
34{
35 class Store ;
36 class StoreMessage ;
37 class StoreUser ;
38 class StoreList ;
39}
40
41//| \class GPop::Store
42/// A message store. Unlike the SMTP message store the POP message
43/// store allows content files to be in the envelope file's parent
44/// directory.
45///
47{
48public:
49 G_EXCEPTION( InvalidDirectory , tx("invalid spool directory") )
50 struct Config /// Configuration structure for GPop::Store.
51 {
52 bool allow_delete {true} ; // working DELE command
53 bool by_name {false} ; // authentication-name used as spool-dir sub-directory
54 bool by_name_mkdir {false} ; // Store::prepare() creates sub-directory if necessary
55
56 Config & set_allow_delete( bool = true ) noexcept ;
57 Config & set_by_name( bool = true ) noexcept ;
58 Config & set_by_name_mkdir( bool = true ) noexcept ;
59 } ;
60
61 Store( const G::Path & spool_dir , const Config & ) ;
62 ///< Constructor. Throws InvalidDirectory.
63
64 void prepare( const std::string & user ) ;
65 ///< Prepares the store for the newly-authenticated
66 ///< user. Creates a pop-by-name sub-directory if
67 ///< necessary.
68
69 G::Path dir() const ;
70 ///< Returns the spool directory path.
71
72 bool allowDelete() const ;
73 ///< Returns true if files can be deleted.
74
75 bool byName() const ;
76 ///< Returns true if the spool directory is affected
77 ///< by the user name.
78
79public:
80 ~Store() = default ;
81 Store( const Store & ) = delete ;
82 Store( Store && ) = delete ;
83 Store & operator=( const Store & ) = delete ;
84 Store & operator=( Store && ) = delete ;
85
86private:
87 static bool accessible( const G::Path & dir , bool ) ;
88
89private:
90 G::Path m_path ;
91 Config m_config ;
92} ;
93
94//| \class GPop::StoreMessage
95/// A structure representing a pop message.
96///
98{
99public:
100 using Size = unsigned long ;
101 StoreMessage( const std::string & name , Size size , bool in_parent ) ;
102 G::Path epath( const G::Path & edir ) const ;
103 G::Path cpath( const G::Path & edir , const G::Path & sdir ) const ;
104 G::Path cpath( const G::Path & ) const ;
105 std::string uidl() const ;
106 static StoreMessage invalid() ;
107
108public:
109 std::string name ;
110 Size size ;
111 bool in_parent ;
112 bool deleted {false} ;
113} ;
114
115//| \class GPop::StoreUser
116/// Holds the list of messages available to a particular pop user.
117///
119{
120public:
121 StoreUser( Store & , const std::string & user ) ;
122 ///< Constructor.
123
124public:
125 ~StoreUser() = default ;
126 StoreUser( const StoreUser & ) = delete ;
127 StoreUser( StoreUser && ) = delete ;
128 StoreUser & operator=( const StoreUser & ) = delete ;
129 StoreUser & operator=( StoreUser && ) = delete ;
130
131private:
132 friend class GPop::StoreList ;
133 Store & m_store ;
134 std::string m_user ;
135 G::Path m_edir ;
136 G::Path m_sdir ;
137 std::vector<StoreMessage> m_list ;
138} ;
139
140//| \class GPop::StoreList
141/// Represents the protocol's view of the pop store having 1-based
142/// message ids. Messages can be marked as deleted and then
143/// actually deleted by commit().
144/// \see RFC-1939
145///
147{
148public:
149 G_EXCEPTION( CannotDelete , tx("cannot delete message file") )
150 G_EXCEPTION( CannotRead , tx("cannot read message file") )
151 using Size = StoreMessage::Size ;
152 using List = std::vector<StoreMessage> ;
153
155 ///< Constructor for an empty list.
156
157 StoreList( const StoreUser & , bool allow_delete ) ;
158 ///< Constructor.
159
160 List::const_iterator cbegin() const ;
161 ///< Returns the begin iterator.
162
163 List::const_iterator cend() const ;
164 ///< Returns the end iterator.
165
166 List::value_type get( int id ) const ;
167 ///< Returns the item with index id-1.
168
169 Size messageCount() const ;
170 ///< Returns the store's message count.
171
172 Size totalByteCount() const ;
173 ///< Returns the store's total byte count.
174
175 std::string uidl( int id ) const ;
176 ///< Returns a message's unique 1-based id.
177
178 bool valid( int id ) const ;
179 ///< Validates a message id.
180
181 Size byteCount( int id ) const ;
182 ///< Returns the message size.
183
184 std::unique_ptr<std::istream> content( int id ) const ;
185 ///< Retrieves the message content.
186
187 void remove( int ) ;
188 ///< Marks the message files for deletion.
189
190 void rollback() ;
191 ///< Rolls back remove()als.
192
193 void commit() ;
194 ///< Commits remove()als. Messages remain marked
195 ///< for deletion so another commit() will emit
196 ///< 'cannot delete' error messages.
197
198public:
199 ~StoreList() = default ;
200 StoreList( const StoreList & ) = delete ;
201 StoreList( StoreList && ) = default ;
202 StoreList & operator=( const StoreList & ) = delete ;
203 StoreList & operator=( StoreList && ) = default ;
204
205private:
206 void deleteFile( const G::Path & , bool & ) const ;
207 bool shared( const StoreMessage & ) const ;
208
209private:
210 bool m_allow_delete {false} ;
211 G::Path m_edir ;
212 G::Path m_sdir ;
213 std::vector<StoreMessage> m_list ;
214} ;
215
216inline GPop::Store::Config & GPop::Store::Config::set_allow_delete( bool b ) noexcept { allow_delete = b ; return *this ; }
217inline GPop::Store::Config & GPop::Store::Config::set_by_name( bool b ) noexcept { by_name = b ; return *this ; }
218inline GPop::Store::Config & GPop::Store::Config::set_by_name_mkdir( bool b ) noexcept { by_name_mkdir = b ; return *this ; }
219
220#endif
Represents the protocol's view of the pop store having 1-based message ids.
Definition: gpopstore.h:147
Size messageCount() const
Returns the store's message count.
Definition: gpopstore.cpp:246
std::string uidl(int id) const
Returns a message's unique 1-based id.
Definition: gpopstore.cpp:349
void rollback()
Rolls back remove()als.
Definition: gpopstore.cpp:357
Size byteCount(int id) const
Returns the message size.
Definition: gpopstore.cpp:278
bool valid(int id) const
Validates a message id.
Definition: gpopstore.cpp:265
StoreList()
Constructor for an empty list.
List::const_iterator cbegin() const
Returns the begin iterator.
Definition: gpopstore.cpp:236
std::unique_ptr< std::istream > content(int id) const
Retrieves the message content.
Definition: gpopstore.cpp:285
Size totalByteCount() const
Returns the store's total byte count.
Definition: gpopstore.cpp:254
void remove(int)
Marks the message files for deletion.
Definition: gpopstore.cpp:307
void commit()
Commits remove()als.
Definition: gpopstore.cpp:316
List::const_iterator cend() const
Returns the end iterator.
Definition: gpopstore.cpp:241
List::value_type get(int id) const
Returns the item with index id-1.
Definition: gpopstore.cpp:271
A structure representing a pop message.
Definition: gpopstore.h:98
Holds the list of messages available to a particular pop user.
Definition: gpopstore.h:119
StoreUser(Store &, const std::string &user)
Constructor.
Definition: gpopstore.cpp:189
A message store.
Definition: gpopstore.h:47
bool allowDelete() const
Returns true if files can be deleted.
Definition: gpopstore.cpp:143
void prepare(const std::string &user)
Prepares the store for the newly-authenticated user.
Definition: gpopstore.cpp:90
bool byName() const
Returns true if the spool directory is affected by the user name.
Definition: gpopstore.cpp:148
Store(const G::Path &spool_dir, const Config &)
Constructor. Throws InvalidDirectory.
Definition: gpopstore.cpp:64
G::Path dir() const
Returns the spool directory path.
Definition: gpopstore.cpp:138
A Path object represents a file system path.
Definition: gpath.h:82
POP3 classes.
Definition: gpop.h:33
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
Configuration structure for GPop::Store.
Definition: gpopstore.h:51