5eb3f9e05c7740bfb0f79090430cf77f82924eb3
[quassel.git] / src / common / protocol.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <utility>
24
25 #include <QByteArray>
26 #include <QDateTime>
27 #include <QVariantList>
28
29 #include "quassel.h"
30
31 namespace Protocol {
32
33 const quint32 magic = 0x42b33f00;
34
35 enum Type
36 {
37     InternalProtocol = 0x00,
38     LegacyProtocol = 0x01,
39     DataStreamProtocol = 0x02
40 };
41
42 enum Feature
43 {
44     Encryption = 0x01,
45     Compression = 0x02
46 };
47
48 enum class Handler
49 {
50     SignalProxy,
51     AuthHandler
52 };
53
54 /*** Handshake, handled by AuthHandler ***/
55
56 struct HandshakeMessage
57 {
58     inline Handler handler() const { return Handler::AuthHandler; }
59 };
60
61 struct RegisterClient : public HandshakeMessage
62 {
63     inline RegisterClient(Quassel::Features clientFeatures, QString clientVersion, QString buildDate, bool sslSupported = false)
64         : features(std::move(clientFeatures))
65         , clientVersion(std::move(clientVersion))
66         , buildDate(std::move(buildDate))
67         , sslSupported(sslSupported)
68     {}
69
70     Quassel::Features features;
71     QString clientVersion;
72     QString buildDate;
73
74     // this is only used by the LegacyProtocol in compat mode
75     bool sslSupported;
76 };
77
78 struct ClientDenied : public HandshakeMessage
79 {
80     inline ClientDenied(QString errorString)
81         : errorString(std::move(errorString))
82     {}
83
84     QString errorString;
85 };
86
87 struct ClientRegistered : public HandshakeMessage
88 {
89     inline ClientRegistered(
90         Quassel::Features coreFeatures, bool coreConfigured, QVariantList backendInfo, QVariantList authenticatorInfo, bool sslSupported)
91         : features(std::move(coreFeatures))
92         , coreConfigured(coreConfigured)
93         , backendInfo(std::move(backendInfo))
94         , authenticatorInfo(std::move(authenticatorInfo))
95         , sslSupported(sslSupported)
96     {}
97
98     Quassel::Features features;
99     bool coreConfigured;
100     QVariantList backendInfo;  // TODO: abstract this better
101
102     // The authenticatorInfo should be optional!
103     QVariantList authenticatorInfo;
104
105     // this is only used by the LegacyProtocol in compat mode
106     bool sslSupported;
107 };
108
109 struct SetupData : public HandshakeMessage
110 {
111     inline SetupData(QString adminUser,
112                      QString adminPassword,
113                      QString backend,
114                      QVariantMap setupData,
115                      QString authenticator = QString(),
116                      QVariantMap authSetupData = QVariantMap())
117         : adminUser(std::move(adminUser))
118         , adminPassword(std::move(adminPassword))
119         , backend(std::move(backend))
120         , setupData(std::move(setupData))
121         , authenticator(std::move(authenticator))
122         , authSetupData(std::move(authSetupData))
123     {}
124
125     QString adminUser;
126     QString adminPassword;
127     QString backend;
128     QVariantMap setupData;
129     QString authenticator;
130     QVariantMap authSetupData;
131 };
132
133 struct SetupFailed : public HandshakeMessage
134 {
135     inline SetupFailed(QString errorString)
136         : errorString(std::move(errorString))
137     {}
138
139     QString errorString;
140 };
141
142 struct SetupDone : public HandshakeMessage
143 {};
144
145 struct Login : public HandshakeMessage
146 {
147     inline Login(QString user, QString password)
148         : user(std::move(user))
149         , password(std::move(password))
150     {}
151
152     QString user;
153     QString password;
154 };
155
156 struct LoginFailed : public HandshakeMessage
157 {
158     inline LoginFailed(QString errorString)
159         : errorString(std::move(errorString))
160     {}
161
162     QString errorString;
163 };
164
165 struct LoginSuccess : public HandshakeMessage
166 {};
167
168 // TODO: more generic format
169 struct SessionState : public HandshakeMessage
170 {
171     inline SessionState() = default;  // needed for QMetaType (for the mono client)
172     inline SessionState(QVariantList identities, QVariantList bufferInfos, QVariantList networkIds)
173         : identities(std::move(identities))
174         , bufferInfos(std::move(bufferInfos))
175         , networkIds(std::move(networkIds))
176     {}
177
178     QVariantList identities;
179     QVariantList bufferInfos;
180     QVariantList networkIds;
181 };
182
183 /*** handled by SignalProxy ***/
184
185 struct SignalProxyMessage
186 {
187     inline Handler handler() const { return Handler::SignalProxy; }
188 };
189
190 struct SyncMessage : public SignalProxyMessage
191 {
192     SyncMessage() = default;
193     SyncMessage(QByteArray className, QString objectName, QByteArray slotName, QVariantList params)
194         : className(std::move(className))
195         , objectName(std::move(objectName))
196         , slotName(std::move(slotName))
197         , params(std::move(params))
198     {}
199
200     QByteArray className;
201     QString objectName;
202     QByteArray slotName;
203     QVariantList params;
204 };
205
206 struct RpcCall : public SignalProxyMessage
207 {
208     RpcCall() = default;
209     RpcCall(QByteArray signalName, QVariantList params)
210         : signalName(std::move(signalName))
211         , params(std::move(params))
212     {}
213
214     QByteArray signalName;
215     QVariantList params;
216 };
217
218 struct InitRequest : public SignalProxyMessage
219 {
220     InitRequest() = default;
221     InitRequest(QByteArray className, QString objectName)
222         : className(std::move(className))
223         , objectName(std::move(objectName))
224     {}
225
226     QByteArray className;
227     QString objectName;
228 };
229
230 struct InitData : public SignalProxyMessage
231 {
232     InitData() = default;
233     InitData(QByteArray className, QString objectName, QVariantMap initData)
234         : className(std::move(className))
235         , objectName(std::move(objectName))
236         , initData(std::move(initData))
237     {}
238
239     QByteArray className;
240     QString objectName;
241     QVariantMap initData;
242 };
243
244 /*** handled by RemoteConnection ***/
245
246 struct HeartBeat
247 {
248     inline HeartBeat(QDateTime timestamp)
249         : timestamp(std::move(timestamp))
250     {}
251
252     QDateTime timestamp;
253 };
254
255 struct HeartBeatReply
256 {
257     inline HeartBeatReply(QDateTime timestamp)
258         : timestamp(std::move(timestamp))
259     {}
260
261     QDateTime timestamp;
262 };
263
264 }  // namespace Protocol
265
266 // Required for InternalPeer
267 Q_DECLARE_METATYPE(Protocol::SyncMessage)
268 Q_DECLARE_METATYPE(Protocol::RpcCall)
269 Q_DECLARE_METATYPE(Protocol::InitRequest)
270 Q_DECLARE_METATYPE(Protocol::InitData)