E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gpopserverprotocol.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 gpopserverprotocol.h
19 ///
20
21 #ifndef G_POP_SERVER_PROTOCOL_H
22 #define G_POP_SERVER_PROTOCOL_H
23
24 #include "gdef.h"
25 #include "gpop.h"
26 #include "gaddress.h"
27 #include "gstatemachine.h"
28 #include "gpopsecrets.h"
29 #include "gpopstore.h"
30 #include "gpopauth.h"
31 #include "gtimer.h"
32 #include "gexception.h"
33
34 namespace GPop
35 {
36 class ServerProtocol ;
37 class ServerProtocolText ;
38 }
39
40 /// \class GPop::ServerProtocol
41 /// Implements the POP server-side protocol.
42 ///
43 /// Uses the ServerProtocol::Sender as its "sideways"
44 /// interface to talk back to the client.
45 ///
46 /// \see RFC-1939
47 ///
48 class GPop::ServerProtocol
49 {
50 public:
51 G_EXCEPTION( ProtocolDone , "pop protocol done" ) ;
52 class Sender /// An interface used by ServerProtocol to send protocol replies.
53 {
54 public: virtual bool protocolSend( const std::string & s , size_t offset ) = 0 ;
55 public: virtual ~Sender() ;
56 private: void operator=( const Sender & ) ; // not implemented
57 } ;
58 class Text /// An interface used by ServerProtocol to provide response text strings.
59 {
60 public: virtual std::string greeting() const = 0 ;
61 public: virtual std::string quit() const = 0 ;
62 public: virtual std::string capa() const = 0 ;
63 public: virtual std::string user( const std::string & id ) const = 0 ;
64 public: virtual ~Text() ;
65 private: void operator=( const Text & ) ; // not implemented
66 } ;
67 struct Config /// A structure containing configuration parameters for ServerProtocol. NOT USED.
68 {
69 bool dummy ;
70 Config() ;
71 } ;
72 class Security /// An interface used by ServerProtocol to enable TLS.
73 {
74 public: virtual bool securityEnabled() const = 0 ;
75 public: virtual void securityStart() = 0 ;
76 public: virtual ~Security() ;
77 private: void operator=( const Security & ) ; // not implemented
78 } ;
79
80 ServerProtocol( Sender & sender , Security & security , Store & store , const Secrets & secrets ,
81 const Text & text , GNet::Address peer_address , Config config ) ;
82 ///< Constructor.
83 ///<
84 ///< The Sender interface is used to send protocol
85 ///< replies back to the client.
86 ///<
87 ///< The Text interface is used to get informational text
88 ///< for returning to the client.
89 ///<
90 ///< All references are kept.
91
92 virtual ~ServerProtocol() ;
93 ///< Destructor.
94
95 void init() ;
96 ///< Starts the protocol.
97
98 void apply( const std::string & line ) ;
99 ///< Called on receipt of a string from the client.
100 ///< The string is expected to be CR-LF terminated.
101 ///< Throws ProtocolDone if done.
102
103 void resume() ;
104 ///< Called when the Sender can send again. The Sender returns
105 ///< false from protocolSend() when blocked, and calls
106 ///< resume() when unblocked.
107
108 void secure() ;
109 ///< Called when the server connection becomes secure.
110
111 private:
112 enum Event
113 {
114 eApop ,
115 eAuth ,
116 eAuthData ,
117 eCapa ,
118 eDele ,
119 eList ,
120 eNoop ,
121 ePass ,
122 eQuit ,
123 eRetr ,
124 eRset ,
125 eSent ,
126 eStat ,
127 eTop ,
128 eUidl ,
129 eUser ,
130 eStls ,
131 eSecure ,
132 eUnknown
133 } ;
134 enum State
135 {
136 sStart ,
137 sEnd ,
138 sActive ,
139 sData ,
140 sAuth ,
141 s_Any ,
142 s_Same
143 } ;
144 typedef const std::string & EventData ;
145 typedef G::StateMachine<ServerProtocol,State,Event,EventData> Fsm ;
146
147 private:
148 ServerProtocol( const ServerProtocol & ) ; // not implemented
149 void operator=( const ServerProtocol & ) ; // not implemented
150 void doQuit( const std::string & line , bool & ) ;
151 void doQuitEarly( const std::string & line , bool & ) ;
152 void doStat( const std::string & line , bool & ) ;
153 void doList( const std::string & line , bool & ) ;
154 void doRetr( const std::string & line , bool & ) ;
155 void doDele( const std::string & line , bool & ) ;
156 void doRset( const std::string & line , bool & ) ;
157 void doUser( const std::string & line , bool & ) ;
158 void doPass( const std::string & line , bool & ) ;
159 void doNoop( const std::string & line , bool & ) ;
160 void doNothing( const std::string & line , bool & ) ;
161 void doApop( const std::string & line , bool & ) ;
162 void doTop( const std::string & line , bool & ) ;
163 void doCapa( const std::string & line , bool & ) ;
164 void doStls( const std::string & line , bool & ) ;
165 void doAuth( const std::string & line , bool & ) ;
166 void doAuthData( const std::string & line , bool & ) ;
167 void doUidl( const std::string & line , bool & ) ;
168 void sendInit() ;
169 void sendError() ;
170 void sendError( const std::string & ) ;
171 void sendOk() ;
172 static const std::string & crlf() ;
173 Event commandEvent( const std::string & ) const ;
174 int commandNumber( const std::string & , int , size_t index = 1U ) const ;
175 void sendList( const std::string & , bool ) ;
176 std::string commandWord( const std::string & ) const ;
177 std::string commandParameter( const std::string & , size_t index = 1U ) const ;
178 std::string commandPart( const std::string & , size_t index ) const ;
179 void sendContent() ;
180 bool sendContentLine( std::string & , bool & ) ;
181 void send( std::string ) ;
182 void lockStore() ;
183 std::string mechanismsWithoutLogin() const ;
184 bool mechanismsIncludePlain() const ;
185
186 private:
187 const Text & m_text ;
188 Sender & m_sender ;
189 Security & m_security ;
190 Store & m_store ;
191 StoreLock m_store_lock ;
192 const Secrets & m_secrets ;
193 Auth m_auth ;
194 GNet::Address m_peer_address ;
195 Fsm m_fsm ;
196 std::string m_user ;
197 unique_ptr<std::istream> m_content ;
198 long m_body_limit ;
199 bool m_in_body ;
200 bool m_secure ;
201 } ;
202
203 /// \class GPop::ServerProtocolText
204 /// A default implementation for the ServerProtocol::Text interface.
205 ///
206 class GPop::ServerProtocolText : public ServerProtocol::Text
207 {
208 public:
209 explicit ServerProtocolText( GNet::Address peer ) ;
210 ///< Constructor.
211
212 virtual std::string greeting() const override ;
213 ///< Final override from GPop::ServerProtocol::Text.
214
215 virtual std::string quit() const override ;
216 ///< Final override from GPop::ServerProtocol::Text.
217
218 virtual std::string capa() const override ;
219 ///< Final override from GPop::ServerProtocol::Text.
220
221 virtual std::string user( const std::string & id ) const override ;
222 ///< Final override from GPop::ServerProtocol::Text.
223 } ;
224
225 #endif
GPop::Secrets
A simple interface to a store of secrets as used in authentication.
Definition: gpopsecrets.h:43
GPop::ServerProtocol
Implements the POP server-side protocol.
Definition: gpopserverprotocol.h:48
GPop::ServerProtocolText::greeting
virtual std::string greeting() const override
Final override from GPop::ServerProtocol::Text.
Definition: gpopserverprotocol.cpp:584
GNet::Address
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
GPop::ServerProtocolText::user
virtual std::string user(const std::string &id) const override
Final override from GPop::ServerProtocol::Text.
Definition: gpopserverprotocol.cpp:599
gstatemachine.h
gpopstore.h
GPop::ServerProtocol::init
void init()
Starts the protocol.
Definition: gpopserverprotocol.cpp:70
GPop::ServerProtocol::~ServerProtocol
virtual ~ServerProtocol()
Destructor.
Definition: gpopserverprotocol.cpp:75
GPop::ServerProtocol::Sender
An interface used by ServerProtocol to send protocol replies.
Definition: gpopserverprotocol.h:52
GPop::Store
A message store.
Definition: gpopstore.h:45
GPop::ServerProtocol::Security
An interface used by ServerProtocol to enable TLS.
Definition: gpopserverprotocol.h:72
GPop::ServerProtocolText::ServerProtocolText
ServerProtocolText(GNet::Address peer)
Constructor.
Definition: gpopserverprotocol.cpp:580
GPop::ServerProtocolText
A default implementation for the ServerProtocol::Text interface.
Definition: gpopserverprotocol.h:206
gtimer.h
GPop::ServerProtocol::Text
An interface used by ServerProtocol to provide response text strings.
Definition: gpopserverprotocol.h:58
gpopsecrets.h
gpop.h
GPop::ServerProtocolText::quit
virtual std::string quit() const override
Final override from GPop::ServerProtocol::Text.
Definition: gpopserverprotocol.cpp:589
GPop::ServerProtocol::ServerProtocol
ServerProtocol(Sender &sender, Security &security, Store &store, const Secrets &secrets, const Text &text, GNet::Address peer_address, Config config)
Constructor.
Definition: gpopserverprotocol.cpp:30
GPop::ServerProtocol::Config
A structure containing configuration parameters for ServerProtocol. NOT USED.
Definition: gpopserverprotocol.h:67
gexception.h
GPop::ServerProtocolText::capa
virtual std::string capa() const override
Final override from GPop::ServerProtocol::Text.
Definition: gpopserverprotocol.cpp:594
GPop::ServerProtocol::secure
void secure()
Called when the server connection becomes secure.
Definition: gpopserverprotocol.cpp:469
gaddress.h
GPop
POP3 classes.
Definition: gpopauth.h:28
gpopauth.h
GPop::ServerProtocol::apply
void apply(const std::string &line)
Called on receipt of a string from the client.
Definition: gpopserverprotocol.cpp:109
G::StateMachine< ServerProtocol, State, Event, EventData >
GPop::ServerProtocol::resume
void resume()
Called when the Sender can send again.
Definition: gpopserverprotocol.cpp:154