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
33 namespace 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///
46 class GPop::Store
47{
48 public:
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
79 public:
80 ~Store() = default ;
81 Store( const Store & ) = delete ;
82 Store( Store && ) = delete ;
83 Store & operator=( const Store & ) = delete ;
84 Store & operator=( Store && ) = delete ;
85
86 private:
87 static bool accessible( const G::Path & dir , bool ) ;
88
89 private:
90 G::Path m_path ;
91 Config m_config ;
92} ;
93
94//| \class GPop::StoreMessage
95/// A structure representing a pop message.
96///
97 class GPop::StoreMessage
98{
99 public:
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
108 public:
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///
118 class GPop::StoreUser
119{
120 public:
121 StoreUser( Store & , const std::string & user ) ;
122 ///< Constructor.
123
124 public:
125 ~StoreUser() = default ;
126 StoreUser( const StoreUser & ) = delete ;
127 StoreUser( StoreUser && ) = delete ;
128 StoreUser & operator=( const StoreUser & ) = delete ;
129 StoreUser & operator=( StoreUser && ) = delete ;
130
131 private:
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///
146 class GPop::StoreList
147{
148 public:
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
154 StoreList() ;
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
198 public:
199 ~StoreList() = default ;
200 StoreList( const StoreList & ) = delete ;
201 StoreList( StoreList && ) = default ;
202 StoreList & operator=( const StoreList & ) = delete ;
203 StoreList & operator=( StoreList && ) = default ;
204
205 private:
206 void deleteFile( const G::Path & , bool & ) const ;
207 bool shared( const StoreMessage & ) const ;
208
209 private:
210 bool m_allow_delete {false} ;
211 G::Path m_edir ;
212 G::Path m_sdir ;
213 std::vector<StoreMessage> m_list ;
214} ;
215
216 inline GPop::Store::Config & GPop::Store::Config::set_allow_delete( bool b ) noexcept { allow_delete = b ; return *this ; }
217 inline GPop::Store::Config & GPop::Store::Config::set_by_name( bool b ) noexcept { by_name = b ; return *this ; }
218 inline GPop::Store::Config & GPop::Store::Config::set_by_name_mkdir( bool b ) noexcept { by_name_mkdir = b ; return *this ; }
219
220#endif
GPop::StoreList
Represents the protocol's view of the pop store having 1-based message ids.
Definition: gpopstore.h:147
GPop::StoreList::messageCount
Size messageCount() const
Returns the store's message count.
Definition: gpopstore.cpp:246
GPop::StoreList::uidl
std::string uidl(int id) const
Returns a message's unique 1-based id.
Definition: gpopstore.cpp:349
GPop::StoreList::rollback
void rollback()
Rolls back remove()als.
Definition: gpopstore.cpp:357
GPop::StoreList::byteCount
Size byteCount(int id) const
Returns the message size.
Definition: gpopstore.cpp:278
GPop::StoreList::valid
bool valid(int id) const
Validates a message id.
Definition: gpopstore.cpp:265
GPop::StoreList::StoreList
StoreList()
Constructor for an empty list.
GPop::StoreList::cbegin
List::const_iterator cbegin() const
Returns the begin iterator.
Definition: gpopstore.cpp:236
GPop::StoreList::content
std::unique_ptr< std::istream > content(int id) const
Retrieves the message content.
Definition: gpopstore.cpp:285
GPop::StoreList::totalByteCount
Size totalByteCount() const
Returns the store's total byte count.
Definition: gpopstore.cpp:254
GPop::StoreList::remove
void remove(int)
Marks the message files for deletion.
Definition: gpopstore.cpp:307
GPop::StoreList::commit
void commit()
Commits remove()als.
Definition: gpopstore.cpp:316
GPop::StoreList::cend
List::const_iterator cend() const
Returns the end iterator.
Definition: gpopstore.cpp:241
GPop::StoreList::get
List::value_type get(int id) const
Returns the item with index id-1.
Definition: gpopstore.cpp:271
GPop::StoreMessage
A structure representing a pop message.
Definition: gpopstore.h:98
GPop::StoreUser
Holds the list of messages available to a particular pop user.
Definition: gpopstore.h:119
GPop::StoreUser::StoreUser
StoreUser(Store &, const std::string &user)
Constructor.
Definition: gpopstore.cpp:189
GPop::Store
A message store.
Definition: gpopstore.h:47
GPop::Store::allowDelete
bool allowDelete() const
Returns true if files can be deleted.
Definition: gpopstore.cpp:143
GPop::Store::prepare
void prepare(const std::string &user)
Prepares the store for the newly-authenticated user.
Definition: gpopstore.cpp:90
GPop::Store::byName
bool byName() const
Returns true if the spool directory is affected by the user name.
Definition: gpopstore.cpp:148
GPop::Store::Store
Store(const G::Path &spool_dir, const Config &)
Constructor. Throws InvalidDirectory.
Definition: gpopstore.cpp:64
GPop::Store::dir
G::Path dir() const
Returns the spool directory path.
Definition: gpopstore.cpp:138
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
gexception.h
gpath.h
GPop
POP3 classes.
Definition: gpop.h:33
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
GPop::Store::Config
Configuration structure for GPop::Store.
Definition: gpopstore.h:51