47dabd9b2ea39fcb9a380566b98397cb3ecafba5
[quassel.git] / src / common / protocol.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 <QByteArray>
24 #include <QDateTime>
25 #include <QVariantList>
26 #include <utility>
27
28 #include "quassel.h"
29
30 namespace Protocol {
31
32 const quint32 magic = 0x42b33f00;
33
34 enum Type {
35     InternalProtocol = 0x00,
36     LegacyProtocol = 0x01,
37     DataStreamProtocol = 0x02
38 };
39
40
41 enum Feature {
42     Encryption = 0x01,
43     Compression = 0x02
44 };
45
46
47 enum class Handler {
48     SignalProxy,
49     AuthHandler
50 };
51
52
53 /*** Handshake, handled by AuthHandler ***/
54
55 struct HandshakeMessage {
56     inline Handler handler() const { return Handler::AuthHandler; }
57 };
58
59
60 struct RegisterClient : public HandshakeMessage
61 {
62     inline RegisterClient(Quassel::Features clientFeatures, QString clientVersion, QString buildDate, bool sslSupported = false)
63         : features(std::move(clientFeatures))
64         , clientVersion(std::move(clientVersion))
65         , buildDate(std::move(buildDate))
66         , sslSupported(sslSupported)
67     {}
68
69     Quassel::Features features;
70     QString clientVersion;
71     QString buildDate;
72
73     // this is only used by the LegacyProtocol in compat mode
74     bool sslSupported;
75 };
76
77
78 struct ClientDenied : public HandshakeMessage
79 {
80     inline ClientDenied(QString errorString)
81     : errorString(std::move(errorString)) {}
82
83     QString errorString;
84 };
85
86
87 struct ClientRegistered : public HandshakeMessage
88 {
89     inline ClientRegistered(Quassel::Features coreFeatures, bool coreConfigured, QVariantList backendInfo, QVariantList authenticatorInfo, bool sslSupported)
90         : features(std::move(coreFeatures))
91         , coreConfigured(coreConfigured)
92         , backendInfo(std::move(backendInfo))
93         , authenticatorInfo(std::move(authenticatorInfo))
94         , sslSupported(sslSupported)
95     {}
96
97     Quassel::Features features;
98     bool coreConfigured;
99     QVariantList backendInfo; // TODO: abstract this better
100
101     // The authenticatorInfo should be optional!
102     QVariantList authenticatorInfo;
103
104     // this is only used by the LegacyProtocol in compat mode
105     bool sslSupported;
106 };
107
108
109 struct SetupData : public HandshakeMessage
110 {
111     inline SetupData(QString adminUser, QString adminPassword, QString backend,
112                      QVariantMap setupData, QString authenticator = QString(),
113                      QVariantMap authSetupData = QVariantMap())
114     : adminUser(std::move(adminUser))
115     , adminPassword(std::move(adminPassword))
116     , backend(std::move(backend))
117     , setupData(std::move(setupData))
118     , authenticator(std::move(authenticator))
119     , authSetupData(std::move(authSetupData))
120     {}
121
122     QString adminUser;
123     QString adminPassword;
124     QString backend;
125     QVariantMap setupData;
126     QString authenticator;
127     QVariantMap authSetupData;
128 };
129
130
131 struct SetupFailed : public HandshakeMessage
132 {
133     inline SetupFailed(QString errorString)
134     : errorString(std::move(errorString)) {}
135
136     QString errorString;
137 };
138
139
140 struct SetupDone : public HandshakeMessage
141 {
142     inline SetupDone() {}
143 };
144
145
146 struct Login : public HandshakeMessage
147 {
148     inline Login(QString user, QString password)
149     : user(std::move(user)), password(std::move(password)) {}
150
151     QString user;
152     QString password;
153 };
154
155
156 struct LoginFailed : public HandshakeMessage
157 {
158     inline LoginFailed(QString errorString)
159     : errorString(std::move(errorString)) {}
160
161     QString errorString;
162 };
163
164
165 struct LoginSuccess : public HandshakeMessage
166 {
167     inline LoginSuccess() {}
168 };
169
170
171 // TODO: more generic format
172 struct SessionState : public HandshakeMessage
173 {
174     inline SessionState() {} // needed for QMetaType (for the mono client)
175     inline SessionState(QVariantList identities, QVariantList bufferInfos, QVariantList networkIds)
176     : identities(std::move(identities)), bufferInfos(std::move(bufferInfos)), networkIds(std::move(networkIds)) {}
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
191 struct SyncMessage : public SignalProxyMessage
192 {
193     SyncMessage() = default;
194     SyncMessage(QByteArray className, QString objectName, QByteArray slotName, QVariantList params)
195         : className(std::move(className)), objectName(std::move(objectName)), slotName(std::move(slotName)), params(std::move(params)) {}
196
197     QByteArray className;
198     QString objectName;
199     QByteArray slotName;
200     QVariantList params;
201 };
202
203
204 struct RpcCall : public SignalProxyMessage
205 {
206     RpcCall() = default;
207     RpcCall(QByteArray slotName, QVariantList params)
208         : slotName(std::move(slotName)), params(std::move(params)) {}
209
210     QByteArray slotName;
211     QVariantList params;
212 };
213
214
215 struct InitRequest : public SignalProxyMessage
216 {
217     InitRequest() = default;
218     InitRequest(QByteArray className, QString objectName)
219         : className(std::move(className)), objectName(std::move(objectName)) {}
220
221     QByteArray className;
222     QString objectName;
223 };
224
225
226 struct InitData : public SignalProxyMessage
227 {
228     InitData() = default;
229     InitData(QByteArray className, QString objectName, QVariantMap initData)
230         : className(std::move(className)), objectName(std::move(objectName)), initData(std::move(initData)) {}
231
232     QByteArray className;
233     QString objectName;
234     QVariantMap initData;
235 };
236
237
238 /*** handled by RemoteConnection ***/
239
240 struct HeartBeat
241 {
242     inline HeartBeat(QDateTime timestamp) : timestamp(std::move(timestamp)) {}
243
244     QDateTime timestamp;
245 };
246
247
248 struct HeartBeatReply
249 {
250     inline HeartBeatReply(QDateTime timestamp) : timestamp(std::move(timestamp)) {}
251
252     QDateTime timestamp;
253 };
254
255
256 }
257
258 // Required for InternalPeer
259 Q_DECLARE_METATYPE(Protocol::SyncMessage)
260 Q_DECLARE_METATYPE(Protocol::RpcCall)
261 Q_DECLARE_METATYPE(Protocol::InitRequest)
262 Q_DECLARE_METATYPE(Protocol::InitData)