E-MailRelay
gpopstore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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
51 Store( const G::Path & spool_dir , bool by_name , bool allow_delete ) ;
52 ///< Constructor. Throws InvalidDirectory.
53
54 G::Path dir() const ;
55 ///< Returns the spool directory path.
56
57 bool allowDelete() const ;
58 ///< Returns true if files can be deleted.
59
60 bool byName() const ;
61 ///< Returns true if the spool directory is affected
62 ///< by the user name.
63
64public:
65 ~Store() = default ;
66 Store( const Store & ) = delete ;
67 Store( Store && ) = delete ;
68 Store & operator=( const Store & ) = delete ;
69 Store & operator=( Store && ) = delete ;
70
71private:
72 static bool accessible( const G::Path & dir , bool ) ;
73
74private:
75 G::Path m_path ;
76 bool m_by_name ;
77 bool m_allow_delete ;
78} ;
79
80//| \class GPop::StoreMessage
81/// A structure representing a pop message.
82///
84{
85public:
86 using Size = unsigned long ;
87 StoreMessage( const std::string & name , Size size , bool in_parent ) ;
88 G::Path epath( const G::Path & edir ) const ;
89 G::Path cpath( const G::Path & edir , const G::Path & sdir ) const ;
90 G::Path cpath( const G::Path & ) const ;
91 std::string uidl() const ;
92 static StoreMessage invalid() ;
93
94public:
95 std::string name ;
96 Size size ;
97 bool in_parent ;
98 bool deleted ;
99} ;
100
101//| \class GPop::StoreUser
102/// Holds the list of messages available to a particular pop user.
103///
105{
106public:
107 StoreUser( Store & , const std::string & user ) ;
108 ///< Constructor.
109
110public:
111 ~StoreUser() = default ;
112 StoreUser( const StoreUser & ) = delete ;
113 StoreUser( StoreUser && ) = delete ;
114 StoreUser & operator=( const StoreUser & ) = delete ;
115 StoreUser & operator=( StoreUser && ) = delete ;
116
117private:
118 friend class GPop::StoreList ;
119 Store & m_store ;
120 std::string m_user ;
121 G::Path m_edir ;
122 G::Path m_sdir ;
123 std::vector<StoreMessage> m_list ;
124} ;
125
126//| \class GPop::StoreList
127/// Represents the protocol's view of the pop store having 1-based
128/// message ids. Messages can be marked as deleted and then
129/// actually deleted by commit().
130/// \see RFC-1939
131///
133{
134public:
135 G_EXCEPTION( CannotDelete , tx("cannot delete message file") ) ;
136 G_EXCEPTION( CannotRead , tx("cannot read message file") ) ;
137 using Size = StoreMessage::Size ;
138 using List = std::vector<StoreMessage> ;
139
141 ///< Constructor for an empty list.
142
143 StoreList( const StoreUser & , bool allow_delete ) ;
144 ///< Constructor.
145
146 List::const_iterator cbegin() const ;
147 ///< Returns the begin iterator.
148
149 List::const_iterator cend() const ;
150 ///< Returns the end iterator.
151
152 List::value_type get( int id ) const ;
153 ///< Returns the item with index id-1.
154
155 Size messageCount() const ;
156 ///< Returns the store's message count.
157
158 Size totalByteCount() const ;
159 ///< Returns the store's total byte count.
160
161 std::string uidl( int id ) const ;
162 ///< Returns a message's unique 1-based id.
163
164 bool valid( int id ) const ;
165 ///< Validates a message id.
166
167 Size byteCount( int id ) const ;
168 ///< Returns the message size.
169
170 std::unique_ptr<std::istream> content( int id ) const ;
171 ///< Retrieves the message content.
172
173 void remove( int ) ;
174 ///< Marks the message files for deletion.
175
176 void rollback() ;
177 ///< Rolls back remove()als.
178
179 void commit() ;
180 ///< Commits remove()als. Messages remain marked
181 ///< for deletion so another commit() will emit
182 ///< 'cannot delete' error messages.
183
184public:
185 ~StoreList() = default ;
186 StoreList( const StoreList & ) = delete ;
187 StoreList( StoreList && ) = default ;
188 StoreList & operator=( const StoreList & ) = delete ;
189 StoreList & operator=( StoreList && ) = default ;
190
191private:
192 void deleteFile( const G::Path & , bool & ) const ;
193 bool shared( const StoreMessage & ) const ;
194
195private:
196 bool m_allow_delete {false} ;
197 G::Path m_edir ;
198 G::Path m_sdir ;
199 std::vector<StoreMessage> m_list ;
200} ;
201
202#endif
Represents the protocol's view of the pop store having 1-based message ids.
Definition: gpopstore.h:133
Size messageCount() const
Returns the store's message count.
Definition: gpopstore.cpp:228
std::string uidl(int id) const
Returns a message's unique 1-based id.
Definition: gpopstore.cpp:323
void rollback()
Rolls back remove()als.
Definition: gpopstore.cpp:330
Size byteCount(int id) const
Returns the message size.
Definition: gpopstore.cpp:258
bool valid(int id) const
Validates a message id.
Definition: gpopstore.cpp:247
StoreList()
Constructor for an empty list.
List::const_iterator cbegin() const
Returns the begin iterator.
Definition: gpopstore.cpp:218
std::unique_ptr< std::istream > content(int id) const
Retrieves the message content.
Definition: gpopstore.cpp:264
Size totalByteCount() const
Returns the store's total byte count.
Definition: gpopstore.cpp:236
void remove(int)
Marks the message files for deletion.
Definition: gpopstore.cpp:284
void commit()
Commits remove()als.
Definition: gpopstore.cpp:290
List::const_iterator cend() const
Returns the end iterator.
Definition: gpopstore.cpp:223
List::value_type get(int id) const
Returns the item with index id-1.
Definition: gpopstore.cpp:252
A structure representing a pop message.
Definition: gpopstore.h:84
Holds the list of messages available to a particular pop user.
Definition: gpopstore.h:105
StoreUser(Store &, const std::string &user)
Constructor.
Definition: gpopstore.cpp:171
A message store.
Definition: gpopstore.h:47
bool allowDelete() const
Returns true if files can be deleted.
Definition: gpopstore.cpp:124
bool byName() const
Returns true if the spool directory is affected by the user name.
Definition: gpopstore.cpp:129
G::Path dir() const
Returns the spool directory path.
Definition: gpopstore.cpp:119
Store(const G::Path &spool_dir, bool by_name, bool allow_delete)
Constructor. Throws InvalidDirectory.
Definition: gpopstore.cpp:57
A Path object represents a file system path.
Definition: gpath.h:73
POP3 classes.
Definition: gpop.h:33
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84