E-MailRelay

gpopserver.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 gpopserver.h
19///
20
21#ifndef G_POP_SERVER_H
22#define G_POP_SERVER_H
23
24#include "gdef.h"
25#include "gmultiserver.h"
26#include "glinebuffer.h"
27#include "gpopserverprotocol.h"
28#include "gsecrets.h"
29#include "gexception.h"
30#include "gstringview.h"
31#include "gstringarray.h"
32#include <string>
33#include <sstream>
34#include <memory>
35#include <list>
36
37 namespace GPop
38{
39 class Server ;
40 class ServerPeer ;
41}
42
43//| \class GPop::ServerPeer
44/// Represents a connection from a POP client. Instances are created
45/// on the heap by GPop::Server.
46/// \see GPop::Server
47///
48 class GPop::ServerPeer : public GNet::ServerPeer , private ServerProtocol::Sender , private ServerProtocol::Security
49{
50 public:
51 G_EXCEPTION( SendError , tx("network send error") )
52
53 ServerPeer( GNet::EventStateUnbound , GNet::ServerPeerInfo && , Store & ,
54 const GAuth::SaslServerSecrets & , const std::string & sasl_server_config ,
55 std::unique_ptr<ServerProtocol::Text> ptext , const ServerProtocol::Config & ) ;
56 ///< Constructor.
57
58 private: // overrides
59 bool protocolSend( std::string_view , std::size_t ) override ; // GPop::ServerProtocol::Sender
60 void onDelete( const std::string & ) override ; // GNet::ServerPeer
61 bool onReceive( const char * , std::size_t , std::size_t , std::size_t , char ) override ; // GNet::ServerPeer
62 void onSecure( const std::string & , const std::string & , const std::string & ) override ; // GNet::SocketProtocolSink
63 void onSendComplete() override ; // GNet::ServerPeer
64 bool securityEnabled() const override ; // GPop::ServerProtocol::Security
65 void securityStart() override ; // GPop::ServerProtocol::Security
66
67 public:
68 ~ServerPeer() override = default ;
69 ServerPeer( const ServerPeer & ) = delete ;
70 ServerPeer( ServerPeer && ) = delete ;
71 ServerPeer & operator=( const ServerPeer & ) = delete ;
72 ServerPeer & operator=( ServerPeer && ) = delete ;
73
74 private:
75 void processLine( const std::string & line ) ;
76
77 private:
78 std::unique_ptr<ServerProtocol::Text> m_ptext ; // order dependency
79 ServerProtocol m_protocol ; // order dependency -- last
80} ;
81
82//| \class GPop::Server
83/// A POP server class.
84///
85 class GPop::Server : public GNet::MultiServer
86{
87 public:
88 G_EXCEPTION( Overflow , tx("too many interface addresses") )
89 struct Config /// A structure containing GPop::Server configuration parameters.
90 {
91 bool allow_remote {false} ;
92 unsigned int port {110} ;
93 G::StringArray addresses ;
94 GNet::ServerPeer::Config net_server_peer_config ;
95 GNet::Server::Config net_server_config ;
96 ServerProtocol::Config protocol_config ;
97 std::string sasl_server_config ;
98
99 Config & set_allow_remote( bool = true ) noexcept ;
100 Config & set_port( unsigned int ) noexcept ;
101 Config & set_addresses( const G::StringArray & ) ;
102 Config & set_net_server_peer_config( const GNet::ServerPeer::Config & ) ;
103 Config & set_net_server_config( const GNet::Server::Config & ) ;
104 Config & set_protocol_config( const ServerProtocol::Config & ) ;
105 Config & set_sasl_server_config( const std::string & ) ;
106 } ;
107
108 Server( GNet::EventState , Store & store , const GAuth::SaslServerSecrets & , const Config & ) ;
109 ///< Constructor. The 'secrets' reference is kept.
110
111 ~Server() override ;
112 ///< Destructor.
113
114 void report( const std::string & group = {} ) const ;
115 ///< Generates helpful diagnostics after construction.
116
117 private: // overrides
118 std::unique_ptr<GNet::ServerPeer> newPeer( GNet::EventStateUnbound , GNet::ServerPeerInfo && , GNet::MultiServer::ServerInfo ) override ;
119
120 public:
121 Server( const Server & ) = delete ;
122 Server( Server && ) = delete ;
123 Server & operator=( const Server & ) = delete ;
124 Server & operator=( Server && ) = delete ;
125
126 private:
127 std::unique_ptr<ServerProtocol::Text> newProtocolText( const GNet::Address & ) const ;
128
129 private:
130 Config m_config ;
131 Store & m_store ;
132 const GAuth::SaslServerSecrets & m_secrets ;
133} ;
134
135 inline GPop::Server::Config & GPop::Server::Config::set_allow_remote( bool b ) noexcept { allow_remote = b ; return *this ; }
136 inline GPop::Server::Config & GPop::Server::Config::set_port( unsigned int p ) noexcept { port = p ; return *this ; }
137 inline GPop::Server::Config & GPop::Server::Config::set_addresses( const G::StringArray & a ) { addresses = a ; return *this ; }
138 inline GPop::Server::Config & GPop::Server::Config::set_net_server_peer_config( const GNet::ServerPeer::Config & c ) { net_server_peer_config = c ; return *this ; }
139 inline GPop::Server::Config & GPop::Server::Config::set_net_server_config( const GNet::Server::Config & c ) { net_server_config = c ; return *this ; }
140 inline GPop::Server::Config & GPop::Server::Config::set_protocol_config( const ServerProtocol::Config & c ) { protocol_config = c ; return *this ; }
141 inline GPop::Server::Config & GPop::Server::Config::set_sasl_server_config( const std::string & s ) { sasl_server_config = s ; return *this ; }
142
143#endif
GAuth::SaslServerSecrets
An interface used by GAuth::SaslServer to obtain authentication secrets.
Definition: gsaslserversecrets.h:40
GNet::Address
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:63
GNet::EventStateUnbound
The EventStateUnbound class is used as a device to force factory methods to plumb-in an ExceptionSour...
Definition: geventstate.h:231
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::MultiServer
A server that listens on more than one address using a facade pattern to multiple GNet::Server instan...
Definition: gmultiserver.h:48
GNet::ServerPeerInfo
A move-only structure used in GNet::Server::newPeer() and containing the new socket.
Definition: gserver.h:142
GNet::ServerPeer
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserverpeer.h:59
GPop::ServerPeer
Represents a connection from a POP client.
Definition: gpopserver.h:49
GPop::ServerPeer::ServerPeer
ServerPeer(GNet::EventStateUnbound, GNet::ServerPeerInfo &&, Store &, const GAuth::SaslServerSecrets &, const std::string &sasl_server_config, std::unique_ptr< ServerProtocol::Text > ptext, const ServerProtocol::Config &)
Constructor.
Definition: gpopserver.cpp:28
GPop::ServerProtocol::Security
An interface used by ServerProtocol to enable TLS.
Definition: gpopserverprotocol.h:80
GPop::ServerProtocol::Sender
An interface used by ServerProtocol to send protocol replies.
Definition: gpopserverprotocol.h:55
GPop::ServerProtocol
Implements the POP server-side protocol.
Definition: gpopserverprotocol.h:50
GPop::Server
A POP server class.
Definition: gpopserver.h:86
GPop::Server::report
void report(const std::string &group={}) const
Generates helpful diagnostics after construction.
Definition: gpopserver.cpp:100
GPop::Server::~Server
~Server() override
Destructor.
Definition: gpopserver.cpp:95
GPop::Server::Server
Server(GNet::EventState, Store &store, const GAuth::SaslServerSecrets &, const Config &)
Constructor. The 'secrets' reference is kept.
Definition: gpopserver.cpp:87
GPop::Store
A message store.
Definition: gpopstore.h:47
gexception.h
glinebuffer.h
gmultiserver.h
gpopserverprotocol.h
gsecrets.h
gstringarray.h
gstringview.h
GPop
POP3 classes.
Definition: gpop.h:33
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
GNet::MultiServer::ServerInfo
A structure used in GNet::MultiServer::newPeer().
Definition: gmultiserver.h:55
GNet::ServerPeer::Config
A configuration structure for GNet::ServerPeer.
Definition: gserverpeer.h:64
GNet::Server::Config
A configuration structure for GNet::Server.
Definition: gserver.h:56
GPop::ServerProtocol::Config
A structure containing configuration parameters for ServerProtocol.
Definition: gpopserverprotocol.h:72
GPop::Server::Config
A structure containing GPop::Server configuration parameters.
Definition: gpopserver.h:90