Akonadi

handler.h
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Till Adam <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 #pragma once
7 
8 #include "connection.h"
9 #include "exception.h"
10 #include "global.h"
11 
12 #include <private/protocol_p.h>
13 
14 namespace Akonadi
15 {
16 namespace Server
17 {
18 class AkonadiServer;
19 class Response;
20 
21 AKONADI_EXCEPTION_MAKE_INSTANCE(HandlerException);
22 
23 /**
24  \defgroup akonadi_server_handler Command handlers
25 
26  All commands supported by the Akonadi server are implemented as sub-classes of Akonadi::Handler.
27 */
28 
29 /**
30 The handler interfaces describes an entity capable of handling an AkonadiIMAP command.*/
31 class Handler
32 {
33 public:
34  Handler() = delete;
35  Handler(const Handler &) = delete;
36  Handler(Handler &&) noexcept = delete;
37  Handler &operator=(const Handler &) = delete;
38  Handler &operator=(Handler &&) noexcept = delete;
39 
40  virtual ~Handler() = default;
41 
42  /**
43  * Set the tag of the command to be processed, and thus of the response
44  * generated by this handler.
45  * @param tag The command tag, an alphanumerical string, normally.
46  */
47  void setTag(quint64 tag);
48 
49  /**
50  * The tag of the command associated with this handler.
51  */
52  quint64 tag() const;
53 
54  void setCommand(const Protocol::CommandPtr &cmd);
55  Protocol::CommandPtr command() const;
56 
57  /**
58  * Find a handler for a command that is always allowed, like LOGOUT.
59  * @param cmd the command string
60  * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception
61  * is thrown from handelLine.
62  */
63  static std::unique_ptr<Handler> findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd, AkonadiServer &akonadi);
64 
65  /**
66  * Find a handler for a command that is allowed when the client is not yet authenticated, like LOGIN.
67  * @param cmd the command string
68  * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception
69  * is thrown from handelLine.
70  */
71  static std::unique_ptr<Handler> findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi);
72 
73  /**
74  * Find a handler for a command that is allowed when the client is authenticated, like LIST, FETCH, etc.
75  * @param cmd the command string
76  * @return an instance to the handler. The handler is deleted after @see handelLine is executed. The caller needs to delete the handler in case an exception
77  * is thrown from handelLine.
78  */
79  static std::unique_ptr<Handler> findHandlerForCommandAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi);
80 
81  void setConnection(Connection *connection);
82  Connection *connection() const;
83  DataStore *storageBackend() const;
84 
85  AkonadiServer &akonadi() const;
86 
87  bool failureResponse(const char *response);
88  bool failureResponse(const QByteArray &response);
89  bool failureResponse(const QString &response);
90 
91  template<typename T> inline bool successResponse();
92  template<typename T> inline bool successResponse(T &&response);
93 
94  template<typename T> inline void sendResponse(T &&response);
95  template<typename T> inline void sendResponse();
96 
97  /**
98  * Parse and handle the IMAP message using the streaming parser. The implementation MUST leave the trailing newline character(s) in the stream!
99  * @return true if parsed successfully, false in case of parse failure
100  */
101  virtual bool parseStream() = 0;
102 
103  bool checkScopeConstraints(const Scope &scope, int permittedScopes);
104 
105 protected:
106  Handler(AkonadiServer &akonadi);
107 
108 private:
109  AkonadiServer &m_akonadi;
110  quint64 m_tag = 0;
111  Connection *m_connection = nullptr;
112  bool m_sentFailureResponse = false;
113 
114 protected:
115  Protocol::CommandPtr m_command;
116 };
117 
118 template<typename T> inline bool Handler::successResponse()
119 {
120  sendResponse<T>(T{});
121  return true;
122 }
123 
124 template<typename T> inline bool Handler::successResponse(T &&response)
125 {
126  sendResponse<T>(std::move(response));
127  return true;
128 }
129 
130 template<typename T> inline void Handler::sendResponse()
131 {
132  m_connection->sendResponse<T>(T{});
133 }
134 
135 template<typename T> inline void Handler::sendResponse(T &&response)
136 {
137  m_connection->sendResponse<T>(std::move(response));
138 }
139 
140 } // namespace Server
141 } // namespace Akonadi
142 
This class handles all the database access.
Definition: datastore.h:94
quint64 tag() const
The tag of the command associated with this handler.
Definition: handler.cpp:184
virtual bool parseStream()=0
Parse and handle the IMAP message using the streaming parser.
static std::unique_ptr< Handler > findHandlerForCommandAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is allowed when the client is authenticated, like LIST,...
Definition: handler.cpp:62
static std::unique_ptr< Handler > findHandlerForCommandAlwaysAllowed(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is always allowed, like LOGOUT.
Definition: handler.cpp:53
static std::unique_ptr< Handler > findHandlerForCommandNonAuthenticated(Protocol::Command::Type cmd, AkonadiServer &akonadi)
Find a handler for a command that is allowed when the client is not yet authenticated,...
Definition: handler.cpp:43
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Definition: handler.h:39
A glue between Qt and the standard library.
An Connection represents one connection of a client to the server.
Definition: connection.h:46
void setTag(quint64 tag)
Set the tag of the command to be processed, and thus of the response generated by this handler.
Definition: handler.cpp:179
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:00:32 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.